Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -27,16 +27,24 @@ templates = {
|
|
27 |
"easy": ["Differentiate the function: f(x) = {a}x^2 + {b}x + {c}", "Find the derivative of: f(x) = {a}x^3 - {b}x + {c}"],
|
28 |
"medium": ["Integrate the function: f(x) = {a}x^2 + {b}x + {c}", "Find the integral of: f(x) = {a}x^3 - {b}x + {c}"],
|
29 |
"hard": ["Solve the differential equation: {a}dy/dx + {b}y = {c}", "Find the solution to the differential equation: {a}d^2y/dx^2 - {b}dy/dx + {c}y = 0"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
}
|
31 |
-
# Add more areas and difficulties as needed
|
32 |
}
|
33 |
|
34 |
-
def generate_synthetic_math_problems(num_problems):
|
35 |
problems = []
|
36 |
-
|
37 |
for _ in range(num_problems):
|
38 |
-
# Randomly choose an area of mathematics
|
39 |
-
area = random.choice(
|
40 |
|
41 |
# Randomly choose a difficulty level
|
42 |
difficulty = random.choice(list(templates[area].keys()))
|
@@ -79,8 +87,8 @@ def solve_problem(problem, max_length):
|
|
79 |
return answer
|
80 |
|
81 |
@spaces.GPU(duration=120)
|
82 |
-
def generate_and_solve_problems(num_problems, max_length):
|
83 |
-
problems = generate_synthetic_math_problems(num_problems)
|
84 |
solved_problems = []
|
85 |
|
86 |
for problem in problems:
|
@@ -92,9 +100,22 @@ def generate_and_solve_problems(num_problems, max_length):
|
|
92 |
|
93 |
return solved_problems
|
94 |
|
95 |
-
def gradio_interface(num_problems, max_length):
|
96 |
print(f"Generating and solving {num_problems} problems with max length {max_length}...")
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
return json.dumps(solved_problems, indent=4)
|
99 |
|
100 |
# Create a Gradio interface
|
@@ -102,7 +123,11 @@ iface = gr.Interface(
|
|
102 |
fn=gradio_interface,
|
103 |
inputs=[
|
104 |
gr.Number(label="Number of Problems", value=10, precision=0),
|
105 |
-
gr.Slider(label="Max Output Length", minimum=10, maximum=200, value=50)
|
|
|
|
|
|
|
|
|
106 |
],
|
107 |
outputs=gr.Textbox(label="Generated and Solved Problems"),
|
108 |
title="Synthetic Math Problem Generator and Solver",
|
|
|
27 |
"easy": ["Differentiate the function: f(x) = {a}x^2 + {b}x + {c}", "Find the derivative of: f(x) = {a}x^3 - {b}x + {c}"],
|
28 |
"medium": ["Integrate the function: f(x) = {a}x^2 + {b}x + {c}", "Find the integral of: f(x) = {a}x^3 - {b}x + {c}"],
|
29 |
"hard": ["Solve the differential equation: {a}dy/dx + {b}y = {c}", "Find the solution to the differential equation: {a}d^2y/dx^2 - {b}dy/dx + {c}y = 0"]
|
30 |
+
},
|
31 |
+
"geometry": {
|
32 |
+
"easy": ["Find the area of a rectangle with length {a} and width {b}", "Calculate the perimeter of a rectangle with length {a} and width {b}"],
|
33 |
+
"medium": ["Find the area of a triangle with base {a} and height {b}", "Calculate the circumference of a circle with radius {a}"],
|
34 |
+
"hard": ["Find the volume of a cylinder with radius {a} and height {b}", "Calculate the surface area of a sphere with radius {a}"]
|
35 |
+
},
|
36 |
+
"trigonometry": {
|
37 |
+
"easy": ["Find sin({a})", "Find cos({a})"],
|
38 |
+
"medium": ["Calculate tan({a})", "Find the value of sin({a}) + cos({a})"],
|
39 |
+
"hard": ["Solve for θ in the equation sin(θ) = {a}", "Find the angle θ for which tan(θ) = {a}"]
|
40 |
}
|
|
|
41 |
}
|
42 |
|
43 |
+
def generate_synthetic_math_problems(num_problems, selected_templates):
|
44 |
problems = []
|
|
|
45 |
for _ in range(num_problems):
|
46 |
+
# Randomly choose an area of mathematics from the selected templates
|
47 |
+
area = random.choice(selected_templates)
|
48 |
|
49 |
# Randomly choose a difficulty level
|
50 |
difficulty = random.choice(list(templates[area].keys()))
|
|
|
87 |
return answer
|
88 |
|
89 |
@spaces.GPU(duration=120)
|
90 |
+
def generate_and_solve_problems(num_problems, max_length, selected_templates):
|
91 |
+
problems = generate_synthetic_math_problems(num_problems, selected_templates)
|
92 |
solved_problems = []
|
93 |
|
94 |
for problem in problems:
|
|
|
100 |
|
101 |
return solved_problems
|
102 |
|
103 |
+
def gradio_interface(num_problems, max_length, algebra, calculus, geometry, trigonometry):
|
104 |
print(f"Generating and solving {num_problems} problems with max length {max_length}...")
|
105 |
+
selected_templates = []
|
106 |
+
if algebra:
|
107 |
+
selected_templates.append("algebra")
|
108 |
+
if calculus:
|
109 |
+
selected_templates.append("calculus")
|
110 |
+
if geometry:
|
111 |
+
selected_templates.append("geometry")
|
112 |
+
if trigonometry:
|
113 |
+
selected_templates.append("trigonometry")
|
114 |
+
|
115 |
+
if not selected_templates:
|
116 |
+
return "Please select at least one math area."
|
117 |
+
|
118 |
+
solved_problems = generate_and_solve_problems(num_problems, max_length, selected_templates)
|
119 |
return json.dumps(solved_problems, indent=4)
|
120 |
|
121 |
# Create a Gradio interface
|
|
|
123 |
fn=gradio_interface,
|
124 |
inputs=[
|
125 |
gr.Number(label="Number of Problems", value=10, precision=0),
|
126 |
+
gr.Slider(label="Max Output Length", minimum=10, maximum=200, value=50),
|
127 |
+
gr.Checkbox(label="Algebra", value=True),
|
128 |
+
gr.Checkbox(label="Calculus", value=True),
|
129 |
+
gr.Checkbox(label="Geometry", value=True),
|
130 |
+
gr.Checkbox(label="Trigonometry", value=True)
|
131 |
],
|
132 |
outputs=gr.Textbox(label="Generated and Solved Problems"),
|
133 |
title="Synthetic Math Problem Generator and Solver",
|