File size: 1,577 Bytes
46cdd2b fc4b393 e513185 46cdd2b fc4b393 46cdd2b 8a73f54 e513185 ee791ea 46cdd2b fc4b393 e513185 fc4b393 46cdd2b e513185 46cdd2b fc4b393 8a73f54 ee791ea 8a73f54 | 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 60 | from __future__ import annotations
from pathlib import Path
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, RedirectResponse
from src.app.api.router import api_router
from src.app.runtime import preload_runtime_on_startup
from src.app.settings import settings
from src.rag.bm25_service import load_or_build_bm25
import os
app = FastAPI(title="Legislation RAG", version="0.2.0")
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router, prefix="/api")
UI_INDEX = Path(__file__).resolve().parent / "statistic" / "index.html"
@app.get("/")
def root():
if UI_INDEX.exists():
return FileResponse(UI_INDEX)
return RedirectResponse(url="/docs")
@app.get("/health")
def health():
return {
"status": "ok",
"collection": settings.qdrant_collection,
}
@app.on_event("startup")
async def startup_event():
print("🚀 Server starting...")
bm25_path = settings.bm25_stats_path # outputs/bm25/bm25_stats.json
# Nếu chưa có → build
if not os.path.exists(bm25_path):
print("⚠️ BM25 stats not found → building...")
load_or_build_bm25(force_rebuild=True)
print("✅ BM25 built successfully!")
else:
print("✅ BM25 stats loaded")
preload_runtime_on_startup(
preload_graph_snapshot=True,
build_graph_if_missing=True,
warm_services=True,
) |