Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import subprocess
|
| 4 |
+
import resource
|
| 5 |
+
import multiprocessing
|
| 6 |
+
import signal
|
| 7 |
+
import time
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# Store running sandboxes
|
| 11 |
+
sandboxes = {}
|
| 12 |
+
|
| 13 |
+
# -------------------------
|
| 14 |
+
# Sandbox worker function
|
| 15 |
+
# -------------------------
|
| 16 |
+
def sandbox_worker(code: str, sandbox_id: str):
|
| 17 |
+
user_dir = f"/tmp/sandbox_{sandbox_id}"
|
| 18 |
+
os.makedirs(user_dir, exist_ok=True)
|
| 19 |
+
os.chdir(user_dir)
|
| 20 |
+
|
| 21 |
+
# Resource limits
|
| 22 |
+
resource.setrlimit(resource.RLIMIT_AS, (4 * 1024**3, 4 * 1024**3)) # 4GB RAM
|
| 23 |
+
resource.setrlimit(resource.RLIMIT_CPU, (7200, 7200)) # 2h CPU
|
| 24 |
+
|
| 25 |
+
# Save user code
|
| 26 |
+
code_file = os.path.join(user_dir, "user_code.py")
|
| 27 |
+
with open(code_file, "w") as f:
|
| 28 |
+
f.write(code)
|
| 29 |
+
|
| 30 |
+
# Run code
|
| 31 |
+
try:
|
| 32 |
+
result = subprocess.run(
|
| 33 |
+
["python3", code_file],
|
| 34 |
+
capture_output=True,
|
| 35 |
+
text=True,
|
| 36 |
+
timeout=7200 # 2h wall-clock
|
| 37 |
+
)
|
| 38 |
+
# Save logs
|
| 39 |
+
with open(os.path.join(user_dir, "stdout.log"), "w") as f:
|
| 40 |
+
f.write(result.stdout)
|
| 41 |
+
with open(os.path.join(user_dir, "stderr.log"), "w") as f:
|
| 42 |
+
f.write(result.stderr)
|
| 43 |
+
except subprocess.TimeoutExpired:
|
| 44 |
+
with open(os.path.join(user_dir, "stderr.log"), "w") as f:
|
| 45 |
+
f.write("Execution timed out (2h limit reached).")
|
| 46 |
+
|
| 47 |
+
# -------------------------
|
| 48 |
+
# Gradio interface functions
|
| 49 |
+
# -------------------------
|
| 50 |
+
def launch_sandbox(code):
|
| 51 |
+
sandbox_id = str(uuid.uuid4())[:8]
|
| 52 |
+
p = multiprocessing.Process(target=sandbox_worker, args=(code, sandbox_id))
|
| 53 |
+
p.start()
|
| 54 |
+
sandboxes[sandbox_id] = {"pid": p.pid, "start_time": time.time()}
|
| 55 |
+
return f"Sandbox launched! ID: {sandbox_id}", sandbox_id
|
| 56 |
+
|
| 57 |
+
def fetch_logs(sandbox_id):
|
| 58 |
+
user_dir = f"/tmp/sandbox_{sandbox_id}"
|
| 59 |
+
if not os.path.exists(user_dir):
|
| 60 |
+
return "Sandbox not found."
|
| 61 |
+
stdout_file = os.path.join(user_dir, "stdout.log")
|
| 62 |
+
stderr_file = os.path.join(user_dir, "stderr.log")
|
| 63 |
+
stdout = open(stdout_file).read() if os.path.exists(stdout_file) else ""
|
| 64 |
+
stderr = open(stderr_file).read() if os.path.exists(stderr_file) else ""
|
| 65 |
+
return f"STDOUT:\n{stdout}\n\nSTDERR:\n{stderr}"
|
| 66 |
+
|
| 67 |
+
def kill_sandbox(sandbox_id):
|
| 68 |
+
if sandbox_id not in sandboxes:
|
| 69 |
+
return "Sandbox not found."
|
| 70 |
+
pid = sandboxes[sandbox_id]["pid"]
|
| 71 |
+
try:
|
| 72 |
+
os.kill(pid, signal.SIGKILL)
|
| 73 |
+
del sandboxes[sandbox_id]
|
| 74 |
+
return f"Sandbox {sandbox_id} killed."
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return f"Error killing sandbox: {e}"
|
| 77 |
+
|
| 78 |
+
def status_sandbox(sandbox_id):
|
| 79 |
+
if sandbox_id not in sandboxes:
|
| 80 |
+
return "Sandbox not found."
|
| 81 |
+
elapsed = time.time() - sandboxes[sandbox_id]["start_time"]
|
| 82 |
+
return f"Running for {int(elapsed)} seconds"
|
| 83 |
+
|
| 84 |
+
# -------------------------
|
| 85 |
+
# Gradio UI
|
| 86 |
+
# -------------------------
|
| 87 |
+
with gr.Blocks() as demo:
|
| 88 |
+
gr.Markdown("# Sandbox Backend")
|
| 89 |
+
|
| 90 |
+
code_input = gr.Textbox(label="Python Code", lines=15, placeholder="Write your code here...")
|
| 91 |
+
launch_btn = gr.Button("Launch Sandbox")
|
| 92 |
+
sandbox_id_output = gr.Textbox(label="Sandbox ID")
|
| 93 |
+
|
| 94 |
+
logs_btn = gr.Button("Fetch Logs")
|
| 95 |
+
logs_output = gr.Textbox(label="Logs", lines=10)
|
| 96 |
+
|
| 97 |
+
kill_btn = gr.Button("Kill Sandbox")
|
| 98 |
+
kill_output = gr.Textbox(label="Kill Status")
|
| 99 |
+
|
| 100 |
+
status_btn = gr.Button("Check Status")
|
| 101 |
+
status_output = gr.Textbox(label="Sandbox Status")
|
| 102 |
+
|
| 103 |
+
# Temporary storage for sandbox ID
|
| 104 |
+
sandbox_id_storage = gr.State(value=None)
|
| 105 |
+
|
| 106 |
+
# -------------------------
|
| 107 |
+
# Button actions
|
| 108 |
+
# -------------------------
|
| 109 |
+
launch_btn.click(
|
| 110 |
+
launch_sandbox,
|
| 111 |
+
inputs=code_input,
|
| 112 |
+
outputs=[sandbox_id_output, sandbox_id_storage]
|
| 113 |
+
)
|
| 114 |
+
logs_btn.click(
|
| 115 |
+
fetch_logs,
|
| 116 |
+
inputs=sandbox_id_storage,
|
| 117 |
+
outputs=logs_output
|
| 118 |
+
)
|
| 119 |
+
kill_btn.click(
|
| 120 |
+
kill_sandbox,
|
| 121 |
+
inputs=sandbox_id_storage,
|
| 122 |
+
outputs=kill_output
|
| 123 |
+
)
|
| 124 |
+
status_btn.click(
|
| 125 |
+
status_sandbox,
|
| 126 |
+
inputs=sandbox_id_storage,
|
| 127 |
+
outputs=status_output
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# -------------------------
|
| 131 |
+
# Launch Gradio app
|
| 132 |
+
# -------------------------
|
| 133 |
+
demo.launch()
|