Brianpuz commited on
Commit
57950c1
·
1 Parent(s): aa17742

Change chatbot

Browse files
Files changed (1) hide show
  1. app.py +36 -20
app.py CHANGED
@@ -518,11 +518,15 @@ def create_interface():
518
  with gr.Row():
519
  with gr.Column(scale=3):
520
  gr.Markdown("**Note**: You are chatting with the currently loaded model. If you've just completed processing, you're testing the modified model. To test the original model, reload it in the Model Processing tab.")
521
- chatbot = gr.Chatbot(
522
- label="Chat Window",
523
- height=400,
524
- type="messages"
 
 
 
525
  )
 
526
  msg = gr.Textbox(
527
  label="Input Message",
528
  placeholder="Enter your question...",
@@ -577,30 +581,42 @@ def create_interface():
577
  outputs=[process_output, process_image]
578
  )
579
 
580
- # Chat functionality with streaming
581
- def user(user_message, history):
582
- return "", history + [{"role": "user", "content": user_message}]
 
 
 
583
 
584
- def bot(history, max_new_tokens, temperature):
585
- if history and history[-1]["role"] == "user":
586
- # Get complete response first
587
- response, _ = processor.chat(history[-1]["content"], history[:-1], max_new_tokens, temperature)
 
 
 
 
 
 
 
 
588
  print(f"DEBUG: Bot function received response: {response[:200]}...")
589
  print(f"DEBUG: Bot function full response: {response}")
590
- print(f"DEBUG: Bot function history before append: {history}")
591
- history.append({"role": "assistant", "content": response})
592
- print(f"DEBUG: Bot function history after append: {history}")
593
- return history
 
594
 
595
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
596
- bot, [chatbot, max_new_tokens, temperature], chatbot
597
  )
598
 
599
- send_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
600
- bot, [chatbot, max_new_tokens, temperature], chatbot
601
  )
602
 
603
- clear.click(lambda: [], None, chatbot, queue=False)
604
 
605
  # Bind organization selection event
606
  export_to_org.change(
 
518
  with gr.Row():
519
  with gr.Column(scale=3):
520
  gr.Markdown("**Note**: You are chatting with the currently loaded model. If you've just completed processing, you're testing the modified model. To test the original model, reload it in the Model Processing tab.")
521
+
522
+ # Use Textbox instead of Chatbot for better compatibility
523
+ chat_display = gr.Textbox(
524
+ label="Chat History",
525
+ lines=20,
526
+ interactive=False,
527
+ value="Chat history will appear here..."
528
  )
529
+
530
  msg = gr.Textbox(
531
  label="Input Message",
532
  placeholder="Enter your question...",
 
581
  outputs=[process_output, process_image]
582
  )
583
 
584
+ # Chat functionality with simple text display
585
+ def user(user_message, chat_history):
586
+ if chat_history == "Chat history will appear here...":
587
+ chat_history = ""
588
+ new_history = chat_history + f"\n\n👤 User: {user_message}"
589
+ return "", new_history
590
 
591
+ def bot(chat_history, max_new_tokens, temperature):
592
+ # Extract the last user message
593
+ lines = chat_history.split('\n')
594
+ user_message = None
595
+ for line in reversed(lines):
596
+ if line.startswith('👤 User: '):
597
+ user_message = line[9:] # Remove "👤 User: " prefix
598
+ break
599
+
600
+ if user_message:
601
+ # Get complete response
602
+ response, _ = processor.chat(user_message, [], max_new_tokens, temperature)
603
  print(f"DEBUG: Bot function received response: {response[:200]}...")
604
  print(f"DEBUG: Bot function full response: {response}")
605
+
606
+ # Add assistant response to chat history
607
+ new_history = chat_history + f"\n\n🤖 Assistant: {response}"
608
+ return new_history
609
+ return chat_history
610
 
611
+ msg.submit(user, [msg, chat_display], [msg, chat_display], queue=False).then(
612
+ bot, [chat_display, max_new_tokens, temperature], chat_display
613
  )
614
 
615
+ send_btn.click(user, [msg, chat_display], [msg, chat_display], queue=False).then(
616
+ bot, [chat_display, max_new_tokens, temperature], chat_display
617
  )
618
 
619
+ clear.click(lambda: "Chat history will appear here...", None, chat_display, queue=False)
620
 
621
  # Bind organization selection event
622
  export_to_org.change(