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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import modelscope_studio as mgr
|
| 7 |
+
from http import HTTPStatus
|
| 8 |
+
import dashscope
|
| 9 |
+
from dashscope import Generation
|
| 10 |
+
from dashscope.api_entities.dashscope_response import Role
|
| 11 |
+
from typing import List, Optional, Tuple, Dict
|
| 12 |
+
from urllib.error import HTTPError
|
| 13 |
+
|
| 14 |
+
default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
|
| 15 |
+
|
| 16 |
+
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
| 17 |
+
dashscope.api_key = YOUR_API_TOKEN
|
| 18 |
+
|
| 19 |
+
History = List[Tuple[str, str]]
|
| 20 |
+
Messages = List[Dict[str, str]]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
latex_delimiters = [{
|
| 24 |
+
"left": "\\(",
|
| 25 |
+
"right": "\\)",
|
| 26 |
+
"display": True
|
| 27 |
+
}, {
|
| 28 |
+
"left": "\\begin\{equation\}",
|
| 29 |
+
"right": "\\end\{equation\}",
|
| 30 |
+
"display": True
|
| 31 |
+
}, {
|
| 32 |
+
"left": "\\begin\{align\}",
|
| 33 |
+
"right": "\\end\{align\}",
|
| 34 |
+
"display": True
|
| 35 |
+
}, {
|
| 36 |
+
"left": "\\begin\{alignat\}",
|
| 37 |
+
"right": "\\end\{alignat\}",
|
| 38 |
+
"display": True
|
| 39 |
+
}, {
|
| 40 |
+
"left": "\\begin\{gather\}",
|
| 41 |
+
"right": "\\end\{gather\}",
|
| 42 |
+
"display": True
|
| 43 |
+
}, {
|
| 44 |
+
"left": "\\begin\{CD\}",
|
| 45 |
+
"right": "\\end\{CD\}",
|
| 46 |
+
"display": True
|
| 47 |
+
}, {
|
| 48 |
+
"left": "\\[",
|
| 49 |
+
"right": "\\]",
|
| 50 |
+
"display": True
|
| 51 |
+
}]
|
| 52 |
+
|
| 53 |
+
def clear_session() -> History:
|
| 54 |
+
return '', []
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def modify_system_session(system: str) -> str:
|
| 58 |
+
if system is None or len(system) == 0:
|
| 59 |
+
system = default_system
|
| 60 |
+
return system, system, []
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def history_to_messages(history: History, system: str) -> Messages:
|
| 64 |
+
messages = [{'role': Role.SYSTEM, 'content': system}]
|
| 65 |
+
for h in history:
|
| 66 |
+
messages.append({'role': Role.USER, 'content': h[0].text})
|
| 67 |
+
messages.append({'role': Role.ASSISTANT, 'content': h[1].text})
|
| 68 |
+
return messages
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
| 72 |
+
assert messages[0]['role'] == Role.SYSTEM
|
| 73 |
+
system = messages[0]['content']
|
| 74 |
+
history = []
|
| 75 |
+
for q, r in zip(messages[1::2], messages[2::2]):
|
| 76 |
+
history.append([q['content'], r['content']])
|
| 77 |
+
return system, history
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def model_chat(query: Optional[str], history: Optional[History], system: str, radio: str
|
| 81 |
+
) -> Tuple[str, str, History, str]:
|
| 82 |
+
if query is None:
|
| 83 |
+
query = ''
|
| 84 |
+
if history is None:
|
| 85 |
+
history = []
|
| 86 |
+
messages = history_to_messages(history, system)
|
| 87 |
+
messages.append({'role': Role.USER, 'content': query})
|
| 88 |
+
|
| 89 |
+
label_model = f"qwen2.5-{radio.lower()}-instruct"
|
| 90 |
+
|
| 91 |
+
gen = Generation.call(
|
| 92 |
+
model=label_model,
|
| 93 |
+
messages=messages,
|
| 94 |
+
result_format='message',
|
| 95 |
+
stream=True
|
| 96 |
+
)
|
| 97 |
+
for response in gen:
|
| 98 |
+
if response.status_code == HTTPStatus.OK:
|
| 99 |
+
role = response.output.choices[0].message.role
|
| 100 |
+
response = response.output.choices[0].message.content
|
| 101 |
+
system, history = messages_to_history(messages + [{'role': role, 'content': response}])
|
| 102 |
+
yield '', history, system
|
| 103 |
+
else:
|
| 104 |
+
raise ValueError('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
|
| 105 |
+
response.request_id, response.status_code,
|
| 106 |
+
response.code, response.message
|
| 107 |
+
))
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def chiose_radio(radio, system):
|
| 111 |
+
mark_ = gr.Markdown(value=f"<center><font size=8>Qwen2.5-{radio}-instructπΎ</center>")
|
| 112 |
+
chatbot = mgr.Chatbot(label=f'{radio.lower()}')
|
| 113 |
+
|
| 114 |
+
if system is None or len(system) == 0:
|
| 115 |
+
system = default_system
|
| 116 |
+
|
| 117 |
+
return mark_, chatbot, system, system, ""
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def update_other_radios(value, other_radio1, other_radio2):
|
| 121 |
+
if value == "":
|
| 122 |
+
if other_radio1 != "":
|
| 123 |
+
selected = other_radio1
|
| 124 |
+
else:
|
| 125 |
+
selected = other_radio2
|
| 126 |
+
return selected, other_radio1, other_radio2
|
| 127 |
+
return value, "", ""
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def main():
|
| 131 |
+
# εε»ΊδΈ€δΈͺζ ηΎ
|
| 132 |
+
with gr.Blocks() as demo:
|
| 133 |
+
gr.Markdown("""<center><font size=8>Qwen2.5: A Party of Foundation Models!</center>""")
|
| 134 |
+
with gr.Row():
|
| 135 |
+
options_1 = ["72B", "32B", "14B", "7B", "3B", "1.5B", "0.5B"]
|
| 136 |
+
options_math = ["Math-72B", "Math-7B", "Math-1.5B"]
|
| 137 |
+
options_coder = ["Coder-7B", "Coder-1.5B"]
|
| 138 |
+
with gr.Row():
|
| 139 |
+
radio1 = gr.Radio(choices=options_1, label="Qwen2.5οΌ", value=options_1[0])
|
| 140 |
+
with gr.Row():
|
| 141 |
+
radio2 = gr.Radio(choices=options_math, label="Qwen2.5-MathοΌ")
|
| 142 |
+
with gr.Row():
|
| 143 |
+
radio3 = gr.Radio(choices=options_coder, label="Qwen2.5-CoderοΌ")
|
| 144 |
+
|
| 145 |
+
radio = gr.Radio(value=options_1[0], visible=False)
|
| 146 |
+
radio1.change(fn=update_other_radios, inputs=[radio1, radio2, radio3], outputs=[radio, radio2, radio3])
|
| 147 |
+
radio2.change(fn=update_other_radios, inputs=[radio2, radio1, radio3], outputs=[radio, radio1, radio3])
|
| 148 |
+
radio3.change(fn=update_other_radios, inputs=[radio3, radio1, radio2], outputs=[radio, radio1, radio2])
|
| 149 |
+
|
| 150 |
+
with gr.Row():
|
| 151 |
+
with gr.Accordion():
|
| 152 |
+
mark_ = gr.Markdown("""<center><font size=8>Qwen2.5-72B-instruct πΎ</center>""")
|
| 153 |
+
with gr.Row():
|
| 154 |
+
with gr.Column(scale=3):
|
| 155 |
+
system_input = gr.Textbox(value=default_system, lines=1, label='System')
|
| 156 |
+
with gr.Column(scale=1):
|
| 157 |
+
modify_system = gr.Button("π οΈ Set system prompt and clear history", scale=2)
|
| 158 |
+
system_state = gr.Textbox(value=default_system, visible=False)
|
| 159 |
+
chatbot = mgr.Chatbot(label=options_1[0].lower(), latex_delimiters=latex_delimiters)
|
| 160 |
+
textbox = gr.Textbox(lines=1, label='Input')
|
| 161 |
+
|
| 162 |
+
with gr.Row():
|
| 163 |
+
clear_history = gr.Button("π§Ή Clear history")
|
| 164 |
+
sumbit = gr.Button("π Send")
|
| 165 |
+
|
| 166 |
+
textbox.submit(model_chat,
|
| 167 |
+
inputs=[textbox, chatbot, system_state, radio],
|
| 168 |
+
outputs=[textbox, chatbot, system_input])
|
| 169 |
+
|
| 170 |
+
sumbit.click(model_chat,
|
| 171 |
+
inputs=[textbox, chatbot, system_state, radio],
|
| 172 |
+
outputs=[textbox, chatbot, system_input],
|
| 173 |
+
concurrency_limit=5)
|
| 174 |
+
clear_history.click(fn=clear_session,
|
| 175 |
+
inputs=[],
|
| 176 |
+
outputs=[textbox, chatbot])
|
| 177 |
+
modify_system.click(fn=modify_system_session,
|
| 178 |
+
inputs=[system_input],
|
| 179 |
+
outputs=[system_state, system_input, chatbot])
|
| 180 |
+
|
| 181 |
+
radio.change(chiose_radio,
|
| 182 |
+
inputs=[radio, system_input],
|
| 183 |
+
outputs=[mark_, chatbot, system_state, system_input, textbox])
|
| 184 |
+
|
| 185 |
+
demo.queue(api_open=False,default_concurrency_limit=40)
|
| 186 |
+
demo.launch(max_threads=40)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
if __name__ == "__main__":
|
| 190 |
+
main()
|