Spaces:
Sleeping
Sleeping
File size: 4,537 Bytes
71c1ad2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | # app/main.py
# FastAPI application factory β entry point for the Hubble AI Engine
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from app.config import get_settings
from app.observability.logging import setup_logging, get_logger
from app.observability.langsmith import setup_langsmith
from app.models.model_registry import model_registry
from app.services.redis_service import redis_service
from app.services.mongo_service import mongo_service
from app.services.gemini_service import gemini_service
from app.pipeline.workflow import get_workflow
from app.api.router import api_router
from app.db.connection import connect_db, close_db
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Application lifespan manager.
Startup:
1. Configure logging
2. Setup LangSmith tracing
3. Connect MongoDB & Redis
4. Initialize Gemini service
5. Load all ML models
6. Compile LangGraph workflow
Shutdown:
1. Disconnect MongoDB & Redis
"""
logger = get_logger(__name__)
settings = get_settings()
# ββ Startup ββ
logger.info("=" * 60)
logger.info("[STARTUP] HUBBLE AI ENGINE β Starting up...")
logger.info("=" * 60)
# 1. LangSmith
langsmith_ok = setup_langsmith()
logger.info("langsmith", enabled=langsmith_ok)
# 2. MongoDB (legacy motor service + Beanie ODM)
logger.info("connecting_mongodb")
await mongo_service.connect()
await connect_db() # initializes Beanie document models
# 3. Redis
logger.info("connecting_redis")
await redis_service.connect()
# 4. Gemini
logger.info("initializing_gemini")
gemini_service.initialize()
# 5. ML Models
logger.info("loading_models")
model_status = await model_registry.load_all()
logger.info("models_loaded", status=model_status)
# 6. LangGraph Workflow
logger.info("compiling_workflow")
get_workflow()
logger.info("=" * 60)
logger.info("[READY] HUBBLE AI ENGINE β Ready!")
logger.info(f" Environment: {settings.env}")
logger.info(f" Port: {settings.port}")
logger.info(f" Docs: http://localhost:{settings.port}/docs")
logger.info("=" * 60)
yield # ββ Application runs here ββ
# ββ Shutdown ββ
logger.info("[SHUTDOWN] HUBBLE β Shutting down...")
await redis_service.disconnect()
await mongo_service.disconnect()
await close_db()
logger.info("Shutdown complete")
def create_app() -> FastAPI:
"""Create and configure the FastAPI application."""
settings = get_settings()
# Configure logging first
setup_logging()
app = FastAPI(
title="Hubble AI Engine β Cyberbullying Detection API",
description=(
"Production-grade content moderation pipeline with layered AI analysis. "
"Supports text, image, and video inputs with risk-based routing."
),
version="4.0.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan,
)
# CORS β use origins from config
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins_list if settings.is_production else ["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount routes
app.include_router(api_router)
@app.get("/", include_in_schema=False)
async def root():
return JSONResponse({
"name": "Hubble Unified API",
"version": "5.0.0",
"docs": "/docs",
"health": "/health",
"endpoints": {
"auth": "POST /api/v1/auth/{register|login|refresh|logout}",
"users": "GET /api/v1/users/me",
"scan_text": "POST /api/v1/scan/text",
"scan_image": "POST /api/v1/scan/image",
"scan_history": "GET /api/v1/scan/history",
"alerts": "GET /api/v1/alerts",
"analyze_text": "POST /api/v1/analyze/text (raw, no auth)",
"analyze_image": "POST /api/v1/analyze/image (raw, no auth)",
},
})
return app
# Create the app instance
app = create_app()
if __name__ == "__main__":
import uvicorn
settings = get_settings()
uvicorn.run(
"app.main:app",
host=settings.host,
port=settings.port,
reload=not settings.is_production,
)
|