From 01c815a17b8a0d80831ae60ed991f5e24154b541 Mon Sep 17 00:00:00 2001 From: paituo <330435863@qq.com> Date: Mon, 19 Aug 2024 08:26:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=85=A5=E5=8F=A3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/main.py | 87 +++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/backend/main.py b/backend/main.py index cbf7969..5f84c63 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,4 +1,5 @@ from dotenv import load_dotenv +from llama_index.core.node_parser import SentenceSplitter load_dotenv() @@ -13,55 +14,55 @@ from app.api.routers.upload import file_upload_router from app.settings import init_settings from app.observability import init_observability from fastapi.staticfiles import StaticFiles - +from phoenix.trace import using_project logger = logging.getLogger("uvicorn") -app = None -def init_webserver(): - global app - app = FastAPI() - environment = os.getenv("ENVIRONMENT", "dev") # Default to 'development' if not set - if environment == "dev": - logger.warning("Running in development mode - allowing CORS for all origins") - app.add_middleware( - CORSMiddleware, - allow_origins=["*"], - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], - ) +usPrj = using_project(os.getenv("PHOENIX_PROJECT_NAME")) +usPrj.__enter__() - def mount_static_files(directory, path): - if os.path.exists(directory): - for dir, _, _ in os.walk(directory): - relative_path = os.path.relpath(dir, directory) - mount_path = path if relative_path == "." else f"{path}/{relative_path}" - logger.info(f"Mounting static files '{dir}' at {mount_path}") - app.mount(mount_path, StaticFiles(directory=dir), name=f"{dir}-static") +init_settings() +init_observability() - # Mount the data files to serve the file viewer - mount_static_files("data", "/api/files/data") - # Mount the output files from tools - mount_static_files("data_output", "/api/files/output") - app.include_router(chat_router, prefix="/api/chat") - app.include_router(file_upload_router, prefix="/api/chat/upload") +app = FastAPI() - # Redirect to documentation page when accessing base URL - @app.get("/") - async def redirect_to_docs(): - return RedirectResponse(url="/docs") +environment = os.getenv("ENVIRONMENT", "dev") # Default to 'development' if not set +if environment == "dev": + logger.warning("Running in development mode - allowing CORS for all origins") + app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) +def mount_static_files(directory, path): + if os.path.exists(directory): + for dir, _, _ in os.walk(directory): + relative_path = os.path.relpath(dir, directory) + mount_path = path if relative_path == "." else f"{path}/{relative_path}" + logger.info(f"Mounting static files '{dir}' at {mount_path}") + app.mount(mount_path, StaticFiles(directory=dir), name=f"{dir}-static") + +# Mount the data files to serve the file viewer +mount_static_files("data", "/api/files/data") +# Mount the output files from tools +mount_static_files("data_output", "/api/files/output") +app.include_router(chat_router, prefix="/api/chat") +app.include_router(file_upload_router, prefix="/api/chat/upload") + +# Redirect to documentation page when accessing base URL +@app.get("/") +async def redirect_to_docs(): + return RedirectResponse(url="/docs") + +SentenceSplitter if __name__ == "__main__": - from phoenix.trace import using_project - with using_project(os.getenv("PHOENIX_PROJECT_NAME")) as obj: + app_host = os.getenv("APP_HOST", "0.0.0.0") + app_port = int(os.getenv("APP_PORT", "8000")) + reload = True if environment == "dev" else False + reload = False + uvicorn.run(app="main:app", host=app_host, port=app_port, reload=reload) - init_settings() - init_observability() - init_webserver() - - app_host = os.getenv("APP_HOST", "0.0.0.0") - app_port = int(os.getenv("APP_PORT", "8000")) - #reload = True if environment == "dev" else False - reload = False - uvicorn.run(app=app, host=app_host, port=app_port, reload=reload) + #usPrj.__exit__()