stsachit's picture
Add pinned BF16 FLUX.3 Klein deployment subset and serving handlers
4b33f36 verified
Raw History Blame Contribute Delete
1.23 kB
"""Authenticated HTTP wrapper; Modal queues requests to one resident GPU worker."""
import hmac
import os
from fastapi import Depends, FastAPI, Header, HTTPException
from flux3_engine import GenerationRequest, LegacyRequest
def create_app(engine, startup_error=None):
token = os.environ.get('FLUX_API_TOKEN')
if not token:
raise RuntimeError('FLUX_API_TOKEN must be set for the cloud API')
def authorize(authorization: str | None = Header(default=None)):
if not hmac.compare_digest((authorization or '').encode(), f'Bearer {token}'.encode()):
raise HTTPException(401, 'Invalid bearer token')
app = FastAPI(title='FLUX.3 Klein', dependencies=[Depends(authorize)])
def ready():
if startup_error or not engine or engine.pipe is None:
raise HTTPException(503, startup_error or 'Model not ready')
@app.get('/health')
def health():
ready()
return engine.health()
@app.post('/generate')
def generate(request: GenerationRequest):
ready()
return engine.generate(request)
@app.post('/sdxl')
def legacy(request: LegacyRequest):
ready()
return engine.legacy(request.model_dump())
return app