Spaces:
Runtime error
Runtime error
Update server.py
Browse files
server.py
CHANGED
|
@@ -1,53 +1,53 @@
|
|
| 1 |
-
|
| 2 |
from fastapi import FastAPI, UploadFile, File
|
| 3 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
import base64
|
|
|
|
|
|
|
| 7 |
from run_inference import infer_images
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
#
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
-
allow_origins=["https://
|
| 15 |
allow_credentials=True,
|
| 16 |
allow_methods=["*"],
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 22 |
|
| 23 |
|
| 24 |
@app.get("/", response_class=HTMLResponse)
|
| 25 |
async def root():
|
| 26 |
-
# serve the HTML page
|
| 27 |
with open("index.html", "r", encoding="utf-8") as f:
|
| 28 |
return HTMLResponse(f.read())
|
| 29 |
|
|
|
|
| 30 |
@app.post("/infer")
|
| 31 |
async def run_inference(images: list[UploadFile] = File(...)):
|
| 32 |
img_paths = []
|
| 33 |
for upload in images:
|
| 34 |
data = await upload.read()
|
| 35 |
-
# force 3-channel BGR
|
| 36 |
bgr = mmcv.imfrombytes(data, flag="color")
|
| 37 |
tmp = f"/tmp/{upload.filename}"
|
| 38 |
mmcv.imwrite(bgr, tmp)
|
| 39 |
img_paths.append(tmp)
|
| 40 |
|
| 41 |
-
# wrap the inference in try/except
|
| 42 |
try:
|
| 43 |
bev_paths = infer_images(img_paths)
|
| 44 |
except Exception as e:
|
| 45 |
logging.exception("❌ inference failed")
|
| 46 |
-
|
| 47 |
-
return JSONResponse(
|
| 48 |
-
status_code=500,
|
| 49 |
-
content={"error": str(e)}
|
| 50 |
-
)
|
| 51 |
|
| 52 |
output = []
|
| 53 |
for p in bev_paths:
|
|
|
|
| 1 |
+
import logging
|
| 2 |
from fastapi import FastAPI, UploadFile, File
|
| 3 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
import base64
|
| 7 |
+
import mmcv
|
| 8 |
+
|
| 9 |
from run_inference import infer_images
|
| 10 |
|
| 11 |
+
# ─── Configure logging ─────────────────────────────────────────────────────────
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
+
# ─── CORS: allow only your Space’s hf.space domain ─────────────────────────────
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
+
allow_origins=["https://yaghi27-regnet-detr3d.hf.space"],
|
| 20 |
allow_credentials=True,
|
| 21 |
allow_methods=["*"],
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# ─── (Optional) serve any JS/CSS under ./static/ ────────────────────────────────
|
| 26 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 27 |
|
| 28 |
|
| 29 |
@app.get("/", response_class=HTMLResponse)
|
| 30 |
async def root():
|
|
|
|
| 31 |
with open("index.html", "r", encoding="utf-8") as f:
|
| 32 |
return HTMLResponse(f.read())
|
| 33 |
|
| 34 |
+
|
| 35 |
@app.post("/infer")
|
| 36 |
async def run_inference(images: list[UploadFile] = File(...)):
|
| 37 |
img_paths = []
|
| 38 |
for upload in images:
|
| 39 |
data = await upload.read()
|
| 40 |
+
# Drop any alpha channel, force 3-channel BGR
|
| 41 |
bgr = mmcv.imfrombytes(data, flag="color")
|
| 42 |
tmp = f"/tmp/{upload.filename}"
|
| 43 |
mmcv.imwrite(bgr, tmp)
|
| 44 |
img_paths.append(tmp)
|
| 45 |
|
|
|
|
| 46 |
try:
|
| 47 |
bev_paths = infer_images(img_paths)
|
| 48 |
except Exception as e:
|
| 49 |
logging.exception("❌ inference failed")
|
| 50 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
output = []
|
| 53 |
for p in bev_paths:
|