Stone164 commited on
Commit
014fcfa
·
verified ·
1 Parent(s): d3b7d4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -26
app.py CHANGED
@@ -186,10 +186,6 @@ with gr.Blocks(css="""
186
  font-family: 'Segoe UI', sans-serif;
187
  }
188
 
189
- .gr-chatbot {
190
- background-color: transparent !important;
191
- }
192
-
193
  .message.user {
194
  background-color: #cce6ff !important; /* Much lighter blue */
195
  color: #000000 !important;
@@ -263,12 +259,12 @@ with gr.Blocks(css="""
263
  请回答下面的问题,开始你的职业规划之旅!
264
  """)
265
 
266
- # 创建聊天界面
267
  chatbot = gr.Chatbot(
268
  height=500,
269
- bubble_full_width=False,
270
  show_label=False,
271
- show_copy_button=True
 
272
  )
273
 
274
  # 输入框和按钮
@@ -287,27 +283,39 @@ with gr.Blocks(css="""
287
  with gr.Row():
288
  reset_btn = gr.Button("🔄 重新开始")
289
 
290
- # 处理事件
 
 
 
 
 
 
 
 
 
 
 
 
 
291
  submit_btn.click(
292
- fn=lambda message, history: ("", history + [[message, None]]),
293
  inputs=[msg, chatbot],
294
- outputs=[msg, chatbot],
295
- queue=False
296
  ).then(
297
- fn=predict,
298
- inputs=[chatbot.get_value()[-1][0], chatbot.get_value()[:-1]],
299
- outputs=[chatbot.get_value()[-1][1]]
300
  )
301
 
 
302
  msg.submit(
303
- fn=lambda message, history: ("", history + [[message, None]]),
304
  inputs=[msg, chatbot],
305
- outputs=[msg, chatbot],
306
- queue=False
307
  ).then(
308
- fn=predict,
309
- inputs=[chatbot.get_value()[-1][0], chatbot.get_value()[:-1]],
310
- outputs=[chatbot.get_value()[-1][1]]
311
  )
312
 
313
  # 重置功能
@@ -327,10 +335,9 @@ with gr.Blocks(css="""
327
  )
328
 
329
  # 当页面加载时自动触发第一个问题
330
- def auto_first_question():
331
- global current_q_index
332
- current_q_index = 1 # 设为1因为会返回第0个问题
333
- return questions[0][1]
334
 
335
- # 自动触发第一个问题
336
- demo.load(auto_first_question, inputs=None, outputs=chatbot, show_progress=False)
 
186
  font-family: 'Segoe UI', sans-serif;
187
  }
188
 
 
 
 
 
189
  .message.user {
190
  background-color: #cce6ff !important; /* Much lighter blue */
191
  color: #000000 !important;
 
259
  请回答下面的问题,开始你的职业规划之旅!
260
  """)
261
 
262
+ # 创建聊天界面 - 修复类型和属性
263
  chatbot = gr.Chatbot(
264
  height=500,
 
265
  show_label=False,
266
+ show_copy_button=True,
267
+ type="messages" # 指定类型为messages
268
  )
269
 
270
  # 输入框和按钮
 
283
  with gr.Row():
284
  reset_btn = gr.Button("🔄 重新开始")
285
 
286
+ # 修复事件处理
287
+ def add_message(message, history):
288
+ history = history or []
289
+ history.append({"role": "user", "content": message})
290
+ return "", history
291
+
292
+ def bot_response(history):
293
+ history = history or []
294
+ user_message = history[-1]["content"]
295
+ bot_message = predict(user_message, history[:-1] if len(history) > 1 else [])
296
+ history.append({"role": "assistant", "content": bot_message})
297
+ return history
298
+
299
+ # 提交按钮点击事件
300
  submit_btn.click(
301
+ fn=add_message,
302
  inputs=[msg, chatbot],
303
+ outputs=[msg, chatbot]
 
304
  ).then(
305
+ fn=bot_response,
306
+ inputs=[chatbot],
307
+ outputs=[chatbot]
308
  )
309
 
310
+ # 文本框提交事件
311
  msg.submit(
312
+ fn=add_message,
313
  inputs=[msg, chatbot],
314
+ outputs=[msg, chatbot]
 
315
  ).then(
316
+ fn=bot_response,
317
+ inputs=[chatbot],
318
+ outputs=[chatbot]
319
  )
320
 
321
  # 重置功能
 
335
  )
336
 
337
  # 当页面加载时自动触发第一个问题
338
+ def auto_first_question():
339
+ return [{"role": "assistant", "content": questions[0][1]}]
340
+
341
+ demo.load(auto_first_question, inputs=None, outputs=chatbot)
342
 
343
+ demo.launch()