Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,6 +89,266 @@ def process(image, progress=gr.Progress()):
|
|
| 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 Tool"
|
| 94 |
description = """
|
|
|
|
| 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 Tool"
|
| 94 |
+
description = """
|
| 95 |
+
<style>
|
| 96 |
+
.custom-container {
|
| 97 |
+
text-align: center;
|
| 98 |
+
max-width: 800px;
|
| 99 |
+
margin: 0 auto;
|
| 100 |
+
padding: 20px;
|
| 101 |
+
background: rgba(10, 10, 30, 0.6);
|
| 102 |
+
border-radius: 15px;
|
| 103 |
+
box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
|
| 104 |
+
}
|
| 105 |
+
.custom-logo {
|
| 106 |
+
display: flex;
|
| 107 |
+
align-items: center;
|
| 108 |
+
justify-content: center;
|
| 109 |
+
margin-bottom: 2rem;
|
| 110 |
+
transform: scale(0.8);
|
| 111 |
+
}
|
| 112 |
+
.logo-mark {
|
| 113 |
+
position: relative;
|
| 114 |
+
width: 60px;
|
| 115 |
+
height: 60px;
|
| 116 |
+
margin-right: 15px;
|
| 117 |
+
animation: logo-spin 12s linear infinite;
|
| 118 |
+
}
|
| 119 |
+
.logo-hexagon {
|
| 120 |
+
position: absolute;
|
| 121 |
+
width: 100%;
|
| 122 |
+
height: 100%;
|
| 123 |
+
background: linear-gradient(135deg, #ff00de, #00ffff);
|
| 124 |
+
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
|
| 125 |
+
animation: pulse-glow 4s infinite alternate;
|
| 126 |
+
}
|
| 127 |
+
.logo-inner {
|
| 128 |
+
position: absolute;
|
| 129 |
+
top: 50%;
|
| 130 |
+
left: 50%;
|
| 131 |
+
transform: translate(-50%, -50%);
|
| 132 |
+
color: #00ffff;
|
| 133 |
+
font-family: 'Orbitron', sans-serif;
|
| 134 |
+
font-weight: 700;
|
| 135 |
+
font-size: 28px;
|
| 136 |
+
text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
|
| 137 |
+
}
|
| 138 |
+
.logo-text {
|
| 139 |
+
font-family: 'Orbitron', sans-serif;
|
| 140 |
+
font-size: 36px;
|
| 141 |
+
font-weight: 700;
|
| 142 |
+
background: linear-gradient(45deg, #ff00de, #00ffff);
|
| 143 |
+
-webkit-background-clip: text;
|
| 144 |
+
background-clip: text;
|
| 145 |
+
-webkit-text-fill-color: transparent;
|
| 146 |
+
text-shadow: 0 0 10px rgba(255, 0, 222, 0.5);
|
| 147 |
+
animation: text-glow 2s infinite alternate;
|
| 148 |
+
}
|
| 149 |
+
.title-text {
|
| 150 |
+
color: #00ffff;
|
| 151 |
+
font-family: 'Orbitron', sans-serif;
|
| 152 |
+
font-size: 2.5em;
|
| 153 |
+
margin: 20px 0;
|
| 154 |
+
text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
|
| 155 |
+
animation: title-pulse 2s infinite alternate;
|
| 156 |
+
}
|
| 157 |
+
.subtitle-text {
|
| 158 |
+
color: #a0a0a0;
|
| 159 |
+
font-family: 'Roboto Mono', monospace;
|
| 160 |
+
font-size: 1.2em;
|
| 161 |
+
margin-top: 10px;
|
| 162 |
+
line-height: 1.5;
|
| 163 |
+
}
|
| 164 |
+
@keyframes logo-spin {
|
| 165 |
+
0% { transform: rotateY(0deg); }
|
| 166 |
+
100% { transform: rotateY(360deg); }
|
| 167 |
+
}
|
| 168 |
+
@keyframes pulse-glow {
|
| 169 |
+
0% { filter: brightness(1) drop-shadow(0 0 5px #00ffff); }
|
| 170 |
+
100% { filter: brightness(1.2) drop-shadow(0 0 15px #00ffff); }
|
| 171 |
+
}
|
| 172 |
+
@keyframes text-glow {
|
| 173 |
+
0% { text-shadow: 0 0 5px rgba(255, 0, 222, 0.5); }
|
| 174 |
+
100% { text-shadow: 0 0 15px rgba(255, 0, 222, 0.8), 0 0 25px rgba(0, 255, 255, 0.5); }
|
| 175 |
+
}
|
| 176 |
+
@keyframes title-pulse {
|
| 177 |
+
0% { text-shadow: 0 0 5px rgba(0, 255, 255, 0.5); }
|
| 178 |
+
100% { text-shadow: 0 0 15px rgba(0, 255, 255, 0.8), 0 0 25px rgba(0, 255, 255, 0.5); }
|
| 179 |
+
}
|
| 180 |
+
</style>
|
| 181 |
+
<div class="custom-container">
|
| 182 |
+
<div class="custom-logo">
|
| 183 |
+
<div class="logo-mark">
|
| 184 |
+
<div class="logo-hexagon"></div>
|
| 185 |
+
<div class="logo-inner">A</div>
|
| 186 |
+
</div>
|
| 187 |
+
<div class="logo-text">AicO Tools</div>
|
| 188 |
+
</div>
|
| 189 |
+
<h1 class="title-text">AI Background Removal</h1>
|
| 190 |
+
<p class="subtitle-text">
|
| 191 |
+
Remove backgrounds instantly using advanced AI technology
|
| 192 |
+
</p>
|
| 193 |
+
</div>
|
| 194 |
+
"""
|
| 195 |
+
|
| 196 |
+
# Create the Gradio interface
|
| 197 |
+
with gr.Blocks(css="""
|
| 198 |
+
/* Import fonts */
|
| 199 |
+
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&family=Roboto+Mono:wght@300;400;700&display=swap');
|
| 200 |
+
|
| 201 |
+
/* Variables */
|
| 202 |
+
:root {
|
| 203 |
+
--neon-cyan: #00ffff;
|
| 204 |
+
--neon-pink: #ff00de;
|
| 205 |
+
--neon-yellow: #ffdd00;
|
| 206 |
+
--dark-background: #0a0a1e;
|
| 207 |
+
--deep-blue: #121238;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
/* Global styles */
|
| 211 |
+
body {
|
| 212 |
+
font-family: 'Roboto Mono', monospace;
|
| 213 |
+
background: linear-gradient(135deg, var(--dark-background) 0%, var(--deep-blue) 100%);
|
| 214 |
+
color: #ffffff;
|
| 215 |
+
min-height: 100vh;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
/* Container styling */
|
| 219 |
+
.container {
|
| 220 |
+
background: rgba(0, 0, 0, 0.6);
|
| 221 |
+
backdrop-filter: blur(10px);
|
| 222 |
+
border-radius: 15px;
|
| 223 |
+
padding: 20px;
|
| 224 |
+
box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
|
| 225 |
+
margin: 20px;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
/* Input/Output areas */
|
| 229 |
+
.input-image, .output-image {
|
| 230 |
+
background: rgba(18, 18, 56, 0.7) !important;
|
| 231 |
+
border: 2px solid var(--neon-cyan) !important;
|
| 232 |
+
border-radius: 12px !important;
|
| 233 |
+
transition: all 0.3s ease !important;
|
| 234 |
+
overflow: hidden !important;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
.input-image:hover, .output-image:hover {
|
| 238 |
+
box-shadow: 0 0 20px rgba(0, 255, 255, 0.4) !important;
|
| 239 |
+
transform: translateY(-2px) !important;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
/* Download button styling */
|
| 243 |
+
.download-container [data-testid="file"] button {
|
| 244 |
+
background: linear-gradient(45deg, var(--neon-cyan), var(--neon-pink)) !important;
|
| 245 |
+
color: white !important;
|
| 246 |
+
border: none !important;
|
| 247 |
+
padding: 12px 28px !important;
|
| 248 |
+
font-family: 'Orbitron', sans-serif !important;
|
| 249 |
+
font-size: 16px !important;
|
| 250 |
+
font-weight: 600 !important;
|
| 251 |
+
text-transform: uppercase !important;
|
| 252 |
+
letter-spacing: 1px !important;
|
| 253 |
+
border-radius: 8px !important;
|
| 254 |
+
cursor: pointer !important;
|
| 255 |
+
transition: all 0.3s ease !important;
|
| 256 |
+
animation: button-glow 2s infinite alternate !important;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
.download-container [data-testid="file"] button:hover {
|
| 260 |
+
transform: translateY(-2px) !important;
|
| 261 |
+
box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4) !important;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
/* Labels */
|
| 265 |
+
label {
|
| 266 |
+
color: var(--neon-cyan) !important;
|
| 267 |
+
font-family: 'Orbitron', sans-serif !important;
|
| 268 |
+
font-size: 1.1em !important;
|
| 269 |
+
text-shadow: 0 0 5px rgba(0, 255, 255, 0.5) !important;
|
| 270 |
+
margin-bottom: 8px !important;
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
/* Progress bar */
|
| 274 |
+
.progress-bar {
|
| 275 |
+
background: linear-gradient(90deg, var(--neon-cyan), var(--neon-pink)) !important;
|
| 276 |
+
height: 4px !important;
|
| 277 |
+
border-radius: 2px !important;
|
| 278 |
+
box-shadow: 0 0 10px rgba(0, 255, 255, 0.5) !important;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
/* Additional Animations */
|
| 282 |
+
@keyframes button-glow {
|
| 283 |
+
0% { box-shadow: 0 0 5px rgba(0, 255, 255, 0.5); }
|
| 284 |
+
100% { box-shadow: 0 0 15px rgba(0, 255, 255, 0.8), 0 0 25px rgba(255, 0, 222, 0.5); }
|
| 285 |
+
}
|
| 286 |
+
""") as demo:
|
| 287 |
+
gr.Markdown(description)
|
| 288 |
+
|
| 289 |
+
with gr.Row(equal_height=True):
|
| 290 |
+
with gr.Column(scale=1):
|
| 291 |
+
input_image = gr.Image(
|
| 292 |
+
type="numpy",
|
| 293 |
+
label="Upload Your Image",
|
| 294 |
+
elem_id="input-image",
|
| 295 |
+
elem_classes="input-image"
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
with gr.Column(scale=1):
|
| 299 |
+
output_image = gr.Image(
|
| 300 |
+
type="numpy",
|
| 301 |
+
label="Result",
|
| 302 |
+
elem_id="output-image",
|
| 303 |
+
elem_classes="output-image"
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
with gr.Row(elem_classes="download-container"):
|
| 307 |
+
download_file = gr.File(
|
| 308 |
+
label="",
|
| 309 |
+
file_count="single",
|
| 310 |
+
interactive=True,
|
| 311 |
+
visible=False
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
+
# Process automatically when image is uploaded
|
| 315 |
+
input_image.change(
|
| 316 |
+
fn=process,
|
| 317 |
+
inputs=input_image,
|
| 318 |
+
outputs=[output_image, download_file],
|
| 319 |
+
show_progress="full"
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
if __name__ == "__main__":
|
| 323 |
+
demo.launch(share=False) ma = torch.max(result)
|
| 324 |
+
mi = torch.min(result)
|
| 325 |
+
result = (result - mi) / (ma - mi)
|
| 326 |
+
|
| 327 |
+
# Convert the result to an image
|
| 328 |
+
result_array = (result * 255).cpu().data.numpy().astype(np.uint8)
|
| 329 |
+
pil_mask = Image.fromarray(np.squeeze(result_array))
|
| 330 |
+
|
| 331 |
+
# Add the mask as alpha channel to the original image
|
| 332 |
+
new_im = orig_image.copy()
|
| 333 |
+
new_im.putalpha(pil_mask)
|
| 334 |
+
|
| 335 |
+
progress(0.8, desc="Preparing download...")
|
| 336 |
+
# Generate a unique filename
|
| 337 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 338 |
+
filename = f"background_removed_{unique_id}.png"
|
| 339 |
+
filepath = os.path.join(OUTPUT_DIR, filename)
|
| 340 |
+
|
| 341 |
+
# Save the processed image
|
| 342 |
+
new_im.save(filepath, format='PNG')
|
| 343 |
+
|
| 344 |
+
# Convert to numpy array for display
|
| 345 |
+
output_array = np.array(new_im.convert("RGBA"))
|
| 346 |
+
|
| 347 |
+
progress(1.0, desc="Done!")
|
| 348 |
+
|
| 349 |
+
# Return the processed image and the file for download
|
| 350 |
+
return output_array, gr.update(visible=True, value=filepath, interactive=True)
|
| 351 |
+
|
| 352 |
# Gradio interface setup
|
| 353 |
title = "Background Removal Tool"
|
| 354 |
description = """
|