-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_code.py
45 lines (42 loc) · 1.4 KB
/
execute_code.py
1
2
3
4
5
6
7
8
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
37
38
39
40
41
42
43
44
45
import os
from dotenv import load_dotenv
import modal
def execute_code():
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
app = modal.App.lookup("mimic-modal", create_if_missing=True)
image = (
modal.Image.debian_slim(python_version="3.10")
.apt_install("git")
.run_commands(
"git clone https://github.com/microsoft/TinyTroupe /root/TinyTroupe",
"cd /root/TinyTroupe && pip install .",
"pip install python-dotenv",
f"echo 'OPENAI_API_KEY={api_key}' > /root/TinyTroupe/.env",
#"echo '[Logging]' > /root/TinyTroupe/tests/config.ini",
#"echo 'LOGLEVEL=DEBUG' >> /root/TinyTroupe/tests/config.ini",
)
.add_local_file("output/final_code.py", "/root/TinyTroupe/examples/final_code.py")
)
print("Creating sandbox with TinyTroupe environment")
with modal.enable_output():
sb = modal.Sandbox.create(
image=image,
workdir="/root/TinyTroupe",
app=app,
)
try:
print("Running final_code.py")
process = sb.exec("python", "examples/final_code.py")
output = process.stdout.read()
print(output)
error = process.stderr.read()
print(error)
finally:
sb.terminate()
if error:
return True, error
else:
return False, output
if __name__ == "__main__":
execute_code()