Spaces:
Sleeping
Sleeping
Commit
·
dec4c30
1
Parent(s):
936b2ba
fix: use /tmp for uploads/results and update template for download routes (Hugging Face Spaces compatibility)
Browse files- app/routers/inference.py +21 -4
app/routers/inference.py
CHANGED
@@ -140,6 +140,7 @@ def upload_image(request: Request, file: UploadFile = File(...)):
|
|
140 |
with open(upload_path, "wb") as buffer:
|
141 |
shutil.copyfileobj(file.file, buffer)
|
142 |
|
|
|
143 |
# Run both inferences
|
144 |
try:
|
145 |
damage_result = run_yolo_inference(DAMAGE_MODEL_PATH, upload_path)
|
@@ -149,9 +150,10 @@ def upload_image(request: Request, file: UploadFile = File(...)):
|
|
149 |
damage_img_path = os.path.join(RESULTS_DIR, f"{session_id}_damage.png")
|
150 |
parts_img_path = os.path.join(RESULTS_DIR, f"{session_id}_parts.png")
|
151 |
json_path = os.path.join(RESULTS_DIR, f"{session_id}_result.json")
|
152 |
-
|
153 |
-
|
154 |
-
|
|
|
155 |
|
156 |
# Defensive: set to None by default
|
157 |
damage_img = None
|
@@ -212,6 +214,21 @@ def upload_image(request: Request, file: UploadFile = File(...)):
|
|
212 |
{
|
213 |
"request": request,
|
214 |
"result": result,
|
215 |
-
"original_image": f"/
|
216 |
}
|
217 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
with open(upload_path, "wb") as buffer:
|
141 |
shutil.copyfileobj(file.file, buffer)
|
142 |
|
143 |
+
|
144 |
# Run both inferences
|
145 |
try:
|
146 |
damage_result = run_yolo_inference(DAMAGE_MODEL_PATH, upload_path)
|
|
|
150 |
damage_img_path = os.path.join(RESULTS_DIR, f"{session_id}_damage.png")
|
151 |
parts_img_path = os.path.join(RESULTS_DIR, f"{session_id}_parts.png")
|
152 |
json_path = os.path.join(RESULTS_DIR, f"{session_id}_result.json")
|
153 |
+
# Use custom download endpoints for Hugging Face Spaces
|
154 |
+
damage_img_url = f"/download/result/{session_id}_damage.png"
|
155 |
+
parts_img_url = f"/download/result/{session_id}_parts.png"
|
156 |
+
json_url = f"/download/result/{session_id}_result.json"
|
157 |
|
158 |
# Defensive: set to None by default
|
159 |
damage_img = None
|
|
|
214 |
{
|
215 |
"request": request,
|
216 |
"result": result,
|
217 |
+
"original_image": f"/download/upload/{session_id}.{ext}"
|
218 |
}
|
219 |
)
|
220 |
+
|
221 |
+
# --- Add routes to serve files from /tmp/uploads and /tmp/results ---
|
222 |
+
@router.get("/download/upload/{filename}")
|
223 |
+
def download_uploaded_file(filename: str):
|
224 |
+
file_path = os.path.join(UPLOAD_DIR, filename)
|
225 |
+
if not os.path.exists(file_path):
|
226 |
+
return JSONResponse(status_code=404, content={"error": "File not found"})
|
227 |
+
return FileResponse(file_path, filename=filename)
|
228 |
+
|
229 |
+
@router.get("/download/result/{filename}")
|
230 |
+
def download_result_file(filename: str):
|
231 |
+
file_path = os.path.join(RESULTS_DIR, filename)
|
232 |
+
if not os.path.exists(file_path):
|
233 |
+
return JSONResponse(status_code=404, content={"error": "File not found"})
|
234 |
+
return FileResponse(file_path, filename=filename)
|