|
import random |
|
import json |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
import gradio as gr |
|
import spaces |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
print(f"Using device: {device}") |
|
|
|
|
|
print("Loading tokenizer...") |
|
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True) |
|
|
|
print("Loading model...") |
|
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True, device_map="auto").to(device) |
|
|
|
|
|
templates = { |
|
"algebra": { |
|
"easy": ["Solve for x: {a}x + {b} = {c}", "Find the value of x: {a}x - {b} = {c}"], |
|
"medium": ["Solve for x: {a}x^2 + {b}x + {c} = 0", "Find the roots of: {a}x^2 - {b}x = {c}"], |
|
"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}"] |
|
}, |
|
"calculus": { |
|
"easy": ["Differentiate the function: f(x) = {a}x^2 + {b}x + {c}", "Find the derivative of: f(x) = {a}x^3 - {b}x + {c}"], |
|
"medium": ["Integrate the function: f(x) = {a}x^2 + {b}x + {c}", "Find the integral of: f(x) = {a}x^3 - {b}x + {c}"], |
|
"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"] |
|
}, |
|
"geometry": { |
|
"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}"], |
|
"medium": ["Find the area of a triangle with base {a} and height {b}", "Calculate the circumference of a circle with radius {a}"], |
|
"hard": ["Find the volume of a cylinder with radius {a} and height {b}", "Calculate the surface area of a sphere with radius {a}"] |
|
}, |
|
"trigonometry": { |
|
"easy": ["Find sin({a})", "Find cos({a})"], |
|
"medium": ["Calculate tan({a})", "Find the value of sin({a}) + cos({a})"], |
|
"hard": ["Solve for θ in the equation sin(θ) = {a}", "Find the angle θ for which tan(θ) = {a}"] |
|
} |
|
} |
|
|
|
def generate_synthetic_math_problems(num_problems, selected_templates): |
|
problems = [] |
|
for _ in range(num_problems): |
|
|
|
area = random.choice(selected_templates) |
|
|
|
|
|
difficulty = random.choice(list(templates[area].keys())) |
|
|
|
|
|
template = random.choice(templates[area][difficulty]) |
|
|
|
|
|
a = random.randint(1, 10) |
|
b = random.randint(1, 10) |
|
c = random.randint(1, 10) |
|
d = random.randint(1, 10) |
|
|
|
|
|
problem = template.format(a=a, b=b, c=c, d=d) |
|
problems.append(problem) |
|
|
|
return problems |
|
|
|
@spaces.GPU(duration=60) |
|
def solve_problem(problem, max_length): |
|
print(f"Solving problem: {problem}") |
|
with torch.no_grad(): |
|
|
|
inputs = tokenizer(problem, return_tensors="pt").to(device) |
|
|
|
|
|
outputs = model.generate(inputs["input_ids"], max_length=max_length, num_return_sequences=1, do_sample=True) |
|
|
|
|
|
response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
if "The answer is " in response: |
|
answer = response.split("The answer is ")[-1].strip() |
|
else: |
|
answer = response.strip() |
|
|
|
print(f"Problem: {problem}, Answer: {answer}") |
|
return answer |
|
|
|
@spaces.GPU(duration=120) |
|
def generate_and_solve_problems(num_problems, max_length, selected_templates): |
|
problems = generate_synthetic_math_problems(num_problems, selected_templates) |
|
solved_problems = [] |
|
|
|
for problem in problems: |
|
answer = solve_problem(problem, max_length) |
|
solved_problems.append({ |
|
"problem": problem, |
|
"answer": answer |
|
}) |
|
|
|
return solved_problems |
|
|
|
def gradio_interface(num_problems, max_length, algebra, calculus, geometry, trigonometry): |
|
print(f"Generating and solving {num_problems} problems with max length {max_length}...") |
|
selected_templates = [] |
|
if algebra: |
|
selected_templates.append("algebra") |
|
if calculus: |
|
selected_templates.append("calculus") |
|
if geometry: |
|
selected_templates.append("geometry") |
|
if trigonometry: |
|
selected_templates.append("trigonometry") |
|
|
|
if not selected_templates: |
|
return "Please select at least one math area." |
|
|
|
solved_problems = generate_and_solve_problems(num_problems, max_length, selected_templates) |
|
return json.dumps(solved_problems, indent=4) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=gradio_interface, |
|
inputs=[ |
|
gr.Number(label="Number of Problems", value=10, precision=0), |
|
gr.Slider(label="Max Output Length", minimum=10, maximum=200, value=50), |
|
gr.Checkbox(label="Algebra", value=True), |
|
gr.Checkbox(label="Calculus", value=True), |
|
gr.Checkbox(label="Geometry", value=True), |
|
gr.Checkbox(label="Trigonometry", value=True) |
|
], |
|
outputs=gr.Textbox(label="Generated and Solved Problems"), |
|
title="Synthetic Math Problem Generator and Solver", |
|
description="Generate and solve synthetic math problems using a HuggingFace model." |
|
) |
|
|
|
iface.launch() |