ford442 commited on
Commit
4da7341
·
verified ·
1 Parent(s): 54c56db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -30,15 +30,15 @@ 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
- 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:
@@ -62,9 +62,9 @@ checkpoint = "microsoft/Phi-3.5-mini-instruct"
62
  #vae = AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16")
63
  vaeXL = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae", safety_checker=None, use_safetensors=False) #, device_map='cpu') #.to(torch.bfloat16) #.to(device=device, dtype=torch.bfloat16)
64
 
65
- pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-medium-bf16").to(device=device, dtype=torch.bfloat16)
66
  #pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-medium-bf16").to(torch.device("cuda:0"))
67
- #pipe = StableDiffusion3Pipeline.from_pretrained("ford442/RealVis_Medium_1.0b_bf16", torch_dtype=torch.bfloat16)
68
  #pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium", token=hftoken, torch_dtype=torch.float32, device_map='balanced')
69
 
70
  # pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
@@ -76,9 +76,9 @@ pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-me
76
  #pipe = torch.compile(pipe)
77
  # pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear")
78
 
79
- refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("ford442/stable-diffusion-xl-refiner-1.0-bf16",vae = vaeXL, requires_aesthetics_score=True) #.to(torch.bfloat16)
80
  #refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float32, requires_aesthetics_score=True, device_map='balanced')
81
- refiner.scheduler=EulerAncestralDiscreteScheduler.from_config(refiner.scheduler.config)
82
  #refiner.enable_model_cpu_offload()
83
 
84
  #pipe.to(device=device, dtype=torch.bfloat16)
@@ -94,7 +94,7 @@ tokenizer.tokenizer_legacy=False
94
  model = AutoModelForCausalLM.from_pretrained(checkpoint).to('cuda')
95
  #model = torch.compile(model)
96
 
97
- upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device("cuda:0"))
98
 
99
  def filter_text(text,phraseC):
100
  """Filters out the text up to and including 'Rewritten Prompt:'."""
@@ -136,7 +136,7 @@ def infer(
136
  latent_file, # Add latents file input
137
  progress=gr.Progress(track_tqdm=True),
138
  ):
139
- upscaler_2.to(torch.device('cpu'))
140
  torch.set_float32_matmul_precision("highest")
141
  seed = random.randint(0, MAX_SEED)
142
  generator = torch.Generator(device='cuda').manual_seed(seed)
@@ -193,7 +193,7 @@ def infer(
193
  else:
194
  enhanced_prompt = prompt
195
  enhanced_prompt_2 = prompt
196
- model.to('cpu')
197
  if latent_file: # Check if a latent file is provided
198
  # initial_latents = pipe.prepare_latents(
199
  # batch_size=1,
@@ -241,6 +241,7 @@ def infer(
241
  max_sequence_length=512
242
  ).images[0]
243
  print('-- got image --')
 
244
  #sd35_image = pipe.vae.decode(sd_image / 0.18215).sample
245
  # sd35_image = sd35_image.cpu().permute(0, 2, 3, 1).float().detach().numpy()
246
  # sd35_image = (sd35_image * 255).round().astype("uint8")
@@ -248,7 +249,7 @@ def infer(
248
  # sd35_path = f"sd35_{seed}.png"
249
  # image_pil.save(sd35_path,optimize=False,compress_level=0)
250
  # upload_to_ftp(sd35_path)
251
- sd35_path = f"sd35_{seed}.png"
252
  sd_image.save(sd35_path,optimize=False,compress_level=0)
253
  upload_to_ftp(sd35_path)
254
  # Convert the generated image to a tensor
@@ -261,6 +262,7 @@ def infer(
261
  #torch.save(generated_latents, latent_path)
262
  #upload_to_ftp(latent_path)
263
  #refiner.scheduler.set_timesteps(num_inference_steps,device)
 
264
  pipe.to(torch.device('cpu'))
265
  refiner.to(device=device, dtype=torch.bfloat16)
266
  refine = refiner(
@@ -276,13 +278,14 @@ def infer(
276
  refine.save(refine_path,optimize=False,compress_level=0)
277
  upload_to_ftp(refine_path)
278
  refiner.to(torch.device('cpu'))
 
279
  upscaler_2.to(torch.device('cuda'))
280
  with torch.no_grad():
281
- upscale2 = upscaler_2(refine, tiling=True, tile_width=256, tile_height=256)
282
  print('-- got upscaled image --')
283
- upscaler_2.to(torch.device('cpu'))
284
  downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
285
- upscale_path = f"sd35_upscale_{seed}.png"
286
  downscale2.save(upscale_path,optimize=False,compress_level=0)
287
  upload_to_ftp(upscale_path)
288
  return refine, seed, enhanced_prompt
 
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
+ #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:
 
62
  #vae = AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16")
63
  vaeXL = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae", safety_checker=None, use_safetensors=False) #, device_map='cpu') #.to(torch.bfloat16) #.to(device=device, dtype=torch.bfloat16)
64
 
65
+ #pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-medium-bf16").to(device=device, dtype=torch.bfloat16)
66
  #pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-medium-bf16").to(torch.device("cuda:0"))
67
+ pipe = StableDiffusion3Pipeline.from_pretrained("ford442/RealVis_Medium_1.0b_bf16", torch_dtype=torch.bfloat16)
68
  #pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium", token=hftoken, torch_dtype=torch.float32, device_map='balanced')
69
 
70
  # pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
 
76
  #pipe = torch.compile(pipe)
77
  # pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear")
78
 
79
+ #refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("ford442/stable-diffusion-xl-refiner-1.0-bf16",vae = vaeXL, requires_aesthetics_score=True) #.to(torch.bfloat16)
80
  #refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float32, requires_aesthetics_score=True, device_map='balanced')
81
+ #refiner.scheduler=EulerAncestralDiscreteScheduler.from_config(refiner.scheduler.config)
82
  #refiner.enable_model_cpu_offload()
83
 
84
  #pipe.to(device=device, dtype=torch.bfloat16)
 
94
  model = AutoModelForCausalLM.from_pretrained(checkpoint).to('cuda')
95
  #model = torch.compile(model)
96
 
97
+ upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1") #.to(torch.device("cuda:0"))
98
 
99
  def filter_text(text,phraseC):
100
  """Filters out the text up to and including 'Rewritten Prompt:'."""
 
136
  latent_file, # Add latents file input
137
  progress=gr.Progress(track_tqdm=True),
138
  ):
139
+ #upscaler_2.to(torch.device('cpu'))
140
  torch.set_float32_matmul_precision("highest")
141
  seed = random.randint(0, MAX_SEED)
142
  generator = torch.Generator(device='cuda').manual_seed(seed)
 
193
  else:
194
  enhanced_prompt = prompt
195
  enhanced_prompt_2 = prompt
196
+ #model.to('cpu')
197
  if latent_file: # Check if a latent file is provided
198
  # initial_latents = pipe.prepare_latents(
199
  # batch_size=1,
 
241
  max_sequence_length=512
242
  ).images[0]
243
  print('-- got image --')
244
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
245
  #sd35_image = pipe.vae.decode(sd_image / 0.18215).sample
246
  # sd35_image = sd35_image.cpu().permute(0, 2, 3, 1).float().detach().numpy()
247
  # sd35_image = (sd35_image * 255).round().astype("uint8")
 
249
  # sd35_path = f"sd35_{seed}.png"
250
  # image_pil.save(sd35_path,optimize=False,compress_level=0)
251
  # upload_to_ftp(sd35_path)
252
+ sd35_path = f"sd35_{timestamp}.png"
253
  sd_image.save(sd35_path,optimize=False,compress_level=0)
254
  upload_to_ftp(sd35_path)
255
  # Convert the generated image to a tensor
 
262
  #torch.save(generated_latents, latent_path)
263
  #upload_to_ftp(latent_path)
264
  #refiner.scheduler.set_timesteps(num_inference_steps,device)
265
+ '''
266
  pipe.to(torch.device('cpu'))
267
  refiner.to(device=device, dtype=torch.bfloat16)
268
  refine = refiner(
 
278
  refine.save(refine_path,optimize=False,compress_level=0)
279
  upload_to_ftp(refine_path)
280
  refiner.to(torch.device('cpu'))
281
+ '''
282
  upscaler_2.to(torch.device('cuda'))
283
  with torch.no_grad():
284
+ upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
285
  print('-- got upscaled image --')
286
+ #upscaler_2.to(torch.device('cpu'))
287
  downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
288
+ upscale_path = f"sd35_upscale_{timestamp}.png"
289
  downscale2.save(upscale_path,optimize=False,compress_level=0)
290
  upload_to_ftp(upscale_path)
291
  return refine, seed, enhanced_prompt