调整入口代码结构
This commit is contained in:
+27
-26
@@ -1,4 +1,5 @@
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from llama_index.core.node_parser import SentenceSplitter
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@@ -13,16 +14,20 @@ from app.api.routers.upload import file_upload_router
|
|||||||
from app.settings import init_settings
|
from app.settings import init_settings
|
||||||
from app.observability import init_observability
|
from app.observability import init_observability
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
from phoenix.trace import using_project
|
||||||
|
|
||||||
logger = logging.getLogger("uvicorn")
|
logger = logging.getLogger("uvicorn")
|
||||||
app = None
|
|
||||||
|
|
||||||
def init_webserver():
|
usPrj = using_project(os.getenv("PHOENIX_PROJECT_NAME"))
|
||||||
global app
|
usPrj.__enter__()
|
||||||
app = FastAPI()
|
|
||||||
environment = os.getenv("ENVIRONMENT", "dev") # Default to 'development' if not set
|
init_settings()
|
||||||
if environment == "dev":
|
init_observability()
|
||||||
|
|
||||||
|
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")
|
logger.warning("Running in development mode - allowing CORS for all origins")
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
@@ -32,7 +37,7 @@ def init_webserver():
|
|||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
|
||||||
def mount_static_files(directory, path):
|
def mount_static_files(directory, path):
|
||||||
if os.path.exists(directory):
|
if os.path.exists(directory):
|
||||||
for dir, _, _ in os.walk(directory):
|
for dir, _, _ in os.walk(directory):
|
||||||
relative_path = os.path.relpath(dir, directory)
|
relative_path = os.path.relpath(dir, directory)
|
||||||
@@ -40,28 +45,24 @@ def init_webserver():
|
|||||||
logger.info(f"Mounting static files '{dir}' at {mount_path}")
|
logger.info(f"Mounting static files '{dir}' at {mount_path}")
|
||||||
app.mount(mount_path, StaticFiles(directory=dir), name=f"{dir}-static")
|
app.mount(mount_path, StaticFiles(directory=dir), name=f"{dir}-static")
|
||||||
|
|
||||||
# Mount the data files to serve the file viewer
|
# Mount the data files to serve the file viewer
|
||||||
mount_static_files("data", "/api/files/data")
|
mount_static_files("data", "/api/files/data")
|
||||||
# Mount the output files from tools
|
# Mount the output files from tools
|
||||||
mount_static_files("data_output", "/api/files/output")
|
mount_static_files("data_output", "/api/files/output")
|
||||||
app.include_router(chat_router, prefix="/api/chat")
|
app.include_router(chat_router, prefix="/api/chat")
|
||||||
app.include_router(file_upload_router, prefix="/api/chat/upload")
|
app.include_router(file_upload_router, prefix="/api/chat/upload")
|
||||||
|
|
||||||
# Redirect to documentation page when accessing base URL
|
# Redirect to documentation page when accessing base URL
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def redirect_to_docs():
|
async def redirect_to_docs():
|
||||||
return RedirectResponse(url="/docs")
|
return RedirectResponse(url="/docs")
|
||||||
|
|
||||||
|
SentenceSplitter
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from phoenix.trace import using_project
|
|
||||||
with using_project(os.getenv("PHOENIX_PROJECT_NAME")) as obj:
|
|
||||||
|
|
||||||
init_settings()
|
|
||||||
init_observability()
|
|
||||||
init_webserver()
|
|
||||||
|
|
||||||
app_host = os.getenv("APP_HOST", "0.0.0.0")
|
app_host = os.getenv("APP_HOST", "0.0.0.0")
|
||||||
app_port = int(os.getenv("APP_PORT", "8000"))
|
app_port = int(os.getenv("APP_PORT", "8000"))
|
||||||
#reload = True if environment == "dev" else False
|
reload = True if environment == "dev" else False
|
||||||
reload = False
|
reload = False
|
||||||
uvicorn.run(app=app, host=app_host, port=app_port, reload=reload)
|
uvicorn.run(app="main:app", host=app_host, port=app_port, reload=reload)
|
||||||
|
|
||||||
|
#usPrj.__exit__()
|
||||||
|
|||||||
Reference in New Issue
Block a user