sofzcc commited on
Commit
93b82c2
·
verified ·
1 Parent(s): 8af9350

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -38,6 +38,7 @@ def get_default_config():
38
  "index_directory": "./index",
39
  },
40
  "models": {
 
41
  "embedding": "sentence-transformers/all-MiniLM-L6-v2",
42
  "qa": "deepset/roberta-base-squad2",
43
  },
@@ -402,13 +403,32 @@ print("=" * 50)
402
  # -----------------------------
403
 
404
  def rag_respond(message, history):
405
- """Handle chat messages for chatbot UI"""
 
 
 
406
  if not message or not str(message).strip():
 
407
  return "", history
408
 
409
  user_msg = str(message)
 
 
 
 
 
 
 
 
410
  bot_reply = rag_index.answer(user_msg)
411
- history = history + [[user_msg, bot_reply]]
 
 
 
 
 
 
 
412
  return "", history
413
 
414
 
@@ -425,7 +445,6 @@ def upload_to_kb(files):
425
 
426
  for f in files:
427
  # Gradio File object or temp file path
428
- # In older Gradio, `f.name` is the temp file path
429
  src_path = getattr(f, "name", None) or str(f)
430
  if not os.path.exists(src_path):
431
  continue
@@ -463,7 +482,7 @@ def rebuild_index():
463
  )
464
 
465
 
466
- # Description + examples
467
  description = WELCOME_MSG
468
  if not rag_index.initialized or rag_index.index is None or not rag_index.chunks:
469
  description += (
@@ -489,39 +508,20 @@ with gr.Blocks(title=CONFIG["client"]["name"]) as demo:
489
  gr.Markdown(description)
490
 
491
  with gr.Tab("Chat"):
492
- chatbot = gr.Chatbot(label="RAG Chat")
 
493
  with gr.Row():
494
  txt = gr.Textbox(
495
  show_label=False,
496
- placeholder="Ask a question about your documents...",
497
  lines=2,
498
  )
 
499
  with gr.Row():
500
  send_btn = gr.Button("Send")
501
  clear_btn = gr.Button("Clear")
502
 
503
- # Pre-fill example buttons if available
504
- if examples:
505
- gr.Markdown("### Example questions")
506
- example_btns = []
507
- with gr.Row():
508
- for ex in examples:
509
- example_btns.append(gr.Button(ex))
510
-
511
- def use_example(example, history):
512
- """When clicking an example, send it as a message"""
513
- bot_reply = rag_index.answer(example)
514
- history = history + [[example, bot_reply]]
515
- return history
516
-
517
- for btn, ex in zip(example_btns, examples):
518
- btn.click(
519
- use_example,
520
- inputs=[gr.State(ex), chatbot],
521
- outputs=chatbot,
522
- )
523
-
524
- # Chat logic wiring
525
  txt.submit(rag_respond, [txt, chatbot], [txt, chatbot])
526
  send_btn.click(rag_respond, [txt, chatbot], [txt, chatbot])
527
  clear_btn.click(lambda: ([], ""), None, [chatbot, txt])
 
38
  "index_directory": "./index",
39
  },
40
  "models": {
41
+ # You can also use "all-MiniLM-L6-v2" here, but this path works well on HF
42
  "embedding": "sentence-transformers/all-MiniLM-L6-v2",
43
  "qa": "deepset/roberta-base-squad2",
44
  },
 
403
  # -----------------------------
404
 
405
  def rag_respond(message, history):
406
+ """Handle chat messages for chatbot UI (messages format)"""
407
+ if history is None:
408
+ history = []
409
+
410
  if not message or not str(message).strip():
411
+ # Keep history unchanged, just clear input
412
  return "", history
413
 
414
  user_msg = str(message)
415
+
416
+ # Append user message
417
+ history.append({
418
+ "role": "user",
419
+ "content": user_msg,
420
+ })
421
+
422
+ # Get bot reply
423
  bot_reply = rag_index.answer(user_msg)
424
+
425
+ # Append assistant message
426
+ history.append({
427
+ "role": "assistant",
428
+ "content": bot_reply,
429
+ })
430
+
431
+ # Clear textbox, return updated history
432
  return "", history
433
 
434
 
 
445
 
446
  for f in files:
447
  # Gradio File object or temp file path
 
448
  src_path = getattr(f, "name", None) or str(f)
449
  if not os.path.exists(src_path):
450
  continue
 
482
  )
483
 
484
 
485
+ # Description + (optional) examples
486
  description = WELCOME_MSG
487
  if not rag_index.initialized or rag_index.index is None or not rag_index.chunks:
488
  description += (
 
508
  gr.Markdown(description)
509
 
510
  with gr.Tab("Chat"):
511
+ chatbot = gr.Chatbot(label="RAG Chat") # messages-format by default
512
+
513
  with gr.Row():
514
  txt = gr.Textbox(
515
  show_label=False,
516
+ placeholder="Ask a question about your documents and press Enter to send...",
517
  lines=2,
518
  )
519
+
520
  with gr.Row():
521
  send_btn = gr.Button("Send")
522
  clear_btn = gr.Button("Clear")
523
 
524
+ # Enter submits, Send button also submits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
525
  txt.submit(rag_respond, [txt, chatbot], [txt, chatbot])
526
  send_btn.click(rag_respond, [txt, chatbot], [txt, chatbot])
527
  clear_btn.click(lambda: ([], ""), None, [chatbot, txt])