rasyosef commited on
Commit
8885176
·
verified ·
1 Parent(s): 574cf42

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer, pipeline
5
+ from threading import Thread
6
+
7
+ # Function that accepts a prompt and generates text using the phi2 pipeline
8
+ def generate(message, chat_history, max_new_tokens=256):
9
+
10
+ history = []
11
+
12
+ for sent, received in chat_history:
13
+ history.append({"role": "user", "content": sent})
14
+ history.append({"role": "assistant", "content": received})
15
+
16
+ history.append({"role": "user", "content": message})
17
+ #print(history)
18
+
19
+ if len(tokenizer.apply_chat_template(history)) > 512:
20
+ yield "chat history is too long"
21
+ else:
22
+ # Streamer
23
+ streamer = TextIteratorStreamer(tokenizer=tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=300.0)
24
+ thread = Thread(target=llama_am,
25
+ kwargs={
26
+ "text_inputs":history,
27
+ "max_new_tokens":max_new_tokens,
28
+ "temperature":0.0,
29
+ "repetition_penalty":1.25,
30
+ "streamer":streamer
31
+ }
32
+ )
33
+ thread.start()
34
+
35
+ generated_text = ""
36
+ for word in streamer:
37
+ generated_text += word
38
+ response = generated_text.strip()
39
+
40
+ yield response
41
+
42
+ # Chat interface with gradio
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown("""
45
+ # Llama 3.2 Amharic Chatbot Demo
46
+
47
+ This chatbot was created using a finetuned version of my 180 million parameter Llama 3.2 Amharic transformer model, [Llama-3.2-Amharic-Instruct-Beta](https://huggingface.co/rasyosef/Phi-1_5-Instruct-v0.1).
48
+ """)
49
+
50
+ tokens_slider = gr.Slider(8, 256, value=64, label="Maximum new tokens", info="A larger `max_new_tokens` parameter value gives you longer text responses but at the cost of a slower response time.")
51
+
52
+ chatbot = gr.ChatInterface(
53
+ chatbot=gr.Chatbot(height=400),
54
+ fn=generate,
55
+ additional_inputs=[tokens_slider],
56
+ stop_btn=None,
57
+ examples=[
58
+ ["የኢትዮጵያ ዋና ከተማ ስም ምንድን ነው?"],
59
+ ["የፈረንሳይ ዋና ከተማ ስም ምንድን ነው?"],
60
+ ["የኢትዮጵያ የመጨረሻው ንጉስ ማን ነበሩ?"],
61
+ ["የ አሜሪካ ፕሬዝዳንት ማን ነው?"],
62
+ ["የ እስራኤል ጠቅላይ ሚንስትር ማን ነው?"],
63
+ ["ሶስት የአፍሪካ ሃገራት ጥቀስልኝ"],
64
+ ["፫ የአውሮፓ ሃገራት ጥቀስልኝ"],
65
+ ["፫ የእስያ ሃገራት ጥቀስልኝ"],
66
+ ["ሶስት የአሜሪካ መሪዎችን ስም ጥቀስ"],
67
+ ["አምስት የአሜሪካ ከተማዎችን ስም ጥቀስ"]
68
+ ]
69
+ )
70
+
71
+ demo.queue().launch(debug=True)