Bwrite commited on
Commit
078e953
Β·
verified Β·
1 Parent(s): 09d5940

Update app.py

Browse files

Admission ChatBot

Files changed (1) hide show
  1. app.py +139 -95
app.py CHANGED
@@ -1,9 +1,3 @@
1
-
2
- # BOUESTI Virtual Assistant Chatbot
3
- # Bamidele Olumilua University of Education, Science, and Technology, Ikere Ekiti
4
-
5
-
6
-
7
  import nltk
8
  import re
9
  import random
@@ -16,11 +10,30 @@ import gradio as gr
16
  warnings.filterwarnings('ignore')
17
 
18
  # Download required NLTK data
19
- nltk.download('punkt')
20
- nltk.download('punkt_tab')
21
- nltk.download('wordnet')
22
- nltk.download('stopwords')
23
- nltk.download('omw-1.4')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  from nltk.stem import WordNetLemmatizer
26
  from nltk.corpus import stopwords
@@ -128,7 +141,7 @@ class BOUESTIChatbot:
128
 
129
  # Documents for Screening
130
  'screening_documents': [
131
- 'O’Level results (WAEC, NECO, or NABTEB).',
132
  'JAMB UTME result slip.',
133
  'JAMB admission letter',
134
  'Birth certificate or age declaration.',
@@ -163,7 +176,7 @@ class BOUESTIChatbot:
163
  "I can help you with questions about BOUESTI admissions, programs, fees, and general information. Could you please ask about one of these topics?"
164
  ],
165
  'screening_documents': [
166
- 'The Documents Required for Screening are: \n1. O’Level results (WAEC, NECO, or NABTEB).\n2. JAMB UTME result slip.\n3. JAMB admission letter\n4. Birth certificate or age declaration.\n5. Passport photographs.\n6. Local Government Identification.\n7. Bio-data form\n8. School fees payment receipt\n9. For Direct Entry, additional documents including A-Level results, HND certificates, or transcripts are required'
167
  ]
168
  }
169
 
@@ -226,8 +239,6 @@ class BOUESTIChatbot:
226
  r'\bhelp guide\b',
227
  r'\bshow.*guide\b'
228
  ]
229
-
230
-
231
  }
232
 
233
  self.vectorizer = TfidfVectorizer(tokenizer=self.tokenize, lowercase=True)
@@ -449,7 +460,6 @@ class BOUESTIChatbot:
449
  response += f"{i}. {doc}\n"
450
  return response
451
 
452
-
453
  def generate_response(self, user_input):
454
  """Generate response based on user input"""
455
  intent = self.detect_intent(user_input)
@@ -490,99 +500,133 @@ class BOUESTIChatbot:
490
  else:
491
  return random.choice(self.responses['default'])
492
 
493
-
494
- def gradio_chat(self, message, history):
495
- """Gradio chat interface function"""
496
- if not message.strip():
497
- return history, ""
498
-
499
- # Generate response
500
- response = self.generate_response(message)
501
-
502
- # Add to history
503
- history.append([message, response])
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;">
521
- <span style="font-size: 2.2em;">πŸŽ“</span> BOUESTI ChatBot
522
- </h1>
523
- <h3 style="text-align: center;">Bamidele Olumilua University of Education, Science, and Technology</h3>
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
 
 
 
 
 
 
 
 
 
 
1
  import nltk
2
  import re
3
  import random
 
10
  warnings.filterwarnings('ignore')
11
 
12
  # Download required NLTK data
13
+ try:
14
+ nltk.data.find('tokenizers/punkt')
15
+ except LookupError:
16
+ nltk.download('punkt')
17
+
18
+ try:
19
+ nltk.data.find('tokenizers/punkt_tab')
20
+ except LookupError:
21
+ nltk.download('punkt_tab')
22
+
23
+ try:
24
+ nltk.data.find('corpora/wordnet')
25
+ except LookupError:
26
+ nltk.download('wordnet')
27
+
28
+ try:
29
+ nltk.data.find('corpora/stopwords')
30
+ except LookupError:
31
+ nltk.download('stopwords')
32
+
33
+ try:
34
+ nltk.data.find('corpora/omw-1.4')
35
+ except LookupError:
36
+ nltk.download('omw-1.4')
37
 
38
  from nltk.stem import WordNetLemmatizer
39
  from nltk.corpus import stopwords
 
141
 
142
  # Documents for Screening
143
  'screening_documents': [
144
+ 'O'Level results (WAEC, NECO, or NABTEB).',
145
  'JAMB UTME result slip.',
146
  'JAMB admission letter',
147
  'Birth certificate or age declaration.',
 
176
  "I can help you with questions about BOUESTI admissions, programs, fees, and general information. Could you please ask about one of these topics?"
177
  ],
178
  'screening_documents': [
179
+ 'The Documents Required for Screening are: \n1. O'Level results (WAEC, NECO, or NABTEB).\n2. JAMB UTME result slip.\n3. JAMB admission letter\n4. Birth certificate or age declaration.\n5. Passport photographs.\n6. Local Government Identification.\n7. Bio-data form\n8. School fees payment receipt\n9. For Direct Entry, additional documents including A-Level results, HND certificates, or transcripts are required'
180
  ]
181
  }
182
 
 
239
  r'\bhelp guide\b',
240
  r'\bshow.*guide\b'
241
  ]
 
 
242
  }
243
 
244
  self.vectorizer = TfidfVectorizer(tokenizer=self.tokenize, lowercase=True)
 
460
  response += f"{i}. {doc}\n"
461
  return response
462
 
 
463
  def generate_response(self, user_input):
464
  """Generate response based on user input"""
465
  intent = self.detect_intent(user_input)
 
500
  else:
501
  return random.choice(self.responses['default'])
502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503
  def create_gradio_interface(self):
504
+ """Create Gradio interface"""
505
+ with gr.Blocks(
506
+ title="BOUESTI Virtual Assistant",
507
+ theme=gr.themes.Soft(),
508
+ css="""
509
+ .gradio-container {
510
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
511
+ }
512
+ .chatbot {
513
+ height: 400px;
514
+ }
515
+ .header-text {
516
+ text-align: center;
517
+ padding: 20px;
518
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
519
+ color: white;
520
+ border-radius: 10px;
521
+ margin-bottom: 20px;
522
+ }
523
+ .instruction-box {
524
+ border: 2px solid #e0e0e0;
525
+ padding: 15px;
526
+ border-radius: 10px;
527
+ margin-bottom: 15px;
528
+ background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
529
+ }
530
+ """
531
+ ) as interface:
532
+
533
+ # Header
534
  gr.HTML("""
535
+ <div class="header-text">
536
+ <h1>πŸŽ“ BOUESTI Virtual Assistant</h1>
537
+ <h3>Bamidele Olumilua University of Education, Science, and Technology</h3>
538
+ <p>Get instant answers about admissions, programs, fees, and more!</p>
 
 
539
  </div>
540
+ """)
541
+
542
+ # Chat interface
543
  with gr.Row():
544
  with gr.Column(scale=4):
545
+ chatbot = gr.Chatbot(
546
+ [],
547
  elem_id="chatbot",
548
  bubble_full_width=False,
549
  height=400,
550
+ show_label=False,
551
+ elem_classes=["chatbot"]
 
 
 
 
 
552
  )
553
+
554
  with gr.Row():
555
+ msg = gr.Textbox(
556
+ placeholder="Ask me about BOUESTI admissions, programs, fees, or any other questions...",
557
+ container=False,
558
+ scale=7,
559
+ show_label=False
560
+ )
561
+ submit_btn = gr.Button("Send", variant="primary", scale=1)
562
+
563
+ clear_btn = gr.Button("Clear Chat", variant="secondary", size="sm")
564
+
565
  with gr.Column(scale=2):
566
+ # Instruction box
567
  gr.HTML("""
568
+ <div class="instruction-box">
569
+ <h4>πŸ’‘ Quick Help</h4>
570
+ <p>Whenever you are stuck, enter the magic word:</p>
571
+ <p><strong style="color: #591305; font-size: 1.1em;">Guide me</strong></p>
572
+ </div>
573
  """)
574
+
575
+ # Quick buttons
576
+ gr.HTML("<h4>πŸš€ Quick Actions</h4>")
577
+
578
+ quick_btns = [
579
+ ("πŸ“‹ Admission Requirements", "admission requirements"),
580
+ ("πŸ’° Fees Structure", "fees"),
581
+ ("πŸŽ“ Programs", "programs"),
582
+ ("πŸ“ž Contact Info", "contact"),
583
+ ("πŸ“„ Screening Documents", "screening documents")
584
+ ]
585
+
586
+ quick_buttons = []
587
+ for btn_text, btn_value in quick_btns:
588
+ btn = gr.Button(btn_text, variant="outline", size="sm")
589
+ quick_buttons.append((btn, btn_value))
590
+
591
  # Event handlers
592
+ def respond(message, history):
593
+ if not message.strip():
594
+ return history, ""
595
+
596
+ # Generate response
597
+ response = self.generate_response(message)
598
+
599
+ # Add to history
600
+ history.append([message, response])
601
+
602
+ return history, ""
603
+
604
+ def clear_chat():
605
+ return [], ""
606
+
607
+ def quick_response(quick_msg, history):
608
+ return respond(quick_msg, history)
609
+
610
+ # Button events
611
+ submit_btn.click(respond, [msg, chatbot], [chatbot, msg])
612
+ msg.submit(respond, [msg, chatbot], [chatbot, msg])
613
+ clear_btn.click(clear_chat, [], [chatbot, msg])
614
+
615
+ # Quick button events
616
+ for btn, value in quick_buttons:
617
+ btn.click(
618
+ lambda x=value: quick_response(x, []),
619
+ [],
620
+ [chatbot, msg]
621
+ )
622
+
623
  return interface
 
 
 
 
 
 
 
 
 
 
 
 
 
624
 
625
+ # Initialize and launch the chatbot
626
+ def main():
627
+ chatbot = BOUESTIChatbot()
628
+ interface = chatbot.create_gradio_interface()
629
+ interface.launch()
630
 
631
+ if __name__ == "__main__":
632
+ main()