leonsimon23 commited on
Commit
feb5622
·
verified ·
1 Parent(s): eb882d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +165 -145
app.py CHANGED
@@ -18,17 +18,17 @@ class FastGPTChat:
18
  "Authorization": f"Bearer {self.api_key}",
19
  "Content-Type": "application/json"
20
  }
21
-
22
  def chat(self, message, chat_history):
23
  if not message.strip():
24
  return "", chat_history
25
-
26
  # 立即添加用户消息到聊天历史
27
  chat_history.append((message, ""))
28
  yield "", chat_history
29
-
30
  chat_id = str(uuid.uuid4())
31
-
32
  # 构建消息历史,包含系统提示词
33
  messages = []
34
  if self.system_prompt:
@@ -36,15 +36,15 @@ class FastGPTChat:
36
  "role": "system",
37
  "content": self.system_prompt
38
  })
39
-
40
  # 添加聊天历史
41
  for user_msg, bot_msg in chat_history[:-1]: # 不包含最新消息
42
  messages.append({"role": "user", "content": user_msg})
43
  messages.append({"role": "assistant", "content": bot_msg})
44
-
45
  # 添加当前用户消息
46
  messages.append({"role": "user", "content": message})
47
-
48
  # 准备请求数据
49
  data = {
50
  "chatId": chat_id,
@@ -53,7 +53,7 @@ class FastGPTChat:
53
  "model": "gpt-4o",
54
  "messages": messages
55
  }
56
-
57
  try:
58
  # 发送流式请求
59
  response = requests.post(
@@ -63,14 +63,14 @@ class FastGPTChat:
63
  stream=True,
64
  timeout=30
65
  )
66
-
67
  if response.status_code != 200:
68
  error_msg = f"API Error: Status {response.status_code}"
69
  logger.error(error_msg)
70
  chat_history[-1] = (message, f"Error: {error_msg}")
71
  yield "", chat_history
72
  return
73
-
74
  # 处理流式响应
75
  full_response = ""
76
  for line in response.iter_lines():
@@ -87,9 +87,9 @@ class FastGPTChat:
87
  yield "", chat_history
88
  except Exception as e:
89
  logger.error(f"Error processing stream: {str(e)}")
90
-
91
  yield "", chat_history
92
-
93
  except requests.exceptions.RequestException as e:
94
  error_msg = f"Request failed: {str(e)}"
95
  logger.error(error_msg)
@@ -98,129 +98,149 @@ class FastGPTChat:
98
 
99
  def create_chat_interface():
100
  fastgpt_chat = FastGPTChat()
101
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  with gr.Blocks(
103
  title="AI用药咨询助手",
104
- css="""
105
- /* 整体容器样式 */
106
- .container {
107
- width: 100%;
108
- max-width: 800px;
109
- margin: auto;
110
- padding: 2rem;
111
- box-sizing: border-box;
112
- }
113
- /* 标题区域样式 */
114
- .title-container {
115
- text-align: center;
116
- margin-bottom: 2rem;
117
- }
118
- .title-container h1 {
119
- color: #2C3E50;
120
- font-size: 2.5rem;
121
- margin-bottom: 0.5rem;
122
- font-weight: 600;
123
- }
124
- .title-container h3 {
125
- color: #7F8C8D;
126
- font-size: 1.2rem;
127
- font-weight: normal;
128
- }
129
- /* 聊天容器样式 */
130
- .chat-container {
131
- background-color: #FFFFFF;
132
- border-radius: 20px;
133
- box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
134
- padding: 2rem;
135
- margin-bottom: 2rem;
136
- max-height: 70vh;
137
- overflow: hidden;
138
- }
139
- /* 聊天框样式 */
140
- .chatbot {
141
- background-color: #F9F9F9;
142
- border-radius: 12px;
143
- padding: 1rem;
144
- margin-bottom: 1.5rem;
145
- height: 400px;
146
- overflow-y: auto;
147
- box-sizing: border-box;
148
- }
149
- /* 消息样式 */
150
- .chatbot .user-message {
151
- background: #5C6BC0;
152
- color: white !important;
153
- border-radius: 18px 18px 4px 18px !important;
154
- padding: 1rem 1.5rem !important;
155
- margin: 1rem 0 !important;
156
- max-width: 80% !important;
157
- float: right !important;
158
- clear: both !important;
159
- box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2) !important;
160
- }
161
- .chatbot .bot-message {
162
- background: #E1E8F0 !important;
163
- border: 1px solid #CBD5E0 !important;
164
- color: #1F2937 !important;
165
- border-radius: 18px 18px 18px 4px !important;
166
- padding: 1rem 1.5rem !important;
167
- margin: 1rem 0 !important;
168
- max-width: 80% !important;
169
- float: left !important;
170
- clear: both !important;
171
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05) !important;
172
- }
173
- /* 输入区域样式 */
174
- .input-container {
175
- background: #FFFFFF;
176
- padding: 1.5rem;
177
- border-radius: 15px;
178
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
179
- }
180
- .message-box {
181
- border: 2px solid #E5E7EB !important;
182
- border-radius: 12px !important;
183
- padding: 1rem !important;
184
- font-size: 1rem !important;
185
- transition: all 0.3s ease !important;
186
- background: #FFFFFF !important;
187
- }
188
- .message-box:focus {
189
- border-color: #6366F1 !important;
190
- box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2) !important;
191
- }
192
- /* 按钮样式 */
193
- .button-container {
194
- display: flex;
195
- gap: 1rem;
196
- margin-top: 1rem;
197
- }
198
- .submit-btn {
199
- background: linear-gradient(135deg, #6366F1, #4F46E5) !important;
200
- color: white !important;
201
- padding: 0.75rem 1.5rem !important;
202
- border-radius: 12px !important;
203
- font-weight: 600 !important;
204
- transition: all 0.3s ease !important;
205
- flex: 1;
206
- }
207
- .submit-btn:hover {
208
- transform: translateY(-2px);
209
- box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3) !important;
210
- }
211
- .clear-btn {
212
- background: #EF4444 !important;
213
- color: white !important;
214
- padding: 0.75rem 1.5rem !important;
215
- border-radius: 12px !important;
216
- font-weight: 600 !important;
217
- transition: all 0.3s ease !important;
218
- }
219
- .clear-btn:hover {
220
- transform: translateY(-2px);
221
- box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3) !important;
222
- }
223
- """
224
  ) as interface:
225
  with gr.Column(elem_classes="container"):
226
  with gr.Column(elem_classes="title-container"):
@@ -230,17 +250,17 @@ def create_chat_interface():
230
  ### 您的专业用药咨询顾问,随时为您解答用药相关问题
231
  """
232
  )
233
-
234
  with gr.Column(elem_classes="chat-container"):
235
  chatbot = gr.Chatbot(
236
- height=400,
237
  container=False,
238
  elem_classes="chatbot",
239
  show_label=False,
240
  bubble_full_width=False,
241
  avatar_images=("👤", "🤖")
242
  )
243
-
244
  with gr.Column(elem_classes="input-container"):
245
  message = gr.Textbox(
246
  show_label=False,
@@ -249,37 +269,37 @@ def create_chat_interface():
249
  elem_classes="message-box",
250
  lines=3
251
  )
252
-
253
  with gr.Row(elem_classes="button-container"):
254
  submit = gr.Button(
255
- "发送 💊",
256
  variant="primary",
257
  elem_classes="submit-btn"
258
  )
259
  clear = gr.Button(
260
- "清空对话 🗑️",
261
  variant="secondary",
262
  elem_classes="clear-btn"
263
  )
264
-
265
  # 设置事件处理
266
- submit.click(
267
  fastgpt_chat.chat,
268
  inputs=[message, chatbot],
269
  outputs=[message, chatbot],
270
  api_name="chat"
271
  )
272
-
273
  message.submit(
274
  fastgpt_chat.chat,
275
  inputs=[message, chatbot],
276
  outputs=[message, chatbot]
277
  )
278
-
279
  clear.click(lambda: None, None, chatbot, queue=False)
280
-
281
  return interface
282
 
283
  if __name__ == "__main__":
284
  demo = create_chat_interface()
285
- demo.launch(debug=True)
 
18
  "Authorization": f"Bearer {self.api_key}",
19
  "Content-Type": "application/json"
20
  }
21
+
22
  def chat(self, message, chat_history):
23
  if not message.strip():
24
  return "", chat_history
25
+
26
  # 立即添加用户消息到聊天历史
27
  chat_history.append((message, ""))
28
  yield "", chat_history
29
+
30
  chat_id = str(uuid.uuid4())
31
+
32
  # 构建消息历史,包含系统提示词
33
  messages = []
34
  if self.system_prompt:
 
36
  "role": "system",
37
  "content": self.system_prompt
38
  })
39
+
40
  # 添加聊天历史
41
  for user_msg, bot_msg in chat_history[:-1]: # 不包含最新消息
42
  messages.append({"role": "user", "content": user_msg})
43
  messages.append({"role": "assistant", "content": bot_msg})
44
+
45
  # 添加当前用户消息
46
  messages.append({"role": "user", "content": message})
47
+
48
  # 准备请求数据
49
  data = {
50
  "chatId": chat_id,
 
53
  "model": "gpt-4o",
54
  "messages": messages
55
  }
56
+
57
  try:
58
  # 发送流式请求
59
  response = requests.post(
 
63
  stream=True,
64
  timeout=30
65
  )
66
+
67
  if response.status_code != 200:
68
  error_msg = f"API Error: Status {response.status_code}"
69
  logger.error(error_msg)
70
  chat_history[-1] = (message, f"Error: {error_msg}")
71
  yield "", chat_history
72
  return
73
+
74
  # 处理流式响应
75
  full_response = ""
76
  for line in response.iter_lines():
 
87
  yield "", chat_history
88
  except Exception as e:
89
  logger.error(f"Error processing stream: {str(e)}")
90
+
91
  yield "", chat_history
92
+
93
  except requests.exceptions.RequestException as e:
94
  error_msg = f"Request failed: {str(e)}"
95
  logger.error(error_msg)
 
98
 
99
  def create_chat_interface():
100
  fastgpt_chat = FastGPTChat()
101
+
102
+ css = """
103
+ /* 整体布局调整 */
104
+ body {
105
+ font-family: 'Arial', sans-serif;
106
+ background-color: #f4f7fc;
107
+ color: #333;
108
+ }
109
+ .container {
110
+ max-width: 900px; /* 限制容器宽度 */
111
+ margin: 20px auto;
112
+ padding: 30px;
113
+ background-color: #fff;
114
+ border-radius: 15px;
115
+ box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
116
+ }
117
+ .title-container {
118
+ text-align: center;
119
+ margin-bottom: 30px;
120
+ }
121
+ .title-container h1 {
122
+ color: #007bff;
123
+ font-size: 2.5rem;
124
+ margin-bottom: 10px;
125
+ }
126
+ .title-container h3 {
127
+ color: #6c757d;
128
+ font-size: 1.2rem;
129
+ font-weight: normal;
130
+ }
131
+ /* 聊天区域样式 */
132
+ .chat-container {
133
+ background-color: #f9f9f9;
134
+ border-radius: 10px;
135
+ margin-bottom: 20px;
136
+ overflow: hidden; /* 防止内部元素溢出 */
137
+ }
138
+ .chatbot {
139
+ height: auto !important; /* 移除固定高度 */
140
+ min-height: 400px; /* 设置最小高度 */
141
+ overflow-y: auto;
142
+ padding: 15px;
143
+ display: flex;
144
+ flex-direction: column;
145
+ }
146
+ .chatbot-row {
147
+ margin-bottom: 10px;
148
+ display: flex;
149
+ align-items: start; /* 对齐消息气泡顶部 */
150
+ }
151
+ .chatbot .user {
152
+ justify-content: flex-end;
153
+ }
154
+ .chatbot .bot {
155
+ justify-content: flex-start;
156
+ }
157
+ .chatbot .user-message,
158
+ .chatbot .bot-message {
159
+ max-width: 75%;
160
+ padding: 10px 15px;
161
+ border-radius: 20px;
162
+ word-wrap: break-word;
163
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
164
+ }
165
+ .chatbot .user-message {
166
+ background-color: #dcf8c6; /* 更清新的用户消息背景 */
167
+ color: #333;
168
+ border-top-right-radius: 5px;
169
+ }
170
+ .chatbot .bot-message {
171
+ background-color: #e1e1eb; /* 更柔和的Bot消息背景 */
172
+ color: #333;
173
+ border-top-left-radius: 5px;
174
+ }
175
+ .chatbot .avatar {
176
+ width: 30px;
177
+ height: 30px;
178
+ border-radius: 50%;
179
+ background-color: #ccc;
180
+ color: #fff;
181
+ display: flex;
182
+ align-items: center;
183
+ justify-content: center;
184
+ margin: 0 10px;
185
+ }
186
+ .chatbot .user .avatar {
187
+ background-color: #007bff;
188
+ }
189
+ .chatbot .bot .avatar {
190
+ background-color: #6c757d;
191
+ }
192
+ .chatbot :last-child {
193
+ margin-bottom: 0;
194
+ }
195
+ /* 输入区域样式 */
196
+ .input-container {
197
+ display: flex;
198
+ gap: 10px;
199
+ }
200
+ .message-box {
201
+ border: 1px solid #ced4da !important;
202
+ border-radius: 8px !important;
203
+ padding: 10px !important;
204
+ font-size: 1rem !important;
205
+ flex-grow: 1;
206
+ transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out !important;
207
+ }
208
+ .message-box:focus {
209
+ border-color: #007bff !important;
210
+ box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25) !important;
211
+ }
212
+ /* 按钮样式 */
213
+ .button-container {
214
+ display: flex;
215
+ gap: 10px;
216
+ }
217
+ .submit-btn, .clear-btn {
218
+ padding: 10px 20px !important;
219
+ border: none !important;
220
+ border-radius: 8px !important;
221
+ color: white !important;
222
+ cursor: pointer;
223
+ transition: background-color 0.3s ease;
224
+ font-size: 1rem !important;
225
+ font-weight: 500 !important;
226
+ }
227
+ .submit-btn {
228
+ background-color: #007bff !important;
229
+ }
230
+ .submit-btn:hover {
231
+ background-color: #0056b3 !important;
232
+ }
233
+ .clear-btn {
234
+ background-color: #dc3545 !important;
235
+ }
236
+ .clear-btn:hover {
237
+ background-color: #bd2130 !important;
238
+ }
239
+ """
240
+
241
  with gr.Blocks(
242
  title="AI用药咨询助手",
243
+ css=css
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  ) as interface:
245
  with gr.Column(elem_classes="container"):
246
  with gr.Column(elem_classes="title-container"):
 
250
  ### 您的专业用药咨询顾问,随时为您解答用药相关问题
251
  """
252
  )
253
+
254
  with gr.Column(elem_classes="chat-container"):
255
  chatbot = gr.Chatbot(
256
+ height=None,
257
  container=False,
258
  elem_classes="chatbot",
259
  show_label=False,
260
  bubble_full_width=False,
261
  avatar_images=("👤", "🤖")
262
  )
263
+
264
  with gr.Column(elem_classes="input-container"):
265
  message = gr.Textbox(
266
  show_label=False,
 
269
  elem_classes="message-box",
270
  lines=3
271
  )
272
+
273
  with gr.Row(elem_classes="button-container"):
274
  submit = gr.Button(
275
+ "发送 💊",
276
  variant="primary",
277
  elem_classes="submit-btn"
278
  )
279
  clear = gr.Button(
280
+ "清空对话 🗑️",
281
  variant="secondary",
282
  elem_classes="clear-btn"
283
  )
284
+
285
  # 设置事件处理
286
+ submit_click = submit.click(
287
  fastgpt_chat.chat,
288
  inputs=[message, chatbot],
289
  outputs=[message, chatbot],
290
  api_name="chat"
291
  )
292
+
293
  message.submit(
294
  fastgpt_chat.chat,
295
  inputs=[message, chatbot],
296
  outputs=[message, chatbot]
297
  )
298
+
299
  clear.click(lambda: None, None, chatbot, queue=False)
300
+
301
  return interface
302
 
303
  if __name__ == "__main__":
304
  demo = create_chat_interface()
305
+ demo.launch(debug=True)