| |
| import gradio as gr |
|
|
| SUBJECTS = ["Mathematics", "English", "Kiswahili", "Physics", "Chemistry", "Biology", "Islamic Studies"] |
|
|
| WELCOME_MD = """ |
| # Elimuhub Tutor π€ |
| **Elimuhub Education Consultants** β Quick demo tutor (mobile friendly). |
| Contact: +254 731-838-387β’ elimuhubconsultant@gmail.com |
| |
| Type your question below or try quick commands: |
| - Say "generate quiz: subject, level, n" e.g., `generate quiz: math, KCSE, 3` |
| - Ask: "How do I solve quadratic equations?" |
| """ |
|
|
| def generate_quiz(subject="General", level="Any", n=3): |
| qlist = [] |
| for i in range(1, n+1): |
| qlist.append(f"{i}. Sample {level} {subject} question #{i}?") |
| return "\n".join(qlist) |
|
|
| def respond(user_message, history): |
| if history is None: |
| history = [] |
| msg = (user_message or "").strip() |
| if not msg: |
| return history, "" |
| low = msg.lower() |
|
|
| if any(greet in low for greet in ["hi", "hello", "hey", "salaam", "assalamu"]): |
| bot = "Hello! π I'm Elimuhub's demo tutor. Tell me the subject and topic (e.g., 'math algebra')." |
| elif low.startswith("generate quiz:"): |
| try: |
| rest = low.split("generate quiz:",1)[1].strip() |
| parts = [p.strip() for p in rest.split(",")] |
| subj = parts[0].title() if len(parts) > 0 and parts[0] else "General" |
| level = parts[1].upper() if len(parts) > 1 and parts[1] else "Any" |
| num = int(parts[2]) if len(parts) > 2 and parts[2].isdigit() else 3 |
| except Exception: |
| subj, level, num = "General", "Any", 3 |
| bot = generate_quiz(subj, level, num) |
| elif any(s.lower() in low for s in SUBJECTS): |
| bot = ("Great β you asked about a subject I recognize. " |
| "Please give the specific topic (e.g., algebra, verb tenses), and I'll give step-by-step help.") |
| elif "kcse" in low or "kcpe" in low or "igcse" in low or "ib" in low: |
| bot = "I can give exam-style tips and sample questions. Tell me the subject and the topic." |
| else: |
| |
| bot = ("Thanks β here's a quick answer guide:\n\n" |
| f"> You asked: {user_message}\n\n" |
| "I'm a demo assistant. For clearer help, say the subject and topic (e.g., 'math: quadratic formula').") |
|
|
| history.append((user_message, bot)) |
| return history, "" |
|
|
| with gr.Blocks() as demo: |
| gr.Markdown(WELCOME_MD) |
| chatbot = gr.Chatbot(elem_id="chatbot", label="Elimuhub Tutor") |
| with gr.Row(): |
| txt = gr.Textbox(show_label=False, placeholder="Type your question here and press Enter...") |
| send = gr.Button("Send") |
| send.click(respond, inputs=[txt, chatbot], outputs=[chatbot, txt]) |
| txt.submit(respond, inputs=[txt, chatbot], outputs=[chatbot, txt]) |
|
|
| if __name__ == "__main__": |
| demo.launch() |