File size: 1,528 Bytes
b8bf5c8
 
225f2b9
b8bf5c8
 
2fb34cd
 
 
b8bf5c8
e2385d4
 
 
b8bf5c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# api/app_new.py
import uvicorn
import re
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .config import setup_logging, check_system_resources, optimize_memory, CORS_ORIGINS, validate_environment
from .database import db_manager
from .routes import router

# ✅ Validate environment
validate_environment()

# ✅ Setup logging
logger = setup_logging()
logger.info("🚀 Starting Medical Chatbot API...")

# ✅ Monitor system resources
check_system_resources(logger)

# ✅ Optimize memory usage
optimize_memory()

# ✅ Initialize FastAPI app
app = FastAPI(
    title="Medical Chatbot API",
    description="AI-powered medical chatbot with RAG and search capabilities",
    version="1.0.0"
)

# ✅ Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=CORS_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ✅ Initialize database connections
try:
    db_manager.initialize_embedding_model()
    db_manager.initialize_mongodb()
    logger.info("✅ Database connections initialized successfully")
except Exception as e:
    logger.error(f"❌ Database initialization failed: {e}")
    raise

# ✅ Include routes
app.include_router(router)

# ✅ Run Uvicorn
if __name__ == "__main__":
    logger.info("[System] ✅ Starting FastAPI Server...")
    try:
        uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
    except Exception as e:
        logger.error(f"❌ Server Startup Failed: {e}")
        exit(1)