b2bomber commited on
Commit
a01173c
·
verified ·
1 Parent(s): 7275a27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -18
app.py CHANGED
@@ -1,49 +1,45 @@
1
  # app.py
2
- # A more user-friendly version with a modern, two-column layout and examples.
3
 
4
  import gradio as gr
5
  from rembg import remove
6
  import traceback
7
 
8
  # --- Core Function ---
9
- # This function processes the image. It remains the same.
10
  def process_image(input_image):
11
  """
12
  Removes the background from an image.
13
  Handles errors gracefully.
14
  """
15
  if input_image is None:
16
- # Provide a more user-friendly error message
17
  raise gr.Error("Please upload an image or select one of the examples.")
18
 
19
  try:
20
- # Use the default rembg model for stability
21
  return remove(input_image)
22
  except Exception as e:
23
  print(traceback.format_exc())
24
- raise gr.Error(f"Failed to process image. Error: {e}")
25
 
26
  # --- Download Function ---
27
- # ✅ NEW: A simple function to pass the image data for download.
28
  def download_output_image(image):
29
- # This function takes the processed image and returns it as a file for download.
 
 
30
  return image
31
 
32
  # --- Gradio App with Modern UI ---
33
- # ✅ FIX: Add the `css` parameter to hide the default buttons.
34
  with gr.Blocks(
35
  theme=gr.themes.Soft(primary_hue="violet"),
36
  css=".meta-buttons { display: none !important; }"
37
  ) as demo:
38
 
39
- # Title and description for the app
40
  gr.Markdown(
41
  """
 
42
  Upload an image, use your webcam, or try one of the examples below to instantly remove the background.
43
  """
44
  )
45
 
46
- # Main two-column layout
47
  with gr.Row(variant="panel"):
48
 
49
  # --- Input Column ---
@@ -55,7 +51,12 @@ with gr.Blocks(
55
  height=400
56
  )
57
 
58
-
 
 
 
 
 
59
  with gr.Row():
60
  clear_btn = gr.ClearButton(
61
  value="Clear",
@@ -71,21 +72,19 @@ with gr.Blocks(
71
  label="Result",
72
  height=400,
73
  value=None,
74
- # ✅ FIX: Hide the default download button as we will create our own.
75
- show_download_button=False
 
76
  )
77
 
78
- # ✅ NEW: Add our custom download button.
79
  download_button = gr.Button(
80
  "Download Image",
81
  icon="https://cdn.iconscout.com/icon/premium/png-256-thumb/download-button-2132338-1794935.png",
82
  variant="primary",
83
  )
84
 
85
- # ✅ NEW: Add a hidden file component to trigger the download.
86
  download_file_output = gr.File(visible=False)
87
 
88
-
89
  # --- Event Listeners ---
90
  submit_btn.click(
91
  fn=process_image,
@@ -94,8 +93,6 @@ with gr.Blocks(
94
  api_name="predict"
95
  )
96
 
97
- # ✅ NEW: Link our new download button to its function.
98
- # When clicked, it takes the content of 'image_output' and sends it to 'download_file_output'.
99
  download_button.click(
100
  fn=download_output_image,
101
  inputs=[image_output],
 
1
  # app.py
2
+ # A more user-friendly version with a non-interactive output image component.
3
 
4
  import gradio as gr
5
  from rembg import remove
6
  import traceback
7
 
8
  # --- Core Function ---
 
9
  def process_image(input_image):
10
  """
11
  Removes the background from an image.
12
  Handles errors gracefully.
13
  """
14
  if input_image is None:
 
15
  raise gr.Error("Please upload an image or select one of the examples.")
16
 
17
  try:
 
18
  return remove(input_image)
19
  except Exception as e:
20
  print(traceback.format_exc())
21
+ raise gr.Error(f"Failed to process image: {e}")
22
 
23
  # --- Download Function ---
 
24
  def download_output_image(image):
25
+ """
26
+ This function takes the processed image and returns it as a file for download.
27
+ """
28
  return image
29
 
30
  # --- Gradio App with Modern UI ---
 
31
  with gr.Blocks(
32
  theme=gr.themes.Soft(primary_hue="violet"),
33
  css=".meta-buttons { display: none !important; }"
34
  ) as demo:
35
 
 
36
  gr.Markdown(
37
  """
38
+ # ✨ AI Background Remover
39
  Upload an image, use your webcam, or try one of the examples below to instantly remove the background.
40
  """
41
  )
42
 
 
43
  with gr.Row(variant="panel"):
44
 
45
  # --- Input Column ---
 
51
  height=400
52
  )
53
 
54
+ gr.Examples(
55
+ examples=["cat.jpg", "person.jpg", "car.jpg"],
56
+ inputs=image_input,
57
+ label="Click an example to start"
58
+ )
59
+
60
  with gr.Row():
61
  clear_btn = gr.ClearButton(
62
  value="Clear",
 
72
  label="Result",
73
  height=400,
74
  value=None,
75
+ show_download_button=False,
76
+ # ✅ FIX: This makes the output box display-only.
77
+ interactive=False
78
  )
79
 
 
80
  download_button = gr.Button(
81
  "Download Image",
82
  icon="https://cdn.iconscout.com/icon/premium/png-256-thumb/download-button-2132338-1794935.png",
83
  variant="primary",
84
  )
85
 
 
86
  download_file_output = gr.File(visible=False)
87
 
 
88
  # --- Event Listeners ---
89
  submit_btn.click(
90
  fn=process_image,
 
93
  api_name="predict"
94
  )
95
 
 
 
96
  download_button.click(
97
  fn=download_output_image,
98
  inputs=[image_output],