Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,21 +25,17 @@ FTP_USER = "ford442"
|
|
| 25 |
FTP_PASS = "GoogleBez12!"
|
| 26 |
FTP_DIR = "1ink.us/stable_diff/" # Remote directory on FTP server
|
| 27 |
|
| 28 |
-
|
| 29 |
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
| 30 |
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
|
| 31 |
-
|
| 32 |
torch.backends.cudnn.deterministic = False
|
| 33 |
-
|
| 34 |
#torch.backends.cuda.preferred_blas_library="cublas"
|
| 35 |
#torch.backends.cuda.preferred_linalg_library="cusolver"
|
| 36 |
|
| 37 |
hftoken = os.getenv("HF_AUTH_TOKEN")
|
| 38 |
|
| 39 |
-
#image_encoder_path = "google/siglip-so400m-patch14-384"
|
| 40 |
-
#ipadapter_path = hf_hub_download(repo_id="InstantX/SD3.5-Large-IP-Adapter", filename="ip-adapter.bin")
|
| 41 |
-
#model_path = 'ford442/stable-diffusion-3.5-medium-bf16'
|
| 42 |
-
|
| 43 |
def upload_to_ftp(filename):
|
| 44 |
try:
|
| 45 |
transport = paramiko.Transport((FTP_HOST, 22))
|
|
@@ -66,7 +62,7 @@ pipe = StableDiffusion3Pipeline.from_pretrained(
|
|
| 66 |
# text_encoder_3=T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_3',token=True),
|
| 67 |
#tokenizer=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer", token=True),
|
| 68 |
#tokenizer_2=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer_2", token=True),
|
| 69 |
-
tokenizer_3=T5TokenizerFast.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", use_fast=True, subfolder="tokenizer_3", token=True),
|
| 70 |
torch_dtype=torch.bfloat16,
|
| 71 |
#use_safetensors=False,
|
| 72 |
)
|
|
@@ -77,90 +73,77 @@ pipe = StableDiffusion3Pipeline.from_pretrained(
|
|
| 77 |
pipe.to(device)
|
| 78 |
#pipe.to(device=device, dtype=torch.bfloat16)
|
| 79 |
|
| 80 |
-
upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device(
|
| 81 |
-
|
| 82 |
-
def filter_text(text,phraseC):
|
| 83 |
-
"""Filters out the text up to and including 'Rewritten Prompt:'."""
|
| 84 |
-
phrase = "Rewritten Prompt:"
|
| 85 |
-
phraseB = "rewritten text:"
|
| 86 |
-
pattern = f"(.*?){re.escape(phrase)}(.*)"
|
| 87 |
-
patternB = f"(.*?){re.escape(phraseB)}(.*)"
|
| 88 |
-
# matchB = re.search(patternB, text)
|
| 89 |
-
matchB = re.search(patternB, text, flags=re.DOTALL)
|
| 90 |
-
if matchB:
|
| 91 |
-
filtered_text = matchB.group(2)
|
| 92 |
-
match = re.search(pattern, filtered_text, flags=re.DOTALL)
|
| 93 |
-
if match:
|
| 94 |
-
filtered_text = match.group(2)
|
| 95 |
-
filtered_text = re.sub(phraseC, "", filtered_text, flags=re.DOTALL) # Replaces the matched pattern with an empty string
|
| 96 |
-
return filtered_text
|
| 97 |
-
else:
|
| 98 |
-
return filtered_text
|
| 99 |
-
else:
|
| 100 |
-
# Handle the case where no match is found
|
| 101 |
-
return text
|
| 102 |
|
| 103 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 104 |
MAX_IMAGE_SIZE = 4096
|
| 105 |
|
| 106 |
-
@spaces.GPU(duration=
|
| 107 |
-
def
|
| 108 |
prompt,
|
| 109 |
negative_prompt_1,
|
| 110 |
negative_prompt_2,
|
| 111 |
negative_prompt_3,
|
| 112 |
-
seed,
|
| 113 |
-
randomize_seed,
|
| 114 |
width,
|
| 115 |
height,
|
| 116 |
guidance_scale,
|
| 117 |
num_inference_steps,
|
| 118 |
-
expanded,
|
| 119 |
-
latent_file, # Add latents file input
|
| 120 |
progress=gr.Progress(track_tqdm=True),
|
| 121 |
):
|
| 122 |
-
upscaler_2.to(torch.device('cpu'))
|
| 123 |
torch.set_float32_matmul_precision("highest")
|
| 124 |
seed = random.randint(0, MAX_SEED)
|
| 125 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# initial_latents = pipe.prepare_latents(
|
| 132 |
-
# batch_size=1,
|
| 133 |
-
# num_channels_latents=pipe.transformer.in_channels,
|
| 134 |
-
# height=pipe.transformer.config.sample_size[0],
|
| 135 |
-
# width=pipe.transformer.config.sample_size[1],
|
| 136 |
-
# dtype=pipe.transformer.dtype,
|
| 137 |
-
# device=pipe.device,
|
| 138 |
-
# generator=generator,
|
| 139 |
-
# )
|
| 140 |
-
sd_image_a = Image.open(latent_file.name)
|
| 141 |
-
print("-- using image file --")
|
| 142 |
-
print('-- generating image --')
|
| 143 |
-
#with torch.no_grad():
|
| 144 |
-
sd_image = pipe(
|
| 145 |
-
prompt=enhanced_prompt, # This conversion is fine
|
| 146 |
negative_prompt=negative_prompt_1,
|
|
|
|
|
|
|
| 147 |
guidance_scale=guidance_scale,
|
| 148 |
num_inference_steps=num_inference_steps,
|
| 149 |
width=width,
|
| 150 |
height=height,
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
negative_prompt=negative_prompt_1,
|
| 165 |
negative_prompt_2=negative_prompt_2,
|
| 166 |
negative_prompt_3=negative_prompt_3,
|
|
@@ -168,93 +151,123 @@ def infer(
|
|
| 168 |
num_inference_steps=num_inference_steps,
|
| 169 |
width=width,
|
| 170 |
height=height,
|
| 171 |
-
# latents=None,
|
| 172 |
-
# output_type='latent',
|
| 173 |
generator=generator,
|
| 174 |
max_sequence_length=512
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
# image_pil = Image.fromarray(sd35_image[0])
|
| 181 |
-
# sd35_path = f"sd35_{seed}.png"
|
| 182 |
-
# image_pil.save(sd35_path,optimize=False,compress_level=0)
|
| 183 |
-
# upload_to_ftp(sd35_path)
|
| 184 |
-
sd35_path = f"sd35l_{seed}.png"
|
| 185 |
-
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 186 |
-
upload_to_ftp(sd35_path)
|
| 187 |
-
# Convert the generated image to a tensor
|
| 188 |
-
#generated_image_tensor = torch.tensor([np.array(sd_image).transpose(2, 0, 1)]).to('cuda') / 255.0
|
| 189 |
-
# Encode the generated image into latents
|
| 190 |
-
#with torch.no_grad():
|
| 191 |
-
# generated_latents = pipe.vae.encode(generated_image_tensor.to(torch.bfloat16)).latent_dist.sample().mul_(0.18215)
|
| 192 |
-
#latent_path = f"sd35m_{seed}.pt"
|
| 193 |
-
# Save the latents to a .pt file
|
| 194 |
-
#torch.save(generated_latents, latent_path)
|
| 195 |
-
#upload_to_ftp(latent_path)
|
| 196 |
# pipe.unet.to('cpu')
|
| 197 |
upscaler_2.to(torch.device('cuda'))
|
| 198 |
with torch.no_grad():
|
| 199 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 200 |
print('-- got upscaled image --')
|
| 201 |
-
#upscaler_2.to(torch.device('cpu'))
|
| 202 |
downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
|
| 203 |
upscale_path = f"sd35l_upscale_{seed}.png"
|
| 204 |
downscale2.save(upscale_path,optimize=False,compress_level=0)
|
| 205 |
upload_to_ftp(upscale_path)
|
| 206 |
return sd_image, seed, enhanced_prompt
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
-
|
|
|
|
| 225 |
prompt,
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
width,
|
| 230 |
height,
|
| 231 |
guidance_scale,
|
| 232 |
num_inference_steps,
|
| 233 |
-
|
| 234 |
):
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
| 255 |
with gr.Column(elem_id="col-container"):
|
| 256 |
-
gr.Markdown(" # Text-to-
|
| 257 |
-
expanded_prompt_output = gr.Textbox(label="
|
| 258 |
with gr.Row():
|
| 259 |
prompt = gr.Text(
|
| 260 |
label="Prompt",
|
|
@@ -263,19 +276,12 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
| 263 |
placeholder="Enter your prompt",
|
| 264 |
container=False,
|
| 265 |
)
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
interactive=True,
|
| 271 |
-
choices=options,
|
| 272 |
-
value=True,
|
| 273 |
-
label="Use expanded prompt: ",
|
| 274 |
-
)
|
| 275 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 276 |
result = gr.Image(label="Result", show_label=False)
|
| 277 |
with gr.Accordion("Advanced Settings", open=True):
|
| 278 |
-
latent_file = gr.File(label="Image File (optional)") # Add latents file input
|
| 279 |
negative_prompt_1 = gr.Text(
|
| 280 |
label="Negative prompt 1",
|
| 281 |
max_lines=1,
|
|
@@ -300,60 +306,92 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
| 300 |
num_iterations = gr.Number(
|
| 301 |
value=1000,
|
| 302 |
label="Number of Iterations")
|
| 303 |
-
seed = gr.Slider(
|
| 304 |
-
label="Seed",
|
| 305 |
-
minimum=0,
|
| 306 |
-
maximum=MAX_SEED,
|
| 307 |
-
step=1,
|
| 308 |
-
value=0,
|
| 309 |
-
)
|
| 310 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 311 |
with gr.Row():
|
| 312 |
width = gr.Slider(
|
| 313 |
label="Width",
|
| 314 |
minimum=256,
|
| 315 |
maximum=MAX_IMAGE_SIZE,
|
| 316 |
step=32,
|
| 317 |
-
value=768,
|
| 318 |
)
|
| 319 |
height = gr.Slider(
|
| 320 |
label="Height",
|
| 321 |
minimum=256,
|
| 322 |
maximum=MAX_IMAGE_SIZE,
|
| 323 |
step=32,
|
| 324 |
-
value=768,
|
| 325 |
)
|
| 326 |
guidance_scale = gr.Slider(
|
| 327 |
label="Guidance scale",
|
| 328 |
minimum=0.0,
|
| 329 |
maximum=30.0,
|
| 330 |
step=0.1,
|
| 331 |
-
value=4.2,
|
| 332 |
)
|
| 333 |
num_inference_steps = gr.Slider(
|
| 334 |
label="Number of inference steps",
|
| 335 |
minimum=1,
|
| 336 |
maximum=500,
|
| 337 |
step=1,
|
| 338 |
-
value=
|
| 339 |
)
|
| 340 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 341 |
gr.on(
|
| 342 |
-
triggers=[
|
| 343 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
inputs=[
|
| 345 |
prompt,
|
| 346 |
negative_prompt_1,
|
| 347 |
negative_prompt_2,
|
| 348 |
negative_prompt_3,
|
| 349 |
-
seed,
|
| 350 |
-
randomize_seed,
|
| 351 |
width,
|
| 352 |
height,
|
| 353 |
guidance_scale,
|
| 354 |
num_inference_steps,
|
| 355 |
-
expanded,
|
| 356 |
-
latent_file, # Add latent_file to the inputs
|
| 357 |
],
|
| 358 |
outputs=[result, seed, expanded_prompt_output],
|
| 359 |
)
|
|
|
|
| 25 |
FTP_PASS = "GoogleBez12!"
|
| 26 |
FTP_DIR = "1ink.us/stable_diff/" # Remote directory on FTP server
|
| 27 |
|
| 28 |
+
torch.backends.cuda.matmul.allow_tf32 = False
|
| 29 |
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
| 30 |
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
|
| 31 |
+
torch.backends.cudnn.allow_tf32 = False
|
| 32 |
torch.backends.cudnn.deterministic = False
|
| 33 |
+
torch.backends.cudnn.benchmark = False
|
| 34 |
#torch.backends.cuda.preferred_blas_library="cublas"
|
| 35 |
#torch.backends.cuda.preferred_linalg_library="cusolver"
|
| 36 |
|
| 37 |
hftoken = os.getenv("HF_AUTH_TOKEN")
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def upload_to_ftp(filename):
|
| 40 |
try:
|
| 41 |
transport = paramiko.Transport((FTP_HOST, 22))
|
|
|
|
| 62 |
# text_encoder_3=T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_3',token=True),
|
| 63 |
#tokenizer=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer", token=True),
|
| 64 |
#tokenizer_2=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer_2", token=True),
|
| 65 |
+
#tokenizer_3=T5TokenizerFast.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", use_fast=True, subfolder="tokenizer_3", token=True),
|
| 66 |
torch_dtype=torch.bfloat16,
|
| 67 |
#use_safetensors=False,
|
| 68 |
)
|
|
|
|
| 73 |
pipe.to(device)
|
| 74 |
#pipe.to(device=device, dtype=torch.bfloat16)
|
| 75 |
|
| 76 |
+
upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device('cpu'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
MAX_SEED = np.iinfo(np.int32).max
|
| 79 |
+
|
| 80 |
MAX_IMAGE_SIZE = 4096
|
| 81 |
|
| 82 |
+
@spaces.GPU(duration=30)
|
| 83 |
+
def infer_30(
|
| 84 |
prompt,
|
| 85 |
negative_prompt_1,
|
| 86 |
negative_prompt_2,
|
| 87 |
negative_prompt_3,
|
|
|
|
|
|
|
| 88 |
width,
|
| 89 |
height,
|
| 90 |
guidance_scale,
|
| 91 |
num_inference_steps,
|
|
|
|
|
|
|
| 92 |
progress=gr.Progress(track_tqdm=True),
|
| 93 |
):
|
|
|
|
| 94 |
torch.set_float32_matmul_precision("highest")
|
| 95 |
seed = random.randint(0, MAX_SEED)
|
| 96 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 97 |
+
print('-- generating image --')
|
| 98 |
+
sd_image = pipe(
|
| 99 |
+
prompt=prompt,
|
| 100 |
+
prompt_2=prompt,
|
| 101 |
+
prompt_3=prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
negative_prompt=negative_prompt_1,
|
| 103 |
+
negative_prompt_2=negative_prompt_2,
|
| 104 |
+
negative_prompt_3=negative_prompt_3,
|
| 105 |
guidance_scale=guidance_scale,
|
| 106 |
num_inference_steps=num_inference_steps,
|
| 107 |
width=width,
|
| 108 |
height=height,
|
| 109 |
+
generator=generator,
|
| 110 |
+
max_sequence_length=512
|
| 111 |
+
).images[0]
|
| 112 |
+
print('-- got image --')
|
| 113 |
+
sd35_path = f"sd35l_{seed}.png"
|
| 114 |
+
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 115 |
+
upload_to_ftp(sd35_path)
|
| 116 |
+
# pipe.unet.to('cpu')
|
| 117 |
+
upscaler_2.to(torch.device('cuda'))
|
| 118 |
+
with torch.no_grad():
|
| 119 |
+
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 120 |
+
print('-- got upscaled image --')
|
| 121 |
+
downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
|
| 122 |
+
upscale_path = f"sd35l_upscale_{seed}.png"
|
| 123 |
+
downscale2.save(upscale_path,optimize=False,compress_level=0)
|
| 124 |
+
upload_to_ftp(upscale_path)
|
| 125 |
+
return sd_image, seed, enhanced_prompt
|
| 126 |
+
|
| 127 |
+
@spaces.GPU(duration=60)
|
| 128 |
+
def infer_60(
|
| 129 |
+
prompt,
|
| 130 |
+
negative_prompt_1,
|
| 131 |
+
negative_prompt_2,
|
| 132 |
+
negative_prompt_3,
|
| 133 |
+
width,
|
| 134 |
+
height,
|
| 135 |
+
guidance_scale,
|
| 136 |
+
num_inference_steps,
|
| 137 |
+
progress=gr.Progress(track_tqdm=True),
|
| 138 |
+
):
|
| 139 |
+
torch.set_float32_matmul_precision("highest")
|
| 140 |
+
seed = random.randint(0, MAX_SEED)
|
| 141 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 142 |
+
print('-- generating image --')
|
| 143 |
+
sd_image = pipe(
|
| 144 |
+
prompt=prompt,
|
| 145 |
+
prompt_2=prompt,
|
| 146 |
+
prompt_3=prompt,
|
| 147 |
negative_prompt=negative_prompt_1,
|
| 148 |
negative_prompt_2=negative_prompt_2,
|
| 149 |
negative_prompt_3=negative_prompt_3,
|
|
|
|
| 151 |
num_inference_steps=num_inference_steps,
|
| 152 |
width=width,
|
| 153 |
height=height,
|
|
|
|
|
|
|
| 154 |
generator=generator,
|
| 155 |
max_sequence_length=512
|
| 156 |
+
).images[0]
|
| 157 |
+
print('-- got image --')
|
| 158 |
+
sd35_path = f"sd35l_{seed}.png"
|
| 159 |
+
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 160 |
+
upload_to_ftp(sd35_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# pipe.unet.to('cpu')
|
| 162 |
upscaler_2.to(torch.device('cuda'))
|
| 163 |
with torch.no_grad():
|
| 164 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 165 |
print('-- got upscaled image --')
|
|
|
|
| 166 |
downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
|
| 167 |
upscale_path = f"sd35l_upscale_{seed}.png"
|
| 168 |
downscale2.save(upscale_path,optimize=False,compress_level=0)
|
| 169 |
upload_to_ftp(upscale_path)
|
| 170 |
return sd_image, seed, enhanced_prompt
|
| 171 |
|
| 172 |
+
@spaces.GPU(duration=90)
|
| 173 |
+
def infer_90(
|
| 174 |
+
prompt,
|
| 175 |
+
negative_prompt_1,
|
| 176 |
+
negative_prompt_2,
|
| 177 |
+
negative_prompt_3,
|
| 178 |
+
width,
|
| 179 |
+
height,
|
| 180 |
+
guidance_scale,
|
| 181 |
+
num_inference_steps,
|
| 182 |
+
progress=gr.Progress(track_tqdm=True),
|
| 183 |
+
):
|
| 184 |
+
torch.set_float32_matmul_precision("highest")
|
| 185 |
+
seed = random.randint(0, MAX_SEED)
|
| 186 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 187 |
+
print('-- generating image --')
|
| 188 |
+
sd_image = pipe(
|
| 189 |
+
prompt=prompt,
|
| 190 |
+
prompt_2=prompt,
|
| 191 |
+
prompt_3=prompt,
|
| 192 |
+
negative_prompt=negative_prompt_1,
|
| 193 |
+
negative_prompt_2=negative_prompt_2,
|
| 194 |
+
negative_prompt_3=negative_prompt_3,
|
| 195 |
+
guidance_scale=guidance_scale,
|
| 196 |
+
num_inference_steps=num_inference_steps,
|
| 197 |
+
width=width,
|
| 198 |
+
height=height,
|
| 199 |
+
generator=generator,
|
| 200 |
+
max_sequence_length=512
|
| 201 |
+
).images[0]
|
| 202 |
+
print('-- got image --')
|
| 203 |
+
sd35_path = f"sd35l_{seed}.png"
|
| 204 |
+
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 205 |
+
upload_to_ftp(sd35_path)
|
| 206 |
+
# pipe.unet.to('cpu')
|
| 207 |
+
upscaler_2.to(torch.device('cuda'))
|
| 208 |
+
with torch.no_grad():
|
| 209 |
+
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 210 |
+
print('-- got upscaled image --')
|
| 211 |
+
downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
|
| 212 |
+
upscale_path = f"sd35l_upscale_{seed}.png"
|
| 213 |
+
downscale2.save(upscale_path,optimize=False,compress_level=0)
|
| 214 |
+
upload_to_ftp(upscale_path)
|
| 215 |
+
return sd_image, seed, enhanced_prompt
|
| 216 |
|
| 217 |
+
@spaces.GPU(duration=100)
|
| 218 |
+
def infer_100(
|
| 219 |
prompt,
|
| 220 |
+
negative_prompt_1,
|
| 221 |
+
negative_prompt_2,
|
| 222 |
+
negative_prompt_3,
|
| 223 |
width,
|
| 224 |
height,
|
| 225 |
guidance_scale,
|
| 226 |
num_inference_steps,
|
| 227 |
+
progress=gr.Progress(track_tqdm=True),
|
| 228 |
):
|
| 229 |
+
torch.set_float32_matmul_precision("highest")
|
| 230 |
+
seed = random.randint(0, MAX_SEED)
|
| 231 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 232 |
+
print('-- generating image --')
|
| 233 |
+
sd_image = pipe(
|
| 234 |
+
prompt=prompt,
|
| 235 |
+
prompt_2=prompt,
|
| 236 |
+
prompt_3=prompt,
|
| 237 |
+
negative_prompt=negative_prompt_1,
|
| 238 |
+
negative_prompt_2=negative_prompt_2,
|
| 239 |
+
negative_prompt_3=negative_prompt_3,
|
| 240 |
+
guidance_scale=guidance_scale,
|
| 241 |
+
num_inference_steps=num_inference_steps,
|
| 242 |
+
width=width,
|
| 243 |
+
height=height,
|
| 244 |
+
generator=generator,
|
| 245 |
+
max_sequence_length=512
|
| 246 |
+
).images[0]
|
| 247 |
+
print('-- got image --')
|
| 248 |
+
sd35_path = f"sd35l_{seed}.png"
|
| 249 |
+
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 250 |
+
upload_to_ftp(sd35_path)
|
| 251 |
+
# pipe.unet.to('cpu')
|
| 252 |
+
upscaler_2.to(torch.device('cuda'))
|
| 253 |
+
with torch.no_grad():
|
| 254 |
+
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 255 |
+
print('-- got upscaled image --')
|
| 256 |
+
downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
|
| 257 |
+
upscale_path = f"sd35l_upscale_{seed}.png"
|
| 258 |
+
downscale2.save(upscale_path,optimize=False,compress_level=0)
|
| 259 |
+
upload_to_ftp(upscale_path)
|
| 260 |
+
return sd_image, seed, enhanced_prompt
|
| 261 |
+
|
| 262 |
+
css = """
|
| 263 |
+
#col-container {margin: 0 auto;max-width: 640px;}
|
| 264 |
+
body{background-color: blue;}
|
| 265 |
+
"""
|
| 266 |
|
| 267 |
with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
| 268 |
with gr.Column(elem_id="col-container"):
|
| 269 |
+
gr.Markdown(" # Text-to-Image StableDiffusion 3.5 Large")
|
| 270 |
+
expanded_prompt_output = gr.Textbox(label="Prompt", lines=1) # Add this line
|
| 271 |
with gr.Row():
|
| 272 |
prompt = gr.Text(
|
| 273 |
label="Prompt",
|
|
|
|
| 276 |
placeholder="Enter your prompt",
|
| 277 |
container=False,
|
| 278 |
)
|
| 279 |
+
run_button_30 = gr.Button("Run 30", scale=0, variant="primary")
|
| 280 |
+
run_button_60 = gr.Button("Run 60", scale=0, variant="primary")
|
| 281 |
+
run_button_90 = gr.Button("Run 90", scale=0, variant="primary")
|
| 282 |
+
run_button_100 = gr.Button("Run 100", scale=0, variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
result = gr.Image(label="Result", show_label=False)
|
| 284 |
with gr.Accordion("Advanced Settings", open=True):
|
|
|
|
| 285 |
negative_prompt_1 = gr.Text(
|
| 286 |
label="Negative prompt 1",
|
| 287 |
max_lines=1,
|
|
|
|
| 306 |
num_iterations = gr.Number(
|
| 307 |
value=1000,
|
| 308 |
label="Number of Iterations")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
with gr.Row():
|
| 310 |
width = gr.Slider(
|
| 311 |
label="Width",
|
| 312 |
minimum=256,
|
| 313 |
maximum=MAX_IMAGE_SIZE,
|
| 314 |
step=32,
|
| 315 |
+
value=768,
|
| 316 |
)
|
| 317 |
height = gr.Slider(
|
| 318 |
label="Height",
|
| 319 |
minimum=256,
|
| 320 |
maximum=MAX_IMAGE_SIZE,
|
| 321 |
step=32,
|
| 322 |
+
value=768,
|
| 323 |
)
|
| 324 |
guidance_scale = gr.Slider(
|
| 325 |
label="Guidance scale",
|
| 326 |
minimum=0.0,
|
| 327 |
maximum=30.0,
|
| 328 |
step=0.1,
|
| 329 |
+
value=4.2,
|
| 330 |
)
|
| 331 |
num_inference_steps = gr.Slider(
|
| 332 |
label="Number of inference steps",
|
| 333 |
minimum=1,
|
| 334 |
maximum=500,
|
| 335 |
step=1,
|
| 336 |
+
value=50,
|
| 337 |
)
|
|
|
|
| 338 |
gr.on(
|
| 339 |
+
triggers=[run_button_30.click, prompt.submit],
|
| 340 |
+
fn=infer_30,
|
| 341 |
+
inputs=[
|
| 342 |
+
prompt,
|
| 343 |
+
negative_prompt_1,
|
| 344 |
+
negative_prompt_2,
|
| 345 |
+
negative_prompt_3,
|
| 346 |
+
width,
|
| 347 |
+
height,
|
| 348 |
+
guidance_scale,
|
| 349 |
+
num_inference_steps,
|
| 350 |
+
],
|
| 351 |
+
outputs=[result, seed, expanded_prompt_output],
|
| 352 |
+
)
|
| 353 |
+
gr.on(
|
| 354 |
+
triggers=[run_button_60.click, prompt.submit],
|
| 355 |
+
fn=infer_60,
|
| 356 |
+
inputs=[
|
| 357 |
+
prompt,
|
| 358 |
+
negative_prompt_1,
|
| 359 |
+
negative_prompt_2,
|
| 360 |
+
negative_prompt_3,
|
| 361 |
+
width,
|
| 362 |
+
height,
|
| 363 |
+
guidance_scale,
|
| 364 |
+
num_inference_steps,
|
| 365 |
+
],
|
| 366 |
+
outputs=[result, seed, expanded_prompt_output],
|
| 367 |
+
)
|
| 368 |
+
gr.on(
|
| 369 |
+
triggers=[run_button_90.click, prompt.submit],
|
| 370 |
+
fn=infer_90,
|
| 371 |
+
inputs=[
|
| 372 |
+
prompt,
|
| 373 |
+
negative_prompt_1,
|
| 374 |
+
negative_prompt_2,
|
| 375 |
+
negative_prompt_3,
|
| 376 |
+
width,
|
| 377 |
+
height,
|
| 378 |
+
guidance_scale,
|
| 379 |
+
num_inference_steps,
|
| 380 |
+
],
|
| 381 |
+
outputs=[result, seed, expanded_prompt_output],
|
| 382 |
+
)
|
| 383 |
+
gr.on(
|
| 384 |
+
triggers=[run_button_100.click, prompt.submit],
|
| 385 |
+
fn=infer_100,
|
| 386 |
inputs=[
|
| 387 |
prompt,
|
| 388 |
negative_prompt_1,
|
| 389 |
negative_prompt_2,
|
| 390 |
negative_prompt_3,
|
|
|
|
|
|
|
| 391 |
width,
|
| 392 |
height,
|
| 393 |
guidance_scale,
|
| 394 |
num_inference_steps,
|
|
|
|
|
|
|
| 395 |
],
|
| 396 |
outputs=[result, seed, expanded_prompt_output],
|
| 397 |
)
|