Bwrite commited on
Commit
62dce3c
·
verified ·
1 Parent(s): eba002b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -40
app.py CHANGED
@@ -504,10 +504,17 @@ class BOUESTIChatbot:
504
 
505
  return history, ""
506
 
 
 
 
 
 
 
 
507
  def create_gradio_interface(self):
508
- """Create Gradio interface"""
509
  with gr.Blocks(title="BOUESTI Virtual Assistant", theme=gr.themes.Soft()) as interface:
510
- # Header
511
  gr.HTML("""
512
  <div class="gradio-header">
513
  <h1 style="text-align: center;">
@@ -517,50 +524,65 @@ class BOUESTIChatbot:
517
  <p style="text-align: center;">Get instant answers about admissions, programs, fees, and more!</p>
518
  </div>
519
  """)
520
-
521
- # Chat interface
522
- with gr.Row():
523
- with gr.Column(scale=4):
524
- chatbot_window = gr.Chatbot([], elem_id="chatbot", bubble_full_width=False, height=400, show_label=False)
525
- with gr.Row():
 
 
 
 
 
526
  msg = gr.Textbox(
527
  placeholder="Ask me about BOUESTI admissions, programs, fees, or any other questions...",
528
  container=False,
529
  scale=7,
530
  show_label=False
531
  )
532
- submit_btn = gr.Button("Send", variant="primary", scale=1)
533
-
534
- clear_btn = gr.Button("Clear Chat", variant="secondary", size="sm")
535
-
536
- with gr.Column(scale=2):
537
- # Instruction box
538
- gr.HTML("""
539
- <div style="border: 1px solid #e0e0e0; padding: 10px; border-radius: 5px; margin-bottom: 10px; background-color: #f9f9f9;">
540
- <p style="margin: 0;">Whenever you are stuck, enter the magic word: <strong style="color: #591305;">Guide me</strong></p>
541
- </div>
542
- """)
543
-
544
- # Handlers must be here inside the Blocks context
545
- def respond(message, history):
546
- if not message.strip():
547
- return history, ""
548
- response = self.generate_response(message)
549
- history.append([message, response])
550
- return history, ""
551
-
552
- def clear_chat():
553
- return [], ""
554
- submit_btn.click(respond, [msg, chatbot_window], [chatbot_window, msg])
555
- msg.submit(respond, [msg, chatbot_window], [chatbot_window, msg])
556
- clear_btn.click(clear_chat, [], [chatbot_window, msg])
557
-
 
 
 
558
  return interface
 
 
 
 
 
 
 
 
 
 
 
 
 
559
 
560
 
561
-
562
- # Initialize and expose the chatbot interface for Hugging Face Spaces
563
- chatbot = BOUESTIChatbot()
564
- demo = chatbot.create_gradio_interface()
565
- demo.launch(debug=True, share=True)
566
-
 
504
 
505
  return history, ""
506
 
507
+ def gradio_chat(self, message, history):
508
+ """Gradio chat interface function"""
509
+ if not message.strip():
510
+ return history, ""
511
+ response = self.generate_response(message)
512
+ return history + [[message, response]], ""
513
+
514
  def create_gradio_interface(self):
515
+ """Create Gradio interface with proper event binding"""
516
  with gr.Blocks(title="BOUESTI Virtual Assistant", theme=gr.themes.Soft()) as interface:
517
+ # Header section
518
  gr.HTML("""
519
  <div class="gradio-header">
520
  <h1 style="text-align: center;">
 
524
  <p style="text-align: center;">Get instant answers about admissions, programs, fees, and more!</p>
525
  </div>
526
  """)
527
+
528
+ # Chat interface layout
529
+ with gr.Row():
530
+ with gr.Column(scale=4):
531
+ chatbot_window = gr.Chatbot(
532
+ value=[],
533
+ elem_id="chatbot",
534
+ bubble_full_width=False,
535
+ height=400,
536
+ show_label=False
537
+ )
538
  msg = gr.Textbox(
539
  placeholder="Ask me about BOUESTI admissions, programs, fees, or any other questions...",
540
  container=False,
541
  scale=7,
542
  show_label=False
543
  )
544
+ with gr.Row():
545
+ submit_btn = gr.Button("Send", variant="primary")
546
+ clear_btn = gr.Button("Clear", variant="secondary")
547
+
548
+ # Instruction panel
549
+ with gr.Column(scale=2):
550
+ gr.HTML("""
551
+ <div style="border: 1px solid #e0e0e0; padding: 10px; border-radius: 5px; margin-bottom: 10px; background-color: #f9f9f9;">
552
+ <p style="margin: 0;">Whenever you are stuck, enter the magic word: <strong style="color: #591305;">Guide me</strong></p>
553
+ </div>
554
+ """)
555
+
556
+ # Event handlers
557
+ submit_btn.click(
558
+ fn=self.gradio_chat,
559
+ inputs=[msg, chatbot_window],
560
+ outputs=[chatbot_window, msg]
561
+ )
562
+ msg.submit(
563
+ fn=self.gradio_chat,
564
+ inputs=[msg, chatbot_window],
565
+ outputs=[chatbot_window, msg]
566
+ )
567
+ clear_btn.click(
568
+ fn=lambda: ([], ""),
569
+ inputs=[],
570
+ outputs=[chatbot_window, msg]
571
+ )
572
+
573
  return interface
574
+
575
+ # Hugging Face Spaces configuration
576
+ if __name__ == "__main__":
577
+ chatbot = BOUESTIChatbot()
578
+ demo = chatbot.create_gradio_interface()
579
+ demo.launch(
580
+ server_name="0.0.0.0",
581
+ server_port=7860,
582
+ enable_queue=True,
583
+ share=False,
584
+ show_error=True
585
+ )
586
+ a few seconds ago
587
 
588