Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,53 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import subprocess
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI(title="Agent Sandbox API")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
@app.post("/run")
|
| 7 |
-
def run_code(payload:
|
| 8 |
"""
|
| 9 |
Execute Python code in a sandboxed subprocess.
|
| 10 |
Input JSON: {"code": "print(2+2)"} or {"code": "2+2"}
|
| 11 |
Output JSON: {"output": "4"} or {"error": "..."}
|
| 12 |
"""
|
| 13 |
-
code = payload.
|
|
|
|
| 14 |
if not code:
|
| 15 |
-
|
| 16 |
|
| 17 |
try:
|
| 18 |
# Run the code in a subprocess with a 5s timeout
|
|
@@ -39,6 +74,21 @@ def run_code(payload: dict = Body(...)):
|
|
| 39 |
return {"output": output}
|
| 40 |
|
| 41 |
except subprocess.TimeoutExpired:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from fastapi import FastAPI, Body, HTTPException, Request
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from typing import List, Dict, Any
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from datetime import datetime
|
| 8 |
import subprocess
|
| 9 |
+
import uuid
|
| 10 |
+
import pandas as pd
|
| 11 |
|
| 12 |
app = FastAPI(title="Agent Sandbox API")
|
| 13 |
|
| 14 |
+
class IncommingMail(BaseModel):
|
| 15 |
+
data: List[Dict[str, Any]] # requires a "data" field in JSON
|
| 16 |
+
timestamp: str = datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")
|
| 17 |
+
id: str = uuid.uuid4())[:5]
|
| 18 |
+
|
| 19 |
+
@property
|
| 20 |
+
def file_path(self):
|
| 21 |
+
return f"{self.timestamp}_{self.id}_sample.parquet"
|
| 22 |
+
|
| 23 |
+
def store_mail(self):
|
| 24 |
+
try:
|
| 25 |
+
import pandas as pd
|
| 26 |
+
data = pd.DataFrame(self.data)
|
| 27 |
+
data.to_parquet(self.file_path)
|
| 28 |
+
except Exception as e:
|
| 29 |
+
raise e
|
| 30 |
+
|
| 31 |
+
class MailResponse(BaseModel):
|
| 32 |
+
status: str
|
| 33 |
+
timestamp: str
|
| 34 |
+
pigeon_hole: str
|
| 35 |
+
pigeon_location: str
|
| 36 |
+
|
| 37 |
+
class CodeScript(BaseModel):
|
| 38 |
+
code: str
|
| 39 |
+
|
| 40 |
@app.post("/run")
|
| 41 |
+
def run_code(payload: CodeScript):
|
| 42 |
"""
|
| 43 |
Execute Python code in a sandboxed subprocess.
|
| 44 |
Input JSON: {"code": "print(2+2)"} or {"code": "2+2"}
|
| 45 |
Output JSON: {"output": "4"} or {"error": "..."}
|
| 46 |
"""
|
| 47 |
+
code = payload.code
|
| 48 |
+
|
| 49 |
if not code:
|
| 50 |
+
raise HTTPException(status_code=400, detail="Received Payload in incorrect format. Must pass key: 'code' with value of the script to run python code.")
|
| 51 |
|
| 52 |
try:
|
| 53 |
# Run the code in a subprocess with a 5s timeout
|
|
|
|
| 74 |
return {"output": output}
|
| 75 |
|
| 76 |
except subprocess.TimeoutExpired:
|
| 77 |
+
raise HTTPException(status_code=408, detail="Execution timed out")
|
| 78 |
+
|
| 79 |
+
@app.post("/inbox", response_model=MailResponse)
|
| 80 |
+
def agent_mailbox(mail: IncommingMail):
|
| 81 |
+
"""
|
| 82 |
+
Save incoming data with a timestamp + unique ID.
|
| 83 |
+
"""
|
| 84 |
+
# Prepare / fetch directory
|
| 85 |
+
inbox_dir = Path("inbox")
|
| 86 |
+
inbox_dir.mkdir(exist_ok=True)
|
| 87 |
+
mail.store_mail()
|
| 88 |
+
|
| 89 |
+
return MailResponse(
|
| 90 |
+
status="Data Saved!",
|
| 91 |
+
timestamp=mail.timestamp,
|
| 92 |
+
pigeon_hole=mail.id,
|
| 93 |
+
pigeon_location=mail.file_path
|
| 94 |
+
)
|