Spaces:
Sleeping
Sleeping
Comprehensive fix for AI response display issues - ensure proper error handling and message consistency
Browse files
app.py
CHANGED
|
@@ -294,12 +294,25 @@ def respond(
|
|
| 294 |
token_to_use = os.environ.get("HF_TOKEN", "")
|
| 295 |
|
| 296 |
if not token_to_use:
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
# 发送webhook通知
|
| 299 |
send_webhook_notification("error", {
|
| 300 |
"type": "missing_hf_token",
|
| 301 |
-
"message": "未提供HF_TOKEN"
|
|
|
|
| 302 |
})
|
|
|
|
| 303 |
return
|
| 304 |
|
| 305 |
# 保存对话历史
|
|
@@ -346,12 +359,19 @@ def respond(
|
|
| 346 |
|
| 347 |
if client is None:
|
| 348 |
error_msg = "错误:无法连接到任何可用的模型。请检查您的Hugging Face访问令牌权限。"
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
# 发送webhook通知
|
| 351 |
send_webhook_notification("error", {
|
| 352 |
"type": "model_connection_failed",
|
| 353 |
-
"message": "无法连接到任何可用模型"
|
|
|
|
| 354 |
})
|
|
|
|
| 355 |
return
|
| 356 |
|
| 357 |
messages = [{"role": "system", "content": effective_system_message}]
|
|
@@ -384,7 +404,7 @@ def respond(
|
|
| 384 |
# 发送webhook通知(仅在有重要对话时)
|
| 385 |
if len(response) > 50: # 只有较长的回复才发送通知
|
| 386 |
send_webhook_notification("new_conversation", {
|
| 387 |
-
"user_message":
|
| 388 |
"ai_response": response,
|
| 389 |
"model_used": model_used,
|
| 390 |
"character_role": character_role
|
|
@@ -403,7 +423,7 @@ def respond(
|
|
| 403 |
send_webhook_notification("error", {
|
| 404 |
"type": "processing_error",
|
| 405 |
"message": str(e),
|
| 406 |
-
"user_message":
|
| 407 |
})
|
| 408 |
|
| 409 |
|
|
|
|
| 294 |
token_to_use = os.environ.get("HF_TOKEN", "")
|
| 295 |
|
| 296 |
if not token_to_use:
|
| 297 |
+
error_msg = "错误:需要Hugging Face访问令牌。请在环境变量中设置HF_TOKEN。"
|
| 298 |
+
# 保存错误信息到历史记录
|
| 299 |
+
conversation_data = {
|
| 300 |
+
"timestamp": datetime.now().isoformat(),
|
| 301 |
+
"message": message,
|
| 302 |
+
"history": history,
|
| 303 |
+
"response": error_msg
|
| 304 |
+
}
|
| 305 |
+
history_list = load_history()
|
| 306 |
+
history_list.append(conversation_data)
|
| 307 |
+
save_history(history_list)
|
| 308 |
+
|
| 309 |
# 发送webhook通知
|
| 310 |
send_webhook_notification("error", {
|
| 311 |
"type": "missing_hf_token",
|
| 312 |
+
"message": "未提供HF_TOKEN",
|
| 313 |
+
"user_message": message
|
| 314 |
})
|
| 315 |
+
yield error_msg
|
| 316 |
return
|
| 317 |
|
| 318 |
# 保存对话历史
|
|
|
|
| 359 |
|
| 360 |
if client is None:
|
| 361 |
error_msg = "错误:无法连接到任何可用的模型。请检查您的Hugging Face访问令牌权限。"
|
| 362 |
+
# 保存错误信息到历史记录
|
| 363 |
+
conversation_data["response"] = error_msg
|
| 364 |
+
history_list = load_history()
|
| 365 |
+
history_list.append(conversation_data)
|
| 366 |
+
save_history(history_list)
|
| 367 |
+
|
| 368 |
# 发送webhook通知
|
| 369 |
send_webhook_notification("error", {
|
| 370 |
"type": "model_connection_failed",
|
| 371 |
+
"message": "无法连接到任何可用模型",
|
| 372 |
+
"user_message": message
|
| 373 |
})
|
| 374 |
+
yield error_msg
|
| 375 |
return
|
| 376 |
|
| 377 |
messages = [{"role": "system", "content": effective_system_message}]
|
|
|
|
| 404 |
# 发送webhook通知(仅在有重要对话时)
|
| 405 |
if len(response) > 50: # 只有较长的回复才发送通知
|
| 406 |
send_webhook_notification("new_conversation", {
|
| 407 |
+
"user_message": message,
|
| 408 |
"ai_response": response,
|
| 409 |
"model_used": model_used,
|
| 410 |
"character_role": character_role
|
|
|
|
| 423 |
send_webhook_notification("error", {
|
| 424 |
"type": "processing_error",
|
| 425 |
"message": str(e),
|
| 426 |
+
"user_message": message
|
| 427 |
})
|
| 428 |
|
| 429 |
|