muhammadsalmanalfaridzi commited on
Commit
dc7d4c6
β€’
1 Parent(s): 3a90bd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -11
app.py CHANGED
@@ -12,6 +12,7 @@ from transformers import pipeline
12
  import numpy as np
13
  import json
14
  import torch
 
15
 
16
  # Load Stable Diffusion Model
17
  def load_stable_diffusion_model():
@@ -354,10 +355,15 @@ def process_images(input_files, bg_method='rembg', watermark_path=None, canvas_s
354
 
355
  return original_images, processed_images, output_zip_path, processing_time
356
 
 
 
 
357
  def gradio_interface(input_files, bg_method, watermark, canvas_size, output_format, bg_choice, custom_color, num_workers):
 
358
  progress = gr.Progress()
359
  watermark_path = watermark.name if watermark else None
360
 
 
361
  if isinstance(input_files, str) and input_files.lower().endswith(('.zip', '.rar')):
362
  return process_images(input_files, bg_method, watermark_path, canvas_size, output_format, bg_choice, custom_color, num_workers, progress)
363
  elif isinstance(input_files, list):
@@ -366,31 +372,41 @@ def gradio_interface(input_files, bg_method, watermark, canvas_size, output_form
366
  return process_images(input_files.name, bg_method, watermark_path, canvas_size, output_format, bg_choice, custom_color, num_workers, progress)
367
 
368
  def show_color_picker(bg_choice):
369
- if bg_choice == 'custom':
370
- return gr.update(visible=True)
371
- return gr.update(visible=False)
372
 
373
  def update_compare(evt: gr.SelectData):
 
374
  if isinstance(evt.value, dict) and 'caption' in evt.value:
375
- input_path = evt.value['caption']
376
  output_path = evt.value['image']['path']
377
- input_path = input_path.split("Input: ")[-1]
378
 
 
379
  original_img = Image.open(input_path)
380
  processed_img = Image.open(output_path)
381
 
 
382
  original_ratio = f"{original_img.width}x{original_img.height}"
383
  processed_ratio = f"{processed_img.width}x{processed_img.height}"
384
 
385
- return gr.update(value=input_path), gr.update(value=output_path), gr.update(value=original_ratio), gr.update(value=processed_ratio)
 
 
 
 
386
  else:
387
- print("No caption found in selection")
388
- return gr.update(value=None), gr.update(value=None), gr.update(value=None), gr.update(value=None)
389
 
390
  def process(input_files, bg_method, watermark, canvas_size, output_format, bg_choice, custom_color, num_workers):
391
- _, processed_images, zip_path, time_taken = gradio_interface(input_files, bg_method, watermark, canvas_size, output_format, bg_choice, custom_color, num_workers)
392
- processed_images_with_captions = [(img, f"Input: {caption}") for img, caption in processed_images]
393
- return processed_images_with_captions, zip_path, f"{time_taken:.2f} seconds"
 
 
 
 
 
394
 
395
  with gr.Blocks(theme="NoCrypt/miku@1.2.2") as iface:
396
  gr.Markdown("# Image Background Removal and Resizing with Optional Watermark")
 
12
  import numpy as np
13
  import json
14
  import torch
15
+ import logging
16
 
17
  # Load Stable Diffusion Model
18
  def load_stable_diffusion_model():
 
355
 
356
  return original_images, processed_images, output_zip_path, processing_time
357
 
358
+ # Set up logging
359
+ logging.basicConfig(level=logging.INFO)
360
+
361
  def gradio_interface(input_files, bg_method, watermark, canvas_size, output_format, bg_choice, custom_color, num_workers):
362
+ """Handles input files and processes them accordingly."""
363
  progress = gr.Progress()
364
  watermark_path = watermark.name if watermark else None
365
 
366
+ # Check the type of input_files and call the process_images function
367
  if isinstance(input_files, str) and input_files.lower().endswith(('.zip', '.rar')):
368
  return process_images(input_files, bg_method, watermark_path, canvas_size, output_format, bg_choice, custom_color, num_workers, progress)
369
  elif isinstance(input_files, list):
 
372
  return process_images(input_files.name, bg_method, watermark_path, canvas_size, output_format, bg_choice, custom_color, num_workers, progress)
373
 
374
  def show_color_picker(bg_choice):
375
+ """Shows the color picker if 'custom' background is selected."""
376
+ return gr.update(visible=(bg_choice == 'custom'))
 
377
 
378
  def update_compare(evt: gr.SelectData):
379
+ """Updates the displayed images and their ratios when a processed image is selected."""
380
  if isinstance(evt.value, dict) and 'caption' in evt.value:
381
+ input_path = evt.value['caption'].split("Input: ")[-1]
382
  output_path = evt.value['image']['path']
 
383
 
384
+ # Open the original and processed images
385
  original_img = Image.open(input_path)
386
  processed_img = Image.open(output_path)
387
 
388
+ # Calculate the aspect ratios
389
  original_ratio = f"{original_img.width}x{original_img.height}"
390
  processed_ratio = f"{processed_img.width}x{processed_img.height}"
391
 
392
+ return (gr.update(value=input_path),
393
+ gr.update(value=output_path),
394
+ gr.update(value=original_ratio),
395
+ gr.update(value=processed_ratio)
396
+ )
397
  else:
398
+ logging.warning("No caption found in selection")
399
+ return (gr.update(value=None),) * 4
400
 
401
  def process(input_files, bg_method, watermark, canvas_size, output_format, bg_choice, custom_color, num_workers):
402
+ """Processes the images and returns the results."""
403
+ try:
404
+ _, processed_images, zip_path, time_taken = gradio_interface(input_files, bg_method, watermark, canvas_size, output_format, bg_choice, custom_color, num_workers)
405
+ processed_images_with_captions = [(img, f"Input: {caption}") for img, caption in processed_images]
406
+ return processed_images_with_captions, zip_path, f"{time_taken:.2f} seconds"
407
+ except Exception as e:
408
+ logging.error(f"Error in processing images: {e}")
409
+ return [], None, "Error during processing"
410
 
411
  with gr.Blocks(theme="NoCrypt/miku@1.2.2") as iface:
412
  gr.Markdown("# Image Background Removal and Resizing with Optional Watermark")