Spaces:
Running
Running
final_update
Browse files
app.py
CHANGED
@@ -1,64 +1,151 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-180M-Amharic-Instruct"
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
11 |
+
|
12 |
+
llama_am = pipeline(
|
13 |
+
"text-generation",
|
14 |
+
model=model,
|
15 |
+
tokenizer=tokenizer,
|
16 |
+
pad_token_id=tokenizer.pad_token_id,
|
17 |
+
eos_token_id=tokenizer.eos_token_id
|
18 |
+
)
|
19 |
+
|
20 |
+
# Function that accepts a prompt and generates text using the phi2 pipeline
|
21 |
+
def generate(message, chat_history, max_new_tokens=64):
|
22 |
+
|
23 |
+
history = []
|
24 |
+
|
25 |
+
for sent, received in chat_history:
|
26 |
+
history.append({"role": "user", "content": sent})
|
27 |
+
history.append({"role": "assistant", "content": received})
|
28 |
+
|
29 |
+
history.append({"role": "user", "content": message})
|
30 |
+
#print(history)
|
31 |
+
|
32 |
+
if len(tokenizer.apply_chat_template(history)) > 512:
|
33 |
+
yield "chat history is too long"
|
34 |
+
else:
|
35 |
+
# Streamer
|
36 |
+
streamer = TextIteratorStreamer(tokenizer=tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=300.0)
|
37 |
+
thread = Thread(target=llama_am,
|
38 |
+
kwargs={
|
39 |
+
"text_inputs":history,
|
40 |
+
"max_new_tokens":max_new_tokens,
|
41 |
+
"repetition_penalty":1.15,
|
42 |
+
"streamer":streamer
|
43 |
+
}
|
44 |
+
)
|
45 |
+
thread.start()
|
46 |
+
|
47 |
+
generated_text = ""
|
48 |
+
for word in streamer:
|
49 |
+
generated_text += word
|
50 |
+
response = generated_text.strip()
|
51 |
+
|
52 |
+
yield response
|
53 |
+
|
54 |
+
# Chat interface with gradio
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("""
|
57 |
+
# Llama 3.2 180M Amharic Chatbot Demo
|
58 |
+
|
59 |
+
This chatbot was created using [Llama-3.2-180M-Amharic-Instruct](https://huggingface.co/rasyosef/Llama-3.2-180M-Amharic-Instruct), a finetuned version of my 180 million parameter [Llama 3.2 180M Amharic](https://huggingface.co/rasyosef/Llama-3.2-180M-Amharic) transformer model.
|
60 |
+
""")
|
61 |
+
|
62 |
+
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.")
|
63 |
+
|
64 |
+
chatbot = gr.ChatInterface(
|
65 |
+
chatbot=gr.Chatbot(height=400),
|
66 |
+
fn=generate,
|
67 |
+
additional_inputs=[tokens_slider],
|
68 |
+
stop_btn=None,
|
69 |
+
cache_examples=False,
|
70 |
+
examples=[
|
71 |
+
["แฐแแแฃ แฅแแดแต แแ
?"],
|
72 |
+
["แจแขแตแฎแตแซ แแ แจแฐแ แตแ แแแตแ แแ?"],
|
73 |
+
["แจแขแตแฎแตแซ แจแแจแจแปแ แแแต แแ แแ แฉ?"],
|
74 |
+
["แจแ แแญแ แแฅแ แแแแ"],
|
75 |
+
["แฐแจแต แแแจแ\n\nแ
แฅแ แ แแ แณ"],
|
76 |
+
["แ แแต แ แตแแ แแแต แแแจแ"],
|
77 |
+
["แจแฐแฐแ แ แฝแแ แ แตแฐแซแจแต แแ แ แญแแต แแ? 'แ แแแณแ'แฃ 'แ แแณแ' แแญแ 'แแแแฐแ' แจแแ แแแฝ แตแฅแข 'แ แชแ แแแ แแ แญ'"],
|
78 |
+
["แจแแจแแณแญ แแ แจแฐแ แตแ แแแตแ แแ?"],
|
79 |
+
["แ แแ แจแ แแชแซ แแฌแแณแแต แแ แแ?"],
|
80 |
+
["แถแตแต แจแ แแชแซ แแแซแต แฅแแตแแ"],
|
81 |
+
["3 แจแ แแชแซ แแชแแฝแ แตแ แฅแแต"],
|
82 |
+
["5 แจแ แแชแซ แจแฐแแแฝแ แฅแแต"],
|
83 |
+
["แ แแตแต แจแ แแฎแ แแแฎแฝแ แฅแแตแแ"],
|
84 |
+
["แ แแแ แแญ แซแแตแ 7 แ แ
แแซแต แแแจแ"]
|
85 |
+
]
|
86 |
+
)
|
87 |
+
|
88 |
+
demo.queue().launch(debug=True,share=True)
|
89 |
+
# from huggingface_hub import InferenceClient
|
90 |
+
|
91 |
+
# """
|
92 |
+
# For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
93 |
+
# """
|
94 |
+
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
95 |
+
|
96 |
+
|
97 |
+
# def respond(
|
98 |
+
# message,
|
99 |
+
# history: list[tuple[str, str]],
|
100 |
+
# system_message,
|
101 |
+
# max_tokens,
|
102 |
+
# temperature,
|
103 |
+
# top_p,
|
104 |
+
# ):
|
105 |
+
# messages = [{"role": "system", "content": system_message}]
|
106 |
+
|
107 |
+
# for val in history:
|
108 |
+
# if val[0]:
|
109 |
+
# messages.append({"role": "user", "content": val[0]})
|
110 |
+
# if val[1]:
|
111 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
112 |
+
|
113 |
+
# messages.append({"role": "user", "content": message})
|
114 |
+
|
115 |
+
# response = ""
|
116 |
+
|
117 |
+
# for message in client.chat_completion(
|
118 |
+
# messages,
|
119 |
+
# max_tokens=max_tokens,
|
120 |
+
# stream=True,
|
121 |
+
# temperature=temperature,
|
122 |
+
# top_p=top_p,
|
123 |
+
# ):
|
124 |
+
# token = message.choices[0].delta.content
|
125 |
+
|
126 |
+
# response += token
|
127 |
+
# yield response
|
128 |
+
|
129 |
+
|
130 |
+
# """
|
131 |
+
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
132 |
+
# """
|
133 |
+
# demo = gr.ChatInterface(
|
134 |
+
# respond,
|
135 |
+
# additional_inputs=[
|
136 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
137 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
138 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
139 |
+
# gr.Slider(
|
140 |
+
# minimum=0.1,
|
141 |
+
# maximum=1.0,
|
142 |
+
# value=0.95,
|
143 |
+
# step=0.05,
|
144 |
+
# label="Top-p (nucleus sampling)",
|
145 |
+
# ),
|
146 |
+
# ],
|
147 |
+
# )
|
148 |
+
|
149 |
+
|
150 |
+
# if __name__ == "__main__":
|
151 |
+
# demo.launch()
|