MarcoXi commited on
Commit
7d13a58
·
verified ·
1 Parent(s): 500f202

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -3,7 +3,7 @@ import requests
3
  import os
4
 
5
  # 假设 DeepSeek-R1 的 API URL 和 API Key
6
- DEEPSEEK_API_URL = os.getenv("DEEPSEEK_API_URL", "https://api.deepseek.com/v1/chat")
7
  DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
8
 
9
  # 定义对话函数
@@ -20,20 +20,28 @@ def answer(question, history):
20
  messages.append({"role": "user", "content": question})
21
 
22
  # 调用 DeepSeek-R1 API
23
- response = requests.post(
24
- DEEPSEEK_API_URL,
25
- json={"messages": messages},
26
- headers={"Authorization": f"Bearer {DEEPSEEK_API_KEY}"}
27
- )
 
 
 
 
 
 
 
28
 
29
- response_data = response.json()
30
- bot_response = response_data.get("choices")[0]["message"]["content"]
 
 
 
31
 
32
- # 更新历史记录
33
- history.append({"role": "user", "content": question})
34
- history.append({"role": "assistant", "content": bot_response})
35
-
36
- return history, history
37
 
38
 
39
  # 创建 Gradio 界面
@@ -63,6 +71,7 @@ with gr.Blocks(css="""
63
  }
64
  .gr-chatbot {
65
  font-family: 'Arial', sans-serif;
 
66
  }
67
  .chatbot-message {
68
  margin: 10px;
@@ -96,4 +105,4 @@ with gr.Blocks(css="""
96
 
97
  txt.submit(answer, [txt, state], [chatbot, state])
98
 
99
- demo.launch()
 
3
  import os
4
 
5
  # 假设 DeepSeek-R1 的 API URL 和 API Key
6
+ DEEPSEEK_API_URL = os.getenv("DEEPSEEK_API_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1")
7
  DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
8
 
9
  # 定义对话函数
 
20
  messages.append({"role": "user", "content": question})
21
 
22
  # 调用 DeepSeek-R1 API
23
+ try:
24
+ # 发送请求到 DeepSeek-R1 API
25
+ response = requests.post(
26
+ DEEPSEEK_API_URL,
27
+ json={"messages": messages},
28
+ headers={"Authorization": f"Bearer {DEEPSEEK_API_KEY}"}
29
+ )
30
+
31
+ # 解析 API 响应
32
+ response_data = response.json()
33
+ print("API 响应数据:", response_data) # 打印响应数据进行调试
34
+ bot_response = response_data.get("choices")[0]["message"]["content"]
35
 
36
+ # 更新历史记录
37
+ history.append({"role": "user", "content": question})
38
+ history.append({"role": "assistant", "content": bot_response})
39
+
40
+ return history, history
41
 
42
+ except Exception as e:
43
+ print("API 请求失败:", e)
44
+ return history, history # 返回空的 history
 
 
45
 
46
 
47
  # 创建 Gradio 界面
 
71
  }
72
  .gr-chatbot {
73
  font-family: 'Arial', sans-serif;
74
+ height: 400px;
75
  }
76
  .chatbot-message {
77
  margin: 10px;
 
105
 
106
  txt.submit(answer, [txt, state], [chatbot, state])
107
 
108
+ demo.launch()