Spaces:
Running
Running
Upload main.py
Browse files- app/main.py +58 -0
app/main.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# --- Model download logic (Hugging Face Hub) ---
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
def download_if_missing(url, dest):
|
| 7 |
+
if not os.path.exists(dest):
|
| 8 |
+
print(f"Downloading model from {url} to {dest}...")
|
| 9 |
+
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
| 10 |
+
with requests.get(url, stream=True) as r:
|
| 11 |
+
r.raise_for_status()
|
| 12 |
+
with open(dest, "wb") as f:
|
| 13 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 14 |
+
f.write(chunk)
|
| 15 |
+
print("Download complete.")
|
| 16 |
+
|
| 17 |
+
# Hugging Face direct download links
|
| 18 |
+
DAMAGE_MODEL_URL = "https://huggingface.co/AItoolstack/car_damage_detection/resolve/main/yolov8_models/damage/weights/weights/best.pt"
|
| 19 |
+
PARTS_MODEL_URL = "https://huggingface.co/AItoolstack/car_damage_detection/resolve/main/yolov8_models/parts/weights/weights/best.pt"
|
| 20 |
+
|
| 21 |
+
DAMAGE_MODEL_PATH = os.path.join("/tmp", "models", "damage", "weights", "weights", "best.pt")
|
| 22 |
+
PARTS_MODEL_PATH = os.path.join("/tmp", "models", "parts", "weights", "weights", "best.pt")
|
| 23 |
+
|
| 24 |
+
download_if_missing(DAMAGE_MODEL_URL, DAMAGE_MODEL_PATH)
|
| 25 |
+
download_if_missing(PARTS_MODEL_URL, PARTS_MODEL_PATH)
|
| 26 |
+
|
| 27 |
+
from fastapi import FastAPI, File, UploadFile, BackgroundTasks
|
| 28 |
+
from fastapi.staticfiles import StaticFiles
|
| 29 |
+
from fastapi.templating import Jinja2Templates
|
| 30 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
| 31 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 32 |
+
import uvicorn
|
| 33 |
+
from datetime import datetime
|
| 34 |
+
import aiofiles
|
| 35 |
+
from pathlib import Path
|
| 36 |
+
import uuid
|
| 37 |
+
from app.routers import inference
|
| 38 |
+
|
| 39 |
+
app = FastAPI(title="Car Damage Detection API")
|
| 40 |
+
|
| 41 |
+
# CORS middleware
|
| 42 |
+
app.add_middleware(
|
| 43 |
+
CORSMiddleware,
|
| 44 |
+
allow_origins=["*"],
|
| 45 |
+
allow_credentials=True,
|
| 46 |
+
allow_methods=["*"],
|
| 47 |
+
allow_headers=["*"],
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Mount static files
|
| 51 |
+
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
| 52 |
+
templates = Jinja2Templates(directory="app/templates")
|
| 53 |
+
|
| 54 |
+
# Include routers
|
| 55 |
+
app.include_router(inference.router)
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|