Spaces:
Paused
Paused
AdriBat1 commited on
Commit Β·
b7534f3
1
Parent(s): 95ad505
Rollback to stable app.py (no streaming)
Browse files- remote-gpu-server/app.py +1 -89
remote-gpu-server/app.py
CHANGED
|
@@ -14,8 +14,6 @@ import glob
|
|
| 14 |
import shutil
|
| 15 |
import os
|
| 16 |
import sys
|
| 17 |
-
import json
|
| 18 |
-
|
| 19 |
|
| 20 |
app = FastAPI()
|
| 21 |
|
|
@@ -120,7 +118,7 @@ def get_system_info():
|
|
| 120 |
|
| 121 |
@app.get("/")
|
| 122 |
def read_root():
|
| 123 |
-
return {"status": "running", "message": "Remote GPU Execution Server
|
| 124 |
|
| 125 |
|
| 126 |
@app.post("/execute")
|
|
@@ -133,92 +131,6 @@ async def execute(data: dict):
|
|
| 133 |
import traceback
|
| 134 |
return {"output": f"β Server Error: {str(e)}\n\n{traceback.format_exc()}", "files": []}
|
| 135 |
|
| 136 |
-
# --- STREAMING LOGIC ---
|
| 137 |
-
from fastapi.responses import StreamingResponse
|
| 138 |
-
|
| 139 |
-
def stream_code_execution(python_code: str):
|
| 140 |
-
"""
|
| 141 |
-
Generatore che esegue codice e yielda output riga per riga.
|
| 142 |
-
Alla fine yielda un JSON speciale con i file.
|
| 143 |
-
"""
|
| 144 |
-
if not python_code or not python_code.strip():
|
| 145 |
-
yield "β Errore: Nessun codice ricevuto.\n"
|
| 146 |
-
return
|
| 147 |
-
|
| 148 |
-
work_dir = tempfile.mkdtemp()
|
| 149 |
-
script_path = os.path.join(work_dir, "script.py")
|
| 150 |
-
|
| 151 |
-
with open(script_path, 'w', encoding='utf-8') as f:
|
| 152 |
-
f.write(python_code)
|
| 153 |
-
|
| 154 |
-
yield "π Avvio esecuzione (STREAMING)...\n"
|
| 155 |
-
yield f"π Directory: {work_dir}\n"
|
| 156 |
-
yield "=" * 50 + "\n"
|
| 157 |
-
|
| 158 |
-
try:
|
| 159 |
-
# Popen per leggere output in tempo reale
|
| 160 |
-
process = subprocess.Popen(
|
| 161 |
-
[sys.executable, script_path],
|
| 162 |
-
stdout=subprocess.PIPE,
|
| 163 |
-
stderr=subprocess.STDOUT, # Combina stderr in stdout
|
| 164 |
-
text=True,
|
| 165 |
-
cwd=work_dir,
|
| 166 |
-
env={**os.environ, "PYTHONUNBUFFERED": "1"},
|
| 167 |
-
bufsize=1 # Line buffered
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
# Leggi riga per riga
|
| 171 |
-
for line in process.stdout:
|
| 172 |
-
yield line
|
| 173 |
-
|
| 174 |
-
process.wait()
|
| 175 |
-
|
| 176 |
-
yield "=" * 50 + "\n"
|
| 177 |
-
status = "β
Successo" if process.returncode == 0 else f"β Errore ({process.returncode})"
|
| 178 |
-
yield f"{status}\n"
|
| 179 |
-
|
| 180 |
-
# RACCOLTA FILE FINALI
|
| 181 |
-
generated_files = []
|
| 182 |
-
# Cerca file generati (immagini, csv, json)
|
| 183 |
-
for ext in ['*.png', '*.jpg', '*.svg', '*.csv', '*.json', '*.txt', '*.pth', '*.pt']:
|
| 184 |
-
if ext == '*.txt': continue
|
| 185 |
-
for filepath in glob.glob(os.path.join(work_dir, ext)):
|
| 186 |
-
filename = os.path.basename(filepath)
|
| 187 |
-
try:
|
| 188 |
-
with open(filepath, "rb") as f:
|
| 189 |
-
if os.path.getsize(filepath) > 10 * 1024 * 1024:
|
| 190 |
-
yield f"β οΈ File '{filename}' troppo grande (>10MB), saltato.\n"
|
| 191 |
-
continue
|
| 192 |
-
encoded = base64.b64encode(f.read()).decode('utf-8')
|
| 193 |
-
generated_files.append({
|
| 194 |
-
"name": filename,
|
| 195 |
-
"data": encoded,
|
| 196 |
-
"mime": "application/octet-stream"
|
| 197 |
-
})
|
| 198 |
-
yield f"π¦ File generato: {filename}\n"
|
| 199 |
-
except Exception as e:
|
| 200 |
-
yield f"β Errore lettura {filename}: {e}\n"
|
| 201 |
-
|
| 202 |
-
# Special marker for files payload
|
| 203 |
-
if generated_files:
|
| 204 |
-
payload = json.dumps({"files": generated_files})
|
| 205 |
-
yield f"__ANTIGRAVITY_FILES_JSON__:{payload}\n"
|
| 206 |
-
|
| 207 |
-
except Exception as e:
|
| 208 |
-
import traceback
|
| 209 |
-
yield f"β Eccezione: {str(e)}\n{traceback.format_exc()}\n"
|
| 210 |
-
|
| 211 |
-
finally:
|
| 212 |
-
shutil.rmtree(work_dir, ignore_errors=True)
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
@app.post("/execute_stream")
|
| 216 |
-
async def execute_stream(data: dict):
|
| 217 |
-
"""Endpoint POST per eseguire codice Python con output streaming"""
|
| 218 |
-
code = data.get("code", "")
|
| 219 |
-
return StreamingResponse(stream_code_execution(code), media_type="text/plain")
|
| 220 |
-
# -----------------------
|
| 221 |
-
|
| 222 |
|
| 223 |
# Interfaccia Gradio (Wrapper semplificato)
|
| 224 |
def gradio_wrapper(code):
|
|
|
|
| 14 |
import shutil
|
| 15 |
import os
|
| 16 |
import sys
|
|
|
|
|
|
|
| 17 |
|
| 18 |
app = FastAPI()
|
| 19 |
|
|
|
|
| 118 |
|
| 119 |
@app.get("/")
|
| 120 |
def read_root():
|
| 121 |
+
return {"status": "running", "message": "Remote GPU Execution Server"}
|
| 122 |
|
| 123 |
|
| 124 |
@app.post("/execute")
|
|
|
|
| 131 |
import traceback
|
| 132 |
return {"output": f"β Server Error: {str(e)}\n\n{traceback.format_exc()}", "files": []}
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# Interfaccia Gradio (Wrapper semplificato)
|
| 136 |
def gradio_wrapper(code):
|