Spaces:
Sleeping
Sleeping
hh
Browse files- app.py +1 -0
- codeexecutor.py +30 -0
- temp.py +45 -29
app.py
CHANGED
|
@@ -198,6 +198,7 @@ interface = gr.Interface(
|
|
| 198 |
gr.Textbox(label="Question", interactive=False), # Non-editable
|
| 199 |
gr.Textbox(label="Answer", interactive=False), # Non-editable
|
| 200 |
gr.Textbox(label="Solution", interactive=True), # Editable textbox for correct solution
|
|
|
|
| 201 |
],
|
| 202 |
title="π’ Math Question Solver",
|
| 203 |
description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
|
|
|
|
| 198 |
gr.Textbox(label="Question", interactive=False), # Non-editable
|
| 199 |
gr.Textbox(label="Answer", interactive=False), # Non-editable
|
| 200 |
gr.Textbox(label="Solution", interactive=True), # Editable textbox for correct solution
|
| 201 |
+
gr.Image(label="Polynomial Plot")
|
| 202 |
],
|
| 203 |
title="π’ Math Question Solver",
|
| 204 |
description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
|
codeexecutor.py
CHANGED
|
@@ -134,3 +134,33 @@ def type_check(self,expr_str):
|
|
| 134 |
|
| 135 |
# Otherwise, classify as other
|
| 136 |
return "Other"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# Otherwise, classify as other
|
| 136 |
return "Other"
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def draw_polynomial_plot(expression):
|
| 140 |
+
try:
|
| 141 |
+
x = sp.symbols('x')
|
| 142 |
+
poly_expr = sp.sympify(expression) # Convert input to sympy expression
|
| 143 |
+
poly_lambda = sp.lambdify(x, poly_expr, 'numpy')
|
| 144 |
+
|
| 145 |
+
# Create the plot
|
| 146 |
+
x_vals = np.linspace(-10, 10, 400)
|
| 147 |
+
y_vals = poly_lambda(x_vals)
|
| 148 |
+
|
| 149 |
+
plt.figure()
|
| 150 |
+
plt.plot(x_vals, y_vals)
|
| 151 |
+
plt.title('Polynomial Plot')
|
| 152 |
+
plt.xlabel('x')
|
| 153 |
+
plt.ylabel('y')
|
| 154 |
+
plt.grid(True)
|
| 155 |
+
|
| 156 |
+
# Save the plot to a file
|
| 157 |
+
plot_filename = "polynomial_plot.png"
|
| 158 |
+
plt.savefig(plot_filename)
|
| 159 |
+
plt.close()
|
| 160 |
+
|
| 161 |
+
return plot_filename
|
| 162 |
+
except:
|
| 163 |
+
return None
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
|
temp.py
CHANGED
|
@@ -2,19 +2,17 @@ import gradio as gr
|
|
| 2 |
import ctranslate2
|
| 3 |
from transformers import AutoTokenizer
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
-
from codeexecutor import get_majority_vote
|
| 6 |
-
|
| 7 |
-
import re
|
| 8 |
|
|
|
|
|
|
|
| 9 |
# Define the model and tokenizer loading
|
| 10 |
model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
|
| 12 |
model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
|
| 13 |
generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
|
| 14 |
-
iterations =
|
| 15 |
-
executor = PythonREPL()
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
# Function to generate predictions using the model
|
| 20 |
def get_prediction(question):
|
|
@@ -45,7 +43,7 @@ def parse_prediction(prediction):
|
|
| 45 |
if answer is None:
|
| 46 |
# If no "Answer:" found, assume last line is the answer
|
| 47 |
answer = lines[-1].strip()
|
| 48 |
-
steps = lines
|
| 49 |
steps_text = '\n'.join(steps).strip()
|
| 50 |
return answer, steps_text
|
| 51 |
|
|
@@ -57,33 +55,42 @@ def majority_vote_with_steps(question, num_iterations=10):
|
|
| 57 |
|
| 58 |
for _ in range(num_iterations):
|
| 59 |
prediction = get_prediction(question)
|
| 60 |
-
answer,
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Get the majority voted answer
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
# Find the steps corresponding to the majority voted answer
|
| 69 |
for i, ans in enumerate(all_answers):
|
| 70 |
if ans == majority_voted_ans:
|
| 71 |
steps_solution = steps_list[i]
|
|
|
|
| 72 |
break
|
| 73 |
else:
|
|
|
|
| 74 |
steps_solution = "No steps found"
|
| 75 |
|
| 76 |
-
return
|
| 77 |
|
| 78 |
-
# Gradio interface for user input and output
|
| 79 |
def gradio_interface(question, correct_answer):
|
| 80 |
-
final_answer, steps_solution = majority_vote_with_steps(question, iterations)
|
| 81 |
-
return
|
| 82 |
-
"Question": question,
|
| 83 |
-
"Majority-Voted Answer": final_answer,
|
| 84 |
-
"Steps to Solve": steps_solution,
|
| 85 |
-
"Correct Solution": correct_answer
|
| 86 |
-
}
|
| 87 |
|
| 88 |
# Custom CSS for enhanced design (unchanged)
|
| 89 |
custom_css = """
|
|
@@ -174,22 +181,31 @@ custom_css = """
|
|
| 174 |
}
|
| 175 |
"""
|
| 176 |
|
| 177 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
interface = gr.Interface(
|
| 179 |
fn=gradio_interface,
|
| 180 |
inputs=[
|
| 181 |
gr.Textbox(label="π§ Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
|
| 182 |
-
|
| 183 |
],
|
| 184 |
outputs=[
|
| 185 |
-
gr.Textbox(label="
|
| 186 |
-
gr.Textbox(label="
|
| 187 |
-
gr.Textbox(label="
|
|
|
|
| 188 |
],
|
| 189 |
title="π’ Math Question Solver",
|
| 190 |
description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
|
| 191 |
-
css=custom_css # Apply custom CSS
|
|
|
|
|
|
|
| 192 |
)
|
| 193 |
|
| 194 |
-
if
|
| 195 |
interface.launch()
|
|
|
|
| 2 |
import ctranslate2
|
| 3 |
from transformers import AutoTokenizer
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
+
from codeexecutor import get_majority_vote,type_check,postprocess_completion,draw_polynomial_plot
|
| 6 |
+
|
|
|
|
| 7 |
|
| 8 |
+
import re
|
| 9 |
+
import os
|
| 10 |
# Define the model and tokenizer loading
|
| 11 |
model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
|
| 13 |
model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
|
| 14 |
generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
|
| 15 |
+
iterations = 4
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Function to generate predictions using the model
|
| 18 |
def get_prediction(question):
|
|
|
|
| 43 |
if answer is None:
|
| 44 |
# If no "Answer:" found, assume last line is the answer
|
| 45 |
answer = lines[-1].strip()
|
| 46 |
+
steps = lines
|
| 47 |
steps_text = '\n'.join(steps).strip()
|
| 48 |
return answer, steps_text
|
| 49 |
|
|
|
|
| 55 |
|
| 56 |
for _ in range(num_iterations):
|
| 57 |
prediction = get_prediction(question)
|
| 58 |
+
answer,sucess= postprocess_completion(prediction, return_status=True, last_code_block=True)
|
| 59 |
+
if sucess:
|
| 60 |
+
all_predictions.append(prediction)
|
| 61 |
+
all_answers.append(answer)
|
| 62 |
+
steps_list.append(prediction)
|
| 63 |
+
majority_voted_ans = get_majority_vote(all_answers)
|
| 64 |
+
else:
|
| 65 |
+
answer, steps = parse_prediction(prediction)
|
| 66 |
+
all_predictions.append(prediction)
|
| 67 |
+
all_answers.append(answer)
|
| 68 |
+
steps_list.append(steps)
|
| 69 |
+
majority_voted_ans = get_majority_vote(all_answers)
|
| 70 |
+
if type_check(majority_voted_ans)=="Polynomial":
|
| 71 |
+
plotfile=draw_polynomial_plot(majority_voted_ans)
|
| 72 |
+
#draw plot of polynomial
|
| 73 |
+
|
| 74 |
|
| 75 |
# Get the majority voted answer
|
| 76 |
+
|
| 77 |
+
|
| 78 |
|
| 79 |
# Find the steps corresponding to the majority voted answer
|
| 80 |
for i, ans in enumerate(all_answers):
|
| 81 |
if ans == majority_voted_ans:
|
| 82 |
steps_solution = steps_list[i]
|
| 83 |
+
answer=parse_prediction(steps_solution)
|
| 84 |
break
|
| 85 |
else:
|
| 86 |
+
answer=majority_voted_ans
|
| 87 |
steps_solution = "No steps found"
|
| 88 |
|
| 89 |
+
return answer, steps_solution,plotfile
|
| 90 |
|
|
|
|
| 91 |
def gradio_interface(question, correct_answer):
|
| 92 |
+
final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
|
| 93 |
+
return question, final_answer, steps_solution, correct_answer,plotfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Custom CSS for enhanced design (unchanged)
|
| 96 |
custom_css = """
|
|
|
|
| 181 |
}
|
| 182 |
"""
|
| 183 |
|
| 184 |
+
# Define the directory path
|
| 185 |
+
flagging_dir = "./flagged_data"
|
| 186 |
+
|
| 187 |
+
# Create the directory if it doesn't exist
|
| 188 |
+
if not os.path.exists(flagging_dir):
|
| 189 |
+
os.makedirs(flagging_dir)
|
| 190 |
+
|
| 191 |
+
# Gradio app setup with flagging
|
| 192 |
interface = gr.Interface(
|
| 193 |
fn=gradio_interface,
|
| 194 |
inputs=[
|
| 195 |
gr.Textbox(label="π§ Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
|
|
|
|
| 196 |
],
|
| 197 |
outputs=[
|
| 198 |
+
gr.Textbox(label="Question", interactive=False), # Non-editable
|
| 199 |
+
gr.Textbox(label="Answer", interactive=False), # Non-editable
|
| 200 |
+
gr.Textbox(label="Solution", interactive=True), # Editable textbox for correct solution
|
| 201 |
+
gr.Image(label="Polynomial Plot")
|
| 202 |
],
|
| 203 |
title="π’ Math Question Solver",
|
| 204 |
description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
|
| 205 |
+
css=custom_css, # Apply custom CSS
|
| 206 |
+
flagging_dir=flagging_dir, # Directory to save flagged data
|
| 207 |
+
allow_flagging="auto" # Allow users to auto flag data
|
| 208 |
)
|
| 209 |
|
| 210 |
+
if __name__ == "__main__":
|
| 211 |
interface.launch()
|