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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -104
app.py CHANGED
@@ -1,110 +1,18 @@
1
- import gradio as gr
2
  import os
3
  from openai import OpenAI
4
 
5
- # 读取环境变量
6
- DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY") # 从环境变量读取 API Key
7
- DASHSCOPE_API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
8
 
9
- # 设置 DashScope API 客户端
10
- client = OpenAI(
11
- api_key=DASHSCOPE_API_KEY,
12
- base_url=DASHSCOPE_API_URL
13
- )
14
-
15
- # 定义对话函数
16
- def answer(question, history):
17
- if history is None:
18
- history = []
19
-
20
- # 将历史记录与用户输入合并
21
- messages = [{'role': 'user', 'content': question}]
22
-
23
- # 向 DashScope API 发送请求
24
- try:
25
- completion = client.chat.completions.create(
26
- model="deepseek-r1", # 使用 DeepSeek-R1 模型
27
- messages=messages
28
- )
29
-
30
- # 获取思考过程与最终答案
31
- reasoning_content = completion.choices[0].message.get("reasoning_content", "没有思考过程")
32
- bot_response = completion.choices[0].message["content"]
33
-
34
- # 输出思考过程与最终答案(用于调试)
35
- print("思考过程:", reasoning_content)
36
- print("最终答案:", bot_response)
37
-
38
- # 更新历史记录
39
- history.append({"role": "user", "content": question})
40
- history.append({"role": "assistant", "content": bot_response})
41
 
42
- return history, history
43
-
44
- except Exception as e:
45
- print("API 请求失败:", e)
46
- return history, history # 返回空的 history
47
-
48
-
49
- # 创建 Gradio 界面
50
- with gr.Blocks(css="""
51
- #chatbot {
52
- height: 400px;
53
- background-color: #f7f7f7;
54
- border-radius: 10px;
55
- padding: 20px;
56
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
57
- }
58
- .gradio-container {
59
- background-color: #212121;
60
- border-radius: 20px;
61
- padding: 40px;
62
- }
63
- .gr-button {
64
- background-color: #4CAF50;
65
- color: white;
66
- border-radius: 10px;
67
- padding: 10px 20px;
68
- border: none;
69
- font-weight: bold;
70
- }
71
- .gr-button:hover {
72
- background-color: #45a049;
73
- }
74
- .gr-chatbot {
75
- font-family: 'Arial', sans-serif;
76
- height: 400px;
77
- }
78
- .chatbot-message {
79
- margin: 10px;
80
- padding: 10px;
81
- border-radius: 15px;
82
- background-color: #e1f5fe;
83
- }
84
- .user-message {
85
- background-color: #a5d6a7;
86
- color: #ffffff;
87
- }
88
- .assistant-message {
89
- background-color: #e3f2fd;
90
- color: #000000;
91
- }
92
- input[type="text"] {
93
- font-size: 16px;
94
- border-radius: 10px;
95
- padding: 10px;
96
- width: 100%;
97
- }
98
- .gradio-container .gr-chatbot .overflow-y-auto {
99
- max-height: 500px;
100
- }
101
- """) as demo:
102
- chatbot = gr.Chatbot(elem_id="chatbot", type="messages") # 使用 Gradio 的聊天组件
103
- state = gr.State([])
104
-
105
- with gr.Row():
106
- txt = gr.Textbox(placeholder="请输入您的问题,并按回车发送", lines=1)
107
-
108
- txt.submit(answer, [txt, state], [chatbot, state])
109
 
110
- demo.launch()
 
 
 
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"])