ihabooe commited on
Commit
98db94f
·
verified ·
1 Parent(s): 2000e29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -39
app.py CHANGED
@@ -33,7 +33,7 @@ def resize_image(image):
33
  # Background removal process
34
  def process(image, progress=gr.Progress()):
35
  if image is None:
36
- return None, None, gr.update(visible=False)
37
 
38
  progress(0, desc="Starting processing...")
39
 
@@ -84,8 +84,8 @@ def process(image, progress=gr.Progress()):
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"
@@ -94,24 +94,28 @@ description = r"""Background removal model developed by <a href='https://BRIA.AI
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):
106
  input_image = gr.Image(type="numpy", label="Upload Image")
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,34 +124,9 @@ with gr.Blocks() as demo:
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__":
153
  demo.launch(share=False)
 
33
  # Background removal process
34
  def process(image, progress=gr.Progress()):
35
  if image is None:
36
+ return None, gr.update(visible=False)
37
 
38
  progress(0, desc="Starting processing...")
39
 
 
84
  output_array = np.array(new_im.convert("RGBA"))
85
 
86
  progress(1.0, desc="Done!")
87
+ # Return the processed image and update download component visibility
88
+ return output_array, gr.update(visible=True, value=filepath)
89
 
90
  # Gradio interface setup
91
  title = "Background Removal"
 
94
  examples = [['./input.jpg']]
95
 
96
  # Create the Gradio interface
97
+ with gr.Blocks(css="""
98
+ .download-btn {
99
+ margin-top: 10px !important;
100
+ }
101
+ .result-container {
102
+ display: flex;
103
+ flex-direction: column;
104
+ }
105
+ """) as demo:
106
  gr.Markdown(f"# {title}")
107
  gr.Markdown(description)
108
 
 
 
 
109
  with gr.Row():
110
  with gr.Column(scale=1):
111
  input_image = gr.Image(type="numpy", label="Upload Image")
112
 
113
+ with gr.Column(scale=1, elem_classes="result-container"):
114
  output_image = gr.Image(type="numpy", label="Result")
115
+ # Use File component as a download button
116
+ download_file = gr.File(label="Download Processed Image",
117
+ elem_classes="download-btn",
118
+ visible=False)
 
119
 
120
  # Set up example images
121
  gr.Examples(examples, inputs=input_image)
 
124
  input_image.change(
125
  fn=process,
126
  inputs=input_image,
127
+ outputs=[output_image, download_file],
128
  show_progress="full"
129
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  if __name__ == "__main__":
132
  demo.launch(share=False)