Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,66 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
)
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 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()
|