hf-image-api / main.py
kajalan's picture
Upload 14 files (#1)
5738ae5
Raw History Blame Contribute Delete
4.98 kB
"""
main.py β€” FastAPI application entry point.
Startup sequence
────────────────
1. Worker thread is started (daemon thread β€” auto-stops with the process).
2. If AUTO_LOAD_MODEL=true, the HuggingFace pipeline is loaded in the
background so the first job doesn't include cold-start download time.
3. The HTTP server starts accepting requests.
Run with:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
"""
from __future__ import annotations
import logging
import os
import threading
from contextlib import asynccontextmanager
import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from config import settings
from routes import generate, health, jobs
from worker import start_worker
# ── Logging ───────────────────────────────────────────────────────────────────
logging.basicConfig(
level=logging.DEBUG if settings.debug else logging.INFO,
format="%(asctime)s %(levelname)-8s %(name)s %(message)s",
datefmt="%H:%M:%S",
)
logger = logging.getLogger("main")
# ── Lifespan (startup / shutdown) ─────────────────────────────────────────────
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("═" * 60)
logger.info(" HF Image Generation API β€” starting up")
logger.info(" Model ID : %s", settings.model_id)
logger.info(" Outputs dir: %s", settings.outputs_dir)
logger.info(" CPU threads: %s", settings.cpu_threads or os.cpu_count())
logger.info("═" * 60)
# 1. Start background worker thread
start_worker()
logger.info("Background worker started.")
# 2. Optionally pre-load the pipeline
if settings.auto_load_model:
def _preload():
from pipeline_manager import pipeline_mgr
try:
pipeline_mgr.load()
except Exception as exc:
logger.exception("Pre-load failed: %s", exc)
t = threading.Thread(target=_preload, name="preload", daemon=True)
t.start()
logger.info("Pipeline pre-load started in background (AUTO_LOAD_MODEL=true).")
else:
logger.info("AUTO_LOAD_MODEL=false β€” pipeline will load on first job.")
yield # ← application runs here
logger.info("Shutting down.")
# ── App ───────────────────────────────────────────────────────────────────────
app = FastAPI(
title="HF CPU Image Generation API",
description=(
"Job-based REST API for Stable Diffusion image generation on CPU "
"using Hugging Face Diffusers. "
"Submit a job β†’ poll status β†’ download image β†’ optionally delete."
),
version="1.0.0",
lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc",
)
# CORS β€” allow everything by default (restrict in production)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ── Global exception handler ───────────────────────────────────────────────────
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logger.exception("Unhandled error on %s %s: %s", request.method, request.url.path, exc)
return JSONResponse(
status_code=500,
content={"detail": "An internal server error occurred.", "error": str(exc)},
)
# ── Root redirect to docs ──────────────────────────────────────────────────────
@app.get("/", include_in_schema=False)
async def root():
from fastapi.responses import RedirectResponse
return RedirectResponse(url="/docs")
# ── Mount routers ──────────────────────────────────────────────────────────────
app.include_router(health.router)
app.include_router(generate.router)
app.include_router(jobs.router)
# ── Dev entrypoint ─────────────────────────────────────────────────────────────
if __name__ == "__main__":
uvicorn.run(
"main:app",
host=settings.host,
port=settings.port,
reload=settings.debug,
log_level="debug" if settings.debug else "info",
)