Spaces:
Sleeping
Sleeping
mimifuel2018
commited on
Commit
•
6436fcc
1
Parent(s):
747d900
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,108 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
import os
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
+
from http import HTTPStatus
|
4 |
+
import dashscope
|
5 |
+
from dashscope import Generation
|
6 |
+
from dashscope.api_entities.dashscope_response import Role
|
7 |
+
from typing import List, Optional, Tuple, Dict
|
8 |
+
|
9 |
+
default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
|
10 |
+
|
11 |
+
dashscope.api_key = os.getenv('YOUR_API_TOKEN') # Ensure this token is managed securely
|
12 |
+
|
13 |
+
History = List[Tuple[str, str]]
|
14 |
+
Messages = List[Dict[str, str]]
|
15 |
+
|
16 |
+
def clear_session() -> History:
|
17 |
+
return '', []
|
18 |
+
|
19 |
+
def modify_system_session(system: str) -> str:
|
20 |
+
if system is None or len(system) == 0:
|
21 |
+
system = default_system
|
22 |
+
return system, system, []
|
23 |
+
|
24 |
+
def history_to_messages(history: History, system: str) -> Messages:
|
25 |
+
messages = [{'role': Role.SYSTEM, 'content': system}]
|
26 |
+
for h in history:
|
27 |
+
messages.append({'role': Role.USER, 'content': h[0]})
|
28 |
+
messages.append({'role': Role.ASSISTANT, 'content': h[1]})
|
29 |
+
return messages
|
30 |
+
|
31 |
+
def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
32 |
+
assert messages[0]['role'] == Role.SYSTEM
|
33 |
+
system = messages[0]['content']
|
34 |
+
history = []
|
35 |
+
for q, r in zip(messages[1::2], messages[2::2]):
|
36 |
+
history.append([q['content'], r['content']])
|
37 |
+
return system, history
|
38 |
+
|
39 |
+
def model_chat(query: Optional[str], history: Optional[History], system: str
|
40 |
+
) -> Tuple[str, str, History]:
|
41 |
+
if query is None:
|
42 |
+
query = ''
|
43 |
+
if history is None:
|
44 |
+
history = []
|
45 |
+
messages = history_to_messages(history, system)
|
46 |
+
messages.append({'role': Role.USER, 'content': query})
|
47 |
+
|
48 |
+
# Request generation with controlled parameters
|
49 |
+
gen = Generation.call(
|
50 |
+
model='qwen2.5-72b-instruct',
|
51 |
+
messages=messages,
|
52 |
+
result_format='message',
|
53 |
+
stream=True,
|
54 |
+
max_new_tokens=150 # Set response length limit
|
55 |
+
)
|
56 |
+
|
57 |
+
for response in gen:
|
58 |
+
if response.status_code == HTTPStatus.OK:
|
59 |
+
role = response.output.choices[0].message.role
|
60 |
+
response = response.output.choices[0].message.content
|
61 |
+
system, history = messages_to_history(messages + [{'role': role, 'content': response}])
|
62 |
+
yield '', history, system
|
63 |
+
else:
|
64 |
+
raise ValueError('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
|
65 |
+
response.request_id, response.status_code,
|
66 |
+
response.code, response.message
|
67 |
+
))
|
68 |
+
|
69 |
+
# Gradio Interface Setup
|
70 |
+
with gr.Blocks() as demo:
|
71 |
+
gr.Markdown("""<center><font size=8>Qwen2.5-72B-Instruct👾</center>""")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column(scale=3):
|
75 |
+
system_input = gr.Textbox(value=default_system, lines=1, label='System')
|
76 |
+
with gr.Column(scale=1):
|
77 |
+
modify_system = gr.Button("🛠️ Set system prompt and clear history", scale=2)
|
78 |
+
system_state = gr.Textbox(value=default_system, visible=False)
|
79 |
+
chatbot = gr.Chatbot(label='Qwen2.5-72B-Instruct')
|
80 |
+
textbox = gr.Textbox(lines=1, label='Input')
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
clear_history = gr.Button("🧹 Clear history")
|
84 |
+
submit = gr.Button("🚀 Send")
|
85 |
+
|
86 |
+
textbox.submit(model_chat,
|
87 |
+
inputs=[textbox, chatbot, system_state],
|
88 |
+
outputs=[textbox, chatbot, system_input],
|
89 |
+
concurrency_limit=5) # Reduced for free plan
|
90 |
+
|
91 |
+
submit.click(model_chat,
|
92 |
+
inputs=[textbox, chatbot, system_state],
|
93 |
+
outputs=[textbox, chatbot, system_input],
|
94 |
+
concurrency_limit=5)
|
95 |
+
|
96 |
+
clear_history.click(fn=clear_session,
|
97 |
+
inputs=[],
|
98 |
+
outputs=[textbox, chatbot],
|
99 |
+
concurrency_limit=5)
|
100 |
+
|
101 |
+
modify_system.click(fn=modify_system_session,
|
102 |
+
inputs=[system_input],
|
103 |
+
outputs=[system_state, system_input, chatbot],
|
104 |
+
concurrency_limit=5)
|
105 |
+
|
106 |
+
# Launching with reduced threads for free plan
|
107 |
+
demo.queue(api_open=False)
|
108 |
+
demo.launch(max_threads=10)
|