Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,14 @@ 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 |
-
|
|
|
|
|
13 |
|
14 |
# Define templates for problems
|
15 |
templates = {
|
@@ -52,21 +56,24 @@ def generate_synthetic_math_problems(num_problems):
|
|
52 |
return problems
|
53 |
|
54 |
def solve_problem(problem):
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
answer
|
67 |
-
|
68 |
-
|
|
|
|
|
69 |
|
|
|
70 |
return answer
|
71 |
|
72 |
def generate_and_solve_problems(num_problems):
|
@@ -83,6 +90,7 @@ def generate_and_solve_problems(num_problems):
|
|
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 |
|
|
|
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 |
+
print(f"Using device: {device}")
|
10 |
|
11 |
# Load model and tokenizer directly to GPU if available
|
12 |
+
print("Loading tokenizer...")
|
13 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
|
14 |
+
|
15 |
+
print("Loading model...")
|
16 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True, device_map="auto").to(device)
|
17 |
|
18 |
# Define templates for problems
|
19 |
templates = {
|
|
|
56 |
return problems
|
57 |
|
58 |
def solve_problem(problem):
|
59 |
+
print(f"Solving problem: {problem}")
|
60 |
+
with torch.no_grad():
|
61 |
+
# Encode the problem
|
62 |
+
inputs = tokenizer(problem, return_tensors="pt").to(device)
|
63 |
+
|
64 |
+
# Generate a response from the model
|
65 |
+
outputs = model.generate(inputs["input_ids"], max_length=50, num_return_sequences=1, do_sample=True)
|
66 |
+
|
67 |
+
# Decode the response
|
68 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
69 |
+
|
70 |
+
# Strip the answer to only the math (assuming answer is preceded by "The answer is ")
|
71 |
+
if "The answer is " in response:
|
72 |
+
answer = response.split("The answer is ")[-1].strip()
|
73 |
+
else:
|
74 |
+
answer = response.strip()
|
75 |
|
76 |
+
print(f"Problem: {problem}, Answer: {answer}")
|
77 |
return answer
|
78 |
|
79 |
def generate_and_solve_problems(num_problems):
|
|
|
90 |
return solved_problems
|
91 |
|
92 |
def gradio_interface(num_problems):
|
93 |
+
print(f"Generating and solving {num_problems} problems...")
|
94 |
solved_problems = generate_and_solve_problems(num_problems)
|
95 |
return json.dumps(solved_problems, indent=4)
|
96 |
|