Spaces:
Sleeping
Sleeping
Download src/autodata_agent/api/app.py from LostDecimal/adaa-agent-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.37 kB
-
https://huggingface.co/spaces/LostDecimal/adaa-agent-backend/resolve/main/src/autodata_agent/api/app.py
- Command line
-
hf download hf://spaces/LostDecimal/adaa-agent-backend/src/autodata_agent/api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/LostDecimal/adaa-agent-backend/resolve/main/src/autodata_agent/api/app.py
2.37 kB
| from __future__ import annotations | |
| from fastapi import FastAPI, Request | |
| from fastapi.exceptions import RequestValidationError | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse | |
| from autodata_agent.api.routes import router | |
| from autodata_agent.core.config import get_settings | |
| from autodata_agent.core.errors import AppError | |
| def _error_payload(code: str, message: str, details: dict | list | None = None) -> dict: | |
| return {"error": {"code": code, "message": message, "details": details or {}}} | |
| def create_app() -> FastAPI: | |
| settings = get_settings() | |
| app = FastAPI( | |
| title="Autonomous Data Analysis Agent Backend", | |
| version="0.1.0", | |
| description=( | |
| "Backend API for data ingestion, autonomous analysis, visualization specs, " | |
| "and history." | |
| ), | |
| ) | |
| if settings.cors_origins: | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.cors_origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| app.include_router(router, prefix="/api/v1") | |
| async def app_error_handler(_: Request, exc: AppError) -> JSONResponse: | |
| return JSONResponse( | |
| status_code=exc.status_code, | |
| content=_error_payload(exc.code, exc.message, exc.details), | |
| ) | |
| async def validation_error_handler(_: Request, exc: RequestValidationError) -> JSONResponse: | |
| return JSONResponse( | |
| status_code=422, | |
| content=_error_payload( | |
| "request_validation_failed", | |
| "Request validation failed.", | |
| exc.errors(), | |
| ), | |
| ) | |
| async def unhandled_error_handler(_: Request, exc: Exception) -> JSONResponse: | |
| return JSONResponse( | |
| status_code=500, | |
| content={ | |
| "error": { | |
| "code": "internal_server_error", | |
| "message": "An unexpected backend error occurred.", | |
| "details": {"reason": str(exc)}, | |
| } | |
| }, | |
| ) | |
| return app | |
| app = create_app() | |