Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from app.config import settings | |
| from app.routes import ask, ingest, conversations, health | |
| app = FastAPI( | |
| title="Health-Tech AI RAG Chatbot", | |
| description="AI-powered chatbot with Retrieval-Augmented Generation", | |
| version="1.0.0", | |
| ) | |
| # CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.cors_origins_list, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Register routes | |
| app.include_router(health.router, tags=["Health"]) | |
| app.include_router(ask.router, prefix="/api", tags=["Chat"]) | |
| app.include_router(ingest.router, prefix="/api", tags=["Ingestion"]) | |
| app.include_router(conversations.router, prefix="/api", tags=["Conversations"]) | |
| async def startup_event(): | |
| """Initialize services on startup.""" | |
| from app.services.vector_store import vector_store_service | |
| from app.database import init_db | |
| import os | |
| # Ensure uploads directory exists | |
| os.makedirs(settings.UPLOAD_DIR, exist_ok=True) | |
| upload_path = os.path.abspath(settings.UPLOAD_DIR) | |
| print(f"π Upload directory: {upload_path}") | |
| # Initialize database | |
| await init_db() | |
| db_path = os.path.abspath("conversations.db") | |
| print(f"β Database initialized: {db_path}") | |
| # Initialize vector store | |
| await vector_store_service.initialize() | |
| print("β Vector store initialized") | |
| # Count existing documents | |
| if os.path.exists(settings.UPLOAD_DIR): | |
| doc_count = len([f for f in os.listdir(settings.UPLOAD_DIR) if os.path.isfile(os.path.join(settings.UPLOAD_DIR, f))]) | |
| print(f"π Existing documents: {doc_count}") | |
| print(f"π Backend ready on port 8000 (env: {settings.APP_ENV})") | |