1inkusFace commited on
Commit
957155d
·
verified ·
1 Parent(s): ab86ad9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -96,6 +96,26 @@ def upload_to_gcs(image_bytes, filename):
96
  except Exception as e:
97
  print(f"❌ An error occurred during GCS upload for {filename}: {e}")
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  def srgb_to_linear(tensor_srgb):
100
  """Converts a batched sRGB PyTorch tensor [0, 1] to a linear tensor."""
101
  return torch.where(
@@ -118,7 +138,7 @@ def create_hdr_avif_bytes(sdr_pil_image):
118
  hdr_16bit_array = (np.clip(linear_numpy_float, 0, 1) * 65535).astype(np.uint16)
119
 
120
  # 4. Create a PIL image that holds the 16-bit data
121
- hdr_pil_image = Image.fromarray(hdr_16bit_array)
122
 
123
  # 5. Save to a bytes buffer as 10-bit AVIF with HDR10 metadata
124
  buffer = io.BytesIO()
@@ -240,9 +260,9 @@ def generate_images_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width,
240
  upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
241
  upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
242
  print('-- got upscaled image --')
243
- downscaled_upscale = upscale2.resize((upscale2.width // 16, upscale2.height // 16), Image.LANCZOS)
244
- sd_avif_bytes = create_hdr_avif_bytes(downscaled_upscale)
245
- return sd_avif_bytes, sd_avif_bytes, prompt
246
 
247
  @spaces.GPU(duration=70)
248
  def generate_images_60(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress=gr.Progress(track_tqdm=True)):
@@ -265,9 +285,9 @@ def generate_images_60(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width,
265
  upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
266
  upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
267
  print('-- got upscaled image --')
268
- downscaled_upscale = upscale2.resize((upscale2.width // 16, upscale2.height // 16), Image.LANCZOS)
269
- sd_avif_bytes = create_hdr_avif_bytes(downscaled_upscale)
270
- return sd_avif_bytes, sd_avif_bytes, prompt
271
 
272
  @spaces.GPU(duration=120)
273
  def generate_images_110(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress=gr.Progress(track_tqdm=True)):
@@ -290,9 +310,9 @@ def generate_images_110(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width,
290
  upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
291
  upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
292
  print('-- got upscaled image --')
293
- downscaled_upscale = upscale2.resize((upscale2.width // 16, upscale2.height // 16), Image.LANCZOS)
294
- sd_avif_bytes = create_hdr_avif_bytes(downscaled_upscale)
295
- return sd_avif_bytes, sd_avif_bytes, prompt
296
 
297
  def run_inference_and_upload_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, save_consent, progress=gr.Progress(track_tqdm=True)):
298
  sd_image, upscaled_image, expanded_prompt = generate_images_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress)
@@ -302,9 +322,9 @@ def run_inference_and_upload_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3
302
  sd_filename = f"sd35ll_{timestamp}.png"
303
  upscale_filename = f"sd35ll_upscale_{timestamp}.png"
304
  sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
305
- #upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
306
- sd_thread.start()
307
- #upscale_thread.start()
308
  else:
309
  print("ℹ️ User did not consent to save. Skipping upload.")
310
  return sd_image, expanded_prompt
@@ -317,9 +337,9 @@ def run_inference_and_upload_60(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3
317
  sd_filename = f"sd35ll_{timestamp}.png"
318
  upscale_filename = f"sd35ll_upscale_{timestamp}.png"
319
  sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
320
- #upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
321
- sd_thread.start()
322
- #upscale_thread.start()
323
  else:
324
  print("ℹ️ User did not consent to save. Skipping upload.")
325
  return sd_image, expanded_prompt
@@ -332,9 +352,9 @@ def run_inference_and_upload_110(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_
332
  sd_filename = f"sd35ll_{timestamp}.png"
333
  upscale_filename = f"sd35ll_upscale_{timestamp}.png"
334
  sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
335
- #upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
336
- sd_thread.start()
337
- #upscale_thread.start()
338
  else:
339
  print("ℹ️ User did not consent to save. Skipping upload.")
340
  return sd_image, expanded_prompt
 
96
  except Exception as e:
97
  print(f"❌ An error occurred during GCS upload for {filename}: {e}")
98
 
99
+ def srgb_to_linear_tensor(img_tensor_srgb):
100
+ """Converts a PyTorch sRGB tensor [0, 1] to a linear tensor."""
101
+ linear_mask = (img_tensor_srgb <= 0.04045).float()
102
+ non_linear_mask = (img_tensor_srgb > 0.04045).float()
103
+ linear_part = img_tensor_srgb / 12.92
104
+ non_linear_part = torch.pow((img_tensor_srgb + 0.055) / 1.055, 2.4)
105
+ img_linear = (linear_part * linear_mask) + (non_linear_part * non_linear_mask)
106
+ return img_linear
107
+
108
+ def linear_to_srgb_tensor(img_tensor_linear):
109
+ """Converts a PyTorch linear tensor [0, 1] to sRGB."""
110
+ # Clamp to prevent negative values from torch.pow
111
+ img_tensor_linear = img_tensor_linear.clamp(min=0.0)
112
+ srgb_mask = (img_tensor_linear <= 0.0031308).float()
113
+ non_srgb_mask = (img_tensor_linear > 0.0031308).float()
114
+ srgb_part = img_tensor_linear * 12.92
115
+ non_srgb_part = 1.055 * torch.pow(img_tensor_linear, 1.0/2.4) - 0.055
116
+ img_srgb = (srgb_part * srgb_mask) + (non_srgb_part * non_srgb_mask)
117
+ return img_srgb.clamp(0.0, 1.0)
118
+
119
  def srgb_to_linear(tensor_srgb):
120
  """Converts a batched sRGB PyTorch tensor [0, 1] to a linear tensor."""
121
  return torch.where(
 
138
  hdr_16bit_array = (np.clip(linear_numpy_float, 0, 1) * 65535).astype(np.uint16)
139
 
140
  # 4. Create a PIL image that holds the 16-bit data
141
+ hdr_pil_image = Image.fromarray(hdr_16bit_array, mode='RGB;16')
142
 
143
  # 5. Save to a bytes buffer as 10-bit AVIF with HDR10 metadata
144
  buffer = io.BytesIO()
 
260
  upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
261
  upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
262
  print('-- got upscaled image --')
263
+ sd_avif_bytes = create_hdr_avif_bytes(upscale2)
264
+ downscaled_upscale = sd_avif_bytes.resize((upscale2.width // 8, upscale2.height // 8), Image.LANCZOS)
265
+ return downscaled_upscale, downscaled_upscale, prompt
266
 
267
  @spaces.GPU(duration=70)
268
  def generate_images_60(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress=gr.Progress(track_tqdm=True)):
 
285
  upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
286
  upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
287
  print('-- got upscaled image --')
288
+ sd_avif_bytes = create_hdr_avif_bytes(upscale2)
289
+ downscaled_upscale = sd_avif_bytes.resize((upscale2.width // 8, upscale2.height // 8), Image.LANCZOS)
290
+ return downscaled_upscale, downscaled_upscale, prompt
291
 
292
  @spaces.GPU(duration=120)
293
  def generate_images_110(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress=gr.Progress(track_tqdm=True)):
 
310
  upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
311
  upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
312
  print('-- got upscaled image --')
313
+ sd_avif_bytes = create_hdr_avif_bytes(upscale2)
314
+ downscaled_upscale = sd_avif_bytes.resize((upscale2.width // 8, upscale2.height // 8), Image.LANCZOS)
315
+ return downscaled_upscale, downscaled_upscale, prompt
316
 
317
  def run_inference_and_upload_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, save_consent, progress=gr.Progress(track_tqdm=True)):
318
  sd_image, upscaled_image, expanded_prompt = generate_images_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress)
 
322
  sd_filename = f"sd35ll_{timestamp}.png"
323
  upscale_filename = f"sd35ll_upscale_{timestamp}.png"
324
  sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
325
+ upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
326
+ #sd_thread.start()
327
+ upscale_thread.start()
328
  else:
329
  print("ℹ️ User did not consent to save. Skipping upload.")
330
  return sd_image, expanded_prompt
 
337
  sd_filename = f"sd35ll_{timestamp}.png"
338
  upscale_filename = f"sd35ll_upscale_{timestamp}.png"
339
  sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
340
+ upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
341
+ #sd_thread.start()
342
+ upscale_thread.start()
343
  else:
344
  print("ℹ️ User did not consent to save. Skipping upload.")
345
  return sd_image, expanded_prompt
 
352
  sd_filename = f"sd35ll_{timestamp}.png"
353
  upscale_filename = f"sd35ll_upscale_{timestamp}.png"
354
  sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
355
+ upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
356
+ #sd_thread.start()
357
+ upscale_thread.start()
358
  else:
359
  print("ℹ️ User did not consent to save. Skipping upload.")
360
  return sd_image, expanded_prompt