kodetr commited on
Commit
030905d
1 Parent(s): 3d3ab43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +198 -48
app.py CHANGED
@@ -1,64 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("kodetr/stunting-qa-v3")
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
+ # 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("kodetr/stunting-qa-v3")
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()
65
+
66
+
67
+ import torch
68
+ from PIL import Image
69
  import gradio as gr
70
+ import spaces
71
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
72
+ import os
73
+ from threading import Thread
74
+
75
 
76
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
77
+ MODEL_ID = "arcee-ai/Arcee-VyLinh"
78
+ MODELS = os.environ.get("MODELS")
79
+ MODEL_NAME = MODELS.split("/")[-1]
80
+
81
+ TITLE = "<h1><center>Arcee-VyLinh ChatUI</center></h1>"
82
+
83
+ DESCRIPTION = f"""
84
+ <h3>MODEL: <a href="https://hf.co/{MODELS}">{MODEL_NAME}</a></h3>
85
+ <center>
86
+ <p>Arce-VyLinh is a Small Language Model specialized in Vietnamese, developed by Arcee.ai
87
+ <br>
88
+ Feel free to test without log.
89
+ </p>
90
+ </center>
91
  """
92
+
93
+ CSS = """
94
+ .duplicate-button {
95
+ margin: auto !important;
96
+ color: white !important;
97
+ background: black !important;
98
+ border-radius: 100vh !important;
99
+ }
100
+ h3 {
101
+ text-align: center;
102
+ }
103
  """
 
104
 
105
+ model = AutoModelForCausalLM.from_pretrained(
106
+ MODEL_ID,
107
+ torch_dtype=torch.bfloat16,
108
+ device_map="auto",
109
+ )
110
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
111
 
112
+ @spaces.GPU
113
+ def stream_chat(message: str, history: list, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
114
+ print(f'message is - {message}')
115
+ print(f'history is - {history}')
116
+ conversation = [{"role": "system", "content": 'Bạn là một trợ lí hữu ích tên là Vy Linh. Hãy trả lời câu hỏi của người dùng bằng Tiếng Việt.'}]
117
+ for prompt, answer in history:
118
+ conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
119
+ conversation.append({"role": "user", "content": message})
 
120
 
121
+ print(f"Conversation is -\n{conversation}")
122
+
123
+ input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
124
+ inputs = tokenizer(input_ids, return_tensors="pt").to(0)
125
+
126
+ streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
127
 
128
+ generate_kwargs = dict(
129
+ inputs,
130
+ streamer=streamer,
131
+ top_k=top_k,
132
+ top_p=top_p,
133
+ repetition_penalty=penalty,
134
+ max_new_tokens=max_new_tokens,
135
+ do_sample=True,
136
+ temperature=temperature,
137
+ eos_token_id = [151645, 151643],
138
+ )
139
+
140
+ thread = Thread(target=model.generate, kwargs=generate_kwargs)
141
+ thread.start()
142
 
143
+ buffer = ""
144
+ for new_text in streamer:
145
+ buffer += new_text
146
+ yield buffer
147
 
 
 
 
 
 
 
 
 
148
 
 
 
149
 
150
+ chatbot = gr.Chatbot(height=600)
151
 
152
+ with gr.Blocks(css=CSS) as demo:
153
+ gr.HTML(TITLE)
154
+ gr.HTML(DESCRIPTION)
155
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
156
+ gr.ChatInterface(
157
+ fn=stream_chat,
158
+ chatbot=chatbot,
159
+ fill_height=True,
160
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
161
+ additional_inputs=[
162
+ gr.Slider(
163
+ minimum=0,
164
+ maximum=1,
165
+ step=0.1,
166
+ value=0.8,
167
+ label="Temperature",
168
+ render=False,
169
+ ),
170
+ gr.Slider(
171
+ minimum=128,
172
+ maximum=4096,
173
+ step=1,
174
+ value=1024,
175
+ label="Max new tokens",
176
+ render=False,
177
+ ),
178
+ gr.Slider(
179
+ minimum=0.0,
180
+ maximum=1.0,
181
+ step=0.1,
182
+ value=0.8,
183
+ label="top_p",
184
+ render=False,
185
+ ),
186
+ gr.Slider(
187
+ minimum=1,
188
+ maximum=20,
189
+ step=1,
190
+ value=20,
191
+ label="top_k",
192
+ render=False,
193
+ ),
194
+ gr.Slider(
195
+ minimum=0.0,
196
+ maximum=2.0,
197
+ step=0.1,
198
+ value=1.0,
199
+ label="Repetition penalty",
200
+ render=False,
201
+ ),
202
+ ],
203
+ examples=[
204
+ ["Viết một lá thư chúc mừng sinh nhật gửi bạn Thục Linh."],
205
+ ["Trường Sa và Hoàng Sa là của nước nào?"],
206
+ ["Giới thiệu về tỉ phú Elon Musk"],
207
+ ["Viết code một trang cá nhân đơn giản bằng html."],
208
+ ],
209
+ cache_examples=False,
210
+ )
211
 
212
 
213
  if __name__ == "__main__":
214
+ demo.launch()