radames commited on
Commit
ee78277
1 Parent(s): 0d09fe1

add img2imgSegmindVegaRT

Browse files
Files changed (1) hide show
  1. pipelines/img2imgSegmindVegaRT.py +209 -0
pipelines/img2imgSegmindVegaRT.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import (
2
+ AutoPipelineForImage2Image,
3
+ LCMScheduler,
4
+ AutoencoderTiny,
5
+ )
6
+ from compel import Compel, ReturnedEmbeddingsType
7
+ import torch
8
+
9
+ try:
10
+ import intel_extension_for_pytorch as ipex # type: ignore
11
+ except:
12
+ pass
13
+
14
+ import psutil
15
+ from config import Args
16
+ from pydantic import BaseModel, Field
17
+ from PIL import Image
18
+ import math
19
+
20
+ base_model = "segmind/Segmind-Vega"
21
+ lora_model = "segmind/Segmind-VegaRT"
22
+ taesd_model = "madebyollin/taesdxl"
23
+
24
+ default_prompt = "close-up photography of old man standing in the rain at night, in a street lit by lamps, leica 35mm summilux"
25
+ default_negative_prompt = "blurry, low quality, render, 3D, oversaturated"
26
+ page_content = """
27
+ <h1 class="text-3xl font-bold">Real-Time SDXL Turbo</h1>
28
+ <h3 class="text-xl font-bold">Image-to-Image</h3>
29
+ <p class="text-sm">
30
+ This demo showcases
31
+ <a
32
+ href="https://huggingface.co/stabilityai/sdxl-turbo"
33
+ target="_blank"
34
+ class="text-blue-500 underline hover:no-underline">SDXL Turbo</a>
35
+ Image to Image pipeline using
36
+ <a
37
+ href="https://huggingface.co/docs/diffusers/main/en/using-diffusers/sdxl_turbo"
38
+ target="_blank"
39
+ class="text-blue-500 underline hover:no-underline">Diffusers</a
40
+ > with a MJPEG stream server.
41
+ </p>
42
+ <p class="text-sm text-gray-500">
43
+ Change the prompt to generate different images, accepts <a
44
+ href="https://github.com/damian0815/compel/blob/main/doc/syntax.md"
45
+ target="_blank"
46
+ class="text-blue-500 underline hover:no-underline">Compel</a
47
+ > syntax.
48
+ </p>
49
+ """
50
+
51
+
52
+ class Pipeline:
53
+ class Info(BaseModel):
54
+ name: str = "img2img"
55
+ title: str = "Image-to-Image Playground 256"
56
+ description: str = "Generates an image from a text prompt"
57
+ input_mode: str = "image"
58
+ page_content: str = page_content
59
+
60
+ class InputParams(BaseModel):
61
+ prompt: str = Field(
62
+ default_prompt,
63
+ title="Prompt",
64
+ field="textarea",
65
+ id="prompt",
66
+ )
67
+ negative_prompt: str = Field(
68
+ default_negative_prompt,
69
+ title="Negative Prompt",
70
+ field="textarea",
71
+ id="negative_prompt",
72
+ hide=True,
73
+ )
74
+ seed: int = Field(
75
+ 2159232, min=0, title="Seed", field="seed", hide=True, id="seed"
76
+ )
77
+ steps: int = Field(
78
+ 4, min=1, max=15, title="Steps", field="range", hide=True, id="steps"
79
+ )
80
+ width: int = Field(
81
+ 1024, min=2, max=15, title="Width", disabled=True, hide=True, id="width"
82
+ )
83
+ height: int = Field(
84
+ 1024, min=2, max=15, title="Height", disabled=True, hide=True, id="height"
85
+ )
86
+ guidance_scale: float = Field(
87
+ 0.2,
88
+ min=0,
89
+ max=20,
90
+ step=0.001,
91
+ title="Guidance Scale",
92
+ field="range",
93
+ hide=True,
94
+ id="guidance_scale",
95
+ )
96
+ strength: float = Field(
97
+ 0.5,
98
+ min=0.25,
99
+ max=1.0,
100
+ step=0.001,
101
+ title="Strength",
102
+ field="range",
103
+ hide=True,
104
+ id="strength",
105
+ )
106
+
107
+ def __init__(self, args: Args, device: torch.device, torch_dtype: torch.dtype):
108
+ if args.safety_checker:
109
+ self.pipe = AutoPipelineForImage2Image.from_pretrained(
110
+ base_model,
111
+ variant="fp16",
112
+ )
113
+ else:
114
+ self.pipe = AutoPipelineForImage2Image.from_pretrained(
115
+ base_model,
116
+ safety_checker=None,
117
+ variant="fp16",
118
+ )
119
+ if args.use_taesd:
120
+ self.pipe.vae = AutoencoderTiny.from_pretrained(
121
+ taesd_model, torch_dtype=torch_dtype, use_safetensors=True
122
+ ).to(device)
123
+
124
+ self.pipe.load_lora_weights(lora_model)
125
+ self.pipe.fuse_lora()
126
+ self.pipe.scheduler = LCMScheduler.from_pretrained(
127
+ base_model, subfolder="scheduler"
128
+ )
129
+ self.pipe.set_progress_bar_config(disable=True)
130
+ self.pipe.to(device=device, dtype=torch_dtype)
131
+ if device.type != "mps":
132
+ self.pipe.unet.to(memory_format=torch.channels_last)
133
+
134
+ # check if computer has less than 64GB of RAM using sys or os
135
+ if psutil.virtual_memory().total < 64 * 1024**3:
136
+ self.pipe.enable_attention_slicing()
137
+
138
+ if args.torch_compile:
139
+ print("Running torch compile")
140
+ self.pipe.unet = torch.compile(
141
+ self.pipe.unet,
142
+ )
143
+ self.pipe.vae = torch.compile(
144
+ self.pipe.vae,
145
+ )
146
+
147
+ self.pipe(
148
+ prompt="warmup",
149
+ image=[Image.new("RGB", (768, 768))],
150
+ )
151
+ if args.compel:
152
+ self.pipe.compel_proc = Compel(
153
+ tokenizer=[self.pipe.tokenizer, self.pipe.tokenizer_2],
154
+ text_encoder=[self.pipe.text_encoder, self.pipe.text_encoder_2],
155
+ returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
156
+ requires_pooled=[False, True],
157
+ )
158
+
159
+ def predict(self, params: "Pipeline.InputParams") -> Image.Image:
160
+ generator = torch.manual_seed(params.seed)
161
+ prompt = params.prompt
162
+ negative_prompt = params.negative_prompt
163
+ prompt_embeds = None
164
+ pooled_prompt_embeds = None
165
+ negative_prompt_embeds = None
166
+ negative_pooled_prompt_embeds = None
167
+ if hasattr(self.pipe, "compel_proc"):
168
+ prompt_embeds = self.pipe.compel_proc(
169
+ [params.prompt, params.negative_prompt]
170
+ )
171
+ prompt = None
172
+ negative_prompt = None
173
+ prompt_embeds = prompt_embeds[0:1]
174
+ pooled_prompt_embeds = pooled_prompt_embeds[0:1]
175
+ negative_prompt_embeds = prompt_embeds[1:2]
176
+ negative_pooled_prompt_embeds = pooled_prompt_embeds[1:2]
177
+
178
+ steps = params.steps
179
+ strength = params.strength
180
+ if int(steps * strength) < 1:
181
+ steps = math.ceil(1 / max(0.10, strength))
182
+
183
+ results = self.pipe(
184
+ image=params.image,
185
+ prompt=prompt,
186
+ negative_prompt=negative_prompt,
187
+ prompt_embeds=prompt_embeds,
188
+ pooled_prompt_embeds=pooled_prompt_embeds,
189
+ negative_prompt_embeds=negative_prompt_embeds,
190
+ negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
191
+ generator=generator,
192
+ strength=strength,
193
+ num_inference_steps=steps,
194
+ guidance_scale=params.guidance_scale,
195
+ width=params.width,
196
+ height=params.height,
197
+ output_type="pil",
198
+ )
199
+
200
+ nsfw_content_detected = (
201
+ results.nsfw_content_detected[0]
202
+ if "nsfw_content_detected" in results
203
+ else False
204
+ )
205
+ if nsfw_content_detected:
206
+ return None
207
+ result_image = results.images[0]
208
+
209
+ return result_image