|
""" |
|
Main FastAPI application entry point |
|
""" |
|
import os |
|
import logging |
|
from fastapi import FastAPI, HTTPException |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from fastapi.staticfiles import StaticFiles |
|
from contextlib import asynccontextmanager |
|
|
|
from app.routers import vqa |
|
from app.services.model_service import ModelService |
|
from app.config import settings |
|
|
|
|
|
logging.basicConfig( |
|
level=logging.INFO, |
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
|
) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
@asynccontextmanager |
|
async def lifespan(app: FastAPI): |
|
|
|
logger.info("Loading VQA model...") |
|
app.state.model_service = ModelService() |
|
app.state.model_service.load_model() |
|
logger.info("VQA model loaded successfully") |
|
yield |
|
|
|
logger.info("Shutting down...") |
|
|
|
|
|
app = FastAPI( |
|
title="VizWiz VQA API", |
|
description="API for Visual Question Answering on images", |
|
version="1.0.0", |
|
lifespan=lifespan |
|
) |
|
|
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["https://vizwiz.vercel.app/"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
static_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "static") |
|
if os.path.exists(static_dir): |
|
app.mount("/static", StaticFiles(directory=static_dir), name="static") |
|
|
|
|
|
app.include_router(vqa.router) |
|
|
|
|
|
@app.get("/health") |
|
async def health_check(): |
|
"""Health check endpoint for monitoring the service""" |
|
if not hasattr(app.state, "model_service") or not app.state.model_service.is_model_loaded(): |
|
raise HTTPException(status_code=503, detail="Model not loaded") |
|
return {"status": "healthy", "model_loaded": True} |
|
|
|
|
|
@app.get("/") |
|
async def root(): |
|
"""Root endpoint returning basic info about the API""" |
|
return { |
|
"name": "VizWiz Visual Question Answering API", |
|
"description": "API for visual question answering on images", |
|
"documentation": "/docs", |
|
"health_check": "/health", |
|
"version": "1.0.0" |
|
} |
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
port = settings.PORT |
|
uvicorn.run("app.main:app", host="0.0.0.0", port=port, reload=False) |