ihabooe commited on
Commit
2000e29
·
verified ·
1 Parent(s): 758eb10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -35
app.py CHANGED
@@ -84,15 +84,9 @@ def process(image, progress=gr.Progress()):
84
  output_array = np.array(new_im.convert("RGBA"))
85
 
86
  progress(1.0, desc="Done!")
87
- # Return image for display and path for download
88
  return output_array, filepath, gr.update(visible=True)
89
 
90
- # Function to handle the download button click
91
- def download_image(filepath):
92
- if filepath and os.path.exists(filepath):
93
- return filepath
94
- return None
95
-
96
  # Gradio interface setup
97
  title = "Background Removal"
98
  description = r"""Background removal model developed by <a href='https://BRIA.AI' target='_blank'><b>BRIA.AI</b></a>, trained on a carefully selected dataset and is available as an open-source model for non-commercial use.<br> For testing, upload your image and wait. Read more at model card <a href='https://huggingface.co/briaai/RMBG-1.4' target='_blank'><b>briaai/RMBG-1.4</b></a>. To purchase a commercial license, simply click <a href='https://go.bria.ai/3ZCBTLH' target='_blank'><b>Here</b></a>. <br>"""
@@ -100,29 +94,12 @@ description = r"""Background removal model developed by <a href='https://BRIA.AI
100
  examples = [['./input.jpg']]
101
 
102
  # Create the Gradio interface
103
- with gr.Blocks(css="""
104
- .download-btn {
105
- background-color: #4CAF50;
106
- border: none;
107
- color: white;
108
- padding: 10px 24px;
109
- text-align: center;
110
- text-decoration: none;
111
- display: inline-block;
112
- font-size: 16px;
113
- margin: 4px 2px;
114
- cursor: pointer;
115
- border-radius: 4px;
116
- }
117
- .download-btn:hover {
118
- background-color: #45a049;
119
- }
120
- """) as demo:
121
  gr.Markdown(f"# {title}")
122
  gr.Markdown(description)
123
 
124
- # Store the processed image path
125
- image_path = gr.State(None)
126
 
127
  with gr.Row():
128
  with gr.Column(scale=1):
@@ -130,10 +107,11 @@ with gr.Blocks(css="""
130
 
131
  with gr.Column(scale=1):
132
  output_image = gr.Image(type="numpy", label="Result")
133
- download_btn = gr.Button("Download Image", elem_id="download_button",
134
- variant="primary", visible=False,
135
- elem_classes="download-btn")
136
- download_output = gr.File(visible=False)
 
137
 
138
  # Set up example images
139
  gr.Examples(examples, inputs=input_image)
@@ -142,15 +120,33 @@ with gr.Blocks(css="""
142
  input_image.change(
143
  fn=process,
144
  inputs=input_image,
145
- outputs=[output_image, image_path, download_btn],
146
  show_progress="full"
147
  )
148
 
149
  # Handle download button click
150
  download_btn.click(
151
- fn=download_image,
152
- inputs=[image_path],
153
- outputs=[download_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  )
155
 
156
  if __name__ == "__main__":
 
84
  output_array = np.array(new_im.convert("RGBA"))
85
 
86
  progress(1.0, desc="Done!")
87
+ # Return the image, filepath and update button visibility
88
  return output_array, filepath, gr.update(visible=True)
89
 
 
 
 
 
 
 
90
  # Gradio interface setup
91
  title = "Background Removal"
92
  description = r"""Background removal model developed by <a href='https://BRIA.AI' target='_blank'><b>BRIA.AI</b></a>, trained on a carefully selected dataset and is available as an open-source model for non-commercial use.<br> For testing, upload your image and wait. Read more at model card <a href='https://huggingface.co/briaai/RMBG-1.4' target='_blank'><b>briaai/RMBG-1.4</b></a>. To purchase a commercial license, simply click <a href='https://go.bria.ai/3ZCBTLH' target='_blank'><b>Here</b></a>. <br>"""
 
94
  examples = [['./input.jpg']]
95
 
96
  # Create the Gradio interface
97
+ with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  gr.Markdown(f"# {title}")
99
  gr.Markdown(description)
100
 
101
+ # Store the filepath
102
+ filepath = gr.State()
103
 
104
  with gr.Row():
105
  with gr.Column(scale=1):
 
107
 
108
  with gr.Column(scale=1):
109
  output_image = gr.Image(type="numpy", label="Result")
110
+ with gr.Row():
111
+ download_btn = gr.Button("Download Image", variant="primary", visible=False)
112
+
113
+ # Hidden download component that will be triggered
114
+ download_file = gr.File(interactive=False, visible=False)
115
 
116
  # Set up example images
117
  gr.Examples(examples, inputs=input_image)
 
120
  input_image.change(
121
  fn=process,
122
  inputs=input_image,
123
+ outputs=[output_image, filepath, download_btn],
124
  show_progress="full"
125
  )
126
 
127
  # Handle download button click
128
  download_btn.click(
129
+ fn=lambda x: x, # Just pass through the filepath
130
+ inputs=[filepath],
131
+ outputs=[download_file], # Output to the download component
132
+ _js="""
133
+ function(path) {
134
+ // Create a temporary anchor element
135
+ const link = document.createElement('a');
136
+ link.href = '/file=' + path;
137
+ link.download = path.split('/').pop();
138
+
139
+ // Append to the document
140
+ document.body.appendChild(link);
141
+
142
+ // Trigger click and remove
143
+ link.click();
144
+ document.body.removeChild(link);
145
+
146
+ // Return the path for the hidden component
147
+ return path;
148
+ }
149
+ """
150
  )
151
 
152
  if __name__ == "__main__":