Spaces:
Sleeping
Sleeping
Update app_bak.py
Browse files- app_bak.py +203 -1
app_bak.py
CHANGED
@@ -1 +1,203 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
import requests
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import logging
|
7 |
+
|
8 |
+
# 设置日志
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
logger = logging.getLogger(__name__)
|
11 |
+
|
12 |
+
# 加载环境变量
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
# API配置
|
18 |
+
BASE_URL = os.getenv("API_URL", "").rstrip('/') # 移除末尾的斜杠
|
19 |
+
API_KEY = os.getenv("API_KEY", "")
|
20 |
+
|
21 |
+
# 构建完整的API URL
|
22 |
+
API_URL = f"{BASE_URL}/v1/chat/completions"
|
23 |
+
|
24 |
+
# 验证环境变量
|
25 |
+
if not BASE_URL or not API_KEY:
|
26 |
+
raise ValueError("""
|
27 |
+
请确保设置了必要的环境变量:
|
28 |
+
- API_URL: API基础地址 (例如: https://api.example.com)
|
29 |
+
- API_KEY: API密钥
|
30 |
+
可以在Hugging Face Space的Settings -> Repository Secrets中设置这些变量
|
31 |
+
""")
|
32 |
+
|
33 |
+
class ChatBot:
|
34 |
+
def __init__(self):
|
35 |
+
# 修正 headers 的设置
|
36 |
+
self.headers = {
|
37 |
+
"Authorization": f"Bearer {API_KEY}", # 正确使用 API_KEY
|
38 |
+
"Content-Type": "application/json",
|
39 |
+
"Accept": "text/event-stream"
|
40 |
+
}
|
41 |
+
self.verify_api_config()
|
42 |
+
|
43 |
+
def verify_api_config(self):
|
44 |
+
try:
|
45 |
+
# 使用 OPTIONS 请求来验证API端点
|
46 |
+
response = requests.options(API_URL, timeout=5)
|
47 |
+
logger.info(f"API endpoint: {API_URL}")
|
48 |
+
logger.info(f"API headers: {self.headers}")
|
49 |
+
|
50 |
+
if response.status_code >= 400:
|
51 |
+
logger.error(f"API配置可能有误: {response.status_code}")
|
52 |
+
logger.error(f"API响应: {response.text[:200]}")
|
53 |
+
except Exception as e:
|
54 |
+
logger.error(f"API连接测试失败: {str(e)}")
|
55 |
+
|
56 |
+
|
57 |
+
def format_message(role, content):
|
58 |
+
return {"role": role, "content": content}
|
59 |
+
|
60 |
+
class ChatBot:
|
61 |
+
def __init__(self):
|
62 |
+
self.headers = {
|
63 |
+
"Authorization": f"Bearer {API_KEY}",
|
64 |
+
"Content-Type": "application/json",
|
65 |
+
"Accept": "text/event-stream" # 添加这行
|
66 |
+
}
|
67 |
+
|
68 |
+
def chat_stream(message, history):
|
69 |
+
chatbot = ChatBot()
|
70 |
+
|
71 |
+
logger.info(f"Sending message: {message}")
|
72 |
+
logger.info(f"API URL: {API_URL}")
|
73 |
+
logger.info(f"Headers: {chatbot.headers}") # 添加header日志
|
74 |
+
|
75 |
+
messages = []
|
76 |
+
for human, assistant in history:
|
77 |
+
messages.append(format_message("user", human))
|
78 |
+
messages.append(format_message("assistant", assistant))
|
79 |
+
messages.append(format_message("user", message))
|
80 |
+
|
81 |
+
try:
|
82 |
+
# 首先验证API是否可用
|
83 |
+
verify_response = requests.get(API_URL)
|
84 |
+
logger.info(f"API验证响应状态码: {verify_response.status_code}")
|
85 |
+
logger.info(f"API验证响应内容: {verify_response.text[:200]}...") # 只记录前200个字符
|
86 |
+
|
87 |
+
payload = {
|
88 |
+
"model": "gpt-4o",
|
89 |
+
"messages": messages,
|
90 |
+
"stream": True,
|
91 |
+
"temperature": 0.7
|
92 |
+
}
|
93 |
+
|
94 |
+
logger.info(f"发送请求数据: {json.dumps(payload, ensure_ascii=False)}")
|
95 |
+
|
96 |
+
response = requests.post(
|
97 |
+
API_URL,
|
98 |
+
headers=chatbot.headers,
|
99 |
+
json=payload,
|
100 |
+
stream=True
|
101 |
+
)
|
102 |
+
|
103 |
+
if response.headers.get('content-type', '').startswith('text/html'):
|
104 |
+
error_msg = "API返回了HTML而不是预期的流式响应。请检查API配置。"
|
105 |
+
logger.error(error_msg)
|
106 |
+
history.append((message, error_msg))
|
107 |
+
return history
|
108 |
+
|
109 |
+
if response.status_code != 200:
|
110 |
+
error_msg = f"API返回错误状态码: {response.status_code}\n错误信息: {response.text}"
|
111 |
+
logger.error(error_msg)
|
112 |
+
history.append((message, error_msg))
|
113 |
+
return history
|
114 |
+
|
115 |
+
partial_message = ""
|
116 |
+
|
117 |
+
for line in response.iter_lines():
|
118 |
+
if line:
|
119 |
+
try:
|
120 |
+
line = line.decode('utf-8')
|
121 |
+
logger.info(f"收到数据: {line}")
|
122 |
+
|
123 |
+
if line.startswith('data: '):
|
124 |
+
line = line[6:]
|
125 |
+
|
126 |
+
if line == '[DONE]':
|
127 |
+
break
|
128 |
+
|
129 |
+
try:
|
130 |
+
chunk = json.loads(line)
|
131 |
+
if chunk and "choices" in chunk:
|
132 |
+
delta = chunk["choices"][0]["delta"]
|
133 |
+
if "content" in delta:
|
134 |
+
content = delta["content"]
|
135 |
+
partial_message += content
|
136 |
+
history.append((message, partial_message))
|
137 |
+
yield history
|
138 |
+
history.pop()
|
139 |
+
except json.JSONDecodeError as e:
|
140 |
+
logger.error(f"JSON解析错误: {e}")
|
141 |
+
continue
|
142 |
+
except Exception as e:
|
143 |
+
logger.error(f"处理响应时出错: {e}")
|
144 |
+
continue
|
145 |
+
|
146 |
+
if not partial_message:
|
147 |
+
error_msg = "未能获取到有效的响应内容"
|
148 |
+
logger.error(error_msg)
|
149 |
+
history.append((message, error_msg))
|
150 |
+
else:
|
151 |
+
history.append((message, partial_message))
|
152 |
+
|
153 |
+
except Exception as e:
|
154 |
+
error_msg = f"请求发生错误: {str(e)}"
|
155 |
+
logger.error(error_msg)
|
156 |
+
history.append((message, error_msg))
|
157 |
+
|
158 |
+
return history
|
159 |
+
|
160 |
+
# Gradio界面配置
|
161 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
162 |
+
chatbot = gr.Chatbot(
|
163 |
+
height=600,
|
164 |
+
show_copy_button=True,
|
165 |
+
avatar_images=["assets/user.png", "assets/assistant.png"],
|
166 |
+
)
|
167 |
+
msg = gr.Textbox(
|
168 |
+
placeholder="在这里输入您的问题...",
|
169 |
+
container=False,
|
170 |
+
scale=7,
|
171 |
+
)
|
172 |
+
with gr.Row():
|
173 |
+
submit = gr.Button("发送", scale=2, variant="primary")
|
174 |
+
clear = gr.Button("清除对话", scale=1)
|
175 |
+
|
176 |
+
# 事件处理
|
177 |
+
msg.submit(
|
178 |
+
chat_stream,
|
179 |
+
[msg, chatbot],
|
180 |
+
[chatbot],
|
181 |
+
api_name="chat"
|
182 |
+
).then(
|
183 |
+
lambda: "",
|
184 |
+
None,
|
185 |
+
[msg],
|
186 |
+
api_name="clear_input"
|
187 |
+
)
|
188 |
+
|
189 |
+
submit.click(
|
190 |
+
chat_stream,
|
191 |
+
[msg, chatbot],
|
192 |
+
[chatbot],
|
193 |
+
).then(
|
194 |
+
lambda: "",
|
195 |
+
None,
|
196 |
+
[msg],
|
197 |
+
)
|
198 |
+
|
199 |
+
clear.click(lambda: [], None, chatbot)
|
200 |
+
|
201 |
+
# 启动应用
|
202 |
+
if __name__ == "__main__":
|
203 |
+
demo.queue().launch(debug=True)
|