-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (46 loc) · 1.46 KB
/
main.py
File metadata and controls
59 lines (46 loc) · 1.46 KB
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
from contextlib import asynccontextmanager
import asyncio
from loguru import logger as log
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from routes.swaggerui import setupSwaggerUI
from routes.file import router as file_router
from routes.search import router as search_router
from log import log_init
from load_pdf import load_all_pdfs
log_init()
@asynccontextmanager
async def lifespan(app: FastAPI):
# init_embedding_db()
log.info("FastAPI application is starting up...")
# 在应用对外可用之前,先在后台线程执行 PDF 加载与入库操作
try:
# await asyncio.get_running_loop().run_in_executor(None, load_all_pdfs)
log.info("load_pdf completed before FastAPI startup.")
except Exception as e:
log.exception("Error while running load_all_pdfs before startup: {}", e)
yield
# after the application stops
log.info("FastAPI application is shutting down.")
pass
app = FastAPI(
title="KB API",
version="1.0.0",
description="",
lifespan=lifespan,
docs_url=None,
redoc_url=None,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
setupSwaggerUI(app)
app.include_router(file_router, tags=["文件管理"])
app.include_router(search_router, tags=["知识检索"])
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)