Spaces:
Sleeping
Sleeping
added some more code
Browse files- Dockerfile +16 -0
- app.py +9 -3
- codeexecutor.py +116 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
COPY . /app
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
EXPOSE 8080
|
| 15 |
+
|
| 16 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import ctranslate2
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
from huggingface_hub import snapshot_download
|
|
|
|
| 6 |
|
| 7 |
# Define the model and tokenizer loading
|
| 8 |
model_prompt = "Solve the following mathematical problem: "
|
|
@@ -22,20 +23,25 @@ def get_prediction(question):
|
|
| 22 |
# Function to perform majority voting across multiple predictions
|
| 23 |
def majority_vote(question, num_iterations=10):
|
| 24 |
all_predictions = []
|
|
|
|
| 25 |
for _ in range(num_iterations):
|
| 26 |
prediction = get_prediction(question)
|
|
|
|
| 27 |
all_predictions.append(prediction)
|
|
|
|
| 28 |
majority_voted_pred = max(set(all_predictions), key=all_predictions.count)
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
# Gradio interface for user input and output
|
| 32 |
def gradio_interface(question, correct_answer):
|
| 33 |
-
final_prediction, all_predictions = majority_vote(question, num_iterations=10)
|
| 34 |
return {
|
| 35 |
"Question": question,
|
| 36 |
"Generated Answers (10 iterations)": all_predictions,
|
| 37 |
"Majority-Voted Prediction": final_prediction,
|
| 38 |
-
"Correct
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
# Gradio app setup
|
|
|
|
| 3 |
import ctranslate2
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
from huggingface_hub import snapshot_download
|
| 6 |
+
from codeexecutor import postprocess_completion,get_majority_vote
|
| 7 |
|
| 8 |
# Define the model and tokenizer loading
|
| 9 |
model_prompt = "Solve the following mathematical problem: "
|
|
|
|
| 23 |
# Function to perform majority voting across multiple predictions
|
| 24 |
def majority_vote(question, num_iterations=10):
|
| 25 |
all_predictions = []
|
| 26 |
+
all_answer=[]
|
| 27 |
for _ in range(num_iterations):
|
| 28 |
prediction = get_prediction(question)
|
| 29 |
+
answer=postprocess_completion(predicted_answer,True,True)
|
| 30 |
all_predictions.append(prediction)
|
| 31 |
+
all_answer.append(answer)
|
| 32 |
majority_voted_pred = max(set(all_predictions), key=all_predictions.count)
|
| 33 |
+
majority_voted_ans=get_majority_vote(all_answer)
|
| 34 |
+
return majority_voted_pred, all_predictions,majority_voted_ans
|
| 35 |
|
| 36 |
# Gradio interface for user input and output
|
| 37 |
def gradio_interface(question, correct_answer):
|
| 38 |
+
final_prediction, all_predictions,final_answer = majority_vote(question, num_iterations=10)
|
| 39 |
return {
|
| 40 |
"Question": question,
|
| 41 |
"Generated Answers (10 iterations)": all_predictions,
|
| 42 |
"Majority-Voted Prediction": final_prediction,
|
| 43 |
+
"Correct solution": correct_answer
|
| 44 |
+
"Majority answer": final_answer
|
| 45 |
}
|
| 46 |
|
| 47 |
# Gradio app setup
|
codeexecutor.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import signal
|
| 4 |
+
import subprocess
|
| 5 |
+
import tempfile
|
| 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 |
+
@contextmanager
|
| 16 |
+
def time_limit(self, seconds):
|
| 17 |
+
def signal_handler(*_):
|
| 18 |
+
raise TimeoutError(f"Timed out after {seconds} seconds.")
|
| 19 |
+
|
| 20 |
+
signal.signal(signal.SIGALRM, signal_handler)
|
| 21 |
+
signal.alarm(seconds)
|
| 22 |
+
try:
|
| 23 |
+
yield
|
| 24 |
+
finally:
|
| 25 |
+
signal.alarm(0)
|
| 26 |
+
|
| 27 |
+
def __call__(self, query):
|
| 28 |
+
query = "import math\nimport numpy as np\nimport sympy as sp\n" + query
|
| 29 |
+
query = query.strip().split("\n")
|
| 30 |
+
if "print(" not in query[-1]:
|
| 31 |
+
if "#" in query[-1]:
|
| 32 |
+
query[-1] = query[-1].split("#")[0]
|
| 33 |
+
query[-1] = "print(" + query[-1] + ")"
|
| 34 |
+
query = "\n".join(query)
|
| 35 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 36 |
+
temp_file_path = os.path.join(temp_dir, "tmp.py")
|
| 37 |
+
with open(temp_file_path, "w", encoding="utf-8") as f:
|
| 38 |
+
f.write(query)
|
| 39 |
+
with self.time_limit(self.timeout):
|
| 40 |
+
result = subprocess.run(
|
| 41 |
+
["python3", temp_file_path],
|
| 42 |
+
capture_output=True,
|
| 43 |
+
check=False,
|
| 44 |
+
text=True,
|
| 45 |
+
timeout=self.timeout,
|
| 46 |
+
)
|
| 47 |
+
if result.returncode == 0:
|
| 48 |
+
output = result.stdout
|
| 49 |
+
return True, output.strip()
|
| 50 |
+
error_msg = result.stderr.strip()
|
| 51 |
+
msgs = error_msg.split("\n")
|
| 52 |
+
new_msgs = []
|
| 53 |
+
want_next = False
|
| 54 |
+
for m in msgs:
|
| 55 |
+
if "Traceback" in m:
|
| 56 |
+
new_msgs.append(m)
|
| 57 |
+
elif m == msgs[-1]:
|
| 58 |
+
new_msgs.append(m)
|
| 59 |
+
elif temp_file_path in m:
|
| 60 |
+
st = m.index('"/') + 1 if '"/' in m else 0
|
| 61 |
+
ed = m.index(temp_file_path) + 1 if temp_file_path in m else None
|
| 62 |
+
clr = m[st:ed] if not ed else m[st:]
|
| 63 |
+
m = m.replace(clr, "")
|
| 64 |
+
new_msgs.append(m)
|
| 65 |
+
want_next = True
|
| 66 |
+
elif want_next:
|
| 67 |
+
new_msgs.append(m)
|
| 68 |
+
want_next = False
|
| 69 |
+
error_msg = "\n".join(new_msgs)
|
| 70 |
+
return False, error_msg.strip()
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def execute_completion(executor, completion, return_status, last_code_block):
|
| 74 |
+
executions = re.findall(r"```python(.*?)```", completion, re.DOTALL)
|
| 75 |
+
if len(executions) == 0:
|
| 76 |
+
return completion, False if return_status else completion
|
| 77 |
+
if last_code_block:
|
| 78 |
+
executions = [executions[-1]]
|
| 79 |
+
outputs = []
|
| 80 |
+
successes = []
|
| 81 |
+
for code in executions:
|
| 82 |
+
success = False
|
| 83 |
+
for lib in ("subprocess", "venv"):
|
| 84 |
+
if lib in code:
|
| 85 |
+
output = f"{lib} is not allowed"
|
| 86 |
+
outputs.append(output)
|
| 87 |
+
successes.append(success)
|
| 88 |
+
continue
|
| 89 |
+
try:
|
| 90 |
+
success, output = executor(code)
|
| 91 |
+
except TimeoutError as e:
|
| 92 |
+
print("Code timed out")
|
| 93 |
+
output = e
|
| 94 |
+
if not success and not return_status:
|
| 95 |
+
output = ""
|
| 96 |
+
outputs.append(output)
|
| 97 |
+
successes.append(success)
|
| 98 |
+
output = str(outputs[-1]).strip()
|
| 99 |
+
success = successes[-1]
|
| 100 |
+
if return_status:
|
| 101 |
+
return output, success
|
| 102 |
+
return output
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def postprocess_completion(text, return_status, last_code_block):
|
| 106 |
+
executor = PythonREPL()
|
| 107 |
+
result = execute_completion(executor, text, return_status=return_status, last_code_block=last_code_block)
|
| 108 |
+
del executor
|
| 109 |
+
return result
|
| 110 |
+
|
| 111 |
+
def get_majority_vote(answers):
|
| 112 |
+
if not len(answers):
|
| 113 |
+
return 0
|
| 114 |
+
c = Counter(answers)
|
| 115 |
+
value, _ = c.most_common()[0]
|
| 116 |
+
return value
|