Spaces:
Sleeping

File size: 4,976 Bytes
5738ae5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
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",
    )