Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import tempfile
|
|
| 10 |
import os
|
| 11 |
import time
|
| 12 |
import uuid
|
|
|
|
| 13 |
|
| 14 |
# Load the pre-trained model
|
| 15 |
print("Loading model...")
|
|
@@ -33,7 +34,7 @@ def resize_image(image):
|
|
| 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,8 +85,23 @@ 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 processed image and update
|
| 88 |
-
return output_array, gr.update(visible=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# Gradio interface setup
|
| 91 |
title = "Background Removal"
|
|
@@ -95,27 +111,47 @@ examples = [['./input.jpg']]
|
|
| 95 |
|
| 96 |
# Create the Gradio interface
|
| 97 |
with gr.Blocks(css="""
|
| 98 |
-
.download-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|
| 101 |
-
.
|
| 102 |
-
|
| 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
|
| 114 |
output_image = gr.Image(type="numpy", label="Result")
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Set up example images
|
| 121 |
gr.Examples(examples, inputs=input_image)
|
|
@@ -124,9 +160,35 @@ with gr.Blocks(css="""
|
|
| 124 |
input_image.change(
|
| 125 |
fn=process,
|
| 126 |
inputs=input_image,
|
| 127 |
-
outputs=[output_image,
|
| 128 |
show_progress="full"
|
| 129 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch(share=False)
|
|
|
|
| 10 |
import os
|
| 11 |
import time
|
| 12 |
import uuid
|
| 13 |
+
import shutil
|
| 14 |
|
| 15 |
# Load the pre-trained model
|
| 16 |
print("Loading model...")
|
|
|
|
| 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 |
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"
|
|
|
|
| 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)
|
|
|
|
| 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)
|