# Bria Fibo

文本到图像模型已经掌握了想象力——但尚未掌握控制力。FIBO 改变了这一现状。

FIBO 基于结构化 JSON 描述进行训练，这些描述的长度可达 1,000 个词以上。该模型旨在理解和控制不同的视觉参数，例如光照、构图、色彩和相机设置，从而实现精确且可复现的输出。

FIBO 仅拥有 80 亿参数，却提供了全新的图像质量、提示词遵循度以及专业控制水平。

FIBO is trained exclusively on a structured prompt and will not work with freeform text prompts. you can use the [FIBO-VLM-prompt-to-JSON](https://huggingface.co/briaai/FIBO-VLM-prompt-to-JSON) model or the [FIBO-gemini-prompt-to-JSON](https://huggingface.co/briaai/FIBO-gemini-prompt-to-JSON)  to convert your freeform text prompt to a structured JSON prompt.

> [!NOTE]
> 避免在 FIBO 中直接使用自由文本提示，因为这无法产生最佳效果。

请参阅 Bria Fibo 的 Hugging Face [页面](https://huggingface.co/briaai/FIBO)以了解更多信息。

## 用法

_由于该模型受访问限制，在使用 diffusers 之前，您需要先前往 [Bria Fibo Hugging Face 页面](https://huggingface.co/briaai/FIBO)，填写表单并接受访问限制。进入后，您需要登录，以便系统确认您已接受该访问限制。_

使用以下命令登录：

```bash
hf auth login
```

## BriaFiboPipeline[[diffusers.BriaFiboPipeline]]

#### diffusers.BriaFiboPipeline[[diffusers.BriaFiboPipeline]]

```python
diffusers.BriaFiboPipeline(transformer: BriaFiboTransformer2DModel, scheduler: diffusers.schedulers.scheduling_flow_match_euler_discrete.FlowMatchEulerDiscreteScheduler | diffusers.schedulers.scheduling_utils.KarrasDiffusionSchedulers, vae: AutoencoderKLWan, text_encoder: SmolLM3ForCausalLM, tokenizer: AutoTokenizer)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/pipelines/bria_fibo/pipeline_bria_fibo.py#L76)

**参数：**

transformer (`BriaFiboTransformer2DModel`)：用于 2D 扩散建模的 transformer 模型。

scheduler (`FlowMatchEulerDiscreteScheduler` 或 `KarrasDiffusionSchedulers`)：用于配合 `transformer` 对编码后的潜变量进行去噪的调度器。

vae (`AutoencoderKLWan`)：变分自编码器，用于将图像编码为潜在表示以及从潜在表示解码为图像。

text_encoder (`SmolLM3ForCausalLM`)：用于处理输入提示的文本编码器。

tokenizer (`AutoTokenizer`)：用于处理输入文本提示词以配合 text_encoder 的 Tokenizer。

#### __调用__[[diffusers.BriaFiboPipeline.__call__]]

```python
__call__(prompt: str | list[str] = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 30, timesteps: list[int] = None, guidance_scale: float = 5, negative_prompt: str | list[str] | None = None, num_images_per_prompt: int | None = 1, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, latents: typing.Optional[torch.FloatTensor] = None, output_type: str | None = 'pil', return_dict: bool = True, joint_attention_kwargs: dict[str, typing.Any] | None = None, callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = None, callback_on_step_end_tensor_inputs: list[str] = ['latents'], max_sequence_length: int = 3000, do_patching = False)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/pipelines/bria_fibo/pipeline_bria_fibo.py#L435)

**参数：**

prompt (`str` 或 `list[str]`)：用于引导图像生成的提示词或提示词列表。

height (`int`，*可选*，默认为 self.unet.config.sample_size * self.vae_scale_factor)：生成图像的高度（以像素为单位）。默认设置为 1024，以获得最佳效果。

width (`int`，*可选*，默认为 self.unet.config.sample_size * self.vae_scale_factor)：生成图像的像素宽度。默认设置为 1024 以获得最佳效果。

num_inference_steps (`int`, *可选*，默认为 50)：去噪步数。更多的去噪步数通常能带来更高质量的图像，但代价是推理速度变慢。

timesteps (`list[int]`，*可选*)：用于在支持 `set_timesteps` 方法中 `timesteps` 参数的调度器中，指定去噪过程所使用的自定义时间步。如果未定义，则使用传入 `num_inference_steps` 时的默认行为。必须按降序排列。

guidance_scale (`float`，*可选*，默认值为 5.0)：如[Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598)中定义的引导尺度。`guidance_scale` 定义为[Imagen Paper](https://huggingface.co/papers/2205.11487)中公式 2 的 `w`。通过设置 `guidance_scale > 1` 来启用引导尺度。较高的引导尺度会促使生成与文本 `prompt` 紧密关联的图像，但通常以牺牲图像质量为代价。

negative_prompt (`str` 或 `list[str]`，*可选*)：用于引导图像生成的负面提示词。当未使用引导时（即 `guidance_scale` 小于 `1` 时）将被忽略。

num_images_per_prompt (`int`，*可选*，默认为 1)：每个提示词生成的图像数量。

generator (`torch.Generator` 或 `list[torch.Generator]`，*可选*)：一个或一组 [torch 生成器](https://pytorch.org/docs/stable/generated/torch.Generator.html)，用于使生成过程具有确定性。

latents (`torch.FloatTensor`, *可选*) : 预先生成的噪声潜变量，从高斯分布中采样，用作图像生成的输入。可用于使用不同的提示词对同一生成过程进行微调。如果未提供，将通过使用提供的随机`generator`进行采样来生成潜变量张量。

output_type (`str`，*可选*，默认为 `"pil"`)：生成图像的输出格式。可选 [PIL](https://pillow.readthedocs.io/en/stable/)：`PIL.Image.Image` 或 `np.array`。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 `~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput` 而不是普通元组。

joint_attention_kwargs (`dict`, *可选*) : 一个 kwargs 字典，如果指定，将作为参数传递给 [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py) 中 `self.processor` 下定义的 `AttentionProcessor`。

callback_on_step_end (`Callable`，*可选*)：一个在推理过程中每个去噪步骤结束时调用的函数。该函数以以下参数被调用：`callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`。`callback_kwargs` 将包含由 `callback_on_step_end_tensor_inputs` 指定的所有张量列表。

callback_on_step_end_tensor_inputs (`List`，*可选*)：用于 `callback_on_step_end` 函数的张量输入列表。列表中指定的张量将作为 `callback_kwargs` 参数传递。您只能包含流水线类 `._callback_tensor_inputs` 属性中列出的变量。

max_sequence_length (`int` 默认值为 3000)：用于 `prompt` 的最大序列长度。

do_patching (`bool`，*可选*，默认为 `False`)：是否使用 patching。

**返回：** `~pipelines.flux.BriaFiboPipelineOutput` 或 `tuple`

`~pipelines.flux.BriaFiboPipelineOutput`（当 `return_dict` 为 True 时），否则为 `tuple`。返回元组时，第一个元素是包含生成图像的列表。

调用生成 pipeline 时调用的函数。

示例：
```python
import torch
from diffusers import BriaFiboPipeline
from diffusers.modular_pipelines import ModularPipeline

torch.set_grad_enabled(False)
vlm_pipe = ModularPipeline.from_pretrained("briaai/FIBO-VLM-prompt-to-JSON", trust_remote_code=True)

pipe = BriaFiboPipeline.from_pretrained(
    "briaai/FIBO",
    trust_remote_code=True,
    torch_dtype=torch.bfloat16,
)
pipe.enable_model_cpu_offload()

with torch.inference_mode():
    # 1. Create a prompt to generate an initial image
    output = vlm_pipe(prompt="a beautiful dog")
    json_prompt_generate = output.values["json_prompt"]

    # Generate the image from the structured json prompt
    results_generate = pipe(prompt=json_prompt_generate, num_inference_steps=50, guidance_scale=5)
    results_generate.images[0].save("image_generate.png")
```

#### encode_prompt[[diffusers.BriaFiboPipeline.encode_prompt]]

```python
encode_prompt(prompt: str | list[str], device: typing.Optional[torch.device] = None, num_images_per_prompt: int = 1, guidance_scale: float = 5, negative_prompt: str | list[str] | None = None, max_sequence_length: int = 3000, lora_scale: float | None = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/pipelines/bria_fibo/pipeline_bria_fibo.py#L201)

**参数：**

prompt (`str` 或 `list[str]`，*可选*)：要编码的提示

device : (`torch.device`): torch 设备

num_images_per_prompt (`int`) : 每个提示词应生成的图像数量

guidance_scale (`float`) : 无分类器引导的引导比例。

negative_prompt (`str` 或 `list[str]`，*可选*)：用于引导图像生成的负面提示词。当未使用引导时（即 `guidance_scale` 小于 `1` 时）将被忽略。

