-
Notifications
You must be signed in to change notification settings - Fork 709
/
img2img.py
109 lines (95 loc) · 3.38 KB
/
img2img.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys
import os
sys.path.append(
os.path.join(
os.path.dirname(__file__),
"..",
"..",
)
)
from utils.wrapper import StreamDiffusionWrapper
import torch
from config import Args
from pydantic import BaseModel, Field
from PIL import Image
import math
base_model = "stabilityai/sd-turbo"
taesd_model = "madebyollin/taesd"
default_prompt = "Portrait of The Joker halloween costume, face painting, with , glare pose, detailed, intricate, full of colour, cinematic lighting, trending on artstation, 8k, hyperrealistic, focused, extreme details, unreal engine 5 cinematic, masterpiece"
default_negative_prompt = "black and white, blurry, low resolution, pixelated, pixel art, low quality, low fidelity"
page_content = """<h1 class="text-3xl font-bold">StreamDiffusion</h1>
<h3 class="text-xl font-bold">Image-to-Image SD-Turbo</h3>
<p class="text-sm">
This demo showcases
<a
href="https://github.com/cumulo-autumn/StreamDiffusion"
target="_blank"
class="text-blue-500 underline hover:no-underline">StreamDiffusion
</a>
Image to Image pipeline using
<a
href="https://huggingface.co/stabilityai/sd-turbo"
target="_blank"
class="text-blue-500 underline hover:no-underline">SD-Turbo</a
> with a MJPEG stream server.
</p>
"""
class Pipeline:
class Info(BaseModel):
name: str = "StreamDiffusion img2img"
input_mode: str = "image"
page_content: str = page_content
class InputParams(BaseModel):
prompt: str = Field(
default_prompt,
title="Prompt",
field="textarea",
id="prompt",
)
# negative_prompt: str = Field(
# default_negative_prompt,
# title="Negative Prompt",
# field="textarea",
# id="negative_prompt",
# )
width: int = Field(
512, min=2, max=15, title="Width", disabled=True, hide=True, id="width"
)
height: int = Field(
512, min=2, max=15, title="Height", disabled=True, hide=True, id="height"
)
def __init__(self, args: Args, device: torch.device, torch_dtype: torch.dtype):
params = self.InputParams()
self.stream = StreamDiffusionWrapper(
model_id_or_path=base_model,
use_tiny_vae=args.taesd,
device=device,
dtype=torch_dtype,
t_index_list=[35, 45],
frame_buffer_size=1,
width=params.width,
height=params.height,
use_lcm_lora=False,
output_type="pil",
warmup=10,
vae_id=None,
acceleration=args.acceleration,
mode="img2img",
use_denoising_batch=True,
cfg_type="none",
use_safety_checker=args.safety_checker,
# enable_similar_image_filter=True,
# similar_image_filter_threshold=0.98,
engine_dir=args.engine_dir,
)
self.last_prompt = default_prompt
self.stream.prepare(
prompt=default_prompt,
negative_prompt=default_negative_prompt,
num_inference_steps=50,
guidance_scale=1.2,
)
def predict(self, params: "Pipeline.InputParams") -> Image.Image:
image_tensor = self.stream.preprocess_image(params.image)
output_image = self.stream(image=image_tensor, prompt=params.prompt)
return output_image