Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer, pipeline
|
5 |
+
from threading import Thread
|
6 |
+
|
7 |
+
model_id = "rasyosef/Llama-3.2-400M-Amharic-Instruct-Poems-Stories-Wikipedia"
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
11 |
+
model_id,
|
12 |
+
torch_dtype=torch.float32,
|
13 |
+
device_map="cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
)
|
15 |
+
|
16 |
+
llama3_am = pipeline(
|
17 |
+
"text-generation",
|
18 |
+
model=model,
|
19 |
+
tokenizer=tokenizer,
|
20 |
+
eos_token_id=tokenizer.eos_token_id,
|
21 |
+
device_map="cuda" if torch.cuda.is_available() else "cpu"
|
22 |
+
)
|
23 |
+
|
24 |
+
# Function that accepts a prompt and generates text
|
25 |
+
def generate(message, chat_history, max_new_tokens=64):
|
26 |
+
|
27 |
+
history = []
|
28 |
+
|
29 |
+
for sent, received in chat_history:
|
30 |
+
history.append({"role": "user", "content": sent})
|
31 |
+
history.append({"role": "assistant", "content": received})
|
32 |
+
|
33 |
+
history.append({"role": "user", "content": message})
|
34 |
+
|
35 |
+
if len(tokenizer.apply_chat_template(history)) > 512:
|
36 |
+
yield "chat history is too long"
|
37 |
+
else:
|
38 |
+
# Streamer
|
39 |
+
streamer = TextIteratorStreamer(tokenizer=tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=300.0)
|
40 |
+
thread = Thread(
|
41 |
+
target=llama3_am,
|
42 |
+
kwargs={
|
43 |
+
"text_inputs":history,
|
44 |
+
"max_new_tokens":max_new_tokens,
|
45 |
+
"repetition_penalty":1.1,
|
46 |
+
"streamer":streamer
|
47 |
+
}
|
48 |
+
)
|
49 |
+
thread.start()
|
50 |
+
|
51 |
+
generated_text = ""
|
52 |
+
for word in streamer:
|
53 |
+
generated_text += word
|
54 |
+
response = generated_text.strip()
|
55 |
+
|
56 |
+
yield response
|
57 |
+
|
58 |
+
# Chat interface with gradio
|
59 |
+
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown("""
|
61 |
+
# Llama 3.2 400M Amharic Chatbot Demo
|
62 |
+
""")
|
63 |
+
|
64 |
+
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.")
|
65 |
+
|
66 |
+
chatbot = gr.ChatInterface(
|
67 |
+
chatbot=gr.Chatbot(height=400),
|
68 |
+
fn=generate,
|
69 |
+
additional_inputs=[tokens_slider],
|
70 |
+
stop_btn=None,
|
71 |
+
examples=[
|
72 |
+
["แฐแแ"],
|
73 |
+
["แฐแแแฃ แฅแแดแต แแ
?"],
|
74 |
+
["แ แแฐ แแแ
?"],
|
75 |
+
["แแฅแ แแแแ"],
|
76 |
+
["แตแ แญแ
แญแณ แแฅแ แปแแแ"],
|
77 |
+
["แ แแต แฐแจแต แ แซแแฐแ"],
|
78 |
+
["แตแ แ
แฅแ แ แแ แณ แฐแจแต แแแจแ"],
|
79 |
+
["แแแต แแแจแ"],
|
80 |
+
["แตแ แตแซ แ แฅแแต แ แแต แแแต แแแจแ"],
|
81 |
+
["แณแแแ แดแแตแฎแต แแ แแ?"],
|
82 |
+
["แณแแแ แแแแญ แแ แแ?"],
|
83 |
+
["แตแ แ แฒแต แ แ แฃ แฉแแจแญแตแฒ แฅแแต แฅแแแณแแฝแ แ แซแแฐแ"],
|
84 |
+
["แตแ แแแ แฅแแต แฅแแแณแแฝแ แแแจแ"],
|
85 |
+
["แตแ แแญแญแฎแถแแต แฅแแต แฅแแแณแแฝแ แแแจแ"],
|
86 |
+
["แแแ แแแตแ แแ?"],
|
87 |
+
["แขแตแฎแญแ แแแตแ แแ?"],
|
88 |
+
]
|
89 |
+
)
|
90 |
+
|
91 |
+
demo.queue().launch(debug=True)
|