File size: 1,483 Bytes
fa69b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import random
import torch
import torch.nn.functional as F
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.responses import Response
from torchvision.utils import save_image

from train import FlowMatchingUNet, sample_images, DEVICE

model = FlowMatchingUNet()


def load_model():
    ckpt = torch.load("weights/checkpoint_latest.pth", map_location="cpu", weights_only=True)
    model.load_state_dict(ckpt["ema"])
    model.to(DEVICE)
    model.eval()


def generate_png(steps: int = 20) -> bytes:
    """Generate one image and return it as PNG bytes."""
    with torch.no_grad():
        samples = sample_images(model, num_samples=1, steps=steps)
        big = F.interpolate(samples, scale_factor=2, mode="bicubic", align_corners=False)
        big = big.clamp(0, 1)
        buffer = io.BytesIO()
        save_image(big.cpu(), buffer, format="png", nrow=1)
        return buffer.getvalue()


@asynccontextmanager
async def lifespan(app: FastAPI):
    load_model()
    yield


app = FastAPI(lifespan=lifespan)


@app.get("/", response_class=Response)
async def get_image():
    try:
        png = generate_png()
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Generation failed: {e}")
    return Response(content=png, media_type="image/png")


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)