Rabbit-Innotech commited on
Commit
172bb91
·
verified ·
1 Parent(s): aef0372

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -12
app.py CHANGED
@@ -411,7 +411,7 @@ class GBVSupportChatbot:
411
  # Combine all data
412
  all_data = []
413
  all_data.extend(context_data)
414
- #all_data.extend([item for item in text_chunks if item not in all_data])
415
 
416
  # Add data to vector store
417
  self.vectorstore.add_texts(all_data)
@@ -463,16 +463,16 @@ class GBVSupportChatbot:
463
  # Generate welcome message
464
  welcome_message = self.user_session.get_welcome_message()
465
 
466
- # Add initial message to start the conversation
467
  chat_history = [(None, welcome_message)]
468
 
469
  # Return welcome message and update UI
470
  return welcome_message, gr.update(visible=True), gr.update(visible=False), chat_history
471
 
472
- def rag_memory_stream(self, message: str, history, user_lang: str = "kin_Latn", system_lang: str = "eng_Latn"):
473
  """Process user message, translate, and generate response."""
474
- # Translate user message to English
475
- english_message = self.translator.translate_text(message, user_lang, system_lang)
476
 
477
  # Add translated message to history
478
  self.user_session.add_to_history("user", english_message)
@@ -484,8 +484,8 @@ class GBVSupportChatbot:
484
  for new_text in rag_chain({"question": english_message}):
485
  full_response += new_text
486
 
487
- # Translate response back to user language
488
- translated_response = self.translator.translate_text(full_response, system_lang, user_lang)
489
 
490
  # Add response to history
491
  self.user_session.add_to_history("assistant", full_response)
@@ -514,10 +514,43 @@ class GBVSupportChatbot:
514
 
515
  # Chatbot section (initially hidden)
516
  with gr.Column(visible=False, elem_id="chatbot_container") as chatbot_container:
517
- chat_interface = gr.ChatInterface(
518
- fn=self.rag_memory_stream,
519
- title="Chat with GBVR",
520
- fill_height=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
  )
522
 
523
  # Footer with version info
@@ -527,7 +560,7 @@ class GBVSupportChatbot:
527
  submit_btn.click(
528
  self.collect_user_info,
529
  inputs=[first_name],
530
- outputs=[response_message, chatbot_container, registration_container, chat_interface.chatbot]
531
  )
532
 
533
  # Add CSS styles
 
411
  # Combine all data
412
  all_data = []
413
  all_data.extend(context_data)
414
+ all_data.extend([item for item in text_chunks if item not in all_data])
415
 
416
  # Add data to vector store
417
  self.vectorstore.add_texts(all_data)
 
463
  # Generate welcome message
464
  welcome_message = self.user_session.get_welcome_message()
465
 
466
+ # For the standard Chatbot component, the history format is different
467
  chat_history = [(None, welcome_message)]
468
 
469
  # Return welcome message and update UI
470
  return welcome_message, gr.update(visible=True), gr.update(visible=False), chat_history
471
 
472
+ def rag_memory_stream(self, message: str, history):
473
  """Process user message, translate, and generate response."""
474
+ # Translate user message to English (from Kinyarwanda by default)
475
+ english_message = self.translator.translate_text(message, "kin_Latn", "eng_Latn")
476
 
477
  # Add translated message to history
478
  self.user_session.add_to_history("user", english_message)
 
484
  for new_text in rag_chain({"question": english_message}):
485
  full_response += new_text
486
 
487
+ # Translate response back to user language (Kinyarwanda by default)
488
+ translated_response = self.translator.translate_text(full_response, "eng_Latn", "kin_Latn")
489
 
490
  # Add response to history
491
  self.user_session.add_to_history("assistant", full_response)
 
514
 
515
  # Chatbot section (initially hidden)
516
  with gr.Column(visible=False, elem_id="chatbot_container") as chatbot_container:
517
+ # Replace ChatInterface with standard Chatbot
518
+ chatbot = gr.Chatbot(
519
+ label="Chat with GBVR",
520
+ height=500,
521
+ show_label=True,
522
+ elem_id="chat_interface"
523
+ )
524
+
525
+ with gr.Row():
526
+ msg = gr.Textbox(
527
+ placeholder="Type your message here...",
528
+ label="Your message",
529
+ show_label=False,
530
+ container=False,
531
+ scale=7
532
+ )
533
+ send_btn = gr.Button("Send", variant="primary", scale=1)
534
+
535
+ # Set up the chat behavior
536
+ def chat_response(message, history):
537
+ # Clear input
538
+ yield history, ""
539
+ # Process message and generate response
540
+ for response in self.rag_memory_stream(message, history):
541
+ history.append((message, response))
542
+ yield history, ""
543
+
544
+ # Configure event handlers
545
+ msg.submit(
546
+ chat_response,
547
+ inputs=[msg, chatbot],
548
+ outputs=[chatbot, msg]
549
+ )
550
+ send_btn.click(
551
+ chat_response,
552
+ inputs=[msg, chatbot],
553
+ outputs=[chatbot, msg]
554
  )
555
 
556
  # Footer with version info
 
560
  submit_btn.click(
561
  self.collect_user_info,
562
  inputs=[first_name],
563
+ outputs=[response_message, chatbot_container, registration_container, chatbot]
564
  )
565
 
566
  # Add CSS styles