Download app.py from amineelh/AgentApi: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/spaces/amineelh/AgentApi/resolve/main/app.py
- Command line
-
hf download hf://spaces/amineelh/AgentApi/app.py
-
curl -L -o app.py https://huggingface.co/spaces/amineelh/AgentApi/resolve/main/app.py
1.85 kB
| import logging | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from config import ( | |
| API_TITLE, | |
| API_DESCRIPTION, | |
| API_VERSION, | |
| ALLOWED_ORIGINS | |
| ) | |
| from routes.analyze import router as analyze_router | |
| from routes.health import router as health_router | |
| from routes.reports import router as reports_router | |
| from agents.agent_setup import initialize_agent | |
| # Logging setup | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # FastAPI setup | |
| app = FastAPI( | |
| title=API_TITLE, | |
| description=API_DESCRIPTION, | |
| version=API_VERSION | |
| ) | |
| # CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=ALLOWED_ORIGINS, | |
| allow_credentials=True, | |
| allow_methods=["GET", "POST"], | |
| allow_headers=["*"], | |
| ) | |
| # Include routers | |
| app.include_router(health_router, tags=["health"]) | |
| app.include_router(analyze_router, tags=["analysis"]) | |
| app.include_router(reports_router, tags=["reports"]) | |
| # Global agent instance | |
| mex_researcher = None | |
| async def startup_event(): | |
| """Initialize the application on startup""" | |
| global mex_researcher | |
| logger.info("Démarrage de l'API...") | |
| try: | |
| mex_researcher = initialize_agent() | |
| logger.info("API démarrée avec succès") | |
| except Exception as e: | |
| logger.error(f"Erreur lors de l'initialisation: {e}") | |
| raise | |
| # Make agent accessible to routes | |
| async def add_agent_to_request(request, call_next): | |
| request.state.agent = mex_researcher | |
| response = await call_next(request) | |
| return response | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "app:app", | |
| host="0.0.0.0", | |
| port=8000, | |
| reload=True, | |
| log_level="info" | |
| ) |