Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request | |
| from contextlib import asynccontextmanager | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from app.routes import router as api_router | |
| from app.model import load_model | |
| from dotenv import load_dotenv | |
| import os | |
| import time | |
| import logging | |
| load_dotenv() | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| raw_repo_id = os.getenv("MODEL_REPO_ID", "") | |
| # Default to a highly capable model that is natively supported and FREE on Hugging Face Serverless InferenceAPI | |
| REPO_ID = raw_repo_id.strip() if raw_repo_id and raw_repo_id.strip() else "Qwen/Qwen3.5-0.8B" | |
| async def lifespan(app: FastAPI): | |
| print("\n" + "="*60, flush=True) | |
| print(f"π INITIALIZING CHAT API: Setup remote LLM ({REPO_ID})", flush=True) | |
| print("="*60 + "\n", flush=True) | |
| # Store the LLM in the application state so routes can access it | |
| app.state.llm = load_model(repo_id=REPO_ID) | |
| print("\nβ API is LIVE on port 7860! Ready for requests.\n", flush=True) | |
| yield | |
| print("\n" + "="*60, flush=True) | |
| print("π Shutting down API. Goodbye!", flush=True) | |
| app = FastAPI( | |
| title="Multimodal Chat API", | |
| description="Production-ready Chat API powered by LangChain and HuggingFaceEndpoint.", | |
| version="1.0.0", | |
| lifespan=lifespan, | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=os.getenv("CORS_ORIGINS", "*").split(","), | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| async def request_logging_middleware(request: Request, call_next): | |
| start = time.time() | |
| response = await call_next(request) | |
| duration_ms = (time.time() - start) * 1000 | |
| if request.url.path != "/api/health": | |
| logger.info( | |
| f"{request.method} {request.url.path} " | |
| f"β {response.status_code} [{duration_ms:.1f}ms]" | |
| ) | |
| return response | |
| app.include_router(api_router, prefix="/api") | |
| async def root(): | |
| return { | |
| "message": "Welcome to the API!", | |
| "docs_url": "/docs", | |
| "endpoints": { | |
| "health": "/api/health", | |
| "chat": "/api/chat" | |
| } | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "app.main:app", | |
| host="0.0.0.0", | |
| port=7860, | |
| workers=1, | |
| log_level="info", | |
| ) | |