Download app/main.py from alielshantory/Smart_Mall_Vehicle_Analytics_System: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/spaces/alielshantory/Smart_Mall_Vehicle_Analytics_System/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/alielshantory/Smart_Mall_Vehicle_Analytics_System/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/alielshantory/Smart_Mall_Vehicle_Analytics_System/resolve/main/app/main.py
4.3 kB
| """ | |
| FastAPI application entry point. | |
| Start with: | |
| uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload | |
| """ | |
| import logging | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(levelname)s - %(message)s", | |
| ) | |
| logger = logging.getLogger(__name__) | |
| async def lifespan(app: FastAPI): | |
| """Initialize resources on startup, clean up on shutdown.""" | |
| # ββ Startup βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| logger.info("Starting up...") | |
| try: | |
| from .pipeline.coordinator import LPRPipeline | |
| app.state.pipeline = LPRPipeline() | |
| logger.info("β Pipeline initialized") | |
| except Exception as e: | |
| logger.error(f"Pipeline failed to initialize: {e}") | |
| app.state.pipeline = None | |
| try: | |
| from .database.connection import Database | |
| from .database.logger import VisitLogger | |
| db = Database() | |
| app.state.db = db | |
| app.state.visit_logger = VisitLogger(db) | |
| logger.info("β Supabase connected") | |
| except Exception as e: | |
| logger.error(f"Database failed to connect: {e}") | |
| app.state.db = None | |
| app.state.visit_logger = None | |
| yield # app runs here | |
| # ββ Shutdown ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| logger.info("Shutting down...") | |
| if hasattr(app.state, "db") and app.state.db: | |
| app.state.db.close() | |
| if hasattr(app.state, "pipeline") and app.state.pipeline: | |
| del app.state.pipeline | |
| logger.info("Shutdown complete.") | |
| # ββ App βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app = FastAPI( | |
| title="Vehicle Intelligence Dashboard β LPR API", | |
| description="License plate detection, OCR, vehicle classification, and purchasing power scoring.", | |
| version="1.0.0", | |
| lifespan=lifespan, | |
| ) | |
| # ββ Middleware ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=[ | |
| "http://localhost:3000", # React CRA | |
| "http://localhost:5173", # Vite | |
| "http://localhost:8080", # your dashboard | |
| ], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # ββ Routes ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| from .api.routes import detect, entries, stats, analytics, leaderboard, cameras | |
| app.include_router(detect.router, prefix="/api") | |
| app.include_router(entries.router, prefix="/api") | |
| app.include_router(stats.router, prefix="/api") | |
| app.include_router(analytics.router, prefix="/api") | |
| app.include_router(leaderboard.router, prefix="/api") | |
| app.include_router(cameras.router, prefix="/api") | |
| # ββ Health Check ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def root(): | |
| return { | |
| "status": "ok", | |
| "message": "Vehicle Intelligence Dashboard LPR API", | |
| "docs": "/docs", | |
| } | |
| def health(): | |
| return { | |
| "pipeline": app.state.pipeline is not None, | |
| "database": app.state.db is not None, | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True) |