70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
|
|
from dotenv import load_dotenv
|
|
from llama_index.core.node_parser import SentenceSplitter
|
|
|
|
load_dotenv()
|
|
|
|
import logging
|
|
import os
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import RedirectResponse
|
|
from app.api.routers.chat import chat_router
|
|
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")
|
|
|
|
|
|
usPrj = using_project(os.getenv("PHOENIX_PROJECT_NAME"))
|
|
usPrj.__enter__()
|
|
|
|
|
|
init_settings()
|
|
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")
|
|
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")
|
|
|
|
@app.get("/")
|
|
async def redirect_to_docs():
|
|
return RedirectResponse(url="/docs")
|
|
|
|
if __name__ == "__main__":
|
|
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)
|
|
|