elimuhub commited on
Commit
a3a7971
Β·
verified Β·
1 Parent(s): 1418428

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +267 -117
app.py CHANGED
@@ -1,122 +1,272 @@
1
  import gradio as gr
2
- import pandas as pd
3
-
4
- # --- Branding & Info ---
5
- ORG_NAME = "Elimuhub Education Consultants"
6
- CONTACTS = """
7
- πŸ“ž Tel: +254 731 838 387
8
- πŸ“§ Email: elimuhubconsultant@gmail.com
9
- 🌐 Website: [elimuhub.simdif.com](https://elimuhub.simdif.com)
10
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # --- Tuition & Homeschooling Packages (DataFrame for display) ---
13
- fees_data = [
14
- ["Homeschooling - Standard (3 days/wk, 2 hrs/day, 24 hrs)", "KES 12,000"],
15
- ["Homeschooling - Intensive (5 days/wk, 4 hrs/day, 80 hrs)", "KES 20,000"],
16
- ["Homeschooling - Comprehensive (7 days/wk, 6 hrs/day, 168 hrs)", "KES 30,000"],
17
- ["Individual Tutoring (Hourly, 1 subject)", "KES 1,500 – 3,000 / hr"],
18
- ["Group Classes (per student, per session)", "KES 800 – 1,500"],
19
- ["Monthly Tuition Packages (varies by subject & hours)", "KES 15,000 – 30,000"],
20
- ["Holiday Programmes (duration dependent)", "KES 12,000 – 30,000"],
21
- ]
22
- fees_df = pd.DataFrame(fees_data, columns=["Package / Service", "Price"])
23
-
24
- # --- FAQ Responses ---
25
- FAQS = {
26
- "subjects": """πŸ“š **Subjects Offered**
27
- - Maths, Sciences, Languages, Humanities
28
- - Primary, Junior Secondary, Senior Secondary
29
- - IGCSE, IB, American K-12, British National
30
- - Adult Education & catch-up tutoring
31
- """,
32
- "curriculum": """πŸ“– **Curricula Covered**
33
- - Kenya: CBC, 8-4-4, KCPE, KCSE
34
- - International: IGCSE, IB, American K-12, British National Curriculum
35
- """,
36
- "homeschool": """🏠 **Homeschooling Services**
37
- - Full-time or part-time homeschooling (structured 4-week packages)
38
- - Personalized schedules & materials included
39
- - Assessments & progress tracking
40
- - Parent guidance available
41
- """,
42
- "tutor": """πŸ‘©β€πŸ« **Tutor Services**
43
- - One-on-one tuition (home or online)
44
- - Group classes available
45
- - Hourly, weekly, or monthly plans
46
- - Free educational assessment included
47
- """,
48
- "location": """πŸ“ **Service Locations**
49
- - In-home tutoring across all Nairobi estates
50
- - Online tutoring across Kenya
51
- - No registration/booking fee required
52
- """,
53
- "contact": f"""☎️ **Contact Us**\n{CONTACTS}"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # --- Chatbot Logic ---
57
- def chatbot(user_input):
58
- text = user_input.lower()
59
-
60
- if "fee" in text or "price" in text or "package" in text:
61
- return "Here are our tuition & homeschooling packages πŸ‘‡ (see the table below).", fees_df
62
- elif "subject" in text:
63
- return FAQS["subjects"], None
64
- elif "curriculum" in text or "syllabus" in text:
65
- return FAQS["curriculum"], None
66
- elif "homeschool" in text:
67
- return FAQS["homeschool"], None
68
- elif "tutor" in text or "teacher" in text:
69
- return FAQS["tutor"], None
70
- elif "location" in text or "where" in text:
71
- return FAQS["location"], None
72
- elif "contact" in text or "phone" in text or "email" in text or "website" in text:
73
- return FAQS["contact"], None
74
- elif "hello" in text or "hi" in text:
75
- return f"πŸ‘‹ Hello, welcome to {ORG_NAME}!\nAsk about *fees*, *subjects*, *curriculum*, *homeschooling*, or *contacts*.", None
76
- else:
77
- return "I can help with tuition, homeschooling, packages, and curricula. Try asking: 'What are your fees?' or use the quick menu below ⬇️", None
78
-
79
- # --- Gradio UI ---
80
- with gr.Blocks() as demo:
81
- gr.Markdown(f"## 🀝 Welcome to {ORG_NAME}\nAsk us about our tuition & homeschooling services!")
82
-
83
- chatbox = gr.Chatbot(label="Chat with Elimuhub")
84
- msg = gr.Textbox(placeholder="Type your question here...")
85
-
86
- fees_table = gr.DataFrame(value=pd.DataFrame([], columns=["Package / Service", "Price"]),
87
- label="Fee Packages", visible=False)
88
-
89
- with gr.Row():
90
- gr.Markdown("### πŸ“Œ Quick Menu")
91
- with gr.Row():
92
- btn_fees = gr.Button("πŸ’° Fees & Packages")
93
- btn_subjects = gr.Button("πŸ“š Subjects")
94
- btn_curriculum = gr.Button("πŸ“– Curriculum")
95
- btn_homeschool = gr.Button("🏠 Homeschooling")
96
- btn_tutors = gr.Button("πŸ‘©β€πŸ« Tutors")
97
- btn_location = gr.Button("πŸ“ Locations")
98
- btn_contact = gr.Button("☎️ Contact Info")
99
-
100
- # Handle text input
101
- def respond(message, history):
102
- response, table = chatbot(message)
103
- history.append((message, response))
104
- if table is not None:
105
- return history, table, gr.update(visible=True)
106
- else:
107
- return history, pd.DataFrame([], columns=["Package / Service", "Price"]), gr.update(visible=False)
108
-
109
- msg.submit(respond, [msg, chatbox], [chatbox, fees_table, fees_table])
110
-
111
- # Button actions (simulate user queries)
112
- btn_fees.click(lambda: respond("fees", []), outputs=[chatbox, fees_table, fees_table])
113
- btn_subjects.click(lambda: respond("subjects", []), outputs=[chatbox, fees_table, fees_table])
114
- btn_curriculum.click(lambda: respond("curriculum", []), outputs=[chatbox, fees_table, fees_table])
115
- btn_homeschool.click(lambda: respond("homeschool", []), outputs=[chatbox, fees_table, fees_table])
116
- btn_tutors.click(lambda: respond("tutor", []), outputs=[chatbox, fees_table, fees_table])
117
- btn_location.click(lambda: respond("location", []), outputs=[chatbox, fees_table, fees_table])
118
- btn_contact.click(lambda: respond("contact", []), outputs=[chatbox, fees_table, fees_table])
119
-
120
- # Launch app
121
  if __name__ == "__main__":
122
- demo.launch()
 
1
  import gradio as gr
2
+ import re
3
+ from datetime import datetime
4
+ import random
5
+
6
+ # Chatbot knowledge base
7
+ ELIMUHUB_KNOWLEDGE = {
8
+ "greeting": {
9
+ "patterns": ["hello", "hi", "hey", "good morning", "good afternoon", "good evening"],
10
+ "responses": [
11
+ "Hello! Welcome to Elimuhub Education Consultants! πŸŽ“ How can I assist you today?",
12
+ "Hi there! Welcome to Elimuhub Education Consultants. How can I help with your educational needs?",
13
+ "Greetings! Thank you for contacting Elimuhub Education Consultants."
14
+ ]
15
+ },
16
+
17
+ "homeschooling": {
18
+ "patterns": ["homeschool", "home school", "home schooling", "homeschooling"],
19
+ "responses": ["""**Our Homeschooling Services:** πŸ“š
20
+
21
+ We provide comprehensive support for families seeking an alternative education path:
22
+
23
+ β€’ **Customized Learning Plans**: Tailored to your child's individual needs and academic goals
24
+ β€’ **Flexible Scheduling**: Adapt to your family's busy schedule
25
+ β€’ **Full-Time or Part-Time**: Options for complete education or supplementary support
26
+ β€’ **Experienced Tutors**: Highly qualified educators in their fields
27
+ β€’ **Exam Preparation**: KCPE, KCSE, IGCSE, and other national/international exams
28
+
29
+ Would you like more specific information about any of these aspects?"""]
30
+ },
31
+
32
+ "subjects": {
33
+ "patterns": ["subjects", "courses", "what do you teach", "which subjects"],
34
+ "responses": ["""**Subjects We Offer:** πŸ“–
35
+
36
+ **Primary & Secondary Level:**
37
+ - Mathematics, Science (Biology, Chemistry, Physics)
38
+ - English, Kiswahili, History, Geography
39
+ - CRE, Business Studies, Agriculture, Computer Studies
40
+
41
+ **International Curricula (IGCSE & IB):**
42
+ - Mathematics, Sciences, English, Business Studies
43
+ - Languages (French, German, Spanish, Italian, Chinese)
44
+ - Humanities (History, Geography)
45
+
46
+ **Other:**
47
+ - Adult Education and Professional Training
48
+
49
+ Which subject are you particularly interested in?"""]
50
+ },
51
+
52
+ "curriculum": {
53
+ "patterns": ["curriculum", "syllabus", "what curriculum", "which system"],
54
+ "responses": ["""**Curricula We Teach:** 🌍
55
+
56
+ **Kenyan System:**
57
+ - Competency-Based Curriculum (CBC)
58
+ - 8-4-4 System (KCPE & KCSE)
59
+
60
+ **International Systems:**
61
+ - British National Curriculum (IGCSE & A-Levels)
62
+ - International Baccalaureate (IB)
63
+ - American K-12 Curriculum
64
+
65
+ **Adult Education:** Support for lifelong learners and retake students
66
+
67
+ Which curriculum are you interested in?"""]
68
+ },
69
+
70
+ "fees": {
71
+ "patterns": ["fees", "price", "cost", "how much", "payment", "packages"],
72
+ "responses": ["""**Our Fee Structure:** πŸ’°
73
+
74
+ *Fees are negotiable with flexible payment options (hourly, weekly, monthly)*
75
+
76
+ **Tuition Packages:**
77
+ - Personalized 1-on-1: KES 1,500 - 3,000/hour
78
+ - Weekly Group: KES 800 - 1,500/session (max 10 students)
79
+ - Monthly Plan: KES 15,000 - 30,000/month
80
+ - Holiday Program: From KES 2,000/session
81
+ - Online Classes: 20% discount on in-person rates
82
+
83
+ **Homeschooling Packages:**
84
+ - Full-Time: KES 30,000 - 50,000+/month
85
+ - Part-Time: KES 700 - 900+/hour
86
+ - Sibling discounts available
87
+
88
+ We offer a **FREE 15-minute consultation** to discuss the best package!"""]
89
+ },
90
+
91
+ "contact": {
92
+ "patterns": ["contact", "whatsapp", "phone", "number", "reach", "talk to person", "consultation"],
93
+ "responses": ["""**Contact Us:** πŸ“ž
94
+
95
+ You can reach us on **WhatsApp: +254 731 838 387**
96
+
97
+ We offer a **FREE 15-minute consultation** to help you find the best educational plan for your child!
98
+
99
+ Feel free to ask any specific questions you may have about our services."""]
100
+ },
101
+
102
+ "thanks": {
103
+ "patterns": ["thank", "thanks", "appreciate"],
104
+ "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."]
105
+ },
106
+
107
+ "default": {
108
+ "responses": [
109
+ "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",
110
+ "That's a great question! At Elimuhub, we specialize in personalized tuition and homeschooling. How can I assist you specifically?",
111
+ "I'd be happy to help! You can ask me about our tuition services, homeschooling programs, subjects offered, or fee structures."
112
+ ]
113
+ }
114
+ }
115
 
116
+ def classify_intent(user_input):
117
+ """Classify user intent based on input patterns"""
118
+ user_input = user_input.lower().strip()
119
+
120
+ for intent, data in ELIMUHUB_KNOWLEDGE.items():
121
+ if intent == "default":
122
+ continue
123
+ for pattern in data["patterns"]:
124
+ if re.search(r'\b' + re.escape(pattern) + r'\b', user_input):
125
+ return intent
126
+ return "default"
127
+
128
+ def get_chatbot_response(user_input, chat_history):
129
+ """Generate appropriate response based on user input"""
130
+ intent = classify_intent(user_input)
131
+ responses = ELIMUHUB_KNOWLEDGE[intent]["responses"]
132
+
133
+ # Select a random response from available options
134
+ response = random.choice(responses)
135
+
136
+ # Add timestamp and branding
137
+ timestamp = datetime.now().strftime("%H:%M")
138
+ branded_response = f"**Elimuhub Assistant** ({timestamp}): {response}"
139
+
140
+ return branded_response
141
+
142
+ def chat_interface(message, chat_history):
143
+ """Main chat interface function"""
144
+ chat_history = chat_history or []
145
+
146
+ if not message.strip():
147
+ return chat_history, "Please type a message to start chatting!"
148
+
149
+ # Get bot response
150
+ bot_response = get_chatbot_response(message, chat_history)
151
+
152
+ # Update chat history
153
+ chat_history.append((message, bot_response))
154
+
155
+ return chat_history, ""
156
+
157
+ # Custom CSS for better styling
158
+ custom_css = """
159
+ .gradio-container {
160
+ font-family: 'Arial', sans-serif;
161
+ max-width: 800px;
162
+ margin: 0 auto;
163
+ }
164
+ .elimuhub-header {
165
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
166
+ padding: 25px;
167
+ border-radius: 15px;
168
+ color: white;
169
+ text-align: center;
170
+ margin-bottom: 20px;
171
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
172
+ }
173
+ .elimuhub-header h1 {
174
+ margin: 0;
175
+ font-size: 2.5em;
176
+ }
177
+ .elimuhub-header p {
178
+ margin: 10px 0 0 0;
179
+ font-size: 1.2em;
180
+ }
181
+ .chatbot {
182
+ min-height: 400px;
183
+ border: 2px solid #e0e0e0;
184
+ border-radius: 10px;
185
+ }
186
+ .quick-questions {
187
+ background: #f8f9fa;
188
+ padding: 15px;
189
+ border-radius: 10px;
190
+ border-left: 4px solid #667eea;
191
  }
192
+ """
193
+
194
+ # Create the Gradio interface
195
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
196
+ with gr.Column():
197
+ # Header
198
+ gr.HTML("""
199
+ <div class="elimuhub-header">
200
+ <h1>πŸŽ“ Elimuhub Education Consultants</h1>
201
+ <p>Personalized Tuition & Homeschooling Services</p>
202
+ <p><em>"Helping students achieve their academic goals"</em></p>
203
+ </div>
204
+ """)
205
+
206
+ # Chatbot
207
+ chatbot = gr.Chatbot(
208
+ label="Chat with Elimuhub Assistant",
209
+ placeholder="Ask me about homeschooling, subjects, curriculum, or fees...",
210
+ height=400,
211
+ show_copy_button=True
212
+ )
213
+
214
+ # Message input
215
+ with gr.Row():
216
+ msg = gr.Textbox(
217
+ label="Your Message",
218
+ placeholder="Type your question here... (e.g., Tell me about homeschooling, What subjects do you offer?)",
219
+ scale=4,
220
+ container=False
221
+ )
222
+ submit_btn = gr.Button("Send πŸ“€", variant="primary", scale=1)
223
+
224
+ # Quick questions section
225
+ with gr.Row():
226
+ gr.HTML("""
227
+ <div class="quick-questions">
228
+ <h3>πŸ’‘ Quick Questions You Can Ask:</h3>
229
+ <p>β€’ "Tell me about homeschooling"<br>
230
+ β€’ "Which subjects do you offer?"<br>
231
+ β€’ "What curriculum do you teach?"<br>
232
+ β€’ "What are your fees and packages?"<br>
233
+ β€’ "How can I contact you?"</p>
234
+ </div>
235
+ """)
236
+
237
+ # Event handlers
238
+ def handle_submit(message, chat_history):
239
+ chat_history = chat_history or []
240
+ if message.strip():
241
+ bot_response = get_chatbot_response(message, chat_history)
242
+ chat_history.append((message, bot_response))
243
+ return chat_history, ""
244
+
245
+ submit_btn.click(
246
+ fn=handle_submit,
247
+ inputs=[msg, chatbot],
248
+ outputs=[chatbot, msg]
249
+ )
250
+
251
+ msg.submit(
252
+ fn=handle_submit,
253
+ inputs=[msg, chatbot],
254
+ outputs=[chatbot, msg]
255
+ )
256
+
257
+ # Examples
258
+ gr.Examples(
259
+ examples=[
260
+ ["Tell me about homeschooling"],
261
+ ["Which subjects do you offer for primary level?"],
262
+ ["What curriculum do you teach?"],
263
+ ["What are your fees for one-on-one tutoring?"],
264
+ ["How can I contact Elimuhub?"],
265
+ ["Do you offer science subjects?"]
266
+ ],
267
+ inputs=msg,
268
+ label="Click on any example to quickly ask:"
269
+ )
270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  if __name__ == "__main__":
272
+ demo.launch(debug=True)