from contextlib import asynccontextmanager from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse import traceback from app.config import get_settings from app.routers import health, session from app.services.transcription import get_transcription_service @asynccontextmanager async def lifespan(app: FastAPI): # Загружаем Whisper модель при старте print("Загрузка Whisper модели...") service = get_transcription_service() service._get_pipeline() print("Whisper модель загружена!") yield # Cleanup при остановке print("Остановка сервиса...") app = FastAPI( title="AI Checklist Agent API", description="API для AI агента заполнения чеклиста созвона с клиентом", version="1.0.0", lifespan=lifespan, debug=True ) @app.exception_handler(Exception) async def global_exception_handler(request: Request, exc: Exception): error_detail = f"{type(exc).__name__}: {str(exc)}\n{traceback.format_exc()}" print(f"GLOBAL ERROR: {error_detail}") return JSONResponse( status_code=500, content={"detail": error_detail} ) # Настройка CORS settings = get_settings() origins = settings.allowed_origins.split(",") if settings.allowed_origins != "*" else ["*"] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Подключаем роутеры app.include_router(health.router) app.include_router(session.router) @app.get("/") async def root(): return { "message": "AI Checklist Agent API", "docs": "/docs", "health": "/health" }