lvwerra HF staff commited on
Commit
7f79b94
1 Parent(s): 84e8b2b

Upload tool

Browse files
Files changed (4) hide show
  1. app.py +4 -0
  2. python_interpreter_tool.py +161 -0
  3. requirements.txt +12 -0
  4. tool_config.json +5 -0
app.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from transformers import launch_gradio_demo
2
+ from python_interpreter_tool import PythonInterpreter
3
+
4
+ launch_gradio_demo(PythonInterpreter)
python_interpreter_tool.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import sys
3
+ import platform
4
+ import faulthandler
5
+ from contextlib import contextmanager
6
+ import signal
7
+
8
+ from transformers import Tool
9
+
10
+
11
+ class PythonInterpreter(Tool):
12
+ name = "python_interpreter_tool"
13
+ description = "Executes Python code and returns results. Requires HF_ALLOW_CODE_EVAL = '1'"
14
+
15
+ inputs = ["text"]
16
+ outputs = ["text"]
17
+ timeout = 10.0
18
+
19
+ def __call__(self, task: str):
20
+ import os
21
+ import shutil
22
+
23
+ if os.getenv("HF_ALLOW_CODE_EVAL", 0) != "1":
24
+ return "Can't execute code, set code evaluation flag for PythonInterpreter"
25
+
26
+ rmtree = shutil.rmtree
27
+ rmdir = os.rmdir
28
+ chdir = os.chdir
29
+
30
+ reliability_guard()
31
+ try:
32
+ exec_globals = {}
33
+ with capture_stdout() as output:
34
+ with time_limit(self.timeout):
35
+ exec(task, exec_globals)
36
+
37
+ captured_output = output.getvalue().strip()
38
+ except Exception as e:
39
+ captured_output = str(e)
40
+
41
+ # Needed for cleaning up.
42
+ shutil.rmtree = rmtree
43
+ os.rmdir = rmdir
44
+ os.chdir = chdir
45
+
46
+ return captured_output
47
+
48
+
49
+ @contextmanager
50
+ def capture_stdout():
51
+ output_buffer = io.StringIO()
52
+ original_stdout = sys.stdout
53
+ try:
54
+ sys.stdout = output_buffer
55
+ yield output_buffer
56
+ finally:
57
+ sys.stdout = original_stdout
58
+
59
+
60
+ @contextmanager
61
+ def time_limit(seconds):
62
+ def signal_handler(signum, frame):
63
+ raise TimeoutException("Timed out!")
64
+
65
+ signal.setitimer(signal.ITIMER_REAL, seconds)
66
+ signal.signal(signal.SIGALRM, signal_handler)
67
+ try:
68
+ yield
69
+ finally:
70
+ signal.setitimer(signal.ITIMER_REAL, 0)
71
+
72
+
73
+ def reliability_guard(maximum_memory_bytes=None):
74
+ """
75
+ This disables various destructive functions and prevents the generated code
76
+ from interfering with the test (e.g. fork bomb, killing other processes,
77
+ removing filesystem files, etc.)
78
+
79
+ WARNING
80
+ This function is NOT a security sandbox. Untrusted code, including, model-
81
+ generated code, should not be blindly executed outside of one. See the
82
+ Codex paper for more information about OpenAI's code sandbox, and proceed
83
+ with caution.
84
+ """
85
+
86
+ if maximum_memory_bytes is not None:
87
+ import resource
88
+
89
+ resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes))
90
+ resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes))
91
+ if not platform.uname().system == "Darwin":
92
+ resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes))
93
+
94
+ faulthandler.disable()
95
+
96
+ import builtins
97
+
98
+ builtins.exit = None
99
+ builtins.quit = None
100
+
101
+ import os
102
+
103
+ os.environ["OMP_NUM_THREADS"] = "1"
104
+
105
+ os.kill = None
106
+ os.system = None
107
+ os.putenv = None
108
+ os.remove = None
109
+ os.removedirs = None
110
+ os.rmdir = None
111
+ os.fchdir = None
112
+ os.setuid = None
113
+ os.fork = None
114
+ os.forkpty = None
115
+ os.killpg = None
116
+ os.rename = None
117
+ os.renames = None
118
+ os.truncate = None
119
+ os.replace = None
120
+ os.unlink = None
121
+ os.fchmod = None
122
+ os.fchown = None
123
+ os.chmod = None
124
+ os.chown = None
125
+ os.chroot = None
126
+ os.fchdir = None
127
+ os.lchflags = None
128
+ os.lchmod = None
129
+ os.lchown = None
130
+ os.getcwd = None
131
+ os.chdir = None
132
+
133
+ import shutil
134
+
135
+ shutil.rmtree = None
136
+ shutil.move = None
137
+ shutil.chown = None
138
+
139
+ import subprocess
140
+
141
+ subprocess.Popen = None # type: ignore
142
+
143
+ #__builtins__["help"] = None
144
+
145
+ import sys
146
+
147
+ sys.modules["ipdb"] = None
148
+ sys.modules["joblib"] = None
149
+ sys.modules["resource"] = None
150
+ sys.modules["psutil"] = None
151
+ sys.modules["tkinter"] = None
152
+
153
+ class TimeoutException(Exception):
154
+ pass
155
+
156
+ """
157
+ import os
158
+ tool = PythonInterpreter()
159
+ print(tool("import os; os.getcwd()"))
160
+ print(os.getcwd())
161
+ """
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ contextlib
2
+ resource
3
+ signal
4
+ builtins
5
+ sys
6
+ faulthandler
7
+ platform
8
+ subprocess
9
+ os
10
+ io
11
+ shutil
12
+ transformers
tool_config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "description": "Executes Python code and returns results. Requires HF_ALLOW_CODE_EVAL = '1'",
3
+ "name": "python_interpreter_tool",
4
+ "tool_class": "python_interpreter_tool.PythonInterpreter"
5
+ }