Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,18 +14,24 @@ import gradio as gr
|
|
| 14 |
# -------------------------
|
| 15 |
sandboxes = {}
|
| 16 |
SANDBOX_TTL = 2 * 3600 # 2 hours
|
|
|
|
| 17 |
|
| 18 |
# -------------------------
|
| 19 |
# Sandbox worker function
|
| 20 |
# -------------------------
|
| 21 |
-
def sandbox_worker(code: str, sandbox_id: str):
|
| 22 |
user_dir = f"/tmp/sandbox_{sandbox_id}"
|
| 23 |
os.makedirs(user_dir, exist_ok=True)
|
| 24 |
os.chdir(user_dir)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# Resource limits
|
| 27 |
-
resource.setrlimit(resource.RLIMIT_AS, (
|
| 28 |
-
resource.setrlimit(resource.RLIMIT_CPU, (
|
| 29 |
|
| 30 |
# Save user code
|
| 31 |
code_file = os.path.join(user_dir, "user_code.py")
|
|
@@ -65,16 +71,16 @@ def cleanup_sandboxes():
|
|
| 65 |
if os.path.exists(folder):
|
| 66 |
shutil.rmtree(folder)
|
| 67 |
del sandboxes[sid]
|
| 68 |
-
time.sleep(60)
|
| 69 |
|
| 70 |
threading.Thread(target=cleanup_sandboxes, daemon=True).start()
|
| 71 |
|
| 72 |
# -------------------------
|
| 73 |
# Gradio interface functions
|
| 74 |
# -------------------------
|
| 75 |
-
def launch_sandbox(code):
|
| 76 |
sandbox_id = str(uuid.uuid4())[:8]
|
| 77 |
-
p = multiprocessing.Process(target=sandbox_worker, args=(code, sandbox_id))
|
| 78 |
p.start()
|
| 79 |
sandboxes[sandbox_id] = {"pid": p.pid, "start_time": time.time()}
|
| 80 |
return f"Sandbox launched! ID: {sandbox_id}", sandbox_id
|
|
@@ -113,9 +119,10 @@ def status_sandbox(sandbox_id):
|
|
| 113 |
# Gradio UI
|
| 114 |
# -------------------------
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
-
gr.Markdown("# Sandbox Backend with
|
| 117 |
|
| 118 |
code_input = gr.Textbox(label="Python Code", lines=15, placeholder="Write your code here...")
|
|
|
|
| 119 |
launch_btn = gr.Button("Launch Sandbox")
|
| 120 |
sandbox_id_output = gr.Textbox(label="Sandbox ID")
|
| 121 |
|
|
@@ -132,7 +139,7 @@ with gr.Blocks() as demo:
|
|
| 132 |
|
| 133 |
launch_btn.click(
|
| 134 |
launch_sandbox,
|
| 135 |
-
inputs=code_input,
|
| 136 |
outputs=[sandbox_id_output, sandbox_id_storage]
|
| 137 |
)
|
| 138 |
logs_btn.click(
|
|
|
|
| 14 |
# -------------------------
|
| 15 |
sandboxes = {}
|
| 16 |
SANDBOX_TTL = 2 * 3600 # 2 hours
|
| 17 |
+
MAX_RAM = 4 * 1024**3 # 4GB
|
| 18 |
|
| 19 |
# -------------------------
|
| 20 |
# Sandbox worker function
|
| 21 |
# -------------------------
|
| 22 |
+
def sandbox_worker(code: str, requirements: str, sandbox_id: str):
|
| 23 |
user_dir = f"/tmp/sandbox_{sandbox_id}"
|
| 24 |
os.makedirs(user_dir, exist_ok=True)
|
| 25 |
os.chdir(user_dir)
|
| 26 |
|
| 27 |
+
# Save requirements.txt
|
| 28 |
+
req_file = os.path.join(user_dir, "requirements.txt")
|
| 29 |
+
with open(req_file, "w") as f:
|
| 30 |
+
f.write(requirements)
|
| 31 |
+
|
| 32 |
# Resource limits
|
| 33 |
+
resource.setrlimit(resource.RLIMIT_AS, (MAX_RAM, MAX_RAM))
|
| 34 |
+
resource.setrlimit(resource.RLIMIT_CPU, (SANDBOX_TTL, SANDBOX_TTL))
|
| 35 |
|
| 36 |
# Save user code
|
| 37 |
code_file = os.path.join(user_dir, "user_code.py")
|
|
|
|
| 71 |
if os.path.exists(folder):
|
| 72 |
shutil.rmtree(folder)
|
| 73 |
del sandboxes[sid]
|
| 74 |
+
time.sleep(60)
|
| 75 |
|
| 76 |
threading.Thread(target=cleanup_sandboxes, daemon=True).start()
|
| 77 |
|
| 78 |
# -------------------------
|
| 79 |
# Gradio interface functions
|
| 80 |
# -------------------------
|
| 81 |
+
def launch_sandbox(code, requirements):
|
| 82 |
sandbox_id = str(uuid.uuid4())[:8]
|
| 83 |
+
p = multiprocessing.Process(target=sandbox_worker, args=(code, requirements, sandbox_id))
|
| 84 |
p.start()
|
| 85 |
sandboxes[sandbox_id] = {"pid": p.pid, "start_time": time.time()}
|
| 86 |
return f"Sandbox launched! ID: {sandbox_id}", sandbox_id
|
|
|
|
| 119 |
# Gradio UI
|
| 120 |
# -------------------------
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
+
gr.Markdown("# Sandbox Backend with requirements.txt support")
|
| 123 |
|
| 124 |
code_input = gr.Textbox(label="Python Code", lines=15, placeholder="Write your code here...")
|
| 125 |
+
req_input = gr.Textbox(label="requirements.txt", lines=5, placeholder="Add dependencies here...")
|
| 126 |
launch_btn = gr.Button("Launch Sandbox")
|
| 127 |
sandbox_id_output = gr.Textbox(label="Sandbox ID")
|
| 128 |
|
|
|
|
| 139 |
|
| 140 |
launch_btn.click(
|
| 141 |
launch_sandbox,
|
| 142 |
+
inputs=[code_input, req_input],
|
| 143 |
outputs=[sandbox_id_output, sandbox_id_storage]
|
| 144 |
)
|
| 145 |
logs_btn.click(
|