app.py
#1
by
elimuhub
- opened
app.py
CHANGED
@@ -1,48 +1,66 @@
|
|
1 |
-
#
|
|
|
2 |
|
3 |
-
|
4 |
-
import google.generativeai as genai
|
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 |
-
response = model.generate_content(prompt)
|
41 |
-
|
42 |
-
# Display AI's response in the app
|
43 |
-
st.subheader("AI's Response:")
|
44 |
-
st.code(response.text, language="python")
|
45 |
-
|
46 |
-
# Optionally, you can add a footer or additional features here
|
47 |
-
st.markdown("---")
|
48 |
-
st.markdown("Powered by **Streamlit** and **Google Gemini AI**")
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
|
4 |
+
SUBJECTS = ["Mathematics", "English", "Kiswahili", "Physics", "Chemistry", "Biology", "Islamic Studies"]
|
|
|
5 |
|
6 |
+
WELCOME_MD = """
|
7 |
+
# Elimuhub Tutor π€
|
8 |
+
**Elimuhub Education Consultants** β Quick demo tutor (mobile friendly).
|
9 |
+
Contact: +254 731-838-387β’ elimuhubconsultant@gmail.com
|
10 |
|
11 |
+
Type your question below or try quick commands:
|
12 |
+
- Say "generate quiz: subject, level, n" e.g., `generate quiz: math, KCSE, 3`
|
13 |
+
- Ask: "How do I solve quadratic equations?"
|
14 |
+
"""
|
15 |
|
16 |
+
def generate_quiz(subject="General", level="Any", n=3):
|
17 |
+
qlist = []
|
18 |
+
for i in range(1, n+1):
|
19 |
+
qlist.append(f"{i}. Sample {level} {subject} question #{i}?")
|
20 |
+
return "\n".join(qlist)
|
21 |
|
22 |
+
def respond(user_message, history):
|
23 |
+
if history is None:
|
24 |
+
history = []
|
25 |
+
msg = (user_message or "").strip()
|
26 |
+
if not msg:
|
27 |
+
return history, ""
|
28 |
+
low = msg.lower()
|
29 |
|
30 |
+
if any(greet in low for greet in ["hi", "hello", "hey", "salaam", "assalamu"]):
|
31 |
+
bot = "Hello! π I'm Elimuhub's demo tutor. Tell me the subject and topic (e.g., 'math algebra')."
|
32 |
+
elif low.startswith("generate quiz:"):
|
33 |
+
try:
|
34 |
+
rest = low.split("generate quiz:",1)[1].strip()
|
35 |
+
parts = [p.strip() for p in rest.split(",")]
|
36 |
+
subj = parts[0].title() if len(parts) > 0 and parts[0] else "General"
|
37 |
+
level = parts[1].upper() if len(parts) > 1 and parts[1] else "Any"
|
38 |
+
num = int(parts[2]) if len(parts) > 2 and parts[2].isdigit() else 3
|
39 |
+
except Exception:
|
40 |
+
subj, level, num = "General", "Any", 3
|
41 |
+
bot = generate_quiz(subj, level, num)
|
42 |
+
elif any(s.lower() in low for s in SUBJECTS):
|
43 |
+
bot = ("Great β you asked about a subject I recognize. "
|
44 |
+
"Please give the specific topic (e.g., algebra, verb tenses), and I'll give step-by-step help.")
|
45 |
+
elif "kcse" in low or "kcpe" in low or "igcse" in low or "ib" in low:
|
46 |
+
bot = "I can give exam-style tips and sample questions. Tell me the subject and the topic."
|
47 |
+
else:
|
48 |
+
# simple demo answer: echo plus tips
|
49 |
+
bot = ("Thanks β here's a quick answer guide:\n\n"
|
50 |
+
f"> You asked: {user_message}\n\n"
|
51 |
+
"I'm a demo assistant. For clearer help, say the subject and topic (e.g., 'math: quadratic formula').")
|
52 |
|
53 |
+
history.append((user_message, bot))
|
54 |
+
return history, ""
|
55 |
|
56 |
+
with gr.Blocks() as demo:
|
57 |
+
gr.Markdown(WELCOME_MD)
|
58 |
+
chatbot = gr.Chatbot(elem_id="chatbot", label="Elimuhub Tutor")
|
59 |
+
with gr.Row():
|
60 |
+
txt = gr.Textbox(show_label=False, placeholder="Type your question here and press Enter...")
|
61 |
+
send = gr.Button("Send")
|
62 |
+
send.click(respond, inputs=[txt, chatbot], outputs=[chatbot, txt])
|
63 |
+
txt.submit(respond, inputs=[txt, chatbot], outputs=[chatbot, txt])
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|