Spaces:
Running
Running
| """FastAPI application factory.""" | |
| from __future__ import annotations | |
| from contextlib import asynccontextmanager | |
| from typing import AsyncGenerator | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from src.api.routes import router | |
| from src.utils.config import get_settings | |
| from src.utils.logging import get_logger | |
| logger = get_logger(__name__) | |
| async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: | |
| """Application lifespan handler.""" | |
| # Startup | |
| logger.info("Starting Ask-the-Web Agent API...") | |
| settings = get_settings() | |
| logger.info(f"Environment: {settings.environment}") | |
| yield | |
| # Shutdown | |
| logger.info("Shutting down Ask-the-Web Agent API...") | |
| def create_app() -> FastAPI: | |
| """Create and configure the FastAPI application. | |
| Returns: | |
| Configured FastAPI application | |
| """ | |
| settings = get_settings() | |
| app = FastAPI( | |
| title="Ask-the-Web Agent API", | |
| description=""" | |
| An AI-powered agent that answers questions using web search and reasoning. | |
| ## Features | |
| - Natural language question answering | |
| - Web search integration | |
| - Source citation | |
| - Follow-up question generation | |
| - Conversation history | |
| ## Usage | |
| Send a POST request to `/api/v1/query` with your question. | |
| """, | |
| version="1.0.0", | |
| docs_url="/docs" if settings.debug else None, | |
| redoc_url="/redoc" if settings.debug else None, | |
| lifespan=lifespan, | |
| ) | |
| # Add CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Configure appropriately for production | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Include API routes | |
| app.include_router(router, prefix="/api/v1") | |
| # Root endpoint | |
| async def root() -> dict: | |
| """Root endpoint with API information.""" | |
| return { | |
| "name": "Ask-the-Web Agent API", | |
| "version": "1.0.0", | |
| "docs": "/docs" if settings.debug else None, | |
| "health": "/api/v1/health", | |
| } | |
| return app | |
| # Create app instance for uvicorn | |
| app = create_app() | |