Black Forest Labs Image Editing

Black Forest Labs Image Editing

Black Forest Labs는 FLUX 모델로 텍스트 설명에 따라 기존 이미지를 수정, 인페인팅, 확장하는 강력한 이미지 편집 기능을 제공해요.

출처: 문서

본문

개요 (Overview)

속성 설명
설명 Black Forest Labs Image Editing은 FLUX Kontext 및 기타 모델로 텍스트 프롬프트에 따라 이미지를 수정, 인페인팅, 확장해요
LiteLLM 라우트 black_forest_labs/
공급자 문서 Black Forest Labs API
지원 작업 /images/edits

설정 (Setup)

API 키

Black Forest Labs에서 API 키를 가져와요.

import os
# Set your Black Forest Labs API key
os.environ["BFL_API_KEY"] = "your-api-key-here"

지원 모델 (Supported Models)

모델 이름 설명 사용 사례
black_forest_labs/flux-kontext-pro FLUX Kontext Pro - 프롬프트로 일반 이미지 편집 일반 편집, 스타일 변환
black_forest_labs/flux-kontext-max FLUX Kontext Max - 프리미엄 품질 편집 고품질 편집
black_forest_labs/flux-pro-1.0-fill FLUX Pro Fill - 마스크로 인페인팅 객체 제거/교체
black_forest_labs/flux-pro-1.0-expand FLUX Pro Expand - 아웃페인팅 이미지 경계 확장

이미지 편집 (Image Editing)

기본 이미지 편집

import os
import litellm

# Set your API key
os.environ["BFL_API_KEY"] = "your-api-key-here"

# Edit an image with a prompt
response = litellm.image_edit(
    model="black_forest_labs/flux-kontext-pro",
    image=open("path/to/your/image.png", "rb"),
    prompt="Add a green leaf to the scene",
)

# BFL returns URLs
print(response.data[0].url)

비동기 이미지 편집

import os
import asyncio
import litellm

# Set your API key
os.environ["BFL_API_KEY"] = "your-api-key-here"

async def edit_image():
    response = await litellm.aimage_edit(
        model="black_forest_labs/flux-kontext-pro",
        image=open("path/to/your/image.png", "rb"),
        prompt="Make this image look like a watercolor painting",
    )
    print(response.data[0].url)

# Run the async function
asyncio.run(edit_image())

마스크로 인페인팅 (Fill)

import os
import litellm

# Set your API key
os.environ["BFL_API_KEY"] = "your-api-key-here"

# Use flux-pro-1.0-fill for inpainting
response = litellm.image_edit(
    model="black_forest_labs/flux-pro-1.0-fill",
    image=open("path/to/your/image.png", "rb"),
    mask=open("path/to/mask.png", "rb"),  # White areas will be edited
    prompt="Replace with a beautiful garden",
    steps=50,  # BFL-specific parameter
    guidance=30,  # BFL-specific parameter
)
print(response.data[0].url)

아웃페인팅 - 이미지 경계 확장 (Expand)

import os
import litellm

# Set your API key
os.environ["BFL_API_KEY"] = "your-api-key-here"

# Use flux-pro-1.0-expand to extend image borders
response = litellm.image_edit(
    model="black_forest_labs/flux-pro-1.0-expand",
    image=open("path/to/your/image.png", "rb"),
    prompt="Continue the scene with a mountain landscape",
    top=256,   # Expand 256 pixels at top
    bottom=256,  # Expand 256 pixels at bottom
    left=128,  # Expand 128 pixels at left
    right=128,  # Expand 128 pixels at right
)
print(response.data[0].url)

BFL 전용 파라미터 이미지 편집

import os
import litellm

# Set your API key
os.environ["BFL_API_KEY"] = "your-api-key-here"

# Edit image with BFL-specific parameters
response = litellm.image_edit(
    model="black_forest_labs/flux-kontext-pro",
    image=open("path/to/your/image.png", "rb"),
    prompt="Transform into cyberpunk style with neon lights",
    seed=42,  # For reproducible results
    output_format="png",  # png or jpeg
    safety_tolerance=2,  # 0-6, higher = more permissive
    aspect_ratio="16:9",  # Output aspect ratio
)
print(response.data[0].url)

LiteLLM Proxy Server 사용법

1. config.yaml 설정

model_list:
  - model_name: bfl-kontext-pro
    litellm_params:
      model: black_forest_labs/flux-kontext-pro
      api_key: os.environ/BFL_API_KEY
      model_info:
        mode: image_edit
  - model_name: bfl-kontext-max
    litellm_params:
      model: black_forest_labs/flux-kontext-max
      api_key: os.environ/BFL_API_KEY
      model_info:
        mode: image_edit
  - model_name: bfl-fill
    litellm_params:
      model: black_forest_labs/flux-pro-1.0-fill
      api_key: os.environ/BFL_API_KEY
      model_info:
        mode: image_edit
  - model_name: bfl-expand
    litellm_params:
      model: black_forest_labs/flux-pro-1.0-expand
      api_key: os.environ/BFL_API_KEY
      model_info:
        mode: image_edit

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

2. Proxy 서버 시작

litellm --config /path/to/config.yaml
# RUNNING on http://0.0.0.0:4000

3. 이미지 편집 요청

OpenAI SDK:

from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
    base_url="http://localhost:4000",
    api_key="sk-<your-litellm-api-key>"
)

# Edit image with FLUX Kontext Pro
response = client.images.edit(
    model="bfl-kontext-pro",
    image=open("path/to/your/image.png", "rb"),
    prompt="Add magical sparkles and fairy dust",
)
print(response.data[0].url)

cURL:

curl --location 'http://localhost:4000/v1/images/edits' \
  --header "Authorization: Bearer ***" \
  --form 'model="bfl-kontext-pro"' \
  --form 'prompt="Add a sunset in the background"' \
  --form 'image=@"path/to/your/image.png"'

지원 파라미터 (Supported Parameters)

OpenAI 호환 파라미터

파라미터 타입 설명 기본값
image file 편집할 이미지 파일 필수
prompt string 원하는 변경 사항의 텍스트 설명 필수
model string 사용할 FLUX 모델 필수
mask file 인페인팅용 마스크 이미지 (flux-pro-1.0-fill) 선택
n integer 이미지 수 (BFL은 요청당 1개 반환) 1
size string aspect_ratio로 매핑 선택
response_format string url 또는 b64_json url

Black Forest Labs 전용 파라미터

파라미터 타입 설명 기본값 모델
seed integer 재현 가능한 결과용 시드 랜덤 전체
output_format string 출력 형식: png 또는 jpeg png 전체
safety_tolerance integer 안전 필터 허용도 (0-6) 2 전체
aspect_ratio string 출력 종횡비 (예: 16:9, 1:1) 원본 Kontext 모델
steps integer 추론 단계 수 모델 기본값 Fill
guidance float Guidance 스케일 모델 기본값 Fill
grow_mask integer 마스크를 확장할 픽셀 수 0 Fill
top integer 위로 확장할 픽셀 수 0 Expand
bottom integer 아래로 확장할 픽셀 수 0 Expand
left integer 왼쪽으로 확장할 픽셀 수 0 Expand
right integer 오른쪽으로 확장할 픽셀 수 0 Expand

동작 방식 (How It Works)

Black Forest Labs는 폴링 기반 API를 사용해요:

  1. 요청 제출: LiteLLM이 이미지와 프롬프트를 BFL로 전송
  2. Task ID 획득: BFL이 task ID와 폴링 URL 반환
  3. 결과 폴링: LiteLLM이 이미지가 준비될 때까지 자동 폴링
  4. 결과 반환: 생성된 이미지 URL 반환

이 폴링은 LiteLLM이 자동으로 처리하므로, image_edit()을 호출하기만 하면 결과를 얻을 수 있어요.

시작하기 (Getting Started)

  1. Black Forest Labs에서 계정 만들기
  2. 대시보드에서 API 키 가져오기
  3. BFL_API_KEY 환경 변수 설정하기
  4. 지원되는 모델과 함께 litellm.image_edit() 사용하기

더 알아보기 (Learn more)