Spaces:
Runtime error
Runtime error
Download app.py from abhidgp1978/MathBuddy: direct link, hf CLI and curl.
- Browser
- Download file 4.44 kB
-
https://huggingface.co/spaces/abhidgp1978/MathBuddy/resolve/main/app.py
- Command line
-
hf download hf://spaces/abhidgp1978/MathBuddy/app.py
-
curl -L -o app.py https://huggingface.co/spaces/abhidgp1978/MathBuddy/resolve/main/app.py
4.44 kB
| 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() | |