MarcoXi commited on
Commit
c0b418a
·
verified ·
1 Parent(s): a7e2bff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -13
app.py CHANGED
@@ -1,18 +1,140 @@
 
1
  import os
2
- from openai import OpenAI
3
 
4
- # 设置 API 密钥和 API 地址
5
- api_key = os.getenv("DASHSCOPE_API_KEY") # 确保环境变量已设置
6
- base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"
7
 
8
- # 创建 API 客户端
9
- client = OpenAI(api_key=api_key, base_url=base_url)
 
 
 
10
 
11
- # 测试调用
12
- response = client.chat.completions.create(
13
- model="deepseek-r1",
14
- messages=[{"role": "user", "content": "9.9和9.11谁大"}]
15
- )
16
 
17
- # 输出响应内容
18
- print(response.choices[0].message["content"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  import os
3
+ import requests
4
 
5
+ # 从环境变量中读取 API 密钥
6
+ DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY")
7
+ DASHSCOPE_API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
8
 
9
+ # 设置 DeepSeek API 客户端
10
+ class DeepSeekClient:
11
+ def __init__(self, api_key, base_url):
12
+ self.api_key = api_key
13
+ self.base_url = base_url
14
 
15
+ def chat(self, question):
16
+ headers = {
17
+ "Authorization": f"Bearer {self.api_key}",
18
+ "Content-Type": "application/json",
19
+ }
20
 
21
+ data = {
22
+ "model": "deepseek-r1", # 使用 DeepSeek 模型
23
+ "messages": [{"role": "user", "content": question}],
24
+ }
25
+
26
+ try:
27
+ response = requests.post(
28
+ f"{self.base_url}/chat/completions",
29
+ headers=headers,
30
+ json=data
31
+ )
32
+ response.raise_for_status()
33
+ result = response.json()
34
+
35
+ # 获取思考过程和最终答案
36
+ reasoning_content = result["choices"][0].get("reasoning_content", "没有思考过程")
37
+ bot_response = result["choices"][0]["message"]["content"]
38
+
39
+ return bot_response, reasoning_content
40
+
41
+ except Exception as e:
42
+ return f"Error: {e}", None
43
+
44
+ # 创建 DeepSeek 客户端
45
+ client = DeepSeekClient(api_key=DASHSCOPE_API_KEY, base_url=DASHSCOPE_API_URL)
46
+
47
+ # 定义聊天函数
48
+ def answer(question, history):
49
+ if history is None:
50
+ history = []
51
+
52
+ # 获取模型响应
53
+ bot_response, reasoning_content = client.chat(question)
54
+
55
+ # 输出思考过程与最终答案(用于调试)
56
+ print("思考过程:", reasoning_content)
57
+ print("最终答案:", bot_response)
58
+
59
+ # 更新历史记录
60
+ history.append({"role": "user", "content": question})
61
+ history.append({"role": "assistant", "content": bot_response})
62
+
63
+ return history, history
64
+
65
+ # 创建 Gradio 界面
66
+ with gr.Blocks(css="""
67
+ /* 修改布局适应手机端 */
68
+ .gradio-container {
69
+ background-color: #f0f0f0;
70
+ border-radius: 15px;
71
+ padding: 20px;
72
+ max-width: 500px;
73
+ margin: auto;
74
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
75
+ }
76
+ .gr-button {
77
+ background-color: #4CAF50;
78
+ color: white;
79
+ border-radius: 10px;
80
+ padding: 12px 25px;
81
+ border: none;
82
+ font-weight: bold;
83
+ }
84
+ .gr-button:hover {
85
+ background-color: #45a049;
86
+ }
87
+ .gr-chatbot {
88
+ font-family: 'Arial', sans-serif;
89
+ height: 300px;
90
+ max-height: 300px;
91
+ overflow-y: auto;
92
+ padding: 10px;
93
+ margin-bottom: 20px;
94
+ background-color: #ffffff;
95
+ border-radius: 15px;
96
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
97
+ }
98
+ .chatbot-message {
99
+ margin: 10px;
100
+ padding: 10px;
101
+ border-radius: 12px;
102
+ background-color: #e1f5fe;
103
+ }
104
+ .user-message {
105
+ background-color: #a5d6a7;
106
+ color: #ffffff;
107
+ }
108
+ .assistant-message {
109
+ background-color: #e3f2fd;
110
+ color: #000000;
111
+ }
112
+ input[type="text"] {
113
+ font-size: 16px;
114
+ border-radius: 10px;
115
+ padding: 12px;
116
+ width: 100%;
117
+ background-color: #f9f9f9;
118
+ margin-top: 10px;
119
+ }
120
+ /* 手机端优化:输入框宽度和响应式设计 */
121
+ @media (max-width: 600px) {
122
+ .gradio-container {
123
+ padding: 15px;
124
+ width: 90%;
125
+ }
126
+ .gr-button {
127
+ padding: 10px 20px;
128
+ }
129
+ }
130
+ """) as demo:
131
+
132
+ chatbot = gr.Chatbot(elem_id="chatbot", type="messages") # 使用 Gradio 的聊天组件
133
+ state = gr.State([])
134
+
135
+ with gr.Row():
136
+ txt = gr.Textbox(placeholder="请输入您的问题并按回车发送", lines=1)
137
+
138
+ txt.submit(answer, [txt, state], [chatbot, state])
139
+
140
+ demo.launch()