Spaces:
Sleeping
Sleeping
Commit ยท
554e2a3
1
Parent(s): d4d2c57
fix: /api/latest-image returns base64 image_data instead of expiring URL
Browse files510 unmodified lines511512513514515516517518519520521522523524525526527528529530531532533534535536537510 unmodified lines@app.get("/api/latest-image")async def api_latest_image(request: Request):global LATEST_OUTPUT_FILENAMEif not LATEST_OUTPUT_FILENAME:# Fallback: scan static/outputs/ directory for the newest fileoutputs_dir = "./static/outputs"if os.path.exists(outputs_dir):files = [f for f in os.listdir(outputs_dir) if f.endswith(".jpg") and not f.endswith("_mask.jpg")]if files:# Sort by modification timefiles.sort(key=lambda x: os.path.getmtime(os.path.join(outputs_dir, x)), reverse=True)LATEST_OUTPUT_FILENAME = files[0]if not LATEST_OUTPUT_FILENAME:return JSONResponse({"status": "error", "message": "No images generated yet"}, status_code=404)proto = request.headers.get("x-forwarded-proto", "http")host = request.headers.get("x-forwarded-host", request.headers.get("host", "localhost:7860"))base_url = f"{proto}://{host}"return JSONResponse({"status": "success","output_filename": LATEST_OUTPUT_FILENAME,"image_url": f"{base_url}/static/outputs/{LATEST_OUTPUT_FILENAME}"})class TryonRequest(BaseModel):510 unmodified lines511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541510 unmodified lines@app.get("/api/latest-image")async def api_latest_image(request: Request):global LATEST_OUTPUT_FILENAMEoutputs_dir = "./static/outputs"if not LATEST_OUTPUT_FILENAME:# Fallback: scan static/outputs/ directory for the newest fileif os.path.exists(outputs_dir):files = [f for f in os.listdir(outputs_dir) if f.endswith(".jpg") and not f.endswith("_mask.jpg")]if files:files.sort(key=lambda x: os.path.getmtime(os.path.join(outputs_dir, x)), reverse=True)LATEST_OUTPUT_FILENAME = files[0]if not LATEST_OUTPUT_FILENAME:return JSONResponse({"status": "error", "message": "No images generated yet"}, status_code=404)file_path = os.path.join(outputs_dir, LATEST_OUTPUT_FILENAME)if not os.path.isfile(file_path):return JSONResponse({"status": "error", "message": f"Image file not found (Space may have restarted): {LATEST_OUTPUT_FILENAME}"}, status_code=404)# Return the image as base64 so the caller never depends on an ephemeral URLwith open(file_path, "rb") as f:image_b64 = base64.b64encode(f.read()).decode("utf-8")return JSONResponse({"status": "success","output_filename": LATEST_OUTPUT_FILENAME,"image_data": f"data:image/jpeg;base64,{image_b64}"})class TryonRequest(BaseModel):
app.py+13-9