Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,147 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
"""
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
|
62 |
if __name__ == "__main__":
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
import gradio as gr
|
4 |
+
import spaces
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
+
import os
|
7 |
+
from threading import Thread
|
8 |
|
9 |
+
|
10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
11 |
+
MODEL_ID = "Qwen/Qwen2-7B-Instruct"
|
12 |
+
MODELS = os.environ.get("MODELS")
|
13 |
+
MODEL_NAME = MODELS.split("/")[-1]
|
14 |
+
|
15 |
+
TITLE = "<h1><center>Qwen2-Vietnamese</center></h1>"
|
16 |
+
|
17 |
+
DESCRIPTION = f"""
|
18 |
+
<h3>MODEL: <a href="https://hf.co/{MODELS}">{MODEL_NAME}</a></h3>
|
19 |
+
<center>
|
20 |
+
<p>Qwen2 is the large language model built by Alibaba Cloud.
|
21 |
+
<br>
|
22 |
+
Feel free to test without log.
|
23 |
+
</p>
|
24 |
+
</center>
|
25 |
"""
|
26 |
+
|
27 |
+
CSS = """
|
28 |
+
.duplicate-button {
|
29 |
+
margin: auto !important;
|
30 |
+
color: white !important;
|
31 |
+
background: black !important;
|
32 |
+
border-radius: 100vh !important;
|
33 |
+
}
|
34 |
+
h3 {
|
35 |
+
text-align: center;
|
36 |
+
}
|
37 |
"""
|
|
|
38 |
|
39 |
+
model = AutoModelForCausalLM.from_pretrained(
|
40 |
+
MODELS,
|
41 |
+
torch_dtype=torch.float16,
|
42 |
+
device_map="auto",
|
43 |
+
)
|
44 |
+
tokenizer = AutoTokenizer.from_pretrained(MODELS)
|
45 |
|
46 |
+
@spaces.GPU
|
47 |
+
def stream_chat(message: str, history: list, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
|
48 |
+
print(f'message is - {message}')
|
49 |
+
print(f'history is - {history}')
|
50 |
+
conversation = []
|
51 |
+
for prompt, answer in history:
|
52 |
+
conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
|
53 |
+
conversation.append({"role": "user", "content": message})
|
|
|
54 |
|
55 |
+
print(f"Conversation is -\n{conversation}")
|
56 |
+
|
57 |
+
input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
58 |
+
inputs = tokenizer(input_ids, return_tensors="pt").to(0)
|
59 |
+
|
60 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
61 |
|
62 |
+
generate_kwargs = dict(
|
63 |
+
inputs,
|
64 |
+
streamer=streamer,
|
65 |
+
top_k=top_k,
|
66 |
+
top_p=top_p,
|
67 |
+
repetition_penalty=penalty,
|
68 |
+
max_new_tokens=max_new_tokens,
|
69 |
+
do_sample=True,
|
70 |
+
temperature=temperature,
|
71 |
+
eos_token_id = [151645, 151643],
|
72 |
+
)
|
73 |
+
|
74 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
75 |
+
thread.start()
|
76 |
|
77 |
+
buffer = ""
|
78 |
+
for new_text in streamer:
|
79 |
+
buffer += new_text
|
80 |
+
yield buffer
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
|
|
|
|
83 |
|
84 |
+
chatbot = gr.Chatbot(height=450)
|
85 |
+
|
86 |
+
with gr.Blocks(css=CSS) as demo:
|
87 |
+
gr.HTML(TITLE)
|
88 |
+
gr.HTML(DESCRIPTION)
|
89 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
90 |
+
gr.ChatInterface(
|
91 |
+
fn=stream_chat,
|
92 |
+
chatbot=chatbot,
|
93 |
+
fill_height=True,
|
94 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
95 |
+
additional_inputs=[
|
96 |
+
gr.Slider(
|
97 |
+
minimum=0,
|
98 |
+
maximum=1,
|
99 |
+
step=0.1,
|
100 |
+
value=0.8,
|
101 |
+
label="Temperature",
|
102 |
+
render=False,
|
103 |
+
),
|
104 |
+
gr.Slider(
|
105 |
+
minimum=128,
|
106 |
+
maximum=4096,
|
107 |
+
step=1,
|
108 |
+
value=1024,
|
109 |
+
label="Max new tokens",
|
110 |
+
render=False,
|
111 |
+
),
|
112 |
+
gr.Slider(
|
113 |
+
minimum=0.0,
|
114 |
+
maximum=1.0,
|
115 |
+
step=0.1,
|
116 |
+
value=0.8,
|
117 |
+
label="top_p",
|
118 |
+
render=False,
|
119 |
+
),
|
120 |
+
gr.Slider(
|
121 |
+
minimum=1,
|
122 |
+
maximum=20,
|
123 |
+
step=1,
|
124 |
+
value=20,
|
125 |
+
label="top_k",
|
126 |
+
render=False,
|
127 |
+
),
|
128 |
+
gr.Slider(
|
129 |
+
minimum=0.0,
|
130 |
+
maximum=2.0,
|
131 |
+
step=0.1,
|
132 |
+
value=1.0,
|
133 |
+
label="Repetition penalty",
|
134 |
+
render=False,
|
135 |
+
),
|
136 |
+
],
|
137 |
+
examples=[
|
138 |
+
["Viết một lá thư chúc mừng sinh nhật gửi bạn Thục Linh."],
|
139 |
+
["Trường Sa và Hoàng Sa là của nước nào?"],
|
140 |
+
["Giới thiệu về tỉ phú Elon Musk"],
|
141 |
+
["Viết code một trang cá nhân đơn giản bằng html."],
|
142 |
+
],
|
143 |
+
cache_examples=False,
|
144 |
+
)
|
145 |
|
146 |
|
147 |
if __name__ == "__main__":
|