Spaces:
Sleeping
Sleeping
Update app-4.py
Browse files
app-4.py
CHANGED
@@ -1,130 +1,55 @@
|
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
"files": input.files,
|
23 |
-
}, None])
|
24 |
-
return chatbot, task_history, None
|
25 |
-
|
26 |
-
|
27 |
-
# def add_mic(chatbot, task_history, mic):
|
28 |
-
# """Add audio to the chat history."""
|
29 |
-
# task_history.append({"role": "user", "content": [{"audio": mic}]})
|
30 |
-
# chatbot.append((f"[Audio input: {mic}]", None))
|
31 |
-
# return chatbot, task_history
|
32 |
-
|
33 |
-
def add_file(chatbot, task_history, audio_file):
|
34 |
-
"""Add audio file to the chat history."""
|
35 |
-
task_history.append({"role": "user", "content": [{"audio": audio_file.name}]})
|
36 |
-
chatbot.append((f"[Audio file: {audio_file.name}]", None))
|
37 |
-
return chatbot, task_history
|
38 |
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
def reset_state(task_history):
|
45 |
-
"""Reset the chat history."""
|
46 |
-
return [], []
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
if task_history and task_history[-1]['role'] == 'assistant':
|
51 |
-
task_history.pop()
|
52 |
-
chatbot.pop()
|
53 |
-
if task_history:
|
54 |
-
chatbot, task_history = predict(chatbot, task_history)
|
55 |
-
return chatbot, task_history
|
56 |
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
response = MultiModalConversation.call(model='qwen2-audio-instruct',
|
61 |
-
messages=task_history)
|
62 |
-
if response.status_code == HTTPStatus.OK:
|
63 |
-
output_text = response.output.choices[0].message.content
|
64 |
-
if isinstance(output_text, list):
|
65 |
-
output_text = next((item.get('text') for item in output_text if 'text' in item), '')
|
66 |
-
elif isinstance(output_text, dict):
|
67 |
-
output_text = output_text.get('text', '')
|
68 |
-
task_history.append({'role': response.output.choices[0].message.role,
|
69 |
-
'content': [{'text': output_text}]})
|
70 |
-
chatbot.append((None, output_text)) # Add the response to chatbot
|
71 |
-
return chatbot, task_history
|
72 |
-
else:
|
73 |
-
error_message = f"Failed to get a response: {response.code} - {response.message}"
|
74 |
-
chatbot.append((None, error_message)) # Add the error message to chatbot
|
75 |
-
return chatbot, task_history
|
76 |
|
|
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
(本WebUI基于Qwen2-Audio-Instruct打造,实现聊天机器人功能。)</center>""")
|
86 |
-
gr.Markdown("""\
|
87 |
-
<center><font size=4>Qwen2-Audio <a href="https://modelscope.cn/models/qwen/Qwen2-Audio-7B">🤖 </a>
|
88 |
-
| <a href="https://huggingface.co/Qwen/Qwen2-Audio-7B">🤗</a>  |
|
89 |
-
Qwen2-Audio-Instruct <a href="https://modelscope.cn/models/qwen/Qwen2-Audio-7B-Instruct">🤖 </a> |
|
90 |
-
<a href="https://huggingface.co/Qwen/Qwen2-Audio-7B-Instruct">🤗</a>  |
|
91 |
-
 <a href="https://github.com/QwenLM/Qwen2-Audio">Github</a></center>""")
|
92 |
-
chatbot = mgr.Chatbot(label='Qwen2-Audio-7B-Instruct', elem_classes="control-height", height=750)
|
93 |
-
# query = gr.Textbox(lines=2, label='Input')
|
94 |
-
# mic = gr.Audio(source="microphone", type="filepath")
|
95 |
-
user_input = mgr.MultimodalInput(
|
96 |
-
interactive=True,
|
97 |
-
sources=['microphone', 'upload'],
|
98 |
-
submit_button_props=dict(value="🚀 Submit (发送)"),
|
99 |
-
upload_button_props=dict(value="📁 Upload (上传文件)", show_progress=True),
|
100 |
)
|
101 |
-
task_history = gr.State([])
|
102 |
-
|
103 |
-
with gr.Row():
|
104 |
-
empty_bin = gr.Button("🧹 Clear History (清除历史)")
|
105 |
-
# submit_btn = gr.Button("🚀 Submit (发送)")
|
106 |
-
regen_btn = gr.Button("🤔️ Regenerate (重试)")
|
107 |
-
# addfile_btn = gr.UploadButton("📁 Upload (上传文件)", file_types=["audio"])
|
108 |
|
109 |
-
#
|
110 |
-
|
111 |
-
# predict, [chatbot, task_history], [chatbot, task_history], show_progress=True
|
112 |
-
# )
|
113 |
|
114 |
-
#
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
)
|
120 |
-
empty_bin.click(reset_state, outputs=[chatbot, task_history], show_progress=True,concurrency_limit = 40)
|
121 |
-
regen_btn.click(regenerate, [chatbot, task_history], [chatbot, task_history], show_progress=True,concurrency_limit = 40)
|
122 |
-
# addfile_btn.upload(add_file, [chatbot, task_history, addfile_btn], [chatbot, task_history], show_progress=True)
|
123 |
|
124 |
-
|
125 |
-
|
126 |
-
inbrowser=True,
|
127 |
-
server_port=7860,
|
128 |
-
server_name="0.0.0.0",
|
129 |
-
max_threads=40
|
130 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
from openai import OpenAI
|
3 |
+
from IPython.display import Image, display
|
4 |
+
# 直接在程式碼中設置 API 金鑰(不推薦,僅供參考)
|
5 |
+
openai.api_key = "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"
|
6 |
+
|
7 |
+
response = openai.images.generate(
|
8 |
+
model="dall-e-3",
|
9 |
+
prompt="a white siamese cat",
|
10 |
+
size="1024x1024",
|
11 |
+
quality="standard",
|
12 |
+
n=1,
|
13 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
image_url = response.data[0].url
|
16 |
|
17 |
+
# 显示图片
|
18 |
+
if image_url.startswith("http"):
|
19 |
+
display(Image(url=image_url))
|
20 |
+
else:
|
21 |
+
print("Error displaying image:", image_url)
|
22 |
|
|
|
|
|
|
|
23 |
|
24 |
+
import openai
|
25 |
+
from IPython.display import Image, display
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# 让用户输入 OpenAI API 密钥
|
28 |
+
api_key = input("Enter your OpenAI API Key: ")
|
29 |
|
30 |
+
# 让用户输入提示 (prompt)
|
31 |
+
prompt = input("Enter your prompt (e.g., 'a white siamese cat'): ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# 设置用户输入的 API 密钥
|
34 |
+
openai.api_key = api_key
|
35 |
|
36 |
+
# 调用 OpenAI API 生成图像
|
37 |
+
try:
|
38 |
+
response = openai.images.generate(
|
39 |
+
model="dall-e-3",
|
40 |
+
prompt=prompt,
|
41 |
+
n=1,
|
42 |
+
size="1024x1024"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# 获取生成的图像 URL
|
46 |
+
image_url = response.data[0].url
|
|
|
|
|
47 |
|
48 |
+
# 显示图像
|
49 |
+
if image_url.startswith("http"):
|
50 |
+
display(Image(url=image_url))
|
51 |
+
else:
|
52 |
+
print("Error displaying image:", image_url)
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
except Exception as e:
|
55 |
+
print(f"Error generating image: {e}”)
|
|
|
|
|
|
|
|
|
|