Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the timbrooks/instruct-pix2pix model #52

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions runner/app/pipelines/image_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from diffusers import (
AutoPipelineForImage2Image,
StableDiffusionInstructPix2PixPipeline,
StableDiffusionXLPipeline,
UNet2DConditionModel,
EulerDiscreteScheduler,
Expand All @@ -11,6 +12,7 @@
from huggingface_hub import file_download, hf_hub_download
import torch
import PIL
import random
from typing import List
import logging
import os
Expand All @@ -22,6 +24,7 @@
logger = logging.getLogger(__name__)

SDXL_LIGHTNING_MODEL_ID = "ByteDance/SDXL-Lightning"
PIX2PIX_MODEL_ID = "timbrooks/instruct-pix2pix"


class ImageToImagePipeline(Pipeline):
Expand Down Expand Up @@ -87,6 +90,12 @@ def __init__(self, model_id: str):
self.ldm.scheduler = EulerDiscreteScheduler.from_config(
self.ldm.scheduler.config, timestep_spacing="trailing"
)
elif PIX2PIX_MODEL_ID in model_id:
kwargs["torch_dtype"] = torch.float16
kwargs["variant"] = "fp16"
self.ldm = StableDiffusionInstructPix2PixPipeline.from_pretrained(
model_id, **kwargs
).to(torch_device)
else:
self.ldm = AutoPipelineForImage2Image.from_pretrained(
model_id, **kwargs
Expand Down Expand Up @@ -142,6 +151,11 @@ def __call__(self, prompt: str, image: PIL.Image, **kwargs) -> List[PIL.Image]:
else:
# Default to 2step
kwargs["num_inference_steps"] = 2
elif PIX2PIX_MODEL_ID in self.model_id:
if "image_guidance_scale" not in kwargs:
kwargs["image_guidance_scale"] = round(random.uniform(1.2, 1.8), ndigits=2)
if "num_inference_steps" not in kwargs:
kwargs["num_inference_steps"] = 50

return self.ldm(prompt, image=image, **kwargs).images

Expand Down
2 changes: 2 additions & 0 deletions runner/app/routes/image_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async def image_to_image(
model_id: Annotated[str, Form()] = "",
strength: Annotated[float, Form()] = 0.8,
guidance_scale: Annotated[float, Form()] = 7.5,
image_guidance_scale: Annotated[float, Form()] = 0,
negative_prompt: Annotated[str, Form()] = "",
seed: Annotated[int, Form()] = None,
num_images_per_prompt: Annotated[int, Form()] = 1,
Expand Down Expand Up @@ -80,6 +81,7 @@ async def image_to_image(
image=image,
strength=strength,
guidance_scale=guidance_scale,
image_guidance_scale=image_guidance_scale,
negative_prompt=negative_prompt,
seed=seed,
num_images_per_prompt=num_images_per_prompt,
Expand Down
1 change: 1 addition & 0 deletions runner/dl_checkpoints.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ if [ "$MODE" = "alpha" ]; then

# Download text-to-image and image-to-image models.
huggingface-cli download ByteDance/SDXL-Lightning --include "*unet.safetensors" --exclude "*lora.safetensors*" --cache-dir models
huggingface-cli download timbrooks/instruct-pix2pix --include "*fp16.safetensors" --exclude "*lora.safetensors*" --cache-dir models

# Download image-to-video models (token-gated).
printf "\nDownloading token-gated models...\n"
Expand Down
5 changes: 5 additions & 0 deletions runner/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@
"title": "Guidance Scale",
"default": 7.5
},
"image_guidance_scale": {
"type": "number",
"title": "Guidance Scale",
"default": 7.5
},
"negative_prompt": {
"type": "string",
"title": "Negative Prompt",
Expand Down
39 changes: 20 additions & 19 deletions worker/runner.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.