from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse, HTMLResponse import uvicorn import logging import sys from src.api.routes.health import health_status from src.api.routes.isrunning import is_running # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.StreamHandler(sys.stdout) ] ) logger = logging.getLogger(__name__) app = FastAPI( title="AdvisorAI Data API", description="API for AdvisorAI data pipeline and health monitoring", version="1.0.0" ) # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.exception_handler(Exception) async def global_exception_handler(request, exc): logger.error(f"Global exception handler caught: {exc}", exc_info=True) return JSONResponse( status_code=500, content={"detail": "Internal server error", "error": str(exc)} ) @app.get('/health') def health(): """Enhanced health check endpoint""" try: return health_status() except Exception as e: logger.error(f"Health check failed: {e}", exc_info=True) raise HTTPException(status_code=500, detail=f"Health check failed: {str(e)}") # Route to check if there are any JSON files under data/merged/features (relative path) @app.get('/status') def status(): """Check if the data pipeline is running and has recent data""" try: return is_running() except Exception as e: logger.error(f"Status check failed: {e}", exc_info=True) raise HTTPException(status_code=500, detail=f"Status check failed: {str(e)}") @app.get('/', response_class=HTMLResponse) def root(): """Root endpoint returns simple HTML so HF Spaces iframe can render it.""" html = """ AdvisorAI Data API

AdvisorAI Data API

Service is running.

""" return HTMLResponse(content=html, status_code=200) @app.get('/api') def api_root(): """JSON root for programmatic clients.""" return { "message": "AdvisorAI Data API", "version": "1.0.0", "endpoints": { "/health": "Health check with system metrics", "/status": "Data pipeline status", "/api": "This JSON endpoint", "/": "HTML landing page for Spaces" } } if __name__ == "__main__": uvicorn.run( "src.api.main:app", host="0.0.0.0", port=10000, workers=1, timeout_keep_alive=30, access_log=True )