Stability AI

Stability AI

이미지·비디오·오디오·3D 생성용 오픈 AI 모델을 만드는 Stability AI를 LiteLLM에서 사용하는 방법을 알아봐요. Stable Diffusion으로 유명해요.

출처: 문서

본문

개요

속성 내용
설명 Stability AI는 이미지·비디오·오디오·3D 생성을 위한 오픈 AI 모델을 만듦. Stable Diffusion으로 유명
LiteLLM 라우트 stability/
공식 문서 Stability AI API ↗
지원 연산 /images/generations, /images/edits

LiteLLM은 Stability AI REST API(Bedrock 아님)를 통해 Stability AI 이미지 생성 호출을 지원해요.

API 키

# env variable
os.environ['STABILITY_API_KEY'] = "your-api-key"

API 키는 Stability AI 플랫폼에서 받을 수 있어요.

이미지 생성

LiteLLM Python SDK 사용법

from litellm import image_generation
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Stability AI image generation call
response = image_generation(
    model="stability/sd3.5-large",
    prompt="A beautiful sunset over a calm ocean",
)
print(response)

LiteLLM Proxy 서버 사용법

1. config.yaml 설정:

model_list:
  - model_name: sd3
    litellm_params:
      model: stability/sd3.5-large
      api_key: os.environ/STABILITY_API_KEY
    model_info:
      mode: image_generation

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

2. Proxy 시작:

litellm --config config.yaml

# RUNNING on http://0.0.0.0:4000

3. 테스트:

curl --location 'http://0.0.0.0:4000/v1/images/generations' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ***" \
--data '{
    "model": "sd3",
    "prompt": "A beautiful sunset over a calm ocean"
}'

고급 사용법 - 추가 파라미터

from litellm import image_generation
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

response = image_generation(
    model="stability/sd3.5-large",
    prompt="A beautiful sunset over a calm ocean",
    size="1792x1024",  # Maps to aspect_ratio 16:9
    negative_prompt="blurry, low quality",  # Stability-specific
    seed=12345,  # For reproducibility
)
print(response)

지원 파라미터

Stability AI는 다음 OpenAI 호환 파라미터를 지원해요:

파라미터 타입 설명 예시
size string 이미지 크기 (aspect_ratio로 매핑) "1024x1024"
n integer 이미지 수 (Stability는 요청당 1개만 반환) 1
response_format string 응답 형식 (Stability는 b64_json만) "b64_json"

크기를 가로세로 비율로 매핑

size 파라미터는 Stability의 aspect_ratio로 자동 매핑돼요:

OpenAI 크기 Stability 가로세로 비율
1024x1024 1:1
1792x1024 16:9
1024x1792 9:16
512x512 1:1
256x256 1:1

Stability 전용 파라미터 사용

Stability AI 전용 파라미터를 요청에 직접 전달할 수 있어요:

from litellm import image_generation
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

response = image_generation(
    model="stability/sd3.5-large",
    prompt="A beautiful sunset over a calm ocean",
    # Stability-specific parameters
    negative_prompt="blurry, watermark, text",
    aspect_ratio="16:9",  # Use directly instead of size
    seed=42,
    output_format="png",  # png, jpeg, or webp
)
print(response)

지원되는 이미지 생성 모델

모델명 함수 호출 설명
sd3 image_generation(model="stability/sd3", ...) Stable Diffusion 3
sd3-large image_generation(model="stability/sd3-large", ...) SD3 Large
sd3-large-turbo image_generation(model="stability/sd3-large-turbo", ...) SD3 Large Turbo (더 빠름)
sd3-medium image_generation(model="stability/sd3-medium", ...) SD3 Medium
sd3.5-large image_generation(model="stability/sd3.5-large", ...) SD 3.5 Large (권장)
sd3.5-large-turbo image_generation(model="stability/sd3.5-large-turbo", ...) SD 3.5 Large Turbo
sd3.5-medium image_generation(model="stability/sd3.5-medium", ...) SD 3.5 Medium
stable-image-ultra image_generation(model="stability/stable-image-ultra", ...) Stable Image Ultra
stable-image-core image_generation(model="stability/stable-image-core", ...) Stable Image Core

사용 가능한 모델과 기능에 대한 자세한 내용은 https://platform.stability.ai/docs/api-reference 참고.

응답 형식

Stability AI는 base64 형식으로 이미지를 반환해요. 응답은 OpenAI 호환이에요:

{
    "created": 1234567890,
    "data": [
        {
            "b64_json": "iVBORw0KGgo..."  # Base64 encoded image
        }
    ]
}

이미지 편집

Stability AI는 inpainting, upscaling, outpainting, 배경 제거 등 다양한 이미지 편집 연산을 지원해요.

선택 파라미터 — 중요: 다른 Stability 모델은 서로 다른 파라미터 요구사항이 있어요:

  • 일부 모델은 prompt가 필요 없음 (예: upscaling, 배경 제거)
  • style-transfer 모델은 image 대신 init_imagestyle_image를 사용
  • outpaint 모델은 숫자 파라미터(left, right, up, down)를 요구

LiteLLM이 이러한 차이점을 자동으로 처리해줘요.

LiteLLM Python SDK 사용법

Inpainting (마스크로 편집):

from litellm import image_edit
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Inpainting - edit specific areas using a mask
response = image_edit(
    model="stability/stable-image-inpaint-v1:0",
    image=open("original_image.png", "rb"),
    mask=open("mask_image.png", "rb"),
    prompt="Add a beautiful sunset in the masked area",
    size="1024x1024",
)
print(response)

이미지 업스케일링:

from litellm import image_edit
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Conservative upscaling - preserves details
response = image_edit(
    model="stability/stable-conservative-upscale-v1:0",
    image=open("low_res_image.png", "rb"),
    prompt="Upscale this image while preserving details",
)

# Creative upscaling - adds creative details
response = image_edit(
    model="stability/stable-creative-upscale-v1:0",
    image=open("low_res_image.png", "rb"),
    prompt="Upscale and enhance with creative details",
    creativity=0.3,  # 0-0.35, higher = more creative
)

# Fast upscaling - quick upscaling (no prompt needed)
response = image_edit(
    model="stability/stable-fast-upscale-v1:0",
    image=open("low_res_image.png", "rb"),
    # No prompt required for fast upscale
)
print(response)

이미지 아웃페인팅:

from litellm import image_edit
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Extend image beyond its borders
response = image_edit(
    model="stability/stable-outpaint-v1:0",
    image=open("original_image.png", "rb"),
    prompt="Extend this landscape with mountains",
    left=100,   # Pixels to extend on the left
    right=100,  # Pixels to extend on the right
    up=50,      # Pixels to extend on top
    down=50,    # Pixels to extend on bottom
)
print(response)

배경 제거:

from litellm import image_edit
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Remove background from image
response = image_edit(
    model="stability/stable-image-remove-background-v1:0",
    image=open("portrait.png", "rb"),
    # No prompt required for fast upscale
)
print(response)

검색 및 교체:

from litellm import image_edit
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Search and replace objects in image
response = image_edit(
    model="stability/stable-image-search-replace-v1:0",
    image=open("scene.png", "rb"),
    prompt="A red sports car",
    search_prompt="blue sedan",  # What to replace
)

# Search and recolor
response = image_edit(
    model="stability/stable-image-search-recolor-v1:0",
    image=open("scene.png", "rb"),
    prompt="Make it golden yellow",
    select_prompt="the car",  # What to recolor
)
print(response)

이미지 제어 (스케치/구조):

from litellm import image_edit
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Control with sketch
response = image_edit(
    model="stability/stable-image-control-sketch-v1:0",
    image=open("sketch.png", "rb"),
    prompt="Turn this sketch into a realistic photo",
    control_strength=0.7,  # 0-1, higher = more control
)

# Control with structure
response = image_edit(
    model="stability/stable-image-control-structure-v1:0",
    image=open("structure_reference.png", "rb"),
    prompt="Generate image following this structure",
    control_strength=0.7,
)
print(response)

객체 지우기:

from litellm import image_edit
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Erase objects from image
response = image_edit(
    model="stability/stable-image-erase-object-v1:0",
    image=open("scene.png", "rb"),
    mask=open("object_mask.png", "rb"),  # Mask the object to erase
    # No prompt needed
)
print(response)

스타일 전송:

from litellm import image_edit
import os

os.environ['STABILITY_API_KEY'] = "your-api-key"

# Transfer style from one image to another
# Note: Uses init_image (via image param) and style_image
response = image_edit(
    model="stability/stable-style-transfer-v1:0",
    image=open("content_image.png", "rb"),  # Maps to init_image
    style_image=open("style_reference.png", "rb"),  # Style to apply
    fidelity=0.5,  # 0-1, balance between content and style
    # No prompt needed
)

print(response)

지원되는 이미지 편집 모델

모델명 함수 호출 설명
stable-image-inpaint-v1:0 image_edit(model="stability/stable-image-inpaint-v1:0", ...) 마스크로 inpainting
stable-conservative-upscale-v1:0 image_edit(model="stability/stable-conservative-upscale-v1:0", ...) 보수적 업스케일링
stable-creative-upscale-v1:0 image_edit(model="stability/stable-creative-upscale-v1:0", ...) 창의적 업스케일링
stable-fast-upscale-v1:0 image_edit(model="stability/stable-fast-upscale-v1:0", ...) 빠른 업스케일링
stable-outpaint-v1:0 image_edit(model="stability/stable-outpaint-v1:0", ...) 이미지 경계 확장
stable-image-remove-background-v1:0 image_edit(model="stability/stable-image-remove-background-v1:0", ...) 배경 제거
stable-image-search-replace-v1:0 image_edit(model="stability/stable-image-search-replace-v1:0", ...) 객체 검색 및 교체
stable-image-search-recolor-v1:0 image_edit(model="stability/stable-image-search-recolor-v1:0", ...) 검색 및 재색
stable-image-control-sketch-v1:0 image_edit(model="stability/stable-image-control-sketch-v1:0", ...) 스케치로 제어
stable-image-control-structure-v1:0 image_edit(model="stability/stable-image-control-structure-v1:0", ...) 구조로 제어
stable-image-erase-object-v1:0 image_edit(model="stability/stable-image-erase-object-v1:0", ...) 객체 지우기
stable-image-style-guide-v1:0 image_edit(model="stability/stable-image-style-guide-v1:0", ...) 스타일 가이드 적용
stable-style-transfer-v1:0 image_edit(model="stability/stable-style-transfer-v1:0", ...) 스타일 전송

LiteLLM Proxy 서버 사용법

1. config.yaml 설정:

model_list:
  - model_name: stability-inpaint
    litellm_params:
      model: stability/stable-image-inpaint-v1:0
      api_key: os.environ/STABILITY_API_KEY
    model_info:
      mode: image_edit

  - model_name: stability-upscale
    litellm_params:
      model: stability/stable-conservative-upscale-v1:0
      api_key: os.environ/STABILITY_API_KEY
    model_info:
      mode: image_edit

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

2. Proxy 시작:

litellm --config config.yaml

# RUNNING on http://0.0.0.0:4000

3. 테스트:

curl -X POST "http://0.0.0.0:4000/v1/images/edits" \
  -H "Authorization: Bearer ***" \
  -F "model=stability-inpaint" \
  -F "image=@original_image.png" \
  -F "mask=@mask_image.png" \
  -F "prompt=Add a beautiful garden in the masked area"

AWS Bedrock (Stability)

LiteLLM은 AWS Bedrock을 통한 Stability AI 모델도 지원해요. 이미 AWS 인프라를 사용 중이라면 유용해요.

Bedrock Stability 사용법

from litellm import image_edit
import os

# Set AWS credentials
os.environ["AWS_ACCESS_KEY_ID"] = "your-access-key"
os.environ["AWS_SECRET_ACCESS_KEY"] = "your-secret-key"
os.environ["AWS_REGION_NAME"] = "us-east-1"

# Bedrock Stability inpainting
response = image_edit(
    model="bedrock/us.stability.stable-image-inpaint-v1:0",
    image=open("original_image.png", "rb"),
    mask=open("mask_image.png", "rb"),
    prompt="Add flowers in the masked area",
)
print(response)

# Fast upscale without prompt
response = image_edit(
    model="bedrock/stability.stable-fast-upscale-v1:0",
    image=open("low_res_image.png", "rb"),
)

# Outpaint with numeric parameters
response = image_edit(
    model="bedrock/stability.stable-outpaint-v1:0",
    image=open("original_image.png", "rb"),
    left=100,   # Automatically converted to int
    right=100,
    up=50,
    down=50,
)

print(response)

지원되는 Bedrock Stability 모델

모든 Stability AI 이미지 편집 모델은 bedrock/ 접두사로 Bedrock에서 사용할 수 있어요:

직접 API 모델 Bedrock 모델 설명
stability/stable-image-inpaint-v1:0 bedrock/us.stability.stable-image-inpaint-v1:0 Inpainting
stability/stable-conservative-upscale-v1:0 bedrock/stability.stable-conservative-upscale-v1:0 Conservative upscaling
stability/stable-creative-upscale-v1:0 bedrock/stability.stable-creative-upscale-v1:0 Creative upscaling
stability/stable-fast-upscale-v1:0 bedrock/stability.stable-fast-upscale-v1:0 Fast upscaling
stability/stable-outpaint-v1:0 bedrock/stability.stable-outpaint-v1:0 Outpainting
stability/stable-image-remove-background-v1:0 bedrock/stability.stable-image-remove-background-v1:0 Remove background
stability/stable-image-search-replace-v1:0 bedrock/stability.stable-image-search-replace-v1:0 Search and replace
stability/stable-image-search-recolor-v1:0 bedrock/stability.stable-image-search-recolor-v1:0 Search and recolor
stability/stable-image-control-sketch-v1:0 bedrock/stability.stable-image-control-sketch-v1:0 Control with sketch
stability/stable-image-control-structure-v1:0 bedrock/stability.stable-image-control-structure-v1:0 Control with structure
stability/stable-image-erase-object-v1:0 bedrock/stability.stable-image-erase-object-v1:0 Erase objects

참고: Bedrock 모델 ID는 리전과 모델에 따라 us.stability.* 또는 stability.* 접두사를 사용할 수 있어요.

라우트 비교

LiteLLM은 Stability AI 모델을 두 가지 라우트로 지원해요:

라우트 제공사 사용 사례 이미지 생성 이미지 편집
stability/ Stability AI Direct API 직접 접근, 모든 최신 모델
bedrock/stability.* AWS Bedrock AWS 통합, 엔터프라이즈 기능

직접 API 접근에는 stability/를, 이미 AWS Bedrock을 사용 중이라면 bedrock/stability.*를 사용해요.

더 알아보기 (Learn more)

  • Stability AI API 레퍼런스
  • AWS Bedrock Stability 모델