Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,99 @@
|
|
| 1 |
"""
|
| 2 |
LibBee - Main Application Entry Point
|
| 3 |
"""
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
# Add current directory to path for src imports
|
| 8 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 9 |
-
|
| 10 |
from fastapi import FastAPI
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
app = FastAPI(
|
| 20 |
title="LibBee - KU Library AI",
|
| 21 |
description="Khalifa University Library AI Assistant",
|
| 22 |
-
version="2.0.0"
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# CORS
|
| 26 |
app.add_middleware(
|
| 27 |
CORSMiddleware,
|
| 28 |
allow_origins=[
|
| 29 |
-
"https://nikeshn.github.io",
|
| 30 |
"http://localhost:8080",
|
| 31 |
"http://localhost:3000",
|
| 32 |
-
"http://127.0.0.1:5500"
|
| 33 |
],
|
| 34 |
allow_credentials=True,
|
| 35 |
-
allow_methods=["POST", "
|
| 36 |
allow_headers=["*"],
|
| 37 |
max_age=86400,
|
| 38 |
)
|
| 39 |
|
| 40 |
-
#
|
|
|
|
|
|
|
| 41 |
app.include_router(agent.router, prefix="/agent", tags=["Agent"])
|
| 42 |
app.include_router(search.router, prefix="/search", tags=["Search"])
|
| 43 |
app.include_router(admin.router, prefix="/admin", tags=["Admin"])
|
| 44 |
|
|
|
|
|
|
|
| 45 |
@app.get("/")
|
| 46 |
def health_check():
|
| 47 |
return {
|
| 48 |
"status": "ok",
|
| 49 |
"version": "2.0.0",
|
| 50 |
"service": "LibBee KU Library AI",
|
| 51 |
-
"
|
|
|
|
| 52 |
}
|
| 53 |
|
|
|
|
| 54 |
@app.get("/config")
|
| 55 |
def public_config():
|
| 56 |
return {
|
| 57 |
"welcome_message": "Hi! I'm LibBee, the Khalifa University Library AI Assistant.",
|
| 58 |
-
"
|
| 59 |
-
"max_results": "5"
|
| 60 |
}
|
| 61 |
|
|
|
|
| 62 |
@app.get("/year")
|
| 63 |
def get_year():
|
| 64 |
from datetime import datetime
|
| 65 |
now = datetime.utcnow()
|
| 66 |
return {"year": now.year, "month": now.month, "date": now.strftime("%Y-%m-%d")}
|
| 67 |
-
|
| 68 |
-
print("β
LibBee API loaded successfully")
|
|
|
|
| 1 |
"""
|
| 2 |
LibBee - Main Application Entry Point
|
| 3 |
"""
|
| 4 |
+
import logging
|
| 5 |
+
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from fastapi import FastAPI
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
# ββ Singletons (imported by routers via `from app import ...`) ββββββββββββββ
|
| 13 |
+
from src.services.rag_service import RAGService
|
| 14 |
+
from src.services.cache_service import CacheService
|
| 15 |
+
|
| 16 |
+
rag_service = RAGService()
|
| 17 |
+
cache_service = CacheService()
|
| 18 |
+
|
| 19 |
+
# ββ Lifespan ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
+
@asynccontextmanager
|
| 21 |
+
async def lifespan(app: FastAPI):
|
| 22 |
+
# STARTUP
|
| 23 |
+
from src.config import get_settings
|
| 24 |
+
from src.services.staff_service import build_staff_index
|
| 25 |
+
|
| 26 |
+
settings = get_settings()
|
| 27 |
+
|
| 28 |
+
# 1. Build in-memory staff role index (instant)
|
| 29 |
+
build_staff_index()
|
| 30 |
+
logger.info("β
Staff index built")
|
| 31 |
+
|
| 32 |
+
# 2. Vectorize knowledge base (calls OpenAI embeddings API ~10-15s)
|
| 33 |
+
if settings.openai_api_key:
|
| 34 |
+
await rag_service.initialize(openai_api_key=settings.openai_api_key)
|
| 35 |
+
else:
|
| 36 |
+
logger.warning("β οΈ OPENAI_API_KEY not set β RAG disabled")
|
| 37 |
|
| 38 |
+
logger.info("β
LibBee startup complete")
|
| 39 |
+
yield
|
| 40 |
+
# SHUTDOWN β nothing needed for in-memory FAISS
|
| 41 |
+
logger.info("LibBee shutting down")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# ββ App ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
app = FastAPI(
|
| 46 |
title="LibBee - KU Library AI",
|
| 47 |
description="Khalifa University Library AI Assistant",
|
| 48 |
+
version="2.0.0",
|
| 49 |
+
lifespan=lifespan,
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# ββ CORS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 53 |
app.add_middleware(
|
| 54 |
CORSMiddleware,
|
| 55 |
allow_origins=[
|
| 56 |
+
"https://nikeshn.github.io",
|
| 57 |
"http://localhost:8080",
|
| 58 |
"http://localhost:3000",
|
| 59 |
+
"http://127.0.0.1:5500",
|
| 60 |
],
|
| 61 |
allow_credentials=True,
|
| 62 |
+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
| 63 |
allow_headers=["*"],
|
| 64 |
max_age=86400,
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# ββ Routers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
+
from src.api import agent, search, admin # noqa: E402 β after app created
|
| 69 |
+
|
| 70 |
app.include_router(agent.router, prefix="/agent", tags=["Agent"])
|
| 71 |
app.include_router(search.router, prefix="/search", tags=["Search"])
|
| 72 |
app.include_router(admin.router, prefix="/admin", tags=["Admin"])
|
| 73 |
|
| 74 |
+
|
| 75 |
+
# ββ Health & Config endpoints βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 76 |
@app.get("/")
|
| 77 |
def health_check():
|
| 78 |
return {
|
| 79 |
"status": "ok",
|
| 80 |
"version": "2.0.0",
|
| 81 |
"service": "LibBee KU Library AI",
|
| 82 |
+
"rag_ready": rag_service.is_ready(),
|
| 83 |
+
"endpoints": ["/agent", "/search", "/admin"],
|
| 84 |
}
|
| 85 |
|
| 86 |
+
|
| 87 |
@app.get("/config")
|
| 88 |
def public_config():
|
| 89 |
return {
|
| 90 |
"welcome_message": "Hi! I'm LibBee, the Khalifa University Library AI Assistant.",
|
| 91 |
+
"max_results": 5,
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
+
|
| 95 |
@app.get("/year")
|
| 96 |
def get_year():
|
| 97 |
from datetime import datetime
|
| 98 |
now = datetime.utcnow()
|
| 99 |
return {"year": now.year, "month": now.month, "date": now.strftime("%Y-%m-%d")}
|
|
|
|
|
|