John Sathya
Add RF-DETR detection service with FastAPI
202fa33
Raw
History Blame Contribute Delete
1.36 kB
"""FastAPI application for component detection."""
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from component_detection.models.schemas import HealthResponse
from component_detection.routes.detection import router as detection_router
from component_detection.services.dependencies import get_detection_service
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Ensure model is loaded on startup, cleanup on shutdown."""
service = get_detection_service()
if not service.is_loaded:
service.load_model()
logger.info("Application started - model ready for inference.")
yield
logger.info("Application shutting down.")
app = FastAPI(
title="Component Detection API",
description="Detect solar panel components (Lfoot, microinverter, rail, solar panel) using RF-DETR.",
version="0.1.0",
lifespan=lifespan,
)
app.include_router(detection_router)
@app.get("/health", response_model=HealthResponse)
async def health_check() -> HealthResponse:
"""Health check endpoint."""
service = get_detection_service()
return HealthResponse(status="ok", model_loaded=service.is_loaded)