Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Switch default Replicate image model to Z Image Turbo

Abi Raja7374acf1d7c5
diff --git a/.gitignore b/.gitignore
index d5545220..9ffc05e5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,6 +6,8 @@ node_modules
# Run logs
backend/run_logs/*
backend/local_assets/*
+backend/image_generation/eval_sets/*
+backend/image_generation/eval_results/*
# Weird Docker setup related files
backend/backend/*
diff --git a/backend/image_generation/generation.py b/backend/image_generation/generation.py
index 71d66d41..1aadb6f0 100644
--- a/backend/image_generation/generation.py
+++ b/backend/image_generation/generation.py
@@ -4,10 +4,15 @@ from typing import List, Literal, Union
from openai import AsyncOpenAI
-from image_generation.replicate import call_replicate
+from image_generation.replicate import (
+ DEFAULT_IMAGE_MODEL,
+ ReplicateImageModel,
+ call_replicate,
+)
REPLICATE_BATCH_SIZE = 20
+REPLICATE_IMAGE_MODEL: ReplicateImageModel = DEFAULT_IMAGE_MODEL
async def process_tasks(
@@ -61,12 +66,26 @@ async def generate_image_dalle(
async def generate_image_replicate(prompt: str, api_key: str) -> str:
- # We use Flux 2 Klein
- return await call_replicate(
- {
+ replicate_input: dict[str, str | int | float | bool]
+ if REPLICATE_IMAGE_MODEL == "flux_2_klein":
+ replicate_input = {
"prompt": prompt,
"aspect_ratio": "1:1",
"output_format": "png",
- },
+ }
+ else:
+ replicate_input = {
+ "prompt": prompt,
+ "width": 1024,
+ "height": 1024,
+ "go_fast": False,
+ "output_format": "png",
+ "guidance_scale": 0,
+ "num_inference_steps": 8,
+ }
+
+ return await call_replicate(
+ replicate_input,
api_key,
+ model=REPLICATE_IMAGE_MODEL,
)
diff --git a/backend/image_generation/replicate.py b/backend/image_generation/replicate.py
index 843eade9..d2116962 100644
--- a/backend/image_generation/replicate.py
+++ b/backend/image_generation/replicate.py
@@ -1,10 +1,17 @@
import asyncio
import httpx
-from typing import Any, Mapping, cast
+from typing import Any, Literal, Mapping, cast
REPLICATE_API_BASE_URL = "https://api.replicate.com/v1"
-FLUX_MODEL_PATH = "black-forest-labs/flux-2-klein-4b"
+ReplicateImageModel = Literal["z_image_turbo", "flux_2_klein"]
+Z_IMAGE_TURBO_MODEL_PATH = "prunaai/z-image-turbo"
+FLUX_2_KLEIN_MODEL_PATH = "black-forest-labs/flux-2-klein-4b"
+MODEL_PATHS: dict[ReplicateImageModel, str] = {
+ "z_image_turbo": Z_IMAGE_TURBO_MODEL_PATH,
+ "flux_2_klein": FLUX_2_KLEIN_MODEL_PATH,
+}
+DEFAULT_IMAGE_MODEL: ReplicateImageModel = "z_image_turbo"
REMOVE_BACKGROUND_VERSION = (
"a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc"
)
@@ -90,7 +97,7 @@ def _extract_output_url(result: Any, context: str) -> str:
return url
if isinstance(result, list) and len(result) > 0:
- first: Any = result[0]
+ first = cast(Any, result[0])
if isinstance(first, str) and first:
return first
if isinstance(first, Mapping):
@@ -136,6 +143,10 @@ async def remove_background(image_url: str, api_token: str) -> str:
return _extract_output_url(result, "background remover")
-async def call_replicate(input: dict[str, str | int], api_token: str) -> str:
- result = await call_replicate_model(FLUX_MODEL_PATH, input, api_token)
- return _extract_output_url(result, "Flux prediction")
+async def call_replicate(
+ input: dict[str, str | int | float | bool],
+ api_token: str,
+ model: ReplicateImageModel = DEFAULT_IMAGE_MODEL,
+) -> str:
+ result = await call_replicate_model(MODEL_PATHS[model], input, api_token)
+ return _extract_output_url(result, f"Replicate image model prediction ({model})")
diff --git a/backend/tests/test_image_generation_replicate.py b/backend/tests/test_image_generation_replicate.py
index 469ebf67..deb165a0 100644
--- a/backend/tests/test_image_generation_replicate.py
+++ b/backend/tests/test_image_generation_replicate.py
@@ -39,7 +39,9 @@ def test_extract_output_url_invalid_raises() -> None:
@pytest.mark.asyncio
-async def test_call_replicate_uses_flux_model(monkeypatch: pytest.MonkeyPatch) -> None:
+async def test_call_replicate_uses_default_z_image_model(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
captured: dict[str, object] = {}
async def fake_call_replicate_model(
@@ -55,7 +57,32 @@ async def test_call_replicate_uses_flux_model(monkeypatch: pytest.MonkeyPatch) -
result = await replicate.call_replicate({"prompt": "test", "seed": 1}, "token-123")
assert result == "https://example.com/flux.png"
- assert captured["model_path"] == replicate.FLUX_MODEL_PATH
+ assert captured["model_path"] == replicate.Z_IMAGE_TURBO_MODEL_PATH
+ assert captured["api_token"] == "token-123"
+
+
+async def test_call_replicate_can_use_flux_model(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ captured: dict[str, object] = {}
+
+ async def fake_call_replicate_model(
+ model_path: str, input: dict[str, object], api_token: str
+ ) -> list[str]:
+ captured["model_path"] = model_path
+ captured["input"] = input
+ captured["api_token"] = api_token
+ return ["https://example.com/flux.png"]
+
+ monkeypatch.setattr(replicate, "call_replicate_model", fake_call_replicate_model)
+
+ result = await replicate.call_replicate(
+ {"prompt": "test", "seed": 1}, "token-123", model="flux_2_klein"
+ )
+
+ assert result == "https://example.com/flux.png"
+ assert captured["model_path"] == replicate.FLUX_2_KLEIN_MODEL_PATH
assert captured["api_token"] == "token-123"