Update app.py
Browse filesAdmission ChatBot
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 |
-
|
20 |
-
nltk.
|
21 |
-
|
22 |
-
nltk.download('
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
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
|
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
|
516 |
-
with gr.Blocks(
|
517 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
518 |
gr.HTML("""
|
519 |
-
<div class="
|
520 |
-
<h1
|
521 |
-
|
522 |
-
|
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
|
529 |
with gr.Row():
|
530 |
with gr.Column(scale=4):
|
531 |
-
|
532 |
-
|
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 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
549 |
with gr.Column(scale=2):
|
|
|
550 |
gr.HTML("""
|
551 |
-
|
552 |
-
|
553 |
-
|
|
|
|
|
554 |
""")
|
555 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
556 |
# Event handlers
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
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()
|