Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,40 @@
|
|
| 1 |
"""
|
| 2 |
-
LibBee - KU Library AI Agent
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
from fastapi import FastAPI, HTTPException, Request
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 7 |
from contextlib import asynccontextmanager
|
| 8 |
import os
|
| 9 |
import sys
|
|
|
|
| 10 |
|
| 11 |
# Add src to path
|
| 12 |
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 13 |
|
| 14 |
-
from src.api import agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@asynccontextmanager
|
| 17 |
async def lifespan(app: FastAPI):
|
| 18 |
"""Startup and shutdown events"""
|
| 19 |
print("π LibBee starting up...")
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
yield
|
|
|
|
| 22 |
print("π Shutting down")
|
| 23 |
|
| 24 |
-
# Create app
|
| 25 |
app = FastAPI(
|
| 26 |
title="LibBee - KU Library AI",
|
| 27 |
description="Khalifa University Library AI Assistant",
|
|
@@ -29,13 +42,14 @@ app = FastAPI(
|
|
| 29 |
lifespan=lifespan
|
| 30 |
)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
app.add_middleware(
|
| 34 |
CORSMiddleware,
|
| 35 |
allow_origins=[
|
| 36 |
-
"https://nikeshn.github.io", # Your GitHub Pages
|
| 37 |
"http://localhost:8080",
|
| 38 |
-
"http://localhost:3000"
|
|
|
|
| 39 |
],
|
| 40 |
allow_credentials=True,
|
| 41 |
allow_methods=["POST", "GET", "OPTIONS"],
|
|
@@ -45,21 +59,31 @@ app.add_middleware(
|
|
| 45 |
|
| 46 |
# Include routers
|
| 47 |
app.include_router(agent.router, prefix="/agent", tags=["Agent"])
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
@app.get("/")
|
| 51 |
def health_check():
|
|
|
|
| 52 |
return {
|
| 53 |
"status": "ok",
|
| 54 |
"version": "2.0.0",
|
| 55 |
-
"
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
@app.get("/config")
|
| 59 |
def public_config():
|
| 60 |
"""Public configuration for frontend"""
|
| 61 |
return {
|
| 62 |
-
"welcome_message": "Hi! I'm LibBee, the Khalifa University Library AI Assistant.",
|
| 63 |
"default_model": "gpt",
|
| 64 |
-
"max_results": "5"
|
|
|
|
| 65 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
LibBee - KU Library AI Agent v2.0
|
| 3 |
+
Khalifa University Library AI Assistant
|
| 4 |
"""
|
| 5 |
from fastapi import FastAPI, HTTPException, Request
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from fastapi.responses import HTMLResponse
|
| 8 |
from contextlib import asynccontextmanager
|
| 9 |
import os
|
| 10 |
import sys
|
| 11 |
+
import json
|
| 12 |
|
| 13 |
# Add src to path
|
| 14 |
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 15 |
|
| 16 |
+
from src.api import agent, search, admin
|
| 17 |
+
from src.services.cache_service import CacheService
|
| 18 |
+
from src.config import get_settings
|
| 19 |
+
|
| 20 |
+
# Global cache instance (HF Space is single-instance, so in-memory is fine)
|
| 21 |
+
cache = CacheService()
|
| 22 |
|
| 23 |
@asynccontextmanager
|
| 24 |
async def lifespan(app: FastAPI):
|
| 25 |
"""Startup and shutdown events"""
|
| 26 |
print("π LibBee starting up...")
|
| 27 |
+
app.state.cache = cache
|
| 28 |
+
app.state.start_time = __import__('time').time()
|
| 29 |
+
|
| 30 |
+
# Load staff directory into cache
|
| 31 |
+
from src.services.staff_service import build_staff_index
|
| 32 |
+
build_staff_index()
|
| 33 |
+
|
| 34 |
yield
|
| 35 |
+
|
| 36 |
print("π Shutting down")
|
| 37 |
|
|
|
|
| 38 |
app = FastAPI(
|
| 39 |
title="LibBee - KU Library AI",
|
| 40 |
description="Khalifa University Library AI Assistant",
|
|
|
|
| 42 |
lifespan=lifespan
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Security: Restrict CORS to your GitHub Pages
|
| 46 |
app.add_middleware(
|
| 47 |
CORSMiddleware,
|
| 48 |
allow_origins=[
|
| 49 |
+
"https://nikeshn.github.io", # Your GitHub Pages
|
| 50 |
"http://localhost:8080",
|
| 51 |
+
"http://localhost:3000",
|
| 52 |
+
"http://127.0.0.1:5500"
|
| 53 |
],
|
| 54 |
allow_credentials=True,
|
| 55 |
allow_methods=["POST", "GET", "OPTIONS"],
|
|
|
|
| 59 |
|
| 60 |
# Include routers
|
| 61 |
app.include_router(agent.router, prefix="/agent", tags=["Agent"])
|
| 62 |
+
app.include_router(search.router, prefix="/search", tags=["Search"])
|
| 63 |
+
app.include_router(admin.router, prefix="/admin", tags=["Admin"])
|
| 64 |
|
| 65 |
@app.get("/")
|
| 66 |
def health_check():
|
| 67 |
+
import time
|
| 68 |
return {
|
| 69 |
"status": "ok",
|
| 70 |
"version": "2.0.0",
|
| 71 |
+
"service": "LibBee KU Library AI",
|
| 72 |
+
"endpoints": ["/agent", "/search", "/admin", "/config"]
|
| 73 |
}
|
| 74 |
|
| 75 |
@app.get("/config")
|
| 76 |
def public_config():
|
| 77 |
"""Public configuration for frontend"""
|
| 78 |
return {
|
| 79 |
+
"welcome_message": "Hi! I'm LibBee, the Khalifa University Library AI Assistant. I can help you find articles and books, search databases, access full text, and answer library questions.",
|
| 80 |
"default_model": "gpt",
|
| 81 |
+
"max_results": "5",
|
| 82 |
+
"maintenance_mode": "false"
|
| 83 |
}
|
| 84 |
+
|
| 85 |
+
@app.get("/year")
|
| 86 |
+
def get_year():
|
| 87 |
+
from datetime import datetime
|
| 88 |
+
now = datetime.utcnow()
|
| 89 |
+
return {"year": now.year, "month": now.month, "date": now.strftime("%Y-%m-%d")}
|