ihabooe commited on
Commit
dc00b2a
·
verified ·
1 Parent(s): bccb9d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -91
app.py CHANGED
@@ -10,7 +10,6 @@ import tempfile
10
  import os
11
  import time
12
  import uuid
13
- import shutil
14
 
15
  # Load the pre-trained model
16
  print("Loading model...")
@@ -34,7 +33,7 @@ def resize_image(image):
34
  # Background removal process
35
  def process(image, progress=gr.Progress()):
36
  if image is None:
37
- return None, None, gr.update(visible=False)
38
 
39
  progress(0, desc="Starting processing...")
40
 
@@ -85,23 +84,8 @@ def process(image, progress=gr.Progress()):
85
  output_array = np.array(new_im.convert("RGBA"))
86
 
87
  progress(1.0, desc="Done!")
88
- # Return the processed image and filepath and update button visibility
89
- return output_array, filepath, gr.update(visible=True)
90
-
91
- # Function to handle the download
92
- def download_image(filepath):
93
- if filepath and os.path.exists(filepath):
94
- filename = os.path.basename(filepath)
95
- # Create a new file in a web-accessible directory
96
- download_path = os.path.join("downloads", filename)
97
- os.makedirs("downloads", exist_ok=True)
98
-
99
- # Copy the file to the download directory
100
- shutil.copy2(filepath, download_path)
101
-
102
- # Return the file path for download
103
- return download_path
104
- return None
105
 
106
  # Gradio interface setup
107
  title = "Background Removal"
@@ -109,86 +93,76 @@ description = r"""Background removal model developed by <a href='https://BRIA.AI
109
 
110
  examples = [['./input.jpg']]
111
 
112
- # Create the Gradio interface
113
- with gr.Blocks(css="""
114
- .download-button {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  background-color: #4CAF50 !important;
116
  color: white !important;
117
  border: none !important;
118
- padding: 10px 24px !important;
119
- text-align: center !important;
120
- text-decoration: none !important;
121
- display: inline-block !important;
122
  font-size: 16px !important;
123
- margin: 10px 0 !important;
124
- cursor: pointer !important;
125
  border-radius: 4px !important;
126
- transition: background-color 0.3s !important;
127
  }
128
- .download-button:hover {
129
  background-color: #45a049 !important;
130
  }
131
- """) as demo:
132
- gr.Markdown(f"# {title}")
133
- gr.Markdown(description)
134
-
135
- # Store the filepath
136
- filepath = gr.State()
137
-
138
- with gr.Row():
139
- with gr.Column(scale=1):
140
- input_image = gr.Image(type="numpy", label="Upload Image")
141
-
142
- with gr.Column(scale=1):
143
- output_image = gr.Image(type="numpy", label="Result")
144
-
145
- # Hidden file component for actual download
146
- download_file = gr.File(visible=False)
147
-
148
- # Custom styled download button
149
- with gr.Row():
150
- download_btn = gr.Button(
151
- "Download Image",
152
- visible=False,
153
- elem_classes=["download-button"]
154
- )
155
-
156
- # Set up example images
157
- gr.Examples(examples, inputs=input_image)
158
-
159
- # Process automatically when image is uploaded
160
- input_image.change(
161
- fn=process,
162
- inputs=input_image,
163
- outputs=[output_image, filepath, download_btn],
164
- show_progress="full"
165
- )
166
-
167
- # Handle download button click
168
- download_btn.click(
169
- fn=download_image,
170
- inputs=[filepath],
171
- outputs=[download_file]
172
- )
173
-
174
- # Add custom HTML for button styling enhancement
175
- gr.HTML("""
176
- <script>
177
- // This script will ensure buttons are properly styled after DOM updates
178
- document.addEventListener('DOMContentLoaded', function() {
179
- // Process might be needed to re-apply styles after Gradio updates the DOM
180
- const observer = new MutationObserver(function() {
181
- const buttons = document.querySelectorAll('.download-button');
182
- buttons.forEach(button => {
183
- button.style.backgroundColor = '#4CAF50';
184
- button.style.color = 'white';
185
- });
186
- });
187
-
188
- observer.observe(document.body, { childList: true, subtree: true });
189
- });
190
- </script>
191
- """)
192
 
193
  if __name__ == "__main__":
194
  demo.launch(share=False)
 
10
  import os
11
  import time
12
  import uuid
 
13
 
14
  # Load the pre-trained model
15
  print("Loading model...")
 
33
  # Background removal process
34
  def process(image, progress=gr.Progress()):
35
  if image is None:
36
+ return None, None
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 both the image for display and the filepath for download
88
+ return output_array, filepath
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # Gradio interface setup
91
  title = "Background Removal"
 
93
 
94
  examples = [['./input.jpg']]
95
 
96
+ # Create HTML with custom CSS
97
+ custom_css = """
98
+ <style>
99
+ .container {
100
+ display: flex;
101
+ flex-direction: row;
102
+ width: 100%;
103
+ }
104
+ .column {
105
+ flex: 1;
106
+ padding: 10px;
107
+ }
108
+ .result-area {
109
+ position: relative;
110
+ }
111
+ .download-btn {
112
+ background-color: #4CAF50;
113
+ color: white;
114
+ border: none;
115
+ padding: 10px 20px;
116
+ text-align: center;
117
+ text-decoration: none;
118
+ display: inline-block;
119
+ font-size: 16px;
120
+ margin-top: 10px;
121
+ border-radius: 4px;
122
+ cursor: pointer;
123
+ transition: background-color 0.3s;
124
+ }
125
+ .download-btn:hover {
126
+ background-color: #45a049;
127
+ }
128
+ </style>
129
+ """
130
+
131
+ # Create the Gradio interface with a simpler approach
132
+ demo = gr.Interface(
133
+ fn=process,
134
+ inputs=gr.Image(type="numpy", label="Upload Image"),
135
+ outputs=[
136
+ gr.Image(type="numpy", label="Result"),
137
+ gr.File(label="Download Processed Image")
138
+ ],
139
+ examples=examples,
140
+ title=title,
141
+ description=description + custom_css,
142
+ allow_flagging="never",
143
+ theme=gr.themes.Default(
144
+ primary_hue="green",
145
+ secondary_hue="green",
146
+ ),
147
+ css="""
148
+ .gradio-container .footer {
149
+ display: none !important;
150
+ }
151
+ /* Style the download button to look like our green button */
152
+ .file-button {
153
  background-color: #4CAF50 !important;
154
  color: white !important;
155
  border: none !important;
156
+ padding: 10px 20px !important;
 
 
 
157
  font-size: 16px !important;
 
 
158
  border-radius: 4px !important;
159
+ margin-top: 10px !important;
160
  }
161
+ .file-button:hover {
162
  background-color: #45a049 !important;
163
  }
164
+ """
165
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  if __name__ == "__main__":
168
  demo.launch(share=False)