Spaces:
Running on Zero

Add Black Forest Labs content moderation

#23
by alexanderwangus - opened
Black Forest Labs org

Why

This demo generates images from any prompt and uploaded image without a content filter. This PR adds Black Forest Labs' moderation to every generation.

What changes

  • At startup, the Space downloads the shared client bfl_moderation.py from black-forest-labs/hf-spaces-moderation-client.
  • Inside the @spaces.GPU function, the prompt and any input images are checked while the image generates, and the finished image is checked before it is returned. A blocked request shows "This request was blocked by our content filter." and returns no image.
  • If the input check cannot run, the request continues, since the output check still runs. If the output check cannot run, the request fails with "The content filter is unavailable, please try again later."
  • The checks run inside the GPU function, so they count against the caller's ZeroGPU quota: get_duration adds 10 s, and generate_image asks for a full GPU (size="xlarge").
  • generate_image only received prompt embeddings, so it now also gets prompt and final_prompt. The input check sees what the user typed; the output check sees the prompt the model was given.
  • Pins torchao==0.17.0. torchao 0.18 imports ScalingType, which only exists in torch โ‰ฅ 2.10, and this Space pins torch==2.8.0, so any rebuild crashed at from diffusers import Flux2Pipeline.
  • Full GPU (size="xlarge"). A rebuilt Space gets a 47 GB GPU slice and the transformer alone is 60 GiB, so every call failed in ZeroGPU's weight packing (NVML_SUCCESS == r INTERNAL ASSERT FAILED), with or without this PR's moderation code. The running build predates this. Each call now spends 2ร— ZeroGPU quota.

The API key comes from the BFL_MODERATION_KEY Space secret. It is added right before this PR merges: without it, every request gets "unavailable".

Testing

On a private copy rebuilt from this PR: benign text-to-image, 1-reference and 3-reference edits โ†’ image (31.2 s, 39.7 s, 94.8 s); explicit prompt and gory edit โ†’ blocked.

alexanderwangus changed pull request status to merged

Sign up or log in to comment