nikeshn commited on
Commit
600046a
Β·
verified Β·
1 Parent(s): 1dd5f41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -22
app.py CHANGED
@@ -1,68 +1,99 @@
1
  """
2
  LibBee - Main Application Entry Point
3
  """
4
- import sys
5
- import os
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
- # Import your routers
14
- from src.api import agent
15
- from src.api import search
16
- from src.api import admin
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Create the FastAPI app instance
 
 
 
 
 
 
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 Configuration
26
  app.add_middleware(
27
  CORSMiddleware,
28
  allow_origins=[
29
- "https://nikeshn.github.io", # Your GitHub Pages
30
  "http://localhost:8080",
31
  "http://localhost:3000",
32
- "http://127.0.0.1:5500"
33
  ],
34
  allow_credentials=True,
35
- allow_methods=["POST", "GET", "OPTIONS"],
36
  allow_headers=["*"],
37
  max_age=86400,
38
  )
39
 
40
- # Include routers
 
 
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
- "endpoints": ["/agent", "/search", "/admin"]
 
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
- "default_model": "gpt",
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")}