Spaces:
Runtime error
Runtime error
File size: 4,442 Bytes
10219a4 98d280f 10219a4 967463e 10219a4 967463e 10219a4 967463e 10219a4 967463e 10219a4 967463e 10219a4 967463e 98d280f 10219a4 967463e 10219a4 967463e 10219a4 967463e 10219a4 967463e 10219a4 967463e 10219a4 967463e 10219a4 | 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | import gradio as gr
from transformers import pipeline
import sympy as sp
import json
import os
# ----------------------------
# Load AI model (reasoning)
# ----------------------------
reasoner = pipeline("text2text-generation", model="google/flan-t5-small", device=-1)
# ----------------------------
# Points storage (JSON file)
# ----------------------------
POINTS_FILE = "points.json"
def load_points():
if os.path.exists(POINTS_FILE):
with open(POINTS_FILE, "r") as f:
return json.load(f)
return {"points": 0}
def save_points(data):
with open(POINTS_FILE, "w") as f:
json.dump(data, f)
# ----------------------------
# Core function: check kidโs answer + hints
# ----------------------------
def check_answer(problem, kid_answer):
data = load_points()
try:
# Sympy parses the math problem
expr = sp.sympify(problem)
correct_answer = sp.simplify(expr)
# Try parsing kidโs answer
try:
kid_val = sp.simplify(kid_answer)
except:
kid_val = kid_answer
# Hints: break problem into simpler parts
hints = []
try:
if isinstance(expr, sp.Basic):
hints.append(f"๐น First, look at the expression: {problem}")
if expr.is_Add or expr.is_Mul:
for arg in expr.args:
hints.append(f"๐น Consider part: {arg}")
if expr.is_Pow:
hints.append("๐น Remember powers are solved before multiplication/addition.")
if expr.is_Mul:
hints.append("๐น Multiplication/division happens before addition/subtraction.")
if expr.is_Add:
hints.append("๐น After simplification, add/subtract the remaining parts.")
except Exception:
hints.append("๐น Try simplifying step by step.")
# Generate friendly reasoning with AI
prompt = f"Explain to a 10-year-old how to solve {problem} step by step in simple language."
reasoning = reasoner(prompt, max_length=180, temperature=0.5)[0]['generated_text']
# Compare answers
if str(kid_val) == str(correct_answer):
data["points"] += 10
save_points(data)
feedback = (
f"๐ Great job! Your answer {kid_answer} is correct.\n\n"
+ "\n".join(hints)
+ f"\n\n๐ก Reasoning:\n{reasoning}\n\nโ
Final Answer: {correct_answer}"
)
else:
feedback = (
f"โ Your answer {kid_answer} is not correct.\n\n"
f"โ
Correct Answer: {correct_answer}\n\n"
+ "\n".join(hints)
+ f"\n\n๐ก Reasoning:\n{reasoning}"
)
return feedback, f"โญ Your Points: {data['points']}"
except Exception as e:
return (f"โ ๏ธ Could not process problem.\nError: {str(e)}",
f"โญ Your Points: {data['points']}")
# ----------------------------
# Build Gradio Interface
# ----------------------------
with gr.Blocks() as demo:
gr.HTML(
"""
<div style='text-align:center;'>
<img src='https://huggingface.co/spaces/abhidgp1978/MathBuddy/resolve/main/logo.png' width='90'/>
</div>
"""
)
gr.Markdown("<div style='text-align:center; font-size:24px; color:#4DA6FF;'>๐งฎ Math Buddy</div>")
gr.Markdown("<div style='text-align:center; color:#FF914D;'>Your Friendly Math Helper!</div>")
with gr.Row():
problem_input = gr.Textbox(
label="โ๏ธ Enter a math problem",
placeholder="Example: 12 / (2 + 4)"
)
with gr.Row():
kid_answer = gr.Textbox(
label="๐ค Your Answer",
placeholder="Type your answer here"
)
with gr.Row():
explanation_output = gr.Textbox(
label="Feedback, Hints & Reasoning",
lines=10
)
with gr.Row():
points_output = gr.Textbox(
label="Progress",
lines=1
)
check_btn = gr.Button("๐ Check My Answer")
check_btn.click(
check_answer,
inputs=[problem_input, kid_answer],
outputs=[explanation_output, points_output]
)
# ----------------------------
# Run app
# ----------------------------
if __name__ == "__main__":
demo.launch()
|