Hamza4100's picture
Update app/main.py
332e741 verified
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"])
@app.on_event("startup")
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})")