FLUX 3 Image Klein
Black Forest Labs' step-distilled FLUX 3, sixteen steps to an image. It expands your prompt into a full scene caption before generating, and returns that caption with the result, so you can see what the image model actually read. A request can name a LoRA adapter to load on top, which is how an adapter trained for FLUX 3 Klein is served. Its weights are gated, so this one needs your own Hugging Face token stored as HF_TOKEN, and that is also the token a private adapter is fetched with.
What runs
This is the whole app. The entry names the model and what serves it, and the build turns that into these files: there is nothing else to write.
"""FLUX 3 Image Klein, generated from a declarative template."""
from __future__ import annotations
import base64
import io
from pydantic import BaseModel, ConfigDict, Field
from runware_serverless import serve
MODEL = "black-forest-labs/flux-3-image-klein"
REVISION = "09561231f44e41381c461caf53d5452005a60ac9"
CACHE = "/weights"
DEFAULT_STEPS = 16
ADAPTER = "requested"
DEFAULT_WEIGHTS = "pytorch_lora_weights.safetensors"
class Request(BaseModel):
# An input this model does not read is a line the caller believed was doing
# something, so it is refused rather than dropped. Without this, pydantic
# ignores it: a width sent to a model of fixed size, or a misspelled seed,
# both come back as a result that silently answered a different request.
model_config = ConfigDict(extra="forbid")
prompt: str = Field(
description=(
"What to generate. A short request is enough: the pipeline expands it into a full"
" scene caption before the image model reads it."
),
)
lora: str | None = Field(
default=None,
description=(
"A Hugging Face repository holding a LoRA adapter for this model, as owner/name. Left"
" out, the model answers on its own. The adapter is fetched with the same token the"
" weights are, so a private one has to be reachable by it."
),
)
lora_weight_name: str = Field(
default=DEFAULT_WEIGHTS,
description="The file inside that repository, which is the name a training run writes.",
)
lora_scale: float = Field(
default=1.0,
ge=0.0,
le=2.0,
description=(
"How strongly the adapter applies. An adapter trained for fewer steps than its recipe"
" asks for is often worth more than 1 here, which costs nothing, while more steps do."
),
)
steps: int = Field(
default=DEFAULT_STEPS,
ge=4,
le=16,
description="The step-distilled checkpoint is trained for 4 to 16 steps.",
)
width: int = Field(default=1024, ge=256, le=2048, multiple_of=16)
height: int = Field(default=1024, ge=256, le=2048, multiple_of=16)
seed: int | None = Field(default=None, description="Repeat a result by repeating its seed.")
class Generated(BaseModel):
image: str = Field(description="The result as a base64 PNG.")
caption: str = Field(
description="The full scene caption the prompt upsampler wrote, which is what the image model read.",
)
lora: str | None
lora_scale: float
steps: int
seed: int | None
@serve
class GeneratedImage:
def load(self) -> None:
if hasattr(self, "pipeline"):
return
# Imported here rather than at the top of the file. The build imports
# this file with the platform's own dependencies, so nothing above
# exists until a worker runs.
import torch
from diffusers import ModularPipeline
self.torch = torch
self.loaded: str | None = None
# Two calls rather than one. from_pretrained builds the pipeline without
# reading any weights, and load_components is what fetches them, so a
# failure to resolve the repository and a failure to load it are
# separate and report separately.
self.pipeline = ModularPipeline.from_pretrained(
MODEL,
revision=REVISION,
cache_dir=CACHE,
)
self.pipeline.load_components(dtype=torch.bfloat16)
self.pipeline.to("cuda")
# The first run allocates and warms the kernels, so no request pays for
# it, and a failure lands in the readiness check where it is reported
# rather than in somebody's first call. Four steps, the low end of what
# this checkpoint takes, because the warmup only has to touch the path.
self.pipeline(
prompt="warmup",
num_inference_steps=4,
width=512,
height=512,
output="images",
)
def use_adapter(self, request: Request) -> None:
"""Make the transformer carry the adapter the request asked for, or none.
Switching costs a download and a merge, so an adapter already in place is
reused and only its scale is set again. A modular pipeline is not a LoRA
loader, so this reaches the transformer component and loads onto that.
"""
transformer = self.pipeline.transformer
wanted = None if request.lora is None else request.lora + ":" + request.lora_weight_name
if wanted != self.loaded:
if self.loaded is not None:
transformer.delete_adapters(ADAPTER)
self.loaded = None
if wanted is not None:
transformer.load_lora_adapter(
request.lora,
weight_name=request.lora_weight_name,
prefix="transformer",
adapter_name=ADAPTER,
cache_dir=CACHE,
)
self.loaded = wanted
if self.loaded is not None:
transformer.set_adapters([ADAPTER], [request.lora_scale])
def predict(self, request: Request) -> Generated:
self.use_adapter(request)
generator = None
if request.seed is not None:
generator = self.torch.Generator(device="cuda").manual_seed(request.seed)
# A modular pipeline returns its whole state and you name what you want
# out of it, rather than an object with an images attribute.
result = self.pipeline(
prompt=request.prompt,
num_inference_steps=request.steps,
width=request.width,
height=request.height,
generator=generator,
output=["images", "prompt"],
)
image = result["images"][0]
caption = result["prompt"]
buffer = io.BytesIO()
image.save(buffer, format="PNG")
return Generated(
image=base64.b64encode(buffer.getvalue()).decode("ascii"),
caption=caption,
lora=request.lora,
lora_scale=request.lora_scale,
steps=request.steps,
seed=request.seed,
)configVersion: 1
id: flux-3-klein
name: FLUX 3 Image Klein
source:
type: code
entry: model.py
compute:
gpuType: rtx-pro-6000
build:
requirements:
- torch==2.14.1
- torchvision==0.29.1
- diffusers==0.42.1
- transformers==5.21.0
- accelerate==1.15.0
- peft==0.21.3
volumes:
- /weights
secrets:
- name: HF_TOKENDeploying
Deploy it as it is, or take the files above and deploy your own copy. Nothing you change reaches back to this page.
runware serverless deploy --from https://runware.ai/docs/serverless/examples/flux-3-klein.ziprunware serverless deploy model.py \
--id flux-3-klein \
--gpu-type rtx-pro-6000 \
--requirement torch==2.14.1 \
--requirement torchvision==0.29.1 \
--requirement diffusers==0.42.1 \
--requirement transformers==5.21.0 \
--requirement accelerate==1.15.0 \
--requirement peft==0.21.3 \
--volume /weights \
--secret HF_TOKENCalling it
One request that works, against the app you just deployed. The fields are the ones the entry declares above, and anything it does not declare is refused rather than ignored.
import { createClient } from '@runware/sdk'
const client = await createClient({ apiKey: process.env.RUNWARE_API_KEY })
const task = await client.invoke({
appId: 'flux-3-klein',
endpointPath: 'predict',
payload: {
request: {
prompt: 'A paper boat drifting down a rain gutter, close up'
}
}
})import asyncio
import os
from runware import Runware
async def main():
client = Runware(api_key=os.environ["RUNWARE_API_KEY"])
try:
task = await client.invoke(
"flux-3-klein",
"predict",
{
"request": {
"prompt": "A paper boat drifting down a rain gutter, close up"
}
},
)
finally:
await client.close()
asyncio.run(main())curl https://api.serverless.runware.ai/v1/apps/flux-3-klein/invoke-async/predict \
-H "Authorization: Bearer $RUNWARE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"taskId": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
"payload": {
"request": {
"prompt": "A paper boat drifting down a rain gutter, close up"
}
}
}'cat <<'EOF' | runware serverless apps invoke flux-3-klein predict -f -
{
"request": {
"prompt": "A paper boat drifting down a rain gutter, close up"
}
}
EOF{
"taskId": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
"payload": {
"request": {
"prompt": "A paper boat drifting down a rain gutter, close up"
}
}
}What it costs
This entry is pinned to rtx-pro-6000, which is the card it was tested on rather than the only one it runs on: --gpu-typedeploys it somewhere else. You pay for that card by the second while a worker holds it, and for nothing else. Pricing covers what meters, and the rates are published per GPU-second.