File size: 2,814 Bytes
f9278ed
 
3403463
f9278ed
3403463
f9278ed
 
 
 
3403463
f9278ed
 
 
 
3403463
f9278ed
 
 
 
 
3403463
f9278ed
 
 
 
 
 
 
3403463
f9278ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3403463
f9278ed
 
3403463
f9278ed
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# app.py
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:
        # simple demo answer: echo plus tips
        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()