Spaces:
Running
Running
terryyz
commited on
Commit
·
844732b
1
Parent(s):
ba99c06
fix
Browse files- sandbox/code_runner.py +8 -3
- sandbox/sandbox_manager.py +9 -2
sandbox/code_runner.py
CHANGED
@@ -10,7 +10,6 @@ import time
|
|
10 |
import gradio as gr
|
11 |
import re
|
12 |
import base64
|
13 |
-
from e2b_code_interpreter import Sandbox as CodeSandbox
|
14 |
from gradio_sandboxcomponent import SandboxComponent
|
15 |
|
16 |
from sandbox.sandbox_state import ChatbotSandboxState
|
@@ -377,7 +376,7 @@ def run_code_interpreter(code: str, code_language: str | None, install_command:
|
|
377 |
Args:
|
378 |
code (str): The code to be executed.
|
379 |
"""
|
380 |
-
sandbox =
|
381 |
|
382 |
stderrs = []
|
383 |
|
@@ -393,8 +392,14 @@ def run_code_interpreter(code: str, code_language: str | None, install_command:
|
|
393 |
if not is_success:
|
394 |
print(f"Install command failed: {stderr}")
|
395 |
|
|
|
396 |
execution = sandbox.run_code(
|
397 |
-
code=
|
|
|
|
|
|
|
|
|
|
|
398 |
language=code_language
|
399 |
)
|
400 |
|
|
|
10 |
import gradio as gr
|
11 |
import re
|
12 |
import base64
|
|
|
13 |
from gradio_sandboxcomponent import SandboxComponent
|
14 |
|
15 |
from sandbox.sandbox_state import ChatbotSandboxState
|
|
|
376 |
Args:
|
377 |
code (str): The code to be executed.
|
378 |
"""
|
379 |
+
sandbox = create_sandbox(is_code_sandbox=True)
|
380 |
|
381 |
stderrs = []
|
382 |
|
|
|
392 |
if not is_success:
|
393 |
print(f"Install command failed: {stderr}")
|
394 |
|
395 |
+
# Add virtual environment to Python path and run user code
|
396 |
execution = sandbox.run_code(
|
397 |
+
code=f"""
|
398 |
+
import sys, glob
|
399 |
+
for path in glob.glob("/home/user/.venv/lib/python*/site-packages"):
|
400 |
+
if path not in sys.path: sys.path.insert(0, path)
|
401 |
+
|
402 |
+
{code}""",
|
403 |
language=code_language
|
404 |
)
|
405 |
|
sandbox/sandbox_manager.py
CHANGED
@@ -5,6 +5,7 @@ Facades for interacting with the e2b sandbox.
|
|
5 |
import json
|
6 |
from typing import Literal
|
7 |
from e2b import Sandbox
|
|
|
8 |
from e2b.sandbox.commands.command_handle import CommandExitException
|
9 |
from e2b.exceptions import TimeoutException
|
10 |
import time
|
@@ -15,14 +16,20 @@ import queue
|
|
15 |
from .constants import SANDBOX_TEMPLATE_ID, SANDBOX_NGINX_PORT, SANDBOX_RETRY_COUNT, SANDBOX_TIMEOUT_SECONDS, INSTALLED_PYPI_PACKAGES
|
16 |
|
17 |
|
18 |
-
def create_sandbox(template: str = SANDBOX_TEMPLATE_ID) -> Sandbox:
|
19 |
'''
|
20 |
Create a new sandbox.
|
21 |
Will retry if the sandbox creation fails.
|
22 |
'''
|
23 |
for attempt in range(1, SANDBOX_RETRY_COUNT + 1):
|
24 |
try:
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
domain="e2b-foxtrot.dev",
|
27 |
template=template,
|
28 |
timeout=SANDBOX_TIMEOUT_SECONDS,
|
|
|
5 |
import json
|
6 |
from typing import Literal
|
7 |
from e2b import Sandbox
|
8 |
+
from e2b_code_interpreter import Sandbox as CodeSandbox
|
9 |
from e2b.sandbox.commands.command_handle import CommandExitException
|
10 |
from e2b.exceptions import TimeoutException
|
11 |
import time
|
|
|
16 |
from .constants import SANDBOX_TEMPLATE_ID, SANDBOX_NGINX_PORT, SANDBOX_RETRY_COUNT, SANDBOX_TIMEOUT_SECONDS, INSTALLED_PYPI_PACKAGES
|
17 |
|
18 |
|
19 |
+
def create_sandbox(template: str = SANDBOX_TEMPLATE_ID, is_code_sandbox: bool = False) -> Sandbox:
|
20 |
'''
|
21 |
Create a new sandbox.
|
22 |
Will retry if the sandbox creation fails.
|
23 |
'''
|
24 |
for attempt in range(1, SANDBOX_RETRY_COUNT + 1):
|
25 |
try:
|
26 |
+
if is_code_sandbox:
|
27 |
+
return CodeSandbox.create(
|
28 |
+
domain="e2b-foxtrot.dev",
|
29 |
+
template=template,
|
30 |
+
timeout=SANDBOX_TIMEOUT_SECONDS,
|
31 |
+
)
|
32 |
+
return Sandbox.create(
|
33 |
domain="e2b-foxtrot.dev",
|
34 |
template=template,
|
35 |
timeout=SANDBOX_TIMEOUT_SECONDS,
|