Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import sympy as sp
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# ----------------------------
|
| 8 |
+
# Load AI model (reasoning)
|
| 9 |
+
# ----------------------------
|
| 10 |
+
reasoner = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 11 |
+
|
| 12 |
+
# ----------------------------
|
| 13 |
+
# Points storage (JSON file)
|
| 14 |
+
# ----------------------------
|
| 15 |
+
POINTS_FILE = "points.json"
|
| 16 |
+
|
| 17 |
+
def load_points():
|
| 18 |
+
if os.path.exists(POINTS_FILE):
|
| 19 |
+
with open(POINTS_FILE, "r") as f:
|
| 20 |
+
return json.load(f)
|
| 21 |
+
return {"points": 0}
|
| 22 |
+
|
| 23 |
+
def save_points(data):
|
| 24 |
+
with open(POINTS_FILE, "w") as f:
|
| 25 |
+
json.dump(data, f)
|
| 26 |
+
|
| 27 |
+
# ----------------------------
|
| 28 |
+
# Core math solver function
|
| 29 |
+
# ----------------------------
|
| 30 |
+
def solve_math(problem):
|
| 31 |
+
data = load_points()
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# Sympy parse + simplify
|
| 35 |
+
expr = sp.sympify(problem)
|
| 36 |
+
answer = sp.simplify(expr)
|
| 37 |
+
|
| 38 |
+
# Generate step-by-step explanation with model
|
| 39 |
+
prompt = f"Explain step by step how to solve: {problem}"
|
| 40 |
+
explanation = reasoner(prompt, max_length=200, temperature=0.3)[0]['generated_text']
|
| 41 |
+
|
| 42 |
+
# Add points
|
| 43 |
+
data["points"] += 10
|
| 44 |
+
save_points(data)
|
| 45 |
+
|
| 46 |
+
return (
|
| 47 |
+
f"📝 Step-by-step:\n{explanation}\n\n✅ Final Answer: {answer}",
|
| 48 |
+
f"⭐ Your Points: {data['points']}"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return (f"⚠️ Oops! I couldn’t understand that problem.\nError: {str(e)}",
|
| 53 |
+
f"⭐ Your Points: {data['points']}")
|
| 54 |
+
|
| 55 |
+
# ----------------------------
|
| 56 |
+
# Build Gradio Interface
|
| 57 |
+
# ----------------------------
|
| 58 |
+
with gr.Blocks(
|
| 59 |
+
css="""
|
| 60 |
+
.gradio-container {
|
| 61 |
+
background: #FAFAFA;
|
| 62 |
+
font-family: 'Nunito', sans-serif;
|
| 63 |
+
}
|
| 64 |
+
.title {
|
| 65 |
+
color: #4DA6FF;
|
| 66 |
+
font-family: 'Baloo', cursive;
|
| 67 |
+
font-size: 2.2em;
|
| 68 |
+
text-align: center;
|
| 69 |
+
margin-bottom: 10px;
|
| 70 |
+
}
|
| 71 |
+
.subtitle {
|
| 72 |
+
color: #FF914D;
|
| 73 |
+
font-size: 1.2em;
|
| 74 |
+
text-align: center;
|
| 75 |
+
margin-bottom: 20px;
|
| 76 |
+
}
|
| 77 |
+
.points {
|
| 78 |
+
color: #82E682;
|
| 79 |
+
font-size: 1.3em;
|
| 80 |
+
font-weight: bold;
|
| 81 |
+
text-align: center;
|
| 82 |
+
margin-top: 10px;
|
| 83 |
+
}
|
| 84 |
+
"""
|
| 85 |
+
) as demo:
|
| 86 |
+
|
| 87 |
+
# Header with logo + title
|
| 88 |
+
gr.HTML(
|
| 89 |
+
"""
|
| 90 |
+
<div style='text-align:center;'>
|
| 91 |
+
<img src='file/logo.png' alt='Math Buddy Logo' width='80'/>
|
| 92 |
+
</div>
|
| 93 |
+
"""
|
| 94 |
+
)
|
| 95 |
+
gr.Markdown("<div class='title'>🧮 Math Buddy</div>")
|
| 96 |
+
gr.Markdown("<div class='subtitle'>Your Friendly Math Helper!</div>")
|
| 97 |
+
|
| 98 |
+
# Input/output
|
| 99 |
+
with gr.Row():
|
| 100 |
+
problem_input = gr.Textbox(
|
| 101 |
+
label="✏️ Enter a math problem",
|
| 102 |
+
placeholder="Example: 12 / (2 + 4)",
|
| 103 |
+
lines=1
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
explanation_output = gr.Textbox(
|
| 108 |
+
label="Step-by-step Explanation",
|
| 109 |
+
lines=6
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
points_output = gr.Textbox(
|
| 114 |
+
label="Progress",
|
| 115 |
+
lines=1
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
solve_btn = gr.Button("🚀 Solve with Math Buddy")
|
| 119 |
+
|
| 120 |
+
solve_btn.click(
|
| 121 |
+
solve_math,
|
| 122 |
+
inputs=problem_input,
|
| 123 |
+
outputs=[explanation_output, points_output]
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# ----------------------------
|
| 127 |
+
# Run app
|
| 128 |
+
# ----------------------------
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|