| | """FastAPI application for HF Agent web interface - API ONLY MODE. |
| | |
| | This backend runs in API-only mode without serving static files. |
| | The frontend is hosted separately and communicates via HTTP/WebSocket. |
| | """ |
| |
|
| | import logging |
| | import os |
| | from contextlib import asynccontextmanager |
| |
|
| | from dotenv import load_dotenv |
| |
|
| | load_dotenv() |
| |
|
| | |
| | if not os.environ.get("HF_TOKEN") and os.environ.get("HF_ADMIN_TOKEN"): |
| | os.environ["HF_TOKEN"] = os.environ.get("HF_ADMIN_TOKEN") |
| |
|
| | from fastapi import FastAPI |
| | from fastapi.middleware.cors import CORSMiddleware |
| |
|
| | from routes.agent import router as agent_router |
| | from routes.auth import router as auth_router |
| |
|
| | |
| | logging.basicConfig( |
| | level=logging.INFO, |
| | format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| | ) |
| | logger = logging.getLogger(__name__) |
| |
|
| |
|
| | @asynccontextmanager |
| | async def lifespan(app: FastAPI): |
| | """Application lifespan handler.""" |
| | logger.info("Starting HF Agent backend (API-only mode)...") |
| | logger.info(f"CORS allowed origins: {os.environ.get('CORS_ORIGINS', '*')}") |
| | yield |
| | logger.info("Shutting down HF Agent backend...") |
| |
|
| |
|
| | app = FastAPI( |
| | title="HF Agent API", |
| | description="ML Engineering Assistant API - Separate Frontend/Backend Mode", |
| | version="1.0.0", |
| | lifespan=lifespan, |
| | ) |
| |
|
| | |
| | |
| | cors_origins = os.environ.get("CORS_ORIGINS", "*") |
| | if cors_origins != "*": |
| | cors_origins = [origin.strip() for origin in cors_origins.split(",")] |
| |
|
| | app.add_middleware( |
| | CORSMiddleware, |
| | allow_origins=cors_origins if isinstance(cors_origins, list) else ["*"], |
| | allow_credentials=True, |
| | allow_methods=["*"], |
| | allow_headers=["*"], |
| | ) |
| |
|
| | |
| | app.include_router(agent_router) |
| | app.include_router(auth_router) |
| |
|
| |
|
| | @app.get("/api") |
| | async def api_root(): |
| | """API root endpoint.""" |
| | return { |
| | "name": "HF Agent API", |
| | "version": "1.0.0", |
| | "mode": "api-only", |
| | "docs": "/docs", |
| | } |
| |
|
| |
|
| | @app.get("/") |
| | async def root(): |
| | """Root endpoint - indicates API-only mode.""" |
| | return { |
| | "status": "ok", |
| | "mode": "api-only", |
| | "message": "Backend is running in API-only mode. Frontend is hosted separately.", |
| | "api_docs": "/docs", |
| | "api_endpoints": "/api", |
| | } |
| |
|
| |
|
| | if __name__ == "__main__": |
| | import uvicorn |
| |
|
| | port = int(os.environ.get("PORT", 7860)) |
| | host = os.environ.get("HOST", "0.0.0.0") |
| | uvicorn.run(app, host=host, port=port) |
| |
|