Skip to content
← Blog

Putting StyleFlow on the web with FastAPI, on CPU

The last of three posts on the Style Transfer project. The first two covered the architecture and the post-processing. This one covers the engineering that makes the model run on this site.

From Streamlit to a module in the API

The first version was a standalone Streamlit app behind NGINX: pick a style, upload a photo, press the button. When I brought my projects together on one personal site, I wanted Style Transfer to be part of it, sharing the UI, the gateway and the upload protections with everything else.

The model is now the app/style_transfer/ module of the FastAPI backend, laid out like RAG Chat:

FolderContents
model/Glow, SAN, VGG-19, ported from the original repo
infer.pyloading the model, running inference
postprocess.pythe pure tensor functions from the previous post
api/three HTTP endpoints
assets/weights and style images, not committed

The three endpoints:

  • GET /api/v1/style/status: whether the model is ready, the list of styles, upload limits. One call is enough to build the page.
  • GET /api/v1/style/styles/{id}/image: a style image, cached forever.
  • POST /api/v1/style/transfer: multipart with styleId, the photo, the strength and whether to smooth.

Porting the model without touching the weights

The checkpoint is loaded with strict=True: every key in the file must match exactly one parameter in the network and vice versa. One wrong name and it will not load. So I kept every class and attribute name from the original repo, including ones that break Python conventions such as AdaIN_SET, and also the layer order of VGG, because vgg_normalised.pth is keyed by position.

There is one deliberate change. The original used numpy and scipy for the random initialisation of the 1×1 convolution (a QR then an LU decomposition). I rewrote it with torch.linalg. Every tensor produced at that step is overwritten by the checkpoint, so only the shapes matter, and the API drops two dependencies.

Keeping the aspect ratio

The original app squashed every image to 512×512, distorting portrait and landscape photos alike. Glow is purely convolutional, so it runs at any size as long as both sides are divisible by 4 (two squeezes). Images are now scaled so the long side is 512, with both sides rounded down to a multiple of 4.

Style images keep their ratio too. During training, styles were 300×400 crops, not squares. Squashing a painting stretches its brush strokes, and the VGG mean/std, which is the style code, shifts with them.

Running on CPU

The server has no GPU. The Docker image installs CPU-only torch, so it does not pull gigabytes of CUDA libraries it would never use. Measured times:

  • about 22 seconds per image when run directly on a machine (network at 512 px, output at 1024 px);
  • about 40 to 75 seconds for the same code inside Docker Desktop on Windows.

At those numbers, several decisions make themselves:

  • Lazy loading. Torch is only imported on the first request. If torch or the weights are missing, the API still starts; only the Style Transfer page reports it is not ready.
  • Cached style codes. Preset style images never change, so each one's 1920-d code is computed once. Each request is then a single pass through Glow.
  • One image at a time. An asyncio.Semaphore(1) wraps inference. Two images sharing the same CPU do not finish sooner; they just both get slower.
  • No blocking the event loop. Inference runs in a threadpool, so the API keeps serving other pages while an image is processing.
  • Rate limiting: 12 requests per 10 minutes per IP, using the same mechanism as the login page.
  • Gateway timeout must be longer than 60 seconds, or the reverse proxy cuts the request before the image is done.

Trusting nothing users upload

Uploads are data from strangers, so:

  • The format is determined by actually decoding the image, not from the file name or Content-Type. Only JPEG, PNG, WebP and HEIC are accepted.
  • Decompression bombs are blocked: a 50-megapixel limit, and Pillow's bomb warning is treated as an error.
  • EXIF and GPS are stripped. The image is rotated according to EXIF, then its pixels are copied into a brand-new buffer. No metadata comes along.
  • Results delete themselves after 24 hours. Outputs are written to the media folder under a UUID name and cleaned up on later requests.

Weights stay out of git

The Glow checkpoint is 199 MB and VGG-19 is 76 MB. Neither is committed. The command

python -m app.cli style fetch

downloads them from the original repo's git-LFS and checks the sha256 of each file, so a truncated or tampered download is caught. In the Docker dev setup the weights folder on the machine is mounted straight in; production uses a dedicated volume, filled once.

Adding a new style just means dropping an image into assets/styles/. The file name becomes its label, with no code or database change.

Looking back

The model itself is almost unchanged from the original repo. Most of the work went into everything around it: keeping the aspect ratio, fixing artefacts caused by the architecture, living with 20 to 70 seconds per image on CPU, and treating every upload as untrusted.