61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
import os
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
from PIL import Image
|
|
|
|
from mflux import Flux1, Config, ModelConfig
|
|
|
|
|
|
class ImageGeneratorTestHelper:
|
|
@staticmethod
|
|
def assert_matches_reference_image(
|
|
reference_image_path: str,
|
|
output_image_path: str,
|
|
model_config: ModelConfig,
|
|
prompt: str,
|
|
steps: int,
|
|
seed: int,
|
|
lora_paths: list[str] | None = None,
|
|
lora_scales: list[float] | None = None,
|
|
):
|
|
# resolve paths
|
|
reference_image_path = ImageGeneratorTestHelper.resolve_path(reference_image_path)
|
|
output_image_path = ImageGeneratorTestHelper.resolve_path(output_image_path)
|
|
lora_paths = [str(ImageGeneratorTestHelper.resolve_path(p)) for p in lora_paths] if lora_paths else None
|
|
|
|
# given
|
|
flux = Flux1(
|
|
model_config=model_config,
|
|
quantize=8,
|
|
lora_paths=lora_paths,
|
|
lora_scales=lora_scales
|
|
) # fmt: off
|
|
|
|
# when
|
|
image = flux.generate_image(
|
|
seed=seed,
|
|
prompt=prompt,
|
|
config=Config(
|
|
num_inference_steps=steps,
|
|
height=341,
|
|
width=768,
|
|
),
|
|
)
|
|
image.save(path=output_image_path)
|
|
|
|
# then
|
|
np.testing.assert_array_equal(
|
|
np.array(Image.open(output_image_path)),
|
|
np.array(Image.open(reference_image_path)),
|
|
err_msg="Generated image doesn't match reference image",
|
|
)
|
|
|
|
# cleanup
|
|
if os.path.exists(output_image_path):
|
|
os.remove(output_image_path)
|
|
|
|
@staticmethod
|
|
def resolve_path(path) -> Path:
|
|
return Path(__file__).parent.parent / "resources" / path
|