Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import json
|
3 |
+
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Ensure the environment has access to a CUDA-capable GPU
|
8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
+
|
10 |
+
# Load model and tokenizer directly to GPU if available
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True, device_map="auto")
|
13 |
+
|
14 |
+
# Define templates for problems
|
15 |
+
templates = {
|
16 |
+
"algebra": {
|
17 |
+
"easy": ["Solve for x: {a}x + {b} = {c}", "Find the value of x: {a}x - {b} = {c}"],
|
18 |
+
"medium": ["Solve for x: {a}x^2 + {b}x + {c} = 0", "Find the roots of: {a}x^2 - {b}x = {c}"],
|
19 |
+
"hard": ["Solve for x: {a}x^3 + {b}x^2 + {c}x + {d} = 0", "Find the value of x in the equation: {a}x^3 - {b}x^2 + {c}x = {d}"]
|
20 |
+
},
|
21 |
+
"calculus": {
|
22 |
+
"easy": ["Differentiate the function: f(x) = {a}x^2 + {b}x + {c}", "Find the derivative of: f(x) = {a}x^3 - {b}x + {c}"],
|
23 |
+
"medium": ["Integrate the function: f(x) = {a}x^2 + {b}x + {c}", "Find the integral of: f(x) = {a}x^3 - {b}x + {c}"],
|
24 |
+
"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"]
|
25 |
+
}
|
26 |
+
# Add more areas and difficulties as needed
|
27 |
+
}
|
28 |
+
|
29 |
+
def generate_synthetic_math_problems(num_problems):
|
30 |
+
problems = []
|
31 |
+
|
32 |
+
for _ in range(num_problems):
|
33 |
+
# Randomly choose an area of mathematics
|
34 |
+
area = random.choice(list(templates.keys()))
|
35 |
+
|
36 |
+
# Randomly choose a difficulty level
|
37 |
+
difficulty = random.choice(list(templates[area].keys()))
|
38 |
+
|
39 |
+
# Randomly choose a template
|
40 |
+
template = random.choice(templates[area][difficulty])
|
41 |
+
|
42 |
+
# Randomly generate parameters
|
43 |
+
a = random.randint(1, 10)
|
44 |
+
b = random.randint(1, 10)
|
45 |
+
c = random.randint(1, 10)
|
46 |
+
d = random.randint(1, 10)
|
47 |
+
|
48 |
+
# Generate the problem using the template and parameters
|
49 |
+
problem = template.format(a=a, b=b, c=c, d=d)
|
50 |
+
problems.append(problem)
|
51 |
+
|
52 |
+
return problems
|
53 |
+
|
54 |
+
def solve_problem(problem):
|
55 |
+
# Encode the problem
|
56 |
+
inputs = tokenizer(problem, return_tensors="pt").to(device)
|
57 |
+
|
58 |
+
# Generate a response from the model
|
59 |
+
outputs = model.generate(inputs["input_ids"], max_length=100)
|
60 |
+
|
61 |
+
# Decode the response
|
62 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
63 |
+
|
64 |
+
# Strip the answer to only the math (assuming answer is preceded by "The answer is ")
|
65 |
+
if "The answer is " in response:
|
66 |
+
answer = response.split("The answer is ")[-1].strip()
|
67 |
+
else:
|
68 |
+
answer = response.strip()
|
69 |
+
|
70 |
+
return answer
|
71 |
+
|
72 |
+
def generate_and_solve_problems(num_problems):
|
73 |
+
problems = generate_synthetic_math_problems(num_problems)
|
74 |
+
solved_problems = []
|
75 |
+
|
76 |
+
for problem in problems:
|
77 |
+
answer = solve_problem(problem)
|
78 |
+
solved_problems.append({
|
79 |
+
"problem": problem,
|
80 |
+
"answer": answer
|
81 |
+
})
|
82 |
+
|
83 |
+
return solved_problems
|
84 |
+
|
85 |
+
def gradio_interface(num_problems):
|
86 |
+
solved_problems = generate_and_solve_problems(num_problems)
|
87 |
+
return json.dumps(solved_problems, indent=4)
|
88 |
+
|
89 |
+
# Create a Gradio interface
|
90 |
+
iface = gr.Interface(
|
91 |
+
fn=gradio_interface,
|
92 |
+
inputs=gr.Number(label="Number of Problems", value=10, precision=0),
|
93 |
+
outputs=gr.Textbox(label="Generated and Solved Problems"),
|
94 |
+
title="Synthetic Math Problem Generator and Solver",
|
95 |
+
description="Generate and solve synthetic math problems using a HuggingFace model."
|
96 |
+
)
|
97 |
+
|
98 |
+
iface.launch()
|