Download run.py from YesWhyNot/alosha-painter: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/YesWhyNot/alosha-painter/resolve/main/run.py
- Command line
-
hf download hf://YesWhyNot/alosha-painter/run.py
-
curl -L -o run.py https://huggingface.co/YesWhyNot/alosha-painter/resolve/main/run.py
1.48 kB
| 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() | |
| async def lifespan(app: FastAPI): | |
| load_model() | |
| yield | |
| app = FastAPI(lifespan=lifespan) | |
| 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) |