Spaces:
Runtime error
Runtime error
littlebird13
commited on
Commit
•
e77241a
1
Parent(s):
dc69b27
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import modelscope_studio as mgr
|
3 |
+
from http import HTTPStatus
|
4 |
+
import os
|
5 |
+
from dashscope import MultiModalConversation
|
6 |
+
import dashscope
|
7 |
+
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
8 |
+
dashscope.api_key = YOUR_API_TOKEN
|
9 |
+
|
10 |
+
def add_text(chatbot, task_history, input):
|
11 |
+
"""Add text to the chat history."""
|
12 |
+
task_history.append({"role": "user", "content": [{"text": input.text}]})
|
13 |
+
task_history.append({"role": "user", "content": [{"audio": input.files[0].path}]})
|
14 |
+
chatbot.append([{
|
15 |
+
"text": input.text,
|
16 |
+
"files": input.files,
|
17 |
+
}, None])
|
18 |
+
return chatbot, task_history, None
|
19 |
+
|
20 |
+
|
21 |
+
# def add_mic(chatbot, task_history, mic):
|
22 |
+
# """Add audio to the chat history."""
|
23 |
+
# task_history.append({"role": "user", "content": [{"audio": mic}]})
|
24 |
+
# chatbot.append((f"[Audio input: {mic}]", None))
|
25 |
+
# return chatbot, task_history
|
26 |
+
|
27 |
+
def add_file(chatbot, task_history, audio_file):
|
28 |
+
"""Add audio file to the chat history."""
|
29 |
+
task_history.append({"role": "user", "content": [{"audio": audio_file.name}]})
|
30 |
+
chatbot.append((f"[Audio file: {audio_file.name}]", None))
|
31 |
+
return chatbot, task_history
|
32 |
+
|
33 |
+
|
34 |
+
def reset_user_input():
|
35 |
+
"""Reset the user input field."""
|
36 |
+
return gr.Textbox.update(value='')
|
37 |
+
|
38 |
+
def reset_state(task_history):
|
39 |
+
"""Reset the chat history."""
|
40 |
+
return [], []
|
41 |
+
|
42 |
+
def regenerate(chatbot, task_history):
|
43 |
+
"""Regenerate the last bot response."""
|
44 |
+
if task_history and task_history[-1]['role'] == 'assistant':
|
45 |
+
task_history.pop()
|
46 |
+
chatbot.pop()
|
47 |
+
if task_history:
|
48 |
+
chatbot, task_history = predict(chatbot, task_history)
|
49 |
+
return chatbot, task_history
|
50 |
+
|
51 |
+
|
52 |
+
def predict(chatbot, task_history):
|
53 |
+
"""Generate a response from the model."""
|
54 |
+
response = MultiModalConversation.call(model='qwen2-audio-instruct',
|
55 |
+
messages=task_history)
|
56 |
+
if response.status_code == HTTPStatus.OK:
|
57 |
+
output_text = response.output.choices[0].message.content
|
58 |
+
if isinstance(output_text, list):
|
59 |
+
output_text = next((item.get('text') for item in output_text if 'text' in item), '')
|
60 |
+
elif isinstance(output_text, dict):
|
61 |
+
output_text = output_text.get('text', '')
|
62 |
+
task_history.append({'role': response.output.choices[0].message.role,
|
63 |
+
'content': [{'text': output_text}]})
|
64 |
+
chatbot.append((None, output_text)) # Add the response to chatbot
|
65 |
+
return chatbot, task_history
|
66 |
+
else:
|
67 |
+
error_message = f"Failed to get a response: {response.code} - {response.message}"
|
68 |
+
chatbot.append((None, error_message)) # Add the error message to chatbot
|
69 |
+
return chatbot, task_history
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("""<p align="center"><img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/qwen.png" style="height: 80px"/><p>""") ## todo
|
75 |
+
gr.Markdown("""<center><font size=8>Qwen2-Audio-Instruct Bot</center>""")
|
76 |
+
gr.Markdown(
|
77 |
+
"""\
|
78 |
+
<center><font size=3>This WebUI is based on Qwen2-Audio-Instruct, developed by Alibaba Cloud. \
|
79 |
+
(本WebUI基于Qwen2-Audio-Instruct打造,实现聊天机器人功能。)</center>""")
|
80 |
+
gr.Markdown("""\
|
81 |
+
<center><font size=4>Qwen2-Audio <a href="https://modelscope.cn/models/qwen/Qwen2-Audio-7B">🤖 </a>
|
82 |
+
| <a href="https://huggingface.co/Qwen/Qwen2-Audio-7B">🤗</a>  |
|
83 |
+
Qwen2-Audio-Instruct <a href="https://modelscope.cn/models/qwen/Qwen2-Audio-7B-Instruct">🤖 </a> |
|
84 |
+
<a href="https://huggingface.co/Qwen/Qwen2-Audio-7B-Instruct">🤗</a>  |
|
85 |
+
 <a href="https://github.com/QwenLM/Qwen2-Audio">Github</a></center>""")
|
86 |
+
chatbot = mgr.Chatbot(label='Qwen2-Audio-7B-Instruct', elem_classes="control-height", height=750)
|
87 |
+
# query = gr.Textbox(lines=2, label='Input')
|
88 |
+
# mic = gr.Audio(source="microphone", type="filepath")
|
89 |
+
user_input = mgr.MultimodalInput(
|
90 |
+
interactive=True,
|
91 |
+
sources=['microphone'],
|
92 |
+
submit_button_props=dict(value="🚀 Submit (发送)")
|
93 |
+
)
|
94 |
+
task_history = gr.State([])
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
empty_bin = gr.Button("🧹 Clear History (清除历史)")
|
98 |
+
# submit_btn = gr.Button("🚀 Submit (发送)")
|
99 |
+
regen_btn = gr.Button("🤔️ Regenerate (重试)")
|
100 |
+
addfile_btn = gr.UploadButton("📁 Upload (上传文件)", file_types=["audio"])
|
101 |
+
|
102 |
+
# mic.change(add_mic, [chatbot, task_history, mic], [chatbot, task_history])
|
103 |
+
# submit_btn.click(add_text, [chatbot, task_history, query], [chatbot, task_history]).then(
|
104 |
+
# predict, [chatbot, task_history], [chatbot, task_history], show_progress=True
|
105 |
+
# )
|
106 |
+
|
107 |
+
# submit_btn.click(reset_user_input, [], [query])
|
108 |
+
user_input.submit(fn=add_text,
|
109 |
+
inputs=[chatbot, task_history, user_input],
|
110 |
+
outputs=[chatbot, task_history, user_input]).then(
|
111 |
+
predict, [chatbot, task_history], [chatbot, task_history], show_progress=True
|
112 |
+
)
|
113 |
+
empty_bin.click(reset_state, outputs=[chatbot, task_history], show_progress=True)
|
114 |
+
regen_btn.click(regenerate, [chatbot, task_history], [chatbot, task_history], show_progress=True)
|
115 |
+
addfile_btn.upload(add_file, [chatbot, task_history, addfile_btn], [chatbot, task_history], show_progress=True)
|
116 |
+
|
117 |
+
demo.queue().launch(
|
118 |
+
share=False,
|
119 |
+
inbrowser=True,
|
120 |
+
server_port=7860,
|
121 |
+
server_name="0.0.0.0",
|
122 |
+
)
|