Spaces:
Sleeping
Sleeping
Update codeexecutor.py
Browse files- codeexecutor.py +108 -153
codeexecutor.py
CHANGED
@@ -1,160 +1,115 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
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 |
-
|
15 |
-
def
|
16 |
-
|
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 |
-
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
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 |
-
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import subprocess
|
4 |
+
import tempfile
|
5 |
+
import multiprocessing
|
6 |
+
from collections import Counter
|
7 |
+
from contextlib import contextmanager
|
8 |
+
from dataclasses import dataclass
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
class PythonREPL:
|
12 |
+
def __init__(self, timeout=5):
|
13 |
+
self.timeout = timeout
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
@staticmethod
|
16 |
+
def _run_code(temp_file_path):
|
17 |
+
result = subprocess.run(
|
18 |
+
["python3", temp_file_path],
|
19 |
+
capture_output=True,
|
20 |
+
check=False,
|
21 |
+
text=True
|
22 |
+
)
|
23 |
+
if result.returncode == 0:
|
24 |
+
return True, result.stdout.strip()
|
25 |
+
else:
|
26 |
+
error_msg = result.stderr.strip()
|
27 |
+
msgs = error_msg.split("\n")
|
28 |
+
new_msgs = []
|
29 |
+
want_next = False
|
30 |
+
for m in msgs:
|
31 |
+
if "Traceback" in m:
|
32 |
+
new_msgs.append(m)
|
33 |
+
elif m == msgs[-1]:
|
34 |
+
new_msgs.append(m)
|
35 |
+
elif temp_file_path in m:
|
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 |
+
def __call__(self, query):
|
48 |
+
query = "import math\nimport numpy as np\nimport sympy as sp\n" + query
|
49 |
+
query = query.strip().split("\n")
|
50 |
+
if "print(" not in query[-1]:
|
51 |
+
if "#" in query[-1]:
|
52 |
+
query[-1] = query[-1].split("#")[0]
|
53 |
+
query[-1] = "print(" + query[-1] + ")"
|
54 |
+
query = "\n".join(query)
|
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 |
+
with multiprocessing.Pool(1) as pool:
|
62 |
+
result = pool.apply_async(self._run_code, (temp_file_path,))
|
63 |
+
try:
|
64 |
+
success, output = result.get(self.timeout)
|
65 |
+
except multiprocessing.TimeoutError:
|
66 |
+
pool.terminate()
|
67 |
+
return False, f"Timed out after {self.timeout} seconds."
|
68 |
+
return success, output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
def execute_completion(executor, completion, return_status, last_code_block):
|
72 |
+
executions = re.findall(r"```python(.*?)```", completion, re.DOTALL)
|
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
|