Spaces:
Sleeping
Sleeping
Add debug logging
Browse files
app.py
CHANGED
|
@@ -67,6 +67,77 @@ 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 |
job = JOBS.get(job_id)
|
|
@@ -77,15 +148,27 @@ def status(job_id: str):
|
|
| 77 |
|
| 78 |
@app.get("/download/{job_id}")
|
| 79 |
def download(job_id: str):
|
|
|
|
| 80 |
job = JOBS.get(job_id)
|
| 81 |
if not job:
|
|
|
|
| 82 |
raise HTTPException(status_code=404, detail="No such job")
|
| 83 |
|
| 84 |
if job.get("state") != "SUCCESS":
|
|
|
|
| 85 |
raise HTTPException(status_code=404, detail="Result not ready")
|
| 86 |
|
| 87 |
stl_path = job.get("result")
|
|
|
|
| 88 |
if not stl_path or not Path(stl_path).exists():
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
return FileResponse(path=stl_path, filename=Path(stl_path).name, media_type="application/sla")
|
|
|
|
| 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)
|
|
|
|
| 148 |
|
| 149 |
@app.get("/download/{job_id}")
|
| 150 |
def download(job_id: str):
|
| 151 |
+
print(f"[DEBUG] Download request for {job_id}")
|
| 152 |
job = JOBS.get(job_id)
|
| 153 |
if not job:
|
| 154 |
+
print(f"[DEBUG] Job {job_id} not found in JOBS: {list(JOBS.keys())}")
|
| 155 |
raise HTTPException(status_code=404, detail="No such job")
|
| 156 |
|
| 157 |
if job.get("state") != "SUCCESS":
|
| 158 |
+
print(f"[DEBUG] Job {job_id} state is {job.get('state')}")
|
| 159 |
raise HTTPException(status_code=404, detail="Result not ready")
|
| 160 |
|
| 161 |
stl_path = job.get("result")
|
| 162 |
+
print(f"[DEBUG] Checking file path: {stl_path}")
|
| 163 |
if not stl_path or not Path(stl_path).exists():
|
| 164 |
+
print(f"[DEBUG] File missing at {stl_path}")
|
| 165 |
+
# List dir to see what's there
|
| 166 |
+
try:
|
| 167 |
+
parent = Path(stl_path).parent
|
| 168 |
+
print(f"[DEBUG] Contents of {parent}: {list(parent.glob('*'))}")
|
| 169 |
+
except Exception as e:
|
| 170 |
+
print(f"[DEBUG] Failed to list dir: {e}")
|
| 171 |
+
|
| 172 |
+
raise HTTPException(status_code=404, detail=f"Result file missing at {stl_path}")
|
| 173 |
|
| 174 |
return FileResponse(path=stl_path, filename=Path(stl_path).name, media_type="application/sla")
|