Spaces:
Sleeping
Sleeping
sanbo
commited on
Commit
·
cc873a6
1
Parent(s):
755ce4f
update sth. at 2024-11-15 19:25:36
Browse files- app.py +31 -18
- requirements.txt +1 -0
app.py
CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
|
|
2 |
import subprocess
|
3 |
from huggingface_hub import InferenceClient
|
4 |
from PIL import Image
|
5 |
-
|
|
|
6 |
# ===================== 核心逻辑模块 =====================
|
7 |
|
8 |
# 初始化模型客户端
|
@@ -37,7 +38,7 @@ def chat_with_model(messages):
|
|
37 |
# ---------- chatgpt-4o-mini 模块 ----------
|
38 |
def chatgpt_4o_mini(messages):
|
39 |
"""
|
40 |
-
调用 gpt-4o-mini API
|
41 |
"""
|
42 |
try:
|
43 |
# 构建请求数据
|
@@ -47,22 +48,34 @@ def chatgpt_4o_mini(messages):
|
|
47 |
"stream": True
|
48 |
}
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
#
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 请求失败,请稍后再试。"
|
@@ -113,7 +126,7 @@ def build_interface():
|
|
113 |
chatbox_button.click(chat_handler, inputs=chatbox_input, outputs=chatbox_output)
|
114 |
|
115 |
# chatgpt-4o-mini 模块
|
116 |
-
with gr.Tab("
|
117 |
chatgpt_input = gr.Textbox(label="输入你的问题", placeholder="请提问...")
|
118 |
chatgpt_output = gr.Textbox(label="回答")
|
119 |
chatgpt_button = gr.Button("发送")
|
|
|
2 |
import subprocess
|
3 |
from huggingface_hub import InferenceClient
|
4 |
from PIL import Image
|
5 |
+
import requests
|
6 |
+
import json
|
7 |
# ===================== 核心逻辑模块 =====================
|
8 |
|
9 |
# 初始化模型客户端
|
|
|
38 |
# ---------- chatgpt-4o-mini 模块 ----------
|
39 |
def chatgpt_4o_mini(messages):
|
40 |
"""
|
41 |
+
调用 gpt-4o-mini API 进行对话,并解析流式响应。
|
42 |
"""
|
43 |
try:
|
44 |
# 构建请求数据
|
|
|
48 |
"stream": True
|
49 |
}
|
50 |
|
51 |
+
# 发送请求
|
52 |
+
url = 'https://sanbo1200-duck2api.hf.space/completions'
|
53 |
+
headers = {'Content-Type': 'application/json'}
|
54 |
+
|
55 |
+
# 使用 requests 发送 POST 请求
|
56 |
+
response = requests.post(url, headers=headers, json=data, stream=True)
|
57 |
+
|
58 |
+
# 检查响应状态码
|
59 |
+
if response.status_code != 200:
|
60 |
+
return f"请求失败,状态码:{response.status_code}"
|
61 |
+
|
62 |
+
# 处理流式响应
|
63 |
+
full_response = ""
|
64 |
+
for line in response.iter_lines():
|
65 |
+
if line:
|
66 |
+
# 解析每一行的内容
|
67 |
+
try:
|
68 |
+
chunk = json.loads(line.decode('utf-8'))
|
69 |
+
# 解析返回的每个 chunk
|
70 |
+
if 'choices' in chunk and len(chunk['choices']) > 0:
|
71 |
+
content = chunk['choices'][0]['delta'].get('content', '')
|
72 |
+
full_response += content
|
73 |
+
except Exception as e:
|
74 |
+
print(f"Error parsing response chunk: {e}")
|
75 |
+
|
76 |
+
# 返回拼接后的完整对话内容
|
77 |
+
return full_response.strip()
|
78 |
|
|
|
|
|
79 |
except Exception as e:
|
80 |
print(f"Error during gpt-4o-mini request: {e}")
|
81 |
return "gpt-4o-mini 请求失败,请稍后再试。"
|
|
|
126 |
chatbox_button.click(chat_handler, inputs=chatbox_input, outputs=chatbox_output)
|
127 |
|
128 |
# chatgpt-4o-mini 模块
|
129 |
+
with gr.Tab("gpt4o"):
|
130 |
chatgpt_input = gr.Textbox(label="输入你的问题", placeholder="请提问...")
|
131 |
chatgpt_output = gr.Textbox(label="回答")
|
132 |
chatgpt_button = gr.Button("发送")
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
gradio
|
2 |
huggingface_hub
|
3 |
Pillow
|
|
|
|
1 |
gradio
|
2 |
huggingface_hub
|
3 |
Pillow
|
4 |
+
requests
|