comrender commited on
Commit
9aa2895
·
verified ·
1 Parent(s): da3febd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -4
app.py CHANGED
@@ -222,13 +222,13 @@ def tiled_flux_img2img(pipe, prompt, image, strength, steps, guidance, generator
222
  return output
223
 
224
  def download_png(image):
225
- """Convert image to PNG and return as downloadable file"""
226
  if image is None:
227
  raise gr.Error("No upscaled image available to download")
228
  buffer = io.BytesIO()
229
  image.save(buffer, format="PNG")
230
- buffer.seek(0)
231
- return buffer
232
 
233
  @spaces.GPU(duration=120)
234
  def enhance_image(
@@ -405,6 +405,9 @@ with gr.Blocks(css=css, title="🎨 Flux dev Creative Upscaler - Florence-2 + FL
405
  # State to store the upscaled image
406
  upscaled_image_state = gr.State()
407
 
 
 
 
408
  # Event handlers
409
  enhance_btn.click(
410
  fn=enhance_image,
@@ -424,7 +427,7 @@ with gr.Blocks(css=css, title="🎨 Flux dev Creative Upscaler - Florence-2 + FL
424
  download_btn.click(
425
  fn=download_png,
426
  inputs=[upscaled_image_state],
427
- outputs=gr.File(label="Download Upscaled Image as PNG")
428
  )
429
 
430
  gr.HTML("""
@@ -494,6 +497,31 @@ with gr.Blocks(css=css, title="🎨 Flux dev Creative Upscaler - Florence-2 + FL
494
  sliderInput.value = 50;
495
  sliderInput.dispatchEvent(new Event('input'));
496
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
497
  });
498
  </script>
499
  """)
 
222
  return output
223
 
224
  def download_png(image):
225
+ """Convert image to PNG and return base64 string for download"""
226
  if image is None:
227
  raise gr.Error("No upscaled image available to download")
228
  buffer = io.BytesIO()
229
  image.save(buffer, format="PNG")
230
+ base64_data = base64.b64encode(buffer.getvalue()).decode('utf-8')
231
+ return base64_data
232
 
233
  @spaces.GPU(duration=120)
234
  def enhance_image(
 
405
  # State to store the upscaled image
406
  upscaled_image_state = gr.State()
407
 
408
+ # Hidden textbox for base64 data
409
+ download_data = gr.Textbox(visible=False, elem_id="download_data")
410
+
411
  # Event handlers
412
  enhance_btn.click(
413
  fn=enhance_image,
 
427
  download_btn.click(
428
  fn=download_png,
429
  inputs=[upscaled_image_state],
430
+ outputs=download_data
431
  )
432
 
433
  gr.HTML("""
 
497
  sliderInput.value = 50;
498
  sliderInput.dispatchEvent(new Event('input'));
499
  }
500
+
501
+ const downloadData = document.querySelector('#download_data textarea');
502
+ if (downloadData) {
503
+ const observer = new MutationObserver(() => {
504
+ const base64 = downloadData.value;
505
+ if (base64) {
506
+ const byteCharacters = atob(base64);
507
+ const byteNumbers = new Array(byteCharacters.length);
508
+ for (let i = 0; i < byteCharacters.length; i++) {
509
+ byteNumbers[i] = byteCharacters.charCodeAt(i);
510
+ }
511
+ const byteArray = new Uint8Array(byteNumbers);
512
+ const blob = new Blob([byteArray], {type: 'image/png'});
513
+ const url = URL.createObjectURL(blob);
514
+ const a = document.createElement('a');
515
+ a.href = url;
516
+ a.download = 'upscaled_image.png';
517
+ a.click();
518
+ URL.revokeObjectURL(url);
519
+ // Clear the textbox
520
+ downloadData.value = '';
521
+ }
522
+ });
523
+ observer.observe(downloadData, {childList: true, subtree: true, characterData: true});
524
+ }
525
  });
526
  </script>
527
  """)