Spaces:
Runtime error
Runtime error
Update server.py
Browse files
server.py
CHANGED
|
@@ -1,48 +1,50 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.responses import
|
| 3 |
-
import
|
|
|
|
| 4 |
import base64
|
| 5 |
from run_inference import infer_images
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 9 |
app.add_middleware(
|
| 10 |
CORSMiddleware,
|
| 11 |
-
allow_origins=["https://yaghi27
|
| 12 |
allow_credentials=True,
|
| 13 |
allow_methods=["*"],
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
async def infer(images: list[UploadFile] = File(...)):
|
| 20 |
-
# Save images temporarily
|
| 21 |
-
img_paths = []
|
| 22 |
-
for img in images:
|
| 23 |
-
path = f"/tmp/{img.filename}"
|
| 24 |
-
with open(path, 'wb') as f:
|
| 25 |
-
f.write(await img.read())
|
| 26 |
-
img_paths.append(path)
|
| 27 |
-
|
| 28 |
-
# Run inference
|
| 29 |
-
bev_images = infer_images(img_paths) # Should return list of file paths to BEV plots
|
| 30 |
-
|
| 31 |
-
# Encode BEV images to base64
|
| 32 |
-
results = []
|
| 33 |
-
for img_path in bev_images:
|
| 34 |
-
with open(img_path, "rb") as f:
|
| 35 |
-
encoded = base64.b64encode(f.read()).decode('utf-8')
|
| 36 |
-
results.append({"bev_image": encoded})
|
| 37 |
-
|
| 38 |
-
return JSONResponse(content=results)
|
| 39 |
|
| 40 |
|
| 41 |
@app.get("/", response_class=HTMLResponse)
|
| 42 |
async def root():
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
if __name__ == "__main__":
|
| 47 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 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 |
+
# 1) CORS: allow requests from your Space’s frontend origin
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
+
allow_origins=["https://huggingface.co/spaces/yaghi27/regnet-detr3d"],
|
| 15 |
allow_credentials=True,
|
| 16 |
allow_methods=["*"],
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# 2) Mount a `static/` folder so that any JS/CSS referenced in index.html will load
|
| 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 |
|
| 31 |
+
@app.post("/infer")
|
| 32 |
+
async def run_inference(images: list[UploadFile] = File(...)):
|
| 33 |
+
# save uploads
|
| 34 |
+
img_paths = []
|
| 35 |
+
for img in images:
|
| 36 |
+
tmp = f"/tmp/{img.filename}"
|
| 37 |
+
with open(tmp, "wb") as f:
|
| 38 |
+
f.write(await img.read())
|
| 39 |
+
img_paths.append(tmp)
|
| 40 |
+
|
| 41 |
+
# run your BEV inference
|
| 42 |
+
bev_paths = infer_images(img_paths)
|
| 43 |
+
|
| 44 |
+
# base64-encode results
|
| 45 |
+
output = []
|
| 46 |
+
for p in bev_paths:
|
| 47 |
+
with open(p, "rb") as f:
|
| 48 |
+
b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 49 |
+
output.append({"bev_image": b64})
|
| 50 |
+
return JSONResponse(content=output)
|