Fibo 2.0 dev
Bria's 8.1B image model, which reads a structured caption rather than a sentence, so what the image is made of is a field you set rather than a phrase you hope lands. The same weights also edit, with up to five reference images, a mask, and text rendered from fonts it was trained against. Its weights are gated, so this one needs your own Hugging Face token stored as HF_TOKEN.
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.
"""Fibo 2.0 dev, generated from a declarative template."""
from __future__ import annotations
import base64
import io
import json
from typing import Any
from pydantic import BaseModel, ConfigDict, Field, model_validator
from runware_serverless import serve
MODEL = "briaai/Fibo-2.0-dev"
REVISION = "b01e5a34a0a9540c65f585c2c281535deabac16d"
CACHE = "/weights"
DEFAULT_STEPS = 30
DEFAULT_GUIDANCE = 5
# The two fonts training drew text images with, which the weights repository
# ships beside the model. Without them, a character the built-in font lacks is
# drawn as that font draws it and the pipeline warns.
FONTS = ("Aileron-Regular.otf", "DejaVuSans.ttf")
# Up to five images can be edited together, and the instruction refers to them in
# order as the pipeline's own markers.
MAX_IMAGES = 5
class Objects(BaseModel):
model_config = ConfigDict(extra="forbid")
description: str
location: str | None = Field(default=None, description="Where in the frame, for example \"center\".")
relative_size: str | None = Field(default=None, description="For example \"large within frame\".")
class Text(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field(description="One run of text to render in the image.")
class Caption(BaseModel):
"""What Fibo 2.0 reads instead of a sentence.
Every field is optional and the ones left out are dropped before the model
sees them, so a caption can be as short as a description. The three mappings
are open rather than typed: Fibo 2.0 was trained on captions as JSON and
reads whatever keys one carries, so fixing a vocabulary here would refuse
keys the model understands.
"""
model_config = ConfigDict(extra="forbid")
short_description: str | None = Field(
default=None,
description="The scene in one sentence. On its own it is already a caption the model was trained on.",
)
objects: list[Objects] | None = Field(default=None, description="What is in the frame, one entry each.")
background_setting: str | None = None
lighting: dict[str, str] | None = Field(
default=None,
description="For example conditions, direction and shadows.",
)
aesthetics: dict[str, str] | None = Field(
default=None,
description="For example composition, color_scheme and mood_atmosphere.",
)
photographic_characteristics: dict[str, str] | None = Field(
default=None,
description="For example depth_of_field, focus and camera_angle.",
)
style_medium: str | None = Field(default=None, description="For example photograph, or illustration.")
text_render: list[Text] | None = Field(
default=None,
description=(
"Text to render in the image. An empty list is not the same as leaving this out: it tells"
" the model the image has no text."
),
)
context: str | None = None
artistic_style: str | None = None
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")
caption: Caption | None = Field(
default=None,
description=(
"What to generate. Required on its own, and optional beside an instruction, where it"
" describes the edited result rather than the image sent in."
),
)
instruction: str | None = Field(
default=None,
description=(
"What to change, in plain words. Only with images, which it can refer to in order as"
" image_1, image_2 and so on wrapped in angle brackets."
),
)
images: list[str] | None = Field(
default=None,
description="Up to five base64 images to edit. Without any, the model generates from the caption.",
)
mask: str | None = Field(
default=None,
description=(
"A base64 mask, white on black and the size of the image, marking the region to"
" regenerate. Only with exactly one image."
),
)
width: int | None = Field(default=None, ge=256, le=2048, multiple_of=16)
height: int | None = Field(default=None, ge=256, le=2048, multiple_of=16)
steps: int = Field(default=DEFAULT_STEPS, ge=1, le=100)
guidance: float = Field(default=DEFAULT_GUIDANCE, ge=1.0, le=20.0)
seed: int | None = Field(default=None, description="Repeat a result by repeating its seed.")
@model_validator(mode="after")
def _coherent(self) -> "Request":
# Refused here rather than left to the pipeline, which answers a
# generation with no caption by generating from an empty prompt: a
# plausible image that nobody asked for.
if not self.images:
if self.instruction is not None:
raise ValueError("instruction needs images to edit; leave it out to generate")
if self.mask is not None:
raise ValueError("mask needs the image it masks")
if self.caption is None:
raise ValueError("caption is required to generate")
else:
if len(self.images) > MAX_IMAGES:
raise ValueError(f"at most {MAX_IMAGES} images, got {len(self.images)}")
if self.instruction is None:
raise ValueError("instruction is required to edit")
if self.mask is not None and len(self.images) != 1:
raise ValueError("mask applies to a single image")
return self
class Generated(BaseModel):
image: str = Field(description="The result as a base64 PNG.")
width: int
height: int
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 BriaFibo2Pipeline
from huggingface_hub import hf_hub_download
self.torch = torch
self.pipeline = BriaFibo2Pipeline.from_pretrained(
MODEL,
revision=REVISION,
dtype=torch.bfloat16,
cache_dir=CACHE,
# Each component lands on the card as it loads, rather than the
# pipeline being assembled on the host and moved. A worker's host RAM
# is half its card's VRAM, so moving a built pipeline has a ceiling
# well below the card's, and the kill arrives as the container's exit
# 137 rather than as a CUDA error.
device_map="cuda",
)
# Fetched at load rather than per request, and from the weights
# repository rather than the image, so the text in a result is drawn with
# the fonts the model was trained against.
self.fonts = [
hf_hub_download(MODEL, name, subfolder="fonts", revision=REVISION, cache_dir=CACHE)
for name in FONTS
]
# The first run compiles kernels and allocates, so no request pays for
# it. It also moves a failure into the readiness check, where it is
# reported, rather than into somebody's first call.
self.pipeline(
prompt=json.dumps({"short_description": "warmup"}),
num_inference_steps=DEFAULT_STEPS,
guidance_scale=DEFAULT_GUIDANCE,
)
def predict(self, request: Request) -> Generated:
generator = None
if request.seed is not None:
generator = self.torch.Generator(device="cuda").manual_seed(request.seed)
images = [self._image(encoded) for encoded in request.images or []]
extra: dict[str, Any] = {}
if images:
extra["image"] = images if len(images) > 1 else images[0]
if request.mask is not None:
extra["mask"] = self._image(request.mask).convert("L")
if request.width is not None:
extra["width"] = request.width
if request.height is not None:
extra["height"] = request.height
image = self.pipeline(
prompt=self._prompt(request),
num_inference_steps=request.steps,
guidance_scale=request.guidance,
generator=generator,
fonts=self.fonts,
**extra,
).images[0]
buffer = io.BytesIO()
image.save(buffer, format="PNG")
return Generated(
image=base64.b64encode(buffer.getvalue()).decode("ascii"),
width=image.width,
height=image.height,
steps=request.steps,
seed=request.seed,
)
@staticmethod
def _prompt(request: Request) -> str:
"""The caption as the model reads it, which is JSON rather than prose.
Serialized compactly and without escaping, as training did. Empty values
are left in: the pipeline drops them itself, and dropping them here would
take an empty text_render with them, which is the one empty the model
reads as meaning the image has no text.
"""
caption = request.caption.model_dump(exclude_none=True) if request.caption else {}
if request.instruction is not None:
caption["edit_instruction"] = request.instruction
return json.dumps(caption, separators=(",", ":"), ensure_ascii=False)
@staticmethod
def _image(encoded: str) -> Any:
from PIL import Image
# Decoded to pixels here rather than passed along as base64. The pipeline
# takes an image, and a URL would be this worker reaching out
# mid-request.
return Image.open(io.BytesIO(base64.b64decode(encoded))).convert("RGB")configVersion: 1
id: fibo-2-0-dev
name: Fibo 2.0 dev
source:
type: code
entry: model.py
compute:
gpuType: rtx-pro-6000
build:
requirements:
- torch==2.14.1
- torchvision==0.29.1
- diffusers==0.43.0
- transformers==5.21.0
- huggingface-hub==2.4.0
- accelerate==1.15.0
- pillow==13.0.0
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/fibo-2-0-dev.ziprunware serverless deploy model.py \
--id fibo-2-0-dev \
--gpu-type rtx-pro-6000 \
--requirement torch==2.14.1 \
--requirement torchvision==0.29.1 \
--requirement diffusers==0.43.0 \
--requirement transformers==5.21.0 \
--requirement huggingface-hub==2.4.0 \
--requirement accelerate==1.15.0 \
--requirement pillow==13.0.0 \
--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: 'fibo-2-0-dev',
endpointPath: 'predict',
payload: {
request: {
caption: {
short_description: 'A red vintage bicycle leaning against a whitewashed wall on a narrow street.',
objects: [
{
description: 'A red vintage bicycle with a wicker basket of lemons.',
location: 'center'
}
],
background_setting: 'A whitewashed wall with a blue wooden door and potted geraniums.',
lighting: {
conditions: 'bright morning sun',
direction: 'from the right'
},
style_medium: 'photograph'
}
}
}
})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(
"fibo-2-0-dev",
"predict",
{
"request": {
"caption": {
"short_description": "A red vintage bicycle leaning against a whitewashed wall on a narrow street.",
"objects": [
{
"description": "A red vintage bicycle with a wicker basket of lemons.",
"location": "center"
}
],
"background_setting": "A whitewashed wall with a blue wooden door and potted geraniums.",
"lighting": {
"conditions": "bright morning sun",
"direction": "from the right"
},
"style_medium": "photograph"
}
}
},
)
finally:
await client.close()
asyncio.run(main())curl https://api.serverless.runware.ai/v1/apps/fibo-2-0-dev/invoke-async/predict \
-H "Authorization: Bearer $RUNWARE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"taskId": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
"payload": {
"request": {
"caption": {
"short_description": "A red vintage bicycle leaning against a whitewashed wall on a narrow street.",
"objects": [
{
"description": "A red vintage bicycle with a wicker basket of lemons.",
"location": "center"
}
],
"background_setting": "A whitewashed wall with a blue wooden door and potted geraniums.",
"lighting": {
"conditions": "bright morning sun",
"direction": "from the right"
},
"style_medium": "photograph"
}
}
}
}'cat <<'EOF' | runware serverless apps invoke fibo-2-0-dev predict -f -
{
"request": {
"caption": {
"short_description": "A red vintage bicycle leaning against a whitewashed wall on a narrow street.",
"objects": [
{
"description": "A red vintage bicycle with a wicker basket of lemons.",
"location": "center"
}
],
"background_setting": "A whitewashed wall with a blue wooden door and potted geraniums.",
"lighting": {
"conditions": "bright morning sun",
"direction": "from the right"
},
"style_medium": "photograph"
}
}
}
EOF{
"taskId": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
"payload": {
"request": {
"caption": {
"short_description": "A red vintage bicycle leaning against a whitewashed wall on a narrow street.",
"objects": [
{
"description": "A red vintage bicycle with a wicker basket of lemons.",
"location": "center"
}
],
"background_setting": "A whitewashed wall with a blue wooden door and potted geraniums.",
"lighting": {
"conditions": "bright morning sun",
"direction": "from the right"
},
"style_medium": "photograph"
}
}
}
}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.