MarcoXi commited on
Commit
cb162de
·
verified ·
1 Parent(s): 2ec37f9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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
+ # 定义对话函数
10
+ def answer(question, history):
11
+ if history is None:
12
+ history = []
13
+
14
+ # 组装对话历史
15
+ messages = [{"role": "system", "content": "你是一个中国厨师,用中文回答做菜的问题。"}]
16
+
17
+ for item in history:
18
+ messages.append({"role": item["role"], "content": item["content"]})
19
+
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 界面
40
+ with gr.Blocks(css="""
41
+ #chatbot {
42
+ height: 400px;
43
+ background-color: #f7f7f7;
44
+ border-radius: 10px;
45
+ padding: 20px;
46
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
47
+ }
48
+ .gradio-container {
49
+ background-color: #212121;
50
+ border-radius: 20px;
51
+ padding: 40px;
52
+ }
53
+ .gr-button {
54
+ background-color: #4CAF50;
55
+ color: white;
56
+ border-radius: 10px;
57
+ padding: 10px 20px;
58
+ border: none;
59
+ font-weight: bold;
60
+ }
61
+ .gr-button:hover {
62
+ background-color: #45a049;
63
+ }
64
+ .gr-chatbot {
65
+ font-family: 'Arial', sans-serif;
66
+ }
67
+ .chatbot-message {
68
+ margin: 10px;
69
+ padding: 10px;
70
+ border-radius: 15px;
71
+ background-color: #e1f5fe;
72
+ }
73
+ .user-message {
74
+ background-color: #a5d6a7;
75
+ color: #ffffff;
76
+ }
77
+ .assistant-message {
78
+ background-color: #e3f2fd;
79
+ color: #000000;
80
+ }
81
+ input[type="text"] {
82
+ font-size: 16px;
83
+ border-radius: 10px;
84
+ padding: 10px;
85
+ width: 100%;
86
+ }
87
+ .gradio-container .gr-chatbot .overflow-y-auto {
88
+ max-height: 500px;
89
+ }
90
+ """) as demo:
91
+ chatbot = gr.Chatbot(elem_id="chatbot", type="messages") # 使用 Gradio 的聊天组件
92
+ state = gr.State([])
93
+
94
+ with gr.Row():
95
+ txt = gr.Textbox(placeholder="请输入您的问题,并按回车发送", lines=1)
96
+
97
+ txt.submit(answer, [txt, state], [chatbot, state])
98
+
99
+ demo.launch()