elimuhub-tutor / app.py
elimuhub's picture
Update app.py
a3a7971 verified
raw
history blame
9.14 kB
import gradio as gr
import re
from datetime import datetime
import random
# Chatbot knowledge base
ELIMUHUB_KNOWLEDGE = {
"greeting": {
"patterns": ["hello", "hi", "hey", "good morning", "good afternoon", "good evening"],
"responses": [
"Hello! Welcome to Elimuhub Education Consultants! πŸŽ“ How can I assist you today?",
"Hi there! Welcome to Elimuhub Education Consultants. How can I help with your educational needs?",
"Greetings! Thank you for contacting Elimuhub Education Consultants."
]
},
"homeschooling": {
"patterns": ["homeschool", "home school", "home schooling", "homeschooling"],
"responses": ["""**Our Homeschooling Services:** πŸ“š
We provide comprehensive support for families seeking an alternative education path:
β€’ **Customized Learning Plans**: Tailored to your child's individual needs and academic goals
β€’ **Flexible Scheduling**: Adapt to your family's busy schedule
β€’ **Full-Time or Part-Time**: Options for complete education or supplementary support
β€’ **Experienced Tutors**: Highly qualified educators in their fields
β€’ **Exam Preparation**: KCPE, KCSE, IGCSE, and other national/international exams
Would you like more specific information about any of these aspects?"""]
},
"subjects": {
"patterns": ["subjects", "courses", "what do you teach", "which subjects"],
"responses": ["""**Subjects We Offer:** πŸ“–
**Primary & Secondary Level:**
- Mathematics, Science (Biology, Chemistry, Physics)
- English, Kiswahili, History, Geography
- CRE, Business Studies, Agriculture, Computer Studies
**International Curricula (IGCSE & IB):**
- Mathematics, Sciences, English, Business Studies
- Languages (French, German, Spanish, Italian, Chinese)
- Humanities (History, Geography)
**Other:**
- Adult Education and Professional Training
Which subject are you particularly interested in?"""]
},
"curriculum": {
"patterns": ["curriculum", "syllabus", "what curriculum", "which system"],
"responses": ["""**Curricula We Teach:** 🌍
**Kenyan System:**
- Competency-Based Curriculum (CBC)
- 8-4-4 System (KCPE & KCSE)
**International Systems:**
- British National Curriculum (IGCSE & A-Levels)
- International Baccalaureate (IB)
- American K-12 Curriculum
**Adult Education:** Support for lifelong learners and retake students
Which curriculum are you interested in?"""]
},
"fees": {
"patterns": ["fees", "price", "cost", "how much", "payment", "packages"],
"responses": ["""**Our Fee Structure:** πŸ’°
*Fees are negotiable with flexible payment options (hourly, weekly, monthly)*
**Tuition Packages:**
- Personalized 1-on-1: KES 1,500 - 3,000/hour
- Weekly Group: KES 800 - 1,500/session (max 10 students)
- Monthly Plan: KES 15,000 - 30,000/month
- Holiday Program: From KES 2,000/session
- Online Classes: 20% discount on in-person rates
**Homeschooling Packages:**
- Full-Time: KES 30,000 - 50,000+/month
- Part-Time: KES 700 - 900+/hour
- Sibling discounts available
We offer a **FREE 15-minute consultation** to discuss the best package!"""]
},
"contact": {
"patterns": ["contact", "whatsapp", "phone", "number", "reach", "talk to person", "consultation"],
"responses": ["""**Contact Us:** πŸ“ž
You can reach us on **WhatsApp: +254 731 838 387**
We offer a **FREE 15-minute consultation** to help you find the best educational plan for your child!
Feel free to ask any specific questions you may have about our services."""]
},
"thanks": {
"patterns": ["thank", "thanks", "appreciate"],
"responses": ["You're welcome! 😊 Let me know if you have any other questions about Elimuhub's services.", "Happy to help! Feel free to ask anything else about our educational programs."]
},
"default": {
"responses": [
"I'm here to help you with Elimuhub's educational services! You can ask me about:\nβ€’ Homeschooling\nβ€’ Subjects offered\nβ€’ Curriculum\nβ€’ Fees & packages\nβ€’ Contact information",
"That's a great question! At Elimuhub, we specialize in personalized tuition and homeschooling. How can I assist you specifically?",
"I'd be happy to help! You can ask me about our tuition services, homeschooling programs, subjects offered, or fee structures."
]
}
}
def classify_intent(user_input):
"""Classify user intent based on input patterns"""
user_input = user_input.lower().strip()
for intent, data in ELIMUHUB_KNOWLEDGE.items():
if intent == "default":
continue
for pattern in data["patterns"]:
if re.search(r'\b' + re.escape(pattern) + r'\b', user_input):
return intent
return "default"
def get_chatbot_response(user_input, chat_history):
"""Generate appropriate response based on user input"""
intent = classify_intent(user_input)
responses = ELIMUHUB_KNOWLEDGE[intent]["responses"]
# Select a random response from available options
response = random.choice(responses)
# Add timestamp and branding
timestamp = datetime.now().strftime("%H:%M")
branded_response = f"**Elimuhub Assistant** ({timestamp}): {response}"
return branded_response
def chat_interface(message, chat_history):
"""Main chat interface function"""
chat_history = chat_history or []
if not message.strip():
return chat_history, "Please type a message to start chatting!"
# Get bot response
bot_response = get_chatbot_response(message, chat_history)
# Update chat history
chat_history.append((message, bot_response))
return chat_history, ""
# Custom CSS for better styling
custom_css = """
.gradio-container {
font-family: 'Arial', sans-serif;
max-width: 800px;
margin: 0 auto;
}
.elimuhub-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 25px;
border-radius: 15px;
color: white;
text-align: center;
margin-bottom: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.elimuhub-header h1 {
margin: 0;
font-size: 2.5em;
}
.elimuhub-header p {
margin: 10px 0 0 0;
font-size: 1.2em;
}
.chatbot {
min-height: 400px;
border: 2px solid #e0e0e0;
border-radius: 10px;
}
.quick-questions {
background: #f8f9fa;
padding: 15px;
border-radius: 10px;
border-left: 4px solid #667eea;
}
"""
# Create the Gradio interface
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
with gr.Column():
# Header
gr.HTML("""
<div class="elimuhub-header">
<h1>πŸŽ“ Elimuhub Education Consultants</h1>
<p>Personalized Tuition & Homeschooling Services</p>
<p><em>"Helping students achieve their academic goals"</em></p>
</div>
""")
# Chatbot
chatbot = gr.Chatbot(
label="Chat with Elimuhub Assistant",
placeholder="Ask me about homeschooling, subjects, curriculum, or fees...",
height=400,
show_copy_button=True
)
# Message input
with gr.Row():
msg = gr.Textbox(
label="Your Message",
placeholder="Type your question here... (e.g., Tell me about homeschooling, What subjects do you offer?)",
scale=4,
container=False
)
submit_btn = gr.Button("Send πŸ“€", variant="primary", scale=1)
# Quick questions section
with gr.Row():
gr.HTML("""
<div class="quick-questions">
<h3>πŸ’‘ Quick Questions You Can Ask:</h3>
<p>β€’ "Tell me about homeschooling"<br>
β€’ "Which subjects do you offer?"<br>
β€’ "What curriculum do you teach?"<br>
β€’ "What are your fees and packages?"<br>
β€’ "How can I contact you?"</p>
</div>
""")
# Event handlers
def handle_submit(message, chat_history):
chat_history = chat_history or []
if message.strip():
bot_response = get_chatbot_response(message, chat_history)
chat_history.append((message, bot_response))
return chat_history, ""
submit_btn.click(
fn=handle_submit,
inputs=[msg, chatbot],
outputs=[chatbot, msg]
)
msg.submit(
fn=handle_submit,
inputs=[msg, chatbot],
outputs=[chatbot, msg]
)
# Examples
gr.Examples(
examples=[
["Tell me about homeschooling"],
["Which subjects do you offer for primary level?"],
["What curriculum do you teach?"],
["What are your fees for one-on-one tutoring?"],
["How can I contact Elimuhub?"],
["Do you offer science subjects?"]
],
inputs=msg,
label="Click on any example to quickly ask:"
)
if __name__ == "__main__":
demo.launch(debug=True)