Makima57 commited on
Commit
44001f9
1 Parent(s): 3d60af0

Upload codeexecutor.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. codeexecutor.py +15 -6
codeexecutor.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import re
3
  import subprocess
@@ -24,7 +25,8 @@ class PythonREPL:
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:
@@ -42,16 +44,22 @@ class PythonREPL:
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")
@@ -112,4 +120,5 @@ def get_majority_vote(answers):
112
  return 0
113
  c = Counter(answers)
114
  value, _ = c.most_common()[0]
115
- return value
 
 
1
+
2
  import os
3
  import re
4
  import subprocess
 
25
  return True, result.stdout.strip()
26
  else:
27
  error_msg = result.stderr.strip()
28
+ msgs = error_msg.split("
29
+ ")
30
  new_msgs = []
31
  want_next = False
32
  for m in msgs:
 
44
  elif want_next:
45
  new_msgs.append(m)
46
  want_next = False
47
+ return False, "
48
+ ".join(new_msgs).strip()
49
 
50
  def __call__(self, query):
51
+ query = "import math
52
+ import numpy as np
53
+ import sympy as sp
54
+ " + query
55
+ query = query.strip().split("
56
+ ")
57
  if "print(" not in query[-1]:
58
  if "#" in query[-1]:
59
  query[-1] = query[-1].split("#")[0]
60
  query[-1] = "print(" + query[-1] + ")"
61
+ query = "
62
+ ".join(query)
63
 
64
  with tempfile.TemporaryDirectory() as temp_dir:
65
  temp_file_path = os.path.join(temp_dir, "tmp.py")
 
120
  return 0
121
  c = Counter(answers)
122
  value, _ = c.most_common()[0]
123
+ return value
124
+