Spaces:
Build error
Build error
Shubham Pawar
Claude Sonnet 5
fix: make ingestion and querying robust; add acceptance-test corpus
c8f7f2e Download src/api/main.py from AIenthusSP/RAG-Documind-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/src/api/main.py
- Command line
-
hf download hf://spaces/AIenthusSP/RAG-Documind-AI/src/api/main.py
-
curl -L -o main.py https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/src/api/main.py
2.78 kB
| """ | |
| FastAPI Application - Main Entry Point | |
| DocuMind AI - RAG-Powered Documentation Assistant API | |
| """ | |
| import logging | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.concurrency import run_in_threadpool | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from src.api.routes import query, ingest, health | |
| from src.api.middleware.errors import ( | |
| ErrorHandlerMiddleware, | |
| RequestLoggingMiddleware, | |
| RateLimitMiddleware, | |
| ) | |
| from config.settings import settings | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
| ) | |
| logger = logging.getLogger(__name__) | |
| async def lifespan(app: FastAPI): | |
| """ | |
| Application lifespan manager. | |
| Handles startup and shutdown events. | |
| """ | |
| # Startup | |
| logger.info("Starting DocuMind AI API...") | |
| logger.info(f"Environment: {settings.environment}") | |
| # Warm the embedding model now, in a worker thread, so the first ingest or | |
| # query request doesn't pay a multi-second (potentially download-bound) | |
| # stall while holding the event loop. | |
| try: | |
| from src.rag.embeddings import get_embeddings | |
| await run_in_threadpool(lambda: get_embeddings().embed_query("warmup")) | |
| logger.info("Embedding model warmed up") | |
| except Exception as e: # noqa: BLE001 - startup must not hard-fail on this | |
| logger.warning(f"Embedding warmup skipped: {e}") | |
| yield | |
| # Shutdown | |
| logger.info("Shutting down DocuMind AI API...") | |
| # Create FastAPI application | |
| app = FastAPI( | |
| title="DocuMind AI", | |
| description="RAG-Powered Documentation Assistant API", | |
| version="1.0.0", | |
| docs_url="/docs", | |
| redoc_url="/redoc", | |
| lifespan=lifespan, | |
| ) | |
| # Configure CORS | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.cors_origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Add custom middleware (order matters - first added = last executed) | |
| app.add_middleware(ErrorHandlerMiddleware) | |
| app.add_middleware(RequestLoggingMiddleware) | |
| app.add_middleware(RateLimitMiddleware, requests_per_minute=60) | |
| # Include routers | |
| app.include_router(health.router, tags=["Health"]) | |
| app.include_router(query.router, prefix="/api", tags=["Query"]) | |
| app.include_router(ingest.router, prefix="/api", tags=["Ingest"]) | |
| async def root(): | |
| """Root endpoint with API info.""" | |
| return { | |
| "name": "DocuMind AI", | |
| "version": "1.0.0", | |
| "description": "RAG-Powered Documentation Assistant", | |
| "docs": "/docs", | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "src.api.main:app", | |
| host="0.0.0.0", | |
| port=8000, | |
| reload=True, | |
| ) | |