feat: support multiple reference images with model-aware API mapping
Replace singular reference_image field with reference_images list to support an arbitrary number of reference images. Map them to the correct BFL API parameter names based on model family: - flux-2-*: input_image, input_image_2, ..., input_image_8 - flux-kontext-*: input_image, input_image_2, ..., input_image_4 - flux 1.x: image_prompt (single) BREAKING CHANGE: reference_image config field renamed to reference_images (list).
This commit is contained in:
parent
b69c38ac13
commit
d565329e16
8 changed files with 112 additions and 23 deletions
|
|
@ -18,6 +18,32 @@ def _encode_image_b64(path: Path) -> str:
|
|||
return base64.b64encode(path.read_bytes()).decode("ascii")
|
||||
|
||||
|
||||
# Parameter names for reference images, keyed by model prefix.
|
||||
_INPUT_IMAGE_KEYS = ["input_image"] + [f"input_image_{i}" for i in range(2, 9)]
|
||||
_IMAGE_PROMPT_KEYS = ["image_prompt"]
|
||||
|
||||
|
||||
def _ref_image_keys(model: str) -> list[str]:
|
||||
"""Return the ordered API parameter names for reference images."""
|
||||
if model.startswith("flux-2-"):
|
||||
return _INPUT_IMAGE_KEYS # up to 8
|
||||
if model.startswith("flux-kontext-"):
|
||||
return _INPUT_IMAGE_KEYS[:4] # up to 4
|
||||
return _IMAGE_PROMPT_KEYS # flux 1.x: single image_prompt
|
||||
|
||||
|
||||
def _add_reference_images(
|
||||
inputs: dict[str, object],
|
||||
reference_images: list[str],
|
||||
model: str,
|
||||
project_dir: Path,
|
||||
) -> None:
|
||||
"""Encode reference images and add them under the correct API keys."""
|
||||
keys = _ref_image_keys(model)
|
||||
for key, ref_name in zip(keys, reference_images, strict=False):
|
||||
inputs[key] = _encode_image_b64(project_dir / ref_name)
|
||||
|
||||
|
||||
class ImageProvider(Provider):
|
||||
"""Generates images via the BlackForestLabs API."""
|
||||
|
||||
|
|
@ -44,9 +70,10 @@ class ImageProvider(Provider):
|
|||
if target_config.height is not None:
|
||||
inputs["height"] = target_config.height
|
||||
|
||||
if target_config.reference_image is not None:
|
||||
ref_path = project_dir / target_config.reference_image
|
||||
inputs["image_prompt"] = _encode_image_b64(ref_path)
|
||||
if target_config.reference_images:
|
||||
_add_reference_images(
|
||||
inputs, target_config.reference_images, resolved_model, project_dir
|
||||
)
|
||||
|
||||
for control_name in target_config.control_images:
|
||||
ctrl_path = project_dir / control_name
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue