Spaces:
Sleeping
Sleeping
Update codeexecutor.py
Browse filesonly majority and steps
- codeexecutor.py +153 -108
codeexecutor.py
CHANGED
@@ -1,115 +1,160 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
from collections import Counter
|
7 |
-
from contextlib import contextmanager
|
8 |
-
from dataclasses import dataclass
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
)
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
st = m.index('"/') + 1 if '"/' in m else 0
|
37 |
-
ed = m.index(temp_file_path) + 1 if temp_file_path in m else None
|
38 |
-
clr = m[st:ed] if not ed else m[st:]
|
39 |
-
m = m.replace(clr, "")
|
40 |
-
new_msgs.append(m)
|
41 |
-
want_next = True
|
42 |
-
elif want_next:
|
43 |
-
new_msgs.append(m)
|
44 |
-
want_next = False
|
45 |
-
return False, "\n".join(new_msgs).strip()
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
57 |
-
temp_file_path = os.path.join(temp_dir, "tmp.py")
|
58 |
-
with open(temp_file_path, "w", encoding="utf-8") as f:
|
59 |
-
f.write(query)
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
if len(executions) == 0:
|
74 |
-
return completion, False if return_status else completion
|
75 |
-
if last_code_block:
|
76 |
-
executions = [executions[-1]]
|
77 |
-
outputs = []
|
78 |
-
successes = []
|
79 |
-
for code in executions:
|
80 |
-
success = False
|
81 |
-
for lib in ("subprocess", "venv"):
|
82 |
-
if lib in code:
|
83 |
-
output = f"{lib} is not allowed"
|
84 |
-
outputs.append(output)
|
85 |
-
successes.append(success)
|
86 |
-
continue
|
87 |
-
try:
|
88 |
-
success, output = executor(code)
|
89 |
-
except TimeoutError as e:
|
90 |
-
print("Code timed out")
|
91 |
-
output = e
|
92 |
-
if not success and not return_status:
|
93 |
-
output = ""
|
94 |
-
outputs.append(output)
|
95 |
-
successes.append(success)
|
96 |
-
output = str(outputs[-1]).strip()
|
97 |
-
success = successes[-1]
|
98 |
-
if return_status:
|
99 |
-
return output, success
|
100 |
-
return output
|
101 |
-
|
102 |
-
|
103 |
-
def postprocess_completion(text, return_status, last_code_block):
|
104 |
-
executor = PythonREPL()
|
105 |
-
result = execute_completion(executor, text, return_status=return_status, last_code_block=last_code_block)
|
106 |
-
del executor
|
107 |
-
return result
|
108 |
-
|
109 |
-
|
110 |
-
def get_majority_vote(answers):
|
111 |
-
if not len(answers):
|
112 |
-
return 0
|
113 |
-
c = Counter(answers)
|
114 |
-
value, _ = c.most_common()[0]
|
115 |
-
return value
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import ctranslate2
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
from huggingface_hub import snapshot_download
|
5 |
+
from codeexecutor import postprocess_completion, get_majority_vote
|
|
|
|
|
|
|
6 |
|
7 |
+
# Define the model and tokenizer loading
|
8 |
+
model_prompt = "Solve the following mathematical problem: "
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
|
10 |
+
model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
|
11 |
+
generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
|
12 |
+
iterations = 10
|
13 |
|
14 |
+
# Function to generate predictions using the model
|
15 |
+
def get_prediction(question):
|
16 |
+
input_text = model_prompt + question
|
17 |
+
input_tokens = tokenizer.tokenize(input_text)
|
18 |
+
results = generator.generate_batch([input_tokens])
|
19 |
+
output_tokens = results[0].sequences[0]
|
20 |
+
predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
|
21 |
+
return predicted_answer
|
22 |
|
23 |
+
# Function to perform majority voting and solve the problem with steps
|
24 |
+
def majority_vote_with_steps(question, num_iterations=10):
|
25 |
+
all_predictions = []
|
26 |
+
all_answer = []
|
27 |
+
steps_to_solve = []
|
28 |
+
|
29 |
+
for _ in range(num_iterations):
|
30 |
+
prediction = get_prediction(question)
|
31 |
+
# Process prediction to get steps and answer
|
32 |
+
answer, success = postprocess_completion(prediction, True, True)
|
33 |
+
all_predictions.append(prediction)
|
34 |
+
all_answer.append(answer)
|
35 |
+
if success:
|
36 |
+
steps_to_solve.append(answer) # Add the steps if code executes successfully
|
37 |
+
|
38 |
+
majority_voted_ans = get_majority_vote(all_answer)
|
39 |
+
|
40 |
+
# If steps to solve exist, return them, else fallback to "No steps found"
|
41 |
+
steps_solution = steps_to_solve[0] if steps_to_solve else "No steps found"
|
42 |
+
|
43 |
+
return majority_voted_ans, steps_solution
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Gradio interface for user input and output
|
46 |
+
def gradio_interface(question, correct_answer):
|
47 |
+
final_answer, steps_solution = majority_vote_with_steps(question, iterations)
|
48 |
+
return {
|
49 |
+
"Question": question,
|
50 |
+
"Majority-Voted Answer": final_answer,
|
51 |
+
"Steps to Solve": steps_solution,
|
52 |
+
"Correct Solution": correct_answer
|
53 |
+
}
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
# Custom CSS for enhanced design
|
56 |
+
custom_css = """
|
57 |
+
body {
|
58 |
+
background-color: #fafafa;
|
59 |
+
font-family: 'Open Sans', sans-serif;
|
60 |
+
}
|
61 |
+
.gradio-container {
|
62 |
+
background-color: #ffffff;
|
63 |
+
border: 3px solid #007acc;
|
64 |
+
border-radius: 15px;
|
65 |
+
padding: 20px;
|
66 |
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
|
67 |
+
max-width: 800px;
|
68 |
+
margin: 50px auto;
|
69 |
+
}
|
70 |
+
h1 {
|
71 |
+
font-family: 'Poppins', sans-serif;
|
72 |
+
color: #007acc;
|
73 |
+
font-weight: bold;
|
74 |
+
font-size: 32px;
|
75 |
+
text-align: center;
|
76 |
+
margin-bottom: 20px;
|
77 |
+
}
|
78 |
+
p {
|
79 |
+
font-family: 'Roboto', sans-serif;
|
80 |
+
font-size: 18px;
|
81 |
+
color: #333;
|
82 |
+
text-align: center;
|
83 |
+
margin-bottom: 15px;
|
84 |
+
}
|
85 |
+
input, textarea {
|
86 |
+
font-family: 'Montserrat', sans-serif;
|
87 |
+
font-size: 16px;
|
88 |
+
padding: 10px;
|
89 |
+
border: 2px solid #007acc;
|
90 |
+
border-radius: 10px;
|
91 |
+
background-color: #f1f8ff;
|
92 |
+
margin-bottom: 15px;
|
93 |
+
}
|
94 |
+
#math_question, #correct_answer {
|
95 |
+
font-size: 20px;
|
96 |
+
font-family: 'Poppins', sans-serif;
|
97 |
+
font-weight: 500px; /* Apply bold */
|
98 |
+
color: #007acc;
|
99 |
+
margin-bottom: 5px;
|
100 |
+
display: inline-block;
|
101 |
+
}
|
102 |
+
|
103 |
+
textarea {
|
104 |
+
min-height: 150px;
|
105 |
+
}
|
106 |
+
.gr-button-primary {
|
107 |
+
background-color: #007acc !important;
|
108 |
+
color: white !important;
|
109 |
+
border-radius: 10px !important;
|
110 |
+
font-size: 18px !important;
|
111 |
+
font-weight: bold !important;
|
112 |
+
padding: 10px 20px !important;
|
113 |
+
font-family: 'Montserrat', sans-serif !important;
|
114 |
+
transition: background-color 0.3s ease !important;
|
115 |
+
}
|
116 |
+
.gr-button-primary:hover {
|
117 |
+
background-color: #005f99 !important;
|
118 |
+
}
|
119 |
+
.gr-button-secondary {
|
120 |
+
background-color: #f44336 !important;
|
121 |
+
color: white !important;
|
122 |
+
border-radius: 10px !important;
|
123 |
+
font-size: 18px !important;
|
124 |
+
font-weight: bold !important;
|
125 |
+
padding: 10px 20px !important;
|
126 |
+
font-family: 'Montserrat', sans-serif !important;
|
127 |
+
transition: background-color 0.3s ease !important;
|
128 |
+
}
|
129 |
+
.gr-button-secondary:hover {
|
130 |
+
background-color: #c62828 !important;
|
131 |
+
}
|
132 |
+
.gr-output {
|
133 |
+
background-color: #e0f7fa;
|
134 |
+
border: 2px solid #007acc;
|
135 |
+
border-radius: 10px;
|
136 |
+
padding: 15px;
|
137 |
+
font-size: 16px;
|
138 |
+
font-family: 'Roboto', sans-serif;
|
139 |
+
font-weight: bold;
|
140 |
+
color: #00796b;
|
141 |
+
}
|
142 |
+
"""
|
143 |
|
144 |
+
# Gradio app setup
|
145 |
+
interface = gr.Interface(
|
146 |
+
fn=gradio_interface,
|
147 |
+
inputs=[
|
148 |
+
gr.Textbox(label="π§ Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
|
149 |
+
gr.Textbox(label="β
Correct Answer", placeholder="Enter the correct answer here...", elem_id="correct_answer"),
|
150 |
+
],
|
151 |
+
outputs=[
|
152 |
+
gr.JSON(label="π Results"), # Display the results in a JSON format
|
153 |
+
],
|
154 |
+
title="π’ Math Question Solver",
|
155 |
+
description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
|
156 |
+
css=custom_css # Apply custom CSS
|
157 |
+
)
|
158 |
|
159 |
+
if __name__ == "__main__":
|
160 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|