Spaces:
Running
Running
delete
Browse files
app.py
DELETED
@@ -1,146 +0,0 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
-
import os
|
4 |
-
from http.client import HTTPMessage
|
5 |
-
|
6 |
-
os.system('pip install dashscope')
|
7 |
-
|
8 |
-
import gradio as gr
|
9 |
-
from http import HTTPStatus
|
10 |
-
import dashscope
|
11 |
-
from dashscope import Generation
|
12 |
-
from dashscope.api_entities.dashscope_response import Role
|
13 |
-
from typing import List, Optional, Tuple, Dict
|
14 |
-
from urllib.error import HTTPError
|
15 |
-
|
16 |
-
default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
|
17 |
-
|
18 |
-
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
19 |
-
dashscope.api_key = YOUR_API_TOKEN
|
20 |
-
|
21 |
-
History = List[Tuple[str, str]]
|
22 |
-
Messages = List[Dict[str, str]]
|
23 |
-
|
24 |
-
|
25 |
-
def clear_session() -> History:
|
26 |
-
return '', []
|
27 |
-
|
28 |
-
|
29 |
-
def modify_system_session(system: str) -> str:
|
30 |
-
if system is None or len(system) == 0:
|
31 |
-
system = default_system
|
32 |
-
return system, system, []
|
33 |
-
|
34 |
-
|
35 |
-
def history_to_messages(history: History, system: str) -> Messages:
|
36 |
-
messages = [{'role': Role.SYSTEM, 'content': system}]
|
37 |
-
for h in history:
|
38 |
-
messages.append({'role': Role.USER, 'content': h[0]})
|
39 |
-
messages.append({'role': Role.ASSISTANT, 'content': h[1]})
|
40 |
-
return messages
|
41 |
-
|
42 |
-
|
43 |
-
def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
44 |
-
assert messages[0]['role'] == Role.SYSTEM
|
45 |
-
system = messages[0]['content']
|
46 |
-
history = []
|
47 |
-
for q, r in zip(messages[1::2], messages[2::2]):
|
48 |
-
history.append([q['content'], r['content']])
|
49 |
-
return system, history
|
50 |
-
|
51 |
-
|
52 |
-
def model_chat(query: Optional[str], history: Optional[History], system: str, radio: str
|
53 |
-
) -> Tuple[str, str, History]:
|
54 |
-
if query is None:
|
55 |
-
query = ''
|
56 |
-
if history is None:
|
57 |
-
history = []
|
58 |
-
messages = history_to_messages(history, system)
|
59 |
-
messages.append({'role': Role.USER, 'content': query})
|
60 |
-
label_model = f"qwen2.5-coder-{radio.lower()}-instruct"
|
61 |
-
gen = Generation.call(
|
62 |
-
model=label_model,
|
63 |
-
messages=messages,
|
64 |
-
result_format='message',
|
65 |
-
stream=True
|
66 |
-
)
|
67 |
-
for response in gen:
|
68 |
-
if response.status_code == HTTPStatus.OK:
|
69 |
-
role = response.output.choices[0].message.role
|
70 |
-
response = response.output.choices[0].message.content
|
71 |
-
system, history = messages_to_history(messages + [{'role': role, 'content': response}])
|
72 |
-
yield '', history, system
|
73 |
-
else:
|
74 |
-
raise HTTPError(code=404, msg='Request id: %s, Status code: %s, error code: %s, error message: %s' % (
|
75 |
-
response.request_id, response.status_code,
|
76 |
-
response.code, response.message), hdrs=HTTPMessage(), url='http://example.com', fp=None)
|
77 |
-
|
78 |
-
|
79 |
-
def chiose_radio(radio, system):
|
80 |
-
mark_ = gr.Markdown(value=f"<center><font size=8>Qwen2.5-Coder-{radio}-instruct👾</center>")
|
81 |
-
chatbot = gr.Chatbot(label=f'Qwen2.5-Coder-{radio.lower()}-instruct')
|
82 |
-
|
83 |
-
if system is None or len(system) == 0:
|
84 |
-
system = default_system
|
85 |
-
|
86 |
-
return mark_, chatbot, system, system, ""
|
87 |
-
|
88 |
-
|
89 |
-
def update_other_radios(value, other_radio1, other_radio2):
|
90 |
-
if value == "":
|
91 |
-
if other_radio1 != "":
|
92 |
-
selected = other_radio1
|
93 |
-
else:
|
94 |
-
selected = other_radio2
|
95 |
-
return selected, other_radio1, other_radio2
|
96 |
-
return value, "", ""
|
97 |
-
|
98 |
-
|
99 |
-
def main():
|
100 |
-
# 创建两个标签
|
101 |
-
with gr.Blocks() as demo:
|
102 |
-
with gr.Row():
|
103 |
-
options_coder = ["0.5B", "1.5B", "3B", "7B", "14B", "32B", ]
|
104 |
-
with gr.Row():
|
105 |
-
radio = gr.Radio(choices=options_coder, label="Qwen2.5-Coder:", value="32B")
|
106 |
-
|
107 |
-
with gr.Row():
|
108 |
-
with gr.Accordion():
|
109 |
-
mark_ = gr.Markdown("""<center><font size=8>Qwen2.5-Coder-32B-Instruct Bot👾</center>""")
|
110 |
-
with gr.Row():
|
111 |
-
with gr.Column(scale=3):
|
112 |
-
system_input = gr.Textbox(value=default_system, lines=1, label='System')
|
113 |
-
with gr.Column(scale=1):
|
114 |
-
modify_system = gr.Button("🛠️ Set system prompt and clear history", scale=2)
|
115 |
-
system_state = gr.Textbox(value=default_system, visible=False)
|
116 |
-
chatbot = gr.Chatbot(label='Qwen2.5-Coder-32B-Instruct')
|
117 |
-
textbox = gr.Textbox(lines=1, label='Input')
|
118 |
-
|
119 |
-
with gr.Row():
|
120 |
-
clear_history = gr.Button("🧹 Clear History")
|
121 |
-
sumbit = gr.Button("🚀 Send")
|
122 |
-
|
123 |
-
textbox.submit(model_chat,
|
124 |
-
inputs=[textbox, chatbot, system_state, radio],
|
125 |
-
outputs=[textbox, chatbot, system_input])
|
126 |
-
sumbit.click(model_chat,
|
127 |
-
inputs=[textbox, chatbot, system_state, radio],
|
128 |
-
outputs=[textbox, chatbot, system_input],
|
129 |
-
concurrency_limit=100)
|
130 |
-
clear_history.click(fn=clear_session,
|
131 |
-
inputs=[],
|
132 |
-
outputs=[textbox, chatbot])
|
133 |
-
modify_system.click(fn=modify_system_session,
|
134 |
-
inputs=[system_input],
|
135 |
-
outputs=[system_state, system_input, chatbot])
|
136 |
-
|
137 |
-
radio.change(chiose_radio,
|
138 |
-
inputs=[radio, system_input],
|
139 |
-
outputs=[mark_, chatbot, system_state, system_input, textbox])
|
140 |
-
|
141 |
-
demo.queue(api_open=False, default_concurrency_limit=40)
|
142 |
-
demo.launch(max_threads=5)
|
143 |
-
|
144 |
-
|
145 |
-
if __name__ == "__main__":
|
146 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|