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,
|
| 37 |
|
| 38 |
progress(0, desc="Starting processing...")
|
| 39 |
|
|
@@ -84,8 +85,9 @@ def process(image, progress=gr.Progress()):
|
|
| 84 |
output_array = np.array(new_im.convert("RGBA"))
|
| 85 |
|
| 86 |
progress(1.0, desc="Done!")
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
# Gradio interface setup
|
| 91 |
title = "Background Removal"
|
|
@@ -93,76 +95,83 @@ description = r"""Background removal model developed by <a href='https://BRIA.AI
|
|
| 93 |
|
| 94 |
examples = [['./input.jpg']]
|
| 95 |
|
| 96 |
-
# Create
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 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 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
background-color: #4CAF50 !important;
|
| 154 |
color: white !important;
|
| 155 |
border: none !important;
|
| 156 |
-
padding: 10px
|
|
|
|
|
|
|
|
|
|
| 157 |
font-size: 16px !important;
|
|
|
|
|
|
|
| 158 |
border-radius: 4px !important;
|
| 159 |
-
|
|
|
|
| 160 |
}
|
| 161 |
-
.file
|
| 162 |
background-color: #45a049 !important;
|
| 163 |
}
|
| 164 |
-
|
| 165 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
| 168 |
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, 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 |
+
|
| 89 |
+
# Return the processed image and the file for download
|
| 90 |
+
return output_array, gr.update(visible=True, value=filepath, interactive=True)
|
| 91 |
|
| 92 |
# Gradio interface setup
|
| 93 |
title = "Background Removal"
|
|
|
|
| 95 |
|
| 96 |
examples = [['./input.jpg']]
|
| 97 |
|
| 98 |
+
# Create the Gradio interface
|
| 99 |
+
with gr.Blocks(css="""
|
| 100 |
+
.download-btn {
|
| 101 |
+
background-color: #4CAF50 !important;
|
| 102 |
+
color: white !important;
|
| 103 |
+
border: none !important;
|
| 104 |
+
padding: 10px 24px !important;
|
| 105 |
+
text-align: center !important;
|
| 106 |
+
text-decoration: none !important;
|
| 107 |
+
display: inline-block !important;
|
| 108 |
+
font-size: 16px !important;
|
| 109 |
+
margin: 10px 0 !important;
|
| 110 |
+
cursor: pointer !important;
|
| 111 |
+
border-radius: 4px !important;
|
| 112 |
+
transition: background-color 0.3s !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
+
.download-btn:hover {
|
| 115 |
+
background-color: #45a049 !important;
|
| 116 |
+
}
|
| 117 |
+
/* Style the file download component to look like a button */
|
| 118 |
+
.download-container [data-testid="file"] {
|
| 119 |
+
display: block !important;
|
| 120 |
+
}
|
| 121 |
+
.download-container [data-testid="file"] > div:first-child {
|
| 122 |
+
display: none !important; /* Hide the file icon */
|
| 123 |
+
}
|
| 124 |
+
.download-container [data-testid="file"] > div:last-child {
|
| 125 |
+
margin-left: 0 !important;
|
| 126 |
+
}
|
| 127 |
+
.download-container [data-testid="file"] button {
|
| 128 |
background-color: #4CAF50 !important;
|
| 129 |
color: white !important;
|
| 130 |
border: none !important;
|
| 131 |
+
padding: 10px 24px !important;
|
| 132 |
+
text-align: center !important;
|
| 133 |
+
text-decoration: none !important;
|
| 134 |
+
display: inline-block !important;
|
| 135 |
font-size: 16px !important;
|
| 136 |
+
margin: 10px 0 !important;
|
| 137 |
+
cursor: pointer !important;
|
| 138 |
border-radius: 4px !important;
|
| 139 |
+
transition: background-color 0.3s !important;
|
| 140 |
+
width: 100% !important;
|
| 141 |
}
|
| 142 |
+
.download-container [data-testid="file"] button:hover {
|
| 143 |
background-color: #45a049 !important;
|
| 144 |
}
|
| 145 |
+
""") as demo:
|
| 146 |
+
gr.Markdown(f"# {title}")
|
| 147 |
+
gr.Markdown(description)
|
| 148 |
+
|
| 149 |
+
with gr.Row():
|
| 150 |
+
with gr.Column(scale=1):
|
| 151 |
+
input_image = gr.Image(type="numpy", label="Upload Image")
|
| 152 |
+
|
| 153 |
+
with gr.Column(scale=1):
|
| 154 |
+
output_image = gr.Image(type="numpy", label="Result")
|
| 155 |
+
|
| 156 |
+
# Direct download file component styled as a button
|
| 157 |
+
with gr.Row(elem_classes="download-container"):
|
| 158 |
+
download_file = gr.File(
|
| 159 |
+
label="",
|
| 160 |
+
file_count="single",
|
| 161 |
+
interactive=True,
|
| 162 |
+
visible=False
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# Set up example images
|
| 166 |
+
gr.Examples(examples, inputs=input_image)
|
| 167 |
+
|
| 168 |
+
# Process automatically when image is uploaded
|
| 169 |
+
input_image.change(
|
| 170 |
+
fn=process,
|
| 171 |
+
inputs=input_image,
|
| 172 |
+
outputs=[output_image, download_file],
|
| 173 |
+
show_progress="full"
|
| 174 |
+
)
|
| 175 |
|
| 176 |
if __name__ == "__main__":
|
| 177 |
demo.launch(share=False)
|