Spaces:
Sleeping
Sleeping
sanbo
commited on
Commit
·
755ce4f
1
Parent(s):
9ddcb59
update sth. at 2024-11-15 19:19:09
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from PIL import Image
|
4 |
|
@@ -33,6 +34,39 @@ def chat_with_model(messages):
|
|
33 |
print(f"Chat generation failed: {e}")
|
34 |
return "聊天生成失败,请稍后再试。"
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# ---------- 图像生成模块 ----------
|
37 |
def image_gen(prompt):
|
38 |
"""
|
@@ -60,7 +94,7 @@ def image_gen(prompt):
|
|
60 |
|
61 |
def build_interface():
|
62 |
"""
|
63 |
-
构建 Gradio
|
64 |
"""
|
65 |
with gr.Blocks() as demo:
|
66 |
# 服务状态显示区域
|
@@ -78,6 +112,18 @@ def build_interface():
|
|
78 |
|
79 |
chatbox_button.click(chat_handler, inputs=chatbox_input, outputs=chatbox_output)
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
# 图像生成模块
|
82 |
with gr.Tab("图像生成"):
|
83 |
image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
|
@@ -97,7 +143,7 @@ def build_interface():
|
|
97 |
image_button.click(image_handler, inputs=image_prompt, outputs=[image_output_1, image_output_2])
|
98 |
|
99 |
gr.Markdown("### 使用说明")
|
100 |
-
gr.Markdown("
|
101 |
|
102 |
return demo
|
103 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import subprocess
|
3 |
from huggingface_hub import InferenceClient
|
4 |
from PIL import Image
|
5 |
|
|
|
34 |
print(f"Chat generation failed: {e}")
|
35 |
return "聊天生成失败,请稍后再试。"
|
36 |
|
37 |
+
# ---------- chatgpt-4o-mini 模块 ----------
|
38 |
+
def chatgpt_4o_mini(messages):
|
39 |
+
"""
|
40 |
+
调用 gpt-4o-mini API 进行对话。
|
41 |
+
"""
|
42 |
+
try:
|
43 |
+
# 构建请求数据
|
44 |
+
data = {
|
45 |
+
"model": "gpt-4o-mini",
|
46 |
+
"messages": [{"role": "system", "content": "你是一个辅助机器人"}] + messages,
|
47 |
+
"stream": True
|
48 |
+
}
|
49 |
+
|
50 |
+
# 执行 curl 请求
|
51 |
+
curl_command = [
|
52 |
+
"curl", "--location", "https://sanbo1200-duck2api.hf.space/completions",
|
53 |
+
"--header", "Content-Type: application/json",
|
54 |
+
"--data", str(data)
|
55 |
+
]
|
56 |
+
|
57 |
+
# 使用 subprocess 调用 curl 命令
|
58 |
+
result = subprocess.run(curl_command, capture_output=True, text=True)
|
59 |
+
response = result.stdout
|
60 |
+
|
61 |
+
if result.returncode != 0:
|
62 |
+
return "请求失败,请稍后再试。"
|
63 |
+
|
64 |
+
# 解析返回结果
|
65 |
+
return response # 这里你可以进一步解析 `response` 格式化成适当的返回信息
|
66 |
+
except Exception as e:
|
67 |
+
print(f"Error during gpt-4o-mini request: {e}")
|
68 |
+
return "gpt-4o-mini 请求失败,请稍后再试。"
|
69 |
+
|
70 |
# ---------- 图像生成模块 ----------
|
71 |
def image_gen(prompt):
|
72 |
"""
|
|
|
94 |
|
95 |
def build_interface():
|
96 |
"""
|
97 |
+
构建 Gradio 界面布局,包括文本聊天、chatgpt-4o-mini 和图像生成模块。
|
98 |
"""
|
99 |
with gr.Blocks() as demo:
|
100 |
# 服务状态显示区域
|
|
|
112 |
|
113 |
chatbox_button.click(chat_handler, inputs=chatbox_input, outputs=chatbox_output)
|
114 |
|
115 |
+
# chatgpt-4o-mini 模块
|
116 |
+
with gr.Tab("chatgpt-4o-mini"):
|
117 |
+
chatgpt_input = gr.Textbox(label="输入你的问题", placeholder="请提问...")
|
118 |
+
chatgpt_output = gr.Textbox(label="回答")
|
119 |
+
chatgpt_button = gr.Button("发送")
|
120 |
+
|
121 |
+
def chatgpt_handler(user_input):
|
122 |
+
messages = [{"role": "user", "content": user_input}]
|
123 |
+
return chatgpt_4o_mini(messages)
|
124 |
+
|
125 |
+
chatgpt_button.click(chatgpt_handler, inputs=chatgpt_input, outputs=chatgpt_output)
|
126 |
+
|
127 |
# 图像生成模块
|
128 |
with gr.Tab("图像生成"):
|
129 |
image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
|
|
|
143 |
image_button.click(image_handler, inputs=image_prompt, outputs=[image_output_1, image_output_2])
|
144 |
|
145 |
gr.Markdown("### 使用说明")
|
146 |
+
gr.Markdown("本助手支持文本聊天、chatgpt-4o-mini 和图像生成功能,使用上方选项卡切换不同功能。")
|
147 |
|
148 |
return demo
|
149 |
|