leonsimon23 commited on
Commit
db47d7c
·
verified ·
1 Parent(s): 60fe0d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -156
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,137 +98,141 @@ class FastGPTChat:
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
- }
144
- .chatbot-row {
145
- margin-bottom: 10px;
146
- display: flex;
147
- align-items: start; /* 对齐消息气泡顶部 */
148
- gap: 10px; /* 消息之间的间距 */
149
- }
150
- .chatbot .user {
151
- justify-content: flex-end;
152
- }
153
- .chatbot .bot {
154
- justify-content: flex-start;
155
- }
156
- .chatbot .user-message,
157
- .chatbot .bot-message {
158
- max-width: 75%;
159
- padding: 10px 15px;
160
- border-radius: 20px;
161
- word-wrap: break-word;
162
- box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
163
- }
164
- .chatbot .user-message {
165
- background-color: #dcf8c6; /* 更清新的用户消息背景 */
166
- color: #333;
167
- border-top-right-radius: 5px;
168
- margin-left: auto; /* 将用户消息推到右边 */
169
- }
170
- .chatbot .bot-message {
171
- background-color: #e1e1eb; /* 更柔和的Bot消息背景 */
172
- color: #333;
173
- border-top-left-radius: 5px;
174
- margin-right: auto; /* 将 Bot 消息推到左边 */
175
- }
176
- /* 移除头像 */
177
- .chatbot .avatar {
178
- display: none;
179
- }
180
- .chatbot :last-child {
181
- margin-bottom: 0;
182
- }
183
- /* 输入区域样式 */
184
- .input-container {
185
- display: flex;
186
- gap: 10px;
187
- }
188
- .message-box {
189
- border: 1px solid #ced4da !important;
190
- border-radius: 8px !important;
191
- padding: 10px !important;
192
- font-size: 1rem !important;
193
- flex-grow: 1;
194
- transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out !important;
195
- }
196
- .message-box:focus {
197
- border-color: #007bff !important;
198
- box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25) !important;
199
- }
200
- /* 按钮样式 */
201
- .button-container {
202
- display: flex;
203
- gap: 10px;
204
- }
205
- .submit-btn, .clear-btn {
206
- padding: 10px 20px !important;
207
- border: none !important;
208
- border-radius: 8px !important;
209
- color: white !important;
210
- cursor: pointer;
211
- transition: background-color 0.3s ease;
212
- font-size: 1rem !important;
213
- font-weight: 500 !important;
214
- }
215
- .submit-btn {
216
- background-color: #007bff !important;
217
- }
218
- .submit-btn:hover {
219
- background-color: #0056b3 !important;
220
- }
221
- .clear-btn {
222
- background-color: #dc3545 !important;
223
- }
224
- .clear-btn:hover {
225
- background-color: #bd2130 !important;
226
- }
227
- """
228
-
229
  with gr.Blocks(
230
  title="AI用药咨询助手",
231
- css=css
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  ) as interface:
233
  with gr.Column(elem_classes="container"):
234
  with gr.Column(elem_classes="title-container"):
@@ -238,38 +242,39 @@ def create_chat_interface():
238
  ### 您的专业用药咨询顾问,随时为您解答用药相关问题
239
  """
240
  )
241
-
242
  with gr.Column(elem_classes="chat-container"):
243
  chatbot = gr.Chatbot(
244
- height=None,
245
  container=False,
246
  elem_classes="chatbot",
247
  show_label=False,
248
  bubble_full_width=False,
249
- avatar_images=None # 禁用头像
250
  )
251
-
252
  with gr.Column(elem_classes="input-container"):
253
  message = gr.Textbox(
254
  show_label=False,
255
  placeholder="请输入您的用药问题...",
256
  container=False,
257
  elem_classes="message-box",
258
- lines=3
 
259
  )
260
-
261
  with gr.Row(elem_classes="button-container"):
262
  submit = gr.Button(
263
- "发送 💊",
264
  variant="primary",
265
  elem_classes="submit-btn"
266
  )
267
  clear = gr.Button(
268
- "清空对话 🗑️",
269
  variant="secondary",
270
  elem_classes="clear-btn"
271
  )
272
-
273
  # 设置事件处理
274
  submit_click = submit.click(
275
  fastgpt_chat.chat,
@@ -277,19 +282,17 @@ def create_chat_interface():
277
  outputs=[message, chatbot],
278
  api_name="chat"
279
  )
280
-
281
  message.submit(
282
  fastgpt_chat.chat,
283
  inputs=[message, chatbot],
284
  outputs=[message, chatbot]
285
  )
286
-
287
  clear.click(lambda: None, None, chatbot, queue=False)
288
-
289
  return interface
290
 
291
  if __name__ == "__main__":
292
  demo = create_chat_interface()
293
- # 请检查您的 FASTGPT_API_KEY 和 FASTGPT_BASE_URL 环境变量是否已正确配置
294
- # 另外,请确保您的网络连接正常,以便能够调用 API。
295
- 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
  with gr.Blocks(
103
  title="AI用药咨询助手",
104
+ css="""
105
+ /* 整体容器样式 */
106
+ .container {
107
+ width: 90%;
108
+ max-width: 1000px; /* 最大宽度限制 */
109
+ margin: auto;
110
+ padding: 1rem; /* 减小内边距 */
111
+ box-sizing: border-box;
112
+ }
113
+ html, body {
114
+ height: 100%; /* 设置 html 和 body 的高度 */
115
+ background-color: #f5f5f5;
116
+ }
117
+ /* 标题区域样式 */
118
+ .title-container {
119
+ text-align: center;
120
+ margin-bottom: 1rem; /* 减小下边距 */
121
+ }
122
+ .title-container h1 {
123
+ color: #2C3E50;
124
+ font-size: 2rem; /* 减小标题字号 */
125
+ margin-bottom: 0.5rem;
126
+ }
127
+ .title-container h3 {
128
+ color: #7F8C8D;
129
+ font-size: 1rem; /* 减小副标题字号 */
130
+ font-weight: normal;
131
+ }
132
+ /* 聊天容器样式 */
133
+ .chat-container {
134
+ background-color: #FFFFFF;
135
+ border-radius: 15px; /* 减小圆角 */
136
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
137
+ padding: 1rem; /* 减小内边距 */
138
+ margin-bottom: 1rem; /* 减小下边距 */
139
+ }
140
+ /* 聊天框样式 */
141
+ .chatbot {
142
+ background-color: transparent;
143
+ border-radius: 15px;
144
+ padding: 0; /* 移除内边距 */
145
+ margin-bottom: 1rem; /* 减小下边距 */
146
+ height: calc(100vh - 350px) !important; /* 动态计算高度 */
147
+ min-height: 300px; /* 最小高度 */
148
+ overflow-y: auto;
149
+ border: none !important;
150
+ }
151
+ /* 消息样式 */
152
+ .chatbot .user-message {
153
+ background: linear-gradient(135deg, #6366F1, #4F46E5) !important;
154
+ color: white !important;
155
+ border-radius: 18px 18px 4px 18px !important;
156
+ padding: 0.8rem 1.2rem !important; /* 减小内边距 */
157
+ margin: 0.5rem 0 !important; /* 减小外边距 */
158
+ max-width: 70% !important; /* 减小最大宽度 */
159
+ float: right !important;
160
+ clear: both !important;
161
+ box-shadow: 0 2px 6px rgba(99, 102, 241, 0.2) !important;
162
+ }
163
+ .chatbot .bot-message {
164
+ background-color: #f2f2f2 !important;
165
+ border: none !important; /* 移除边框 */
166
+ color: #1F2937 !important;
167
+ border-radius: 18px 18px 18px 4px !important;
168
+ padding: 0.8rem 1.2rem !important; /* 减小内边距 */
169
+ margin: 0.5rem 0 !important; /* 减小外边距 */
170
+ max-width: 70% !important; /* 减小最大宽度 */
171
+ float: left !important;
172
+ clear: both !important;
173
+ box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05) !important;
174
+ }
175
+ .chatbot .user-message p, .chatbot .bot-message p{
176
+ margin: 0 !important;
177
+ line-height: 1.5 !important;
178
+ }
179
+ .chatbot .user-message:before, .chatbot .bot-message:before {
180
+ content: none !important;
181
+ }
182
+ .chatbot .user-message:after, .chatbot .bot-message:after {
183
+ content: none !important;
184
+ }
185
+ /* 输入区域样式 */
186
+ .input-container {
187
+ background: white;
188
+ padding: 1rem; /* 减小内边距 */
189
+ border-radius: 15px;
190
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
191
+ }
192
+ .message-box {
193
+ border: 1px solid #E5E7EB !important; /* 减小边框宽度 */
194
+ border-radius: 12px !important;
195
+ padding: 0.8rem !important; /* 减小内边距 */
196
+ font-size: 0.9rem !important; /* 减小字号 */
197
+ transition: all 0.3s ease !important;
198
+ background: white !important;
199
+ }
200
+ .message-box:focus {
201
+ border-color: #6366F1 !important;
202
+ box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2) !important; /* 减小阴影 */
203
+ }
204
+ /* 按钮样式 */
205
+ .button-container {
206
+ display: flex;
207
+ gap: 0.5rem; /* 减小间距 */
208
+ margin-top: 0.5rem; /* 减小上边距 */
209
+ }
210
+ .submit-btn {
211
+ background: linear-gradient(135deg, #6366F1, #4F46E5) !important;
212
+ color: white !important;
213
+ padding: 0.6rem 1.2rem !important; /* 减小内边距 */
214
+ border-radius: 12px !important;
215
+ font-weight: 500 !important; /* 减小字重 */
216
+ transition: all 0.3s ease !important;
217
+ flex: 1;
218
+ }
219
+ .submit-btn:hover {
220
+ transform: translateY(-1px); /* 减小位移 */
221
+ box-shadow: 0 2px 6px rgba(99, 102, 241, 0.3) !important; /* 减小阴影 */
222
+ }
223
+ .clear-btn {
224
+ background: #EF4444 !important;
225
+ color: white !important;
226
+ padding: 0.6rem 1.2rem !important; /* 减小内边距 */
227
+ border-radius: 12px !important;
228
+ font-weight: 500 !important; /* 减小字重 */
229
+ transition: all 0.3s ease !important;
230
+ }
231
+ .clear-btn:hover {
232
+ transform: translateY(-1px); /* 减小位移 */
233
+ box-shadow: 0 2px 6px rgba(239, 68, 68, 0.3) !important; /* 减小阴影 */
234
+ }
235
+ """
236
  ) as interface:
237
  with gr.Column(elem_classes="container"):
238
  with gr.Column(elem_classes="title-container"):
 
242
  ### 您的专业用药咨询顾问,随时为您解答用药相关问题
243
  """
244
  )
245
+
246
  with gr.Column(elem_classes="chat-container"):
247
  chatbot = gr.Chatbot(
248
+ height=600,
249
  container=False,
250
  elem_classes="chatbot",
251
  show_label=False,
252
  bubble_full_width=False,
253
+ avatar_images=("👤", "🤖")
254
  )
255
+
256
  with gr.Column(elem_classes="input-container"):
257
  message = gr.Textbox(
258
  show_label=False,
259
  placeholder="请输入您的用药问题...",
260
  container=False,
261
  elem_classes="message-box",
262
+ lines=1, # 减小输入框初始行数
263
+ max_lines=5 # 设置最大行数
264
  )
265
+
266
  with gr.Row(elem_classes="button-container"):
267
  submit = gr.Button(
268
+ "发送 💊",
269
  variant="primary",
270
  elem_classes="submit-btn"
271
  )
272
  clear = gr.Button(
273
+ "清空对话 🗑️",
274
  variant="secondary",
275
  elem_classes="clear-btn"
276
  )
277
+
278
  # 设置事件处理
279
  submit_click = submit.click(
280
  fastgpt_chat.chat,
 
282
  outputs=[message, chatbot],
283
  api_name="chat"
284
  )
285
+
286
  message.submit(
287
  fastgpt_chat.chat,
288
  inputs=[message, chatbot],
289
  outputs=[message, chatbot]
290
  )
291
+
292
  clear.click(lambda: None, None, chatbot, queue=False)
293
+
294
  return interface
295
 
296
  if __name__ == "__main__":
297
  demo = create_chat_interface()
298
+ demo.queue().launch(debug=True, server_name="0.0.0.0")