Spaces:
Running
Running
Fix syntax error in app.py
Browse files
app.py
CHANGED
|
@@ -67,77 +67,6 @@ async def upload_image(background_tasks: BackgroundTasks, file: UploadFile = Fil
|
|
| 67 |
return {"job_id": job_id}
|
| 68 |
|
| 69 |
|
| 70 |
-
@app.get("/status/{job_id}")
|
| 71 |
-
def status(job_id: str):
|
| 72 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException, BackgroundTasks
|
| 73 |
-
from fastapi.responses import FileResponse, JSONResponse
|
| 74 |
-
from uuid import uuid4
|
| 75 |
-
from pathlib import Path
|
| 76 |
-
import shutil
|
| 77 |
-
import os
|
| 78 |
-
import json
|
| 79 |
-
from dotenv import load_dotenv
|
| 80 |
-
from tasks import process_image_task
|
| 81 |
-
|
| 82 |
-
load_dotenv()
|
| 83 |
-
|
| 84 |
-
# Directories
|
| 85 |
-
# Use /tmp for Hugging Face Spaces as it is writable
|
| 86 |
-
UPLOAD_DIR = Path("/tmp/uploads")
|
| 87 |
-
RESULT_DIR = Path("/tmp/results")
|
| 88 |
-
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
| 89 |
-
RESULT_DIR.mkdir(parents=True, exist_ok=True)
|
| 90 |
-
|
| 91 |
-
# In-memory job store (Global variable)
|
| 92 |
-
# Since HF Spaces (Free) runs 1 replica, this works for a demo.
|
| 93 |
-
JOBS = {}
|
| 94 |
-
|
| 95 |
-
app = FastAPI(title="Depth->STL processing service (Standalone)")
|
| 96 |
-
|
| 97 |
-
def update_job_status(job_id: str, state: str, detail: str = "", result: str = ""):
|
| 98 |
-
JOBS[job_id] = {
|
| 99 |
-
"state": state,
|
| 100 |
-
"detail": detail,
|
| 101 |
-
"result": result
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
@app.get("/health")
|
| 105 |
-
def health_check():
|
| 106 |
-
return {"status": "ok"}
|
| 107 |
-
|
| 108 |
-
@app.post("/upload/")
|
| 109 |
-
async def upload_image(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
|
| 110 |
-
# Basic validation
|
| 111 |
-
if not file.content_type.startswith("image/"):
|
| 112 |
-
raise HTTPException(status_code=400, detail="File must be an image")
|
| 113 |
-
|
| 114 |
-
job_id = str(uuid4())
|
| 115 |
-
safe_name = Path(file.filename).name
|
| 116 |
-
fname = f"{job_id}_{safe_name}"
|
| 117 |
-
save_path = UPLOAD_DIR / fname
|
| 118 |
-
|
| 119 |
-
# Save uploaded file
|
| 120 |
-
try:
|
| 121 |
-
with save_path.open("wb") as buffer:
|
| 122 |
-
shutil.copyfileobj(file.file, buffer)
|
| 123 |
-
except Exception as e:
|
| 124 |
-
raise HTTPException(status_code=500, detail=f"Failed to save upload: {e}")
|
| 125 |
-
|
| 126 |
-
# Mark queued
|
| 127 |
-
update_job_status(job_id, "QUEUED", "Job received and queued")
|
| 128 |
-
|
| 129 |
-
# Add to background tasks
|
| 130 |
-
background_tasks.add_task(
|
| 131 |
-
process_image_task,
|
| 132 |
-
str(save_path),
|
| 133 |
-
str(RESULT_DIR),
|
| 134 |
-
job_id,
|
| 135 |
-
update_job_status
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
return {"job_id": job_id}
|
| 139 |
-
|
| 140 |
-
|
| 141 |
@app.get("/status/{job_id}")
|
| 142 |
def status(job_id: str):
|
| 143 |
job = JOBS.get(job_id)
|
|
|
|
| 67 |
return {"job_id": job_id}
|
| 68 |
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
@app.get("/status/{job_id}")
|
| 71 |
def status(job_id: str):
|
| 72 |
job = JOBS.get(job_id)
|