ruthtamiru commited on
Commit
a8d2b76
ยท
verified ยท
1 Parent(s): 522e587

final_update

Browse files
Files changed (1) hide show
  1. app.py +151 -64
app.py CHANGED
@@ -1,64 +1,151 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- 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
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()