ford442 commited on
Commit
992baec
·
verified ·
1 Parent(s): b66268c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -64
app.py CHANGED
@@ -13,13 +13,13 @@ import numpy as np
13
  from PIL import Image
14
  import torch
15
  from diffusers import AutoencoderKL, StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
 
16
  from typing import Tuple
17
  import paramiko
18
  import datetime
19
  from gradio import themes
20
  from image_gen_aux import UpscaleWithModel
21
  from ip_adapter import IPAdapterXL
22
-
23
  from huggingface_hub import snapshot_download
24
 
25
  torch.backends.cuda.matmul.allow_tf32 = False
@@ -28,7 +28,7 @@ torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
28
  torch.backends.cudnn.allow_tf32 = False
29
  torch.backends.cudnn.deterministic = False
30
  torch.backends.cudnn.benchmark = False
31
- torch.backends.cuda.preferred_blas_library="cublas"
32
  # torch.backends.cuda.preferred_linalg_library="cusolver"
33
 
34
  torch.set_float32_matmul_precision("highest")
@@ -92,7 +92,6 @@ local_folder = os.path.join(local_repo_path, subfolder)
92
  local_folder2 = os.path.join(local_repo_path, subfolder2) # Path to the ip_adapter dir
93
  ip_ckpt = os.path.join(local_folder2, "ip-adapter_sdxl_vit-h.bin") # Correct path
94
 
95
- #sampling_schedule = AysSchedules["StableDiffusionXLTimesteps"]
96
  upscaler = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device("cuda:0"))
97
 
98
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
@@ -106,20 +105,7 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
106
 
107
  def load_and_prepare_model():
108
  #vae = AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16", safety_checker=None)
109
- vaeX = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae", safety_checker=None,use_safetensors=False, token=HF_TOKEN)
110
- #vae = AutoencoderKL.from_pretrained('cross-attention/asymmetric-autoencoder-kl-x-2',use_safetensors=False)
111
- #vae = AutoencoderKL.from_single_file('https://huggingface.co/ford442/sdxl-vae-bf16/mySLR/myslrVAE_v10.safetensors')
112
- #vaeX = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse",use_safetensors=True)
113
- #vaeX = AutoencoderKL.from_pretrained('ford442/Juggernaut-XI-v11-fp32',subfolder='vae') # ,use_safetensors=True FAILS
114
- #vaeX = AutoencoderKL.from_pretrained('ford442/RealVisXL_V5.0_FP64',subfolder='vae').to(torch.bfloat16) # ,use_safetensors=True FAILS
115
- #unetX = UNet2DConditionModel.from_pretrained('ford442/RealVisXL_V5.0_BF16',subfolder='unet').to(torch.bfloat16) # ,use_safetensors=True FAILS
116
- # vae = AutoencoderKL.from_pretrained("BeastHF/MyBack_SDXL_Juggernaut_XL_VAE/MyBack_SDXL_Juggernaut_XL_VAE_V10(version_X).safetensors",safety_checker=None).to(torch.bfloat16)
117
- #sched = EulerAncestralDiscreteScheduler.from_pretrained("SG161222/RealVisXL_V5.0", subfolder='scheduler',beta_schedule="scaled_linear", steps_offset=1,timestep_spacing="trailing"))
118
- #sched = EulerAncestralDiscreteScheduler.from_pretrained("SG161222/RealVisXL_V5.0", subfolder='scheduler', steps_offset=1,timestep_spacing="trailing")
119
- sched = EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear", token=HF_TOKEN) #, beta_start=0.00085, beta_end=0.012, steps_offset=1,use_karras_sigmas=True, token=HF_TOKEN)
120
- #sched = EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear")
121
- #pipeX = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V5.0").to(torch.bfloat16)
122
- #pipeX = StableDiffusionXLPipeline.from_pretrained("ford442/Juggernaut-XI-v11-fp32",use_safetensors=True)
123
  pipe = StableDiffusionXLPipeline.from_pretrained(
124
  'ford442/RealVisXL_V5.0_BF16',
125
  #'ford442/Juggernaut-XI-v11-fp32',
@@ -130,22 +116,10 @@ def load_and_prepare_model():
130
  # custom_pipeline="lpw_stable_diffusion_xl",
131
  #use_safetensors=True,
132
  token=HF_TOKEN,
133
- # vae=AutoencoderKL.from_pretrained("BeastHF/MyBack_SDXL_Juggernaut_XL_VAE/MyBack_SDXL_Juggernaut_XL_VAE_V10(version_X).safetensors",repo_type='model',safety_checker=None),
134
- # vae=AutoencoderKL.from_pretrained("stabilityai/sdxl-vae",repo_type='model',safety_checker=None, torch_dtype=torch.float32),
135
- # vae=AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16",repo_type='model',safety_checker=None),
136
- #vae=vae,
137
- #unet=pipeX.unet,
138
- #scheduler = sched,
139
- # scheduler = EulerAncestralDiscreteScheduler.from_config(pipeX.scheduler.config, beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1)
140
- #scheduler=EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset =1)
141
  )
142
- #pipe.vae = AsymmetricAutoencoderKL.from_pretrained('cross-attention/asymmetric-autoencoder-kl-x-2').to(torch.bfloat16) # ,use_safetensors=True FAILS
143
-
144
- #pipe.vae = AutoencoderKL.from_pretrained('ford442/Juggernaut-XI-v11-fp32',subfolder='vae') # ,use_safetensors=True FAILS
145
-
146
- #pipe.vae = AutoencoderKL.from_pretrained('stabilityai/sdxl-vae-bf16',subfolder='vae')
147
- #pipe.vae = AutoencoderKL.from_pretrained('stabilityai/sdxl-vae',subfolder='vae',force_upcast=False,scaling_factor= 0.182158767676)
148
- #pipe.vae.to(torch.bfloat16)
149
 
150
  '''
151
  scaling_factor (`float`, *optional*, defaults to 0.18215):
@@ -162,30 +136,13 @@ def load_and_prepare_model():
162
 
163
  '''
164
 
165
- #sched = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear",use_karras_sigmas=True, algorithm_type="dpmsolver++")
166
- #pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1)
167
- #pipe.scheduler = DPMSolverMultistepScheduler.from_pretrained('SG161222/RealVisXL_V5.0', subfolder='scheduler', algorithm_type='sde-dpmsolver++')
168
  pipe.vae = vaeX #.to(torch.bfloat16)
169
- #pipe.unet = unetX
170
-
171
  pipe.vae.do_resize=False
172
  #pipe.vae.do_rescale=False
173
  #pipe.vae.do_convert_rgb=True
174
- pipe.vae.vae_scale_factor=8
175
-
176
- pipe.scheduler = sched
177
- #pipe.vae=vae.to(torch.bfloat16)
178
- #pipe.unet=pipeX.unet
179
- #pipe.scheduler=EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1)
180
- #pipe.scheduler=EulerAncestralDiscreteScheduler.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='scheduler',beta_schedule="scaled_linear")
181
-
182
- pipe.to(device=device, dtype=torch.bfloat16)
183
- #pipe.to(torch.bfloat16)
184
- #apply_hidiffusion(pipe)
185
-
186
- #pipe.unet.set_default_attn_processor()
187
  pipe.vae.set_default_attn_processor()
188
-
189
  print(f'Pipeline: ')
190
  #print(f'_optional_components: {pipe._optional_components}')
191
  #print(f'watermark: {pipe.watermark}')
@@ -195,19 +152,14 @@ def load_and_prepare_model():
195
  #print(f'UNET: {pipe.unet}')
196
  pipe.watermark=None
197
  pipe.safety_checker=None
198
- #pipe.to(torch.device("cuda:0"))
199
- #pipe.vae.to(torch.bfloat16)
200
- #pipe.to(device, torch.bfloat16)
201
- #del pipeX
202
- #sched = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear", algorithm_type="dpmsolver++")
203
- #sched = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, beta_schedule="linear", algorithm_type="dpmsolver++")
204
- #sched = DDIMScheduler.from_config(pipe.scheduler.config)
205
  return pipe
206
 
207
  # Preload and compile both models
208
  pipe = load_and_prepare_model()
209
  ip_model = IPAdapterXL(pipe, local_folder, ip_ckpt, device)
210
-
 
 
211
  MAX_SEED = np.iinfo(np.int32).max
212
 
213
  neg_prompt_2 = " 'non-photorealistic':1.5, 'unrealistic skin','unattractive face':1.3, 'low quality':1.1, ('dull color scheme', 'dull colors', 'digital noise':1.2),'amateurish', 'poorly drawn face':1.3, 'poorly drawn', 'distorted face', 'low resolution', 'simplistic' "
@@ -270,6 +222,8 @@ def generate_30(
270
  samples=1,
271
  progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
272
  ):
 
 
273
  seed = random.randint(0, MAX_SEED)
274
  generator = torch.Generator(device='cuda').manual_seed(seed)
275
  if latent_file is not None: # Check if a latent file is provided
@@ -298,7 +252,6 @@ def generate_30(
298
  filename= f'rv_IP_{timestamp}.png'
299
  print("-- using image file --")
300
  print('-- generating image --')
301
- #with torch.no_grad():
302
  sd_image = ip_model.generate(
303
  pil_image_1=sd_image_a,
304
  pil_image_2=sd_image_b,
@@ -322,7 +275,7 @@ def generate_30(
322
  sd_image[0].save(filename,optimize=False,compress_level=0)
323
  upload_to_ftp(filename)
324
  uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
325
- #torch.setfloat32_matmul_precision("medium")
326
  with torch.no_grad():
327
  upscale = upscaler(sd_image, tiling=True, tile_width=256, tile_height=256)
328
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
@@ -359,6 +312,8 @@ def generate_60(
359
  samples=1,
360
  progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
361
  ):
 
 
362
  seed = random.randint(0, MAX_SEED)
363
  generator = torch.Generator(device='cuda').manual_seed(seed)
364
  if latent_file is not None: # Check if a latent file is provided
@@ -387,7 +342,6 @@ def generate_60(
387
  filename= f'rv_IP_{timestamp}.png'
388
  print("-- using image file --")
389
  print('-- generating image --')
390
- #with torch.no_grad():
391
  sd_image = ip_model.generate(
392
  pil_image_1=sd_image_a,
393
  pil_image_2=sd_image_b,
@@ -411,7 +365,7 @@ def generate_60(
411
  sd_image[0].save(filename,optimize=False,compress_level=0)
412
  upload_to_ftp(filename)
413
  uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
414
- #torch.setfloat32_matmul_precision("medium")
415
  with torch.no_grad():
416
  upscale = upscaler(sd_image, tiling=True, tile_width=256, tile_height=256)
417
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
@@ -448,6 +402,8 @@ def generate_90(
448
  samples=1,
449
  progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
450
  ):
 
 
451
  seed = random.randint(0, MAX_SEED)
452
  generator = torch.Generator(device='cuda').manual_seed(seed)
453
  if latent_file is not None: # Check if a latent file is provided
@@ -500,7 +456,7 @@ def generate_90(
500
  sd_image[0].save(filename,optimize=False,compress_level=0)
501
  upload_to_ftp(filename)
502
  uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
503
- #torch.setfloat32_matmul_precision("medium")
504
  with torch.no_grad():
505
  upscale = upscaler(sd_image, tiling=True, tile_width=256, tile_height=256)
506
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
 
13
  from PIL import Image
14
  import torch
15
  from diffusers import AutoencoderKL, StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
16
+ from transformers import CLIPTextModelWithProjection, CLIPTextModel
17
  from typing import Tuple
18
  import paramiko
19
  import datetime
20
  from gradio import themes
21
  from image_gen_aux import UpscaleWithModel
22
  from ip_adapter import IPAdapterXL
 
23
  from huggingface_hub import snapshot_download
24
 
25
  torch.backends.cuda.matmul.allow_tf32 = False
 
28
  torch.backends.cudnn.allow_tf32 = False
29
  torch.backends.cudnn.deterministic = False
30
  torch.backends.cudnn.benchmark = False
31
+ #torch.backends.cuda.preferred_blas_library="cublas"
32
  # torch.backends.cuda.preferred_linalg_library="cusolver"
33
 
34
  torch.set_float32_matmul_precision("highest")
 
92
  local_folder2 = os.path.join(local_repo_path, subfolder2) # Path to the ip_adapter dir
93
  ip_ckpt = os.path.join(local_folder2, "ip-adapter_sdxl_vit-h.bin") # Correct path
94
 
 
95
  upscaler = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device("cuda:0"))
96
 
97
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
 
105
 
106
  def load_and_prepare_model():
107
  #vae = AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16", safety_checker=None)
108
+ vaeX = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae", safety_checker=None, use_safetensors=False, low_cpu_mem_usage=False, torch_dtype=torch.float32, token=True) #.to(device).to(torch.bfloat16) #.to(device=device, dtype=torch.bfloat16)
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  pipe = StableDiffusionXLPipeline.from_pretrained(
110
  'ford442/RealVisXL_V5.0_BF16',
111
  #'ford442/Juggernaut-XI-v11-fp32',
 
116
  # custom_pipeline="lpw_stable_diffusion_xl",
117
  #use_safetensors=True,
118
  token=HF_TOKEN,
119
+ text_encoder=None,
120
+ text_encoder_2=None,
121
+ vae=None,
 
 
 
 
 
122
  )
 
 
 
 
 
 
 
123
 
124
  '''
125
  scaling_factor (`float`, *optional*, defaults to 0.18215):
 
136
 
137
  '''
138
 
139
+ pipe.to(device=device, dtype=torch.bfloat16)
 
 
140
  pipe.vae = vaeX #.to(torch.bfloat16)
 
 
141
  pipe.vae.do_resize=False
142
  #pipe.vae.do_rescale=False
143
  #pipe.vae.do_convert_rgb=True
144
+ pipe.vae.vae_scale_factor=8 #pipe.unet.set_default_attn_processor()
 
 
 
 
 
 
 
 
 
 
 
 
145
  pipe.vae.set_default_attn_processor()
 
146
  print(f'Pipeline: ')
147
  #print(f'_optional_components: {pipe._optional_components}')
148
  #print(f'watermark: {pipe.watermark}')
 
152
  #print(f'UNET: {pipe.unet}')
153
  pipe.watermark=None
154
  pipe.safety_checker=None
 
 
 
 
 
 
 
155
  return pipe
156
 
157
  # Preload and compile both models
158
  pipe = load_and_prepare_model()
159
  ip_model = IPAdapterXL(pipe, local_folder, ip_ckpt, device)
160
+ text_encoder=CLIPTextModel.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='text_encoder',token=True).to(device=device, dtype=torch.bfloat16)
161
+ text_encoder_2=CLIPTextModelWithProjection.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='text_encoder_2',token=True).to(device=device, dtype=torch.bfloat16)
162
+
163
  MAX_SEED = np.iinfo(np.int32).max
164
 
165
  neg_prompt_2 = " 'non-photorealistic':1.5, 'unrealistic skin','unattractive face':1.3, 'low quality':1.1, ('dull color scheme', 'dull colors', 'digital noise':1.2),'amateurish', 'poorly drawn face':1.3, 'poorly drawn', 'distorted face', 'low resolution', 'simplistic' "
 
222
  samples=1,
223
  progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
224
  ):
225
+ pipe.text_encoder=text_encoder
226
+ pipe.text_encoder_2=text_encoder_2
227
  seed = random.randint(0, MAX_SEED)
228
  generator = torch.Generator(device='cuda').manual_seed(seed)
229
  if latent_file is not None: # Check if a latent file is provided
 
252
  filename= f'rv_IP_{timestamp}.png'
253
  print("-- using image file --")
254
  print('-- generating image --')
 
255
  sd_image = ip_model.generate(
256
  pil_image_1=sd_image_a,
257
  pil_image_2=sd_image_b,
 
275
  sd_image[0].save(filename,optimize=False,compress_level=0)
276
  upload_to_ftp(filename)
277
  uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
278
+ torch.setfloat32_matmul_precision("medium")
279
  with torch.no_grad():
280
  upscale = upscaler(sd_image, tiling=True, tile_width=256, tile_height=256)
281
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
 
312
  samples=1,
313
  progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
314
  ):
315
+ pipe.text_encoder=text_encoder
316
+ pipe.text_encoder_2=text_encoder_2
317
  seed = random.randint(0, MAX_SEED)
318
  generator = torch.Generator(device='cuda').manual_seed(seed)
319
  if latent_file is not None: # Check if a latent file is provided
 
342
  filename= f'rv_IP_{timestamp}.png'
343
  print("-- using image file --")
344
  print('-- generating image --')
 
345
  sd_image = ip_model.generate(
346
  pil_image_1=sd_image_a,
347
  pil_image_2=sd_image_b,
 
365
  sd_image[0].save(filename,optimize=False,compress_level=0)
366
  upload_to_ftp(filename)
367
  uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
368
+ torch.setfloat32_matmul_precision("medium")
369
  with torch.no_grad():
370
  upscale = upscaler(sd_image, tiling=True, tile_width=256, tile_height=256)
371
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
 
402
  samples=1,
403
  progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
404
  ):
405
+ pipe.text_encoder=text_encoder
406
+ pipe.text_encoder_2=text_encoder_2
407
  seed = random.randint(0, MAX_SEED)
408
  generator = torch.Generator(device='cuda').manual_seed(seed)
409
  if latent_file is not None: # Check if a latent file is provided
 
456
  sd_image[0].save(filename,optimize=False,compress_level=0)
457
  upload_to_ftp(filename)
458
  uploadNote(prompt,num_inference_steps,guidance_scale,timestamp)
459
+ torch.setfloat32_matmul_precision("medium")
460
  with torch.no_grad():
461
  upscale = upscaler(sd_image, tiling=True, tile_width=256, tile_height=256)
462
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)