Spaces:
Running
Running
| import os | |
| import sys | |
| from pathlib import Path | |
| from contextlib import asynccontextmanager | |
| # Add project root and local src to path to allow direct execution | |
| current_dir = Path(__file__).resolve().parent | |
| project_root = current_dir.parent.parent.parent.parent # d:\...pipeline | |
| sys.path.append(str(project_root)) | |
| sys.path.append(str(current_dir.parent)) # d:\...\rag-api | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from src.core.config import settings | |
| from src.api.routes import rag, analytics, interactions, accounts, news, auth, top_stories | |
| from src.infrastructure.database import init_db | |
| from src.api.dependencies import prewarm_models | |
| import threading | |
| import uvicorn | |
| async def lifespan(app: FastAPI): | |
| # Startup | |
| init_db() | |
| # Pre-warming moved to lazy loading or manual script to prevent | |
| # initialization timeouts/crashes on the first run. | |
| # thread = threading.Thread(target=prewarm_models) | |
| # thread.start() | |
| yield | |
| # Shutdown (if needed) | |
| pass | |
| app = FastAPI( | |
| title=settings.PROJECT_NAME, | |
| openapi_url=f"{settings.API_V1_STR}/openapi.json", | |
| lifespan=lifespan | |
| ) | |
| # Set all CORS enabled origins | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| app.include_router(rag.router, prefix=f"{settings.API_V1_STR}/rag", tags=["RAG"]) | |
| app.include_router(analytics.router, prefix=f"{settings.API_V1_STR}/analytics", tags=["Analytics"]) | |
| app.include_router(interactions.router, prefix=f"{settings.API_V1_STR}/interactions", tags=["Interactions"]) | |
| app.include_router(accounts.router, prefix=f"{settings.API_V1_STR}/accounts", tags=["Accounts"]) | |
| # Alias for registration convenience | |
| app.include_router(accounts.router, prefix=f"{settings.API_V1_STR}/users", tags=["Users"], include_in_schema=False) | |
| app.include_router(auth.router, prefix=f"{settings.API_V1_STR}/auth", tags=["Auth"]) | |
| app.include_router(news.router, prefix=f"{settings.API_V1_STR}/news", tags=["News"]) | |
| app.include_router(top_stories.router, prefix=f"{settings.API_V1_STR}", tags=["Top Stories"]) | |
| def get_status(): | |
| return {"status": "online", "message": "RAG API Service is operational"} | |
| def read_root(): | |
| return {"message": "Welcome to the RAG API Service", "status": "online"} | |
| if __name__ == "__main__": | |
| # Start the server directly from this script | |
| # Use the app object directly for simplicity when running as main | |
| uvicorn.run(app, host="0.0.0.0", port=8000) | |