Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -319,232 +319,5 @@ with gr.Blocks(css="""
|
|
| 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 = """
|
| 355 |
-
<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
|
| 356 |
-
<div class="logo-container">
|
| 357 |
-
<div class="logo-mark">
|
| 358 |
-
<div class="logo-hexagon"></div>
|
| 359 |
-
<div class="logo-inner">A</div>
|
| 360 |
-
</div>
|
| 361 |
-
<div class="logo-text">AicO Tools</div>
|
| 362 |
-
</div>
|
| 363 |
-
<h1 style="color: #00ffff; font-size: 2.5em; margin: 20px 0; text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);">
|
| 364 |
-
AI Background Removal
|
| 365 |
-
</h1>
|
| 366 |
-
<p style="color: #a0a0a0; font-size: 1.2em;">
|
| 367 |
-
Remove backgrounds instantly using advanced AI technology
|
| 368 |
-
</p>
|
| 369 |
-
</div>
|
| 370 |
-
"""
|
| 371 |
-
|
| 372 |
-
# Create the Gradio interface
|
| 373 |
-
with gr.Blocks(css="""
|
| 374 |
-
/* Import fonts */
|
| 375 |
-
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&family=Roboto+Mono:wght@300;400;700&display=swap');
|
| 376 |
-
|
| 377 |
-
/* Variables */
|
| 378 |
-
:root {
|
| 379 |
-
--neon-cyan: #00ffff;
|
| 380 |
-
--neon-pink: #ff00de;
|
| 381 |
-
--neon-yellow: #ffdd00;
|
| 382 |
-
--dark-background: #0a0a1e;
|
| 383 |
-
--deep-blue: #121238;
|
| 384 |
-
}
|
| 385 |
-
|
| 386 |
-
/* Global styles */
|
| 387 |
-
body {
|
| 388 |
-
font-family: 'Roboto Mono', monospace;
|
| 389 |
-
background: linear-gradient(135deg, var(--dark-background) 0%, var(--deep-blue) 100%);
|
| 390 |
-
color: #ffffff;
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
-
/* Logo styling */
|
| 394 |
-
.logo-container {
|
| 395 |
-
display: flex;
|
| 396 |
-
align-items: center;
|
| 397 |
-
margin-bottom: 2rem;
|
| 398 |
-
animation: float 3s ease-in-out infinite;
|
| 399 |
-
}
|
| 400 |
-
|
| 401 |
-
.logo-mark {
|
| 402 |
-
position: relative;
|
| 403 |
-
width: 40px;
|
| 404 |
-
height: 40px;
|
| 405 |
-
margin-right: 12px;
|
| 406 |
-
animation: spin 12s linear infinite;
|
| 407 |
-
}
|
| 408 |
-
|
| 409 |
-
.logo-hexagon {
|
| 410 |
-
position: absolute;
|
| 411 |
-
width: 100%;
|
| 412 |
-
height: 100%;
|
| 413 |
-
background: linear-gradient(135deg, var(--neon-pink), var(--neon-cyan));
|
| 414 |
-
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
|
| 415 |
-
animation: pulse-glow 4s infinite alternate;
|
| 416 |
-
}
|
| 417 |
-
|
| 418 |
-
.logo-inner {
|
| 419 |
-
position: absolute;
|
| 420 |
-
top: 50%;
|
| 421 |
-
left: 50%;
|
| 422 |
-
transform: translate(-50%, -50%);
|
| 423 |
-
color: var(--neon-cyan);
|
| 424 |
-
font-family: 'Orbitron', sans-serif;
|
| 425 |
-
font-weight: 700;
|
| 426 |
-
font-size: 20px;
|
| 427 |
-
}
|
| 428 |
-
|
| 429 |
-
.logo-text {
|
| 430 |
-
font-family: 'Orbitron', sans-serif;
|
| 431 |
-
font-size: 24px;
|
| 432 |
-
font-weight: 700;
|
| 433 |
-
color: var(--neon-pink);
|
| 434 |
-
text-shadow: 0 0 10px rgba(255, 0, 222, 0.5);
|
| 435 |
-
}
|
| 436 |
-
|
| 437 |
-
/* Container styling */
|
| 438 |
-
.container {
|
| 439 |
-
background: rgba(0, 0, 0, 0.6);
|
| 440 |
-
backdrop-filter: blur(10px);
|
| 441 |
-
border-radius: 10px;
|
| 442 |
-
padding: 20px;
|
| 443 |
-
box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
|
| 444 |
-
}
|
| 445 |
-
|
| 446 |
-
/* Input/Output areas */
|
| 447 |
-
.input-image, .output-image {
|
| 448 |
-
background: rgba(18, 18, 56, 0.7) !important;
|
| 449 |
-
border: 2px solid var(--neon-cyan) !important;
|
| 450 |
-
border-radius: 8px !important;
|
| 451 |
-
transition: all 0.3s ease !important;
|
| 452 |
-
}
|
| 453 |
-
|
| 454 |
-
.input-image:hover, .output-image:hover {
|
| 455 |
-
box-shadow: 0 0 15px rgba(0, 255, 255, 0.3) !important;
|
| 456 |
-
transform: translateY(-2px) !important;
|
| 457 |
-
}
|
| 458 |
-
|
| 459 |
-
/* Download button styling */
|
| 460 |
-
.download-container [data-testid="file"] button {
|
| 461 |
-
background: linear-gradient(45deg, var(--neon-cyan), var(--neon-pink)) !important;
|
| 462 |
-
color: white !important;
|
| 463 |
-
border: none !important;
|
| 464 |
-
padding: 12px 28px !important;
|
| 465 |
-
font-family: 'Orbitron', sans-serif !important;
|
| 466 |
-
font-size: 16px !important;
|
| 467 |
-
font-weight: 600 !important;
|
| 468 |
-
text-transform: uppercase !important;
|
| 469 |
-
letter-spacing: 1px !important;
|
| 470 |
-
border-radius: 8px !important;
|
| 471 |
-
cursor: pointer !important;
|
| 472 |
-
transition: all 0.3s ease !important;
|
| 473 |
-
animation: pulse-glow 2s infinite alternate !important;
|
| 474 |
-
}
|
| 475 |
-
|
| 476 |
-
.download-container [data-testid="file"] button:hover {
|
| 477 |
-
transform: translateY(-2px) !important;
|
| 478 |
-
box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4) !important;
|
| 479 |
-
}
|
| 480 |
-
|
| 481 |
-
/* Labels */
|
| 482 |
-
label {
|
| 483 |
-
color: var(--neon-cyan) !important;
|
| 484 |
-
font-family: 'Orbitron', sans-serif !important;
|
| 485 |
-
font-size: 1.1em !important;
|
| 486 |
-
text-shadow: 0 0 5px rgba(0, 255, 255, 0.5) !important;
|
| 487 |
-
}
|
| 488 |
-
|
| 489 |
-
/* Progress bar */
|
| 490 |
-
.progress-bar {
|
| 491 |
-
background: linear-gradient(90deg, var(--neon-cyan), var(--neon-pink)) !important;
|
| 492 |
-
height: 4px !important;
|
| 493 |
-
border-radius: 2px !important;
|
| 494 |
-
box-shadow: 0 0 10px rgba(0, 255, 255, 0.5) !important;
|
| 495 |
-
}
|
| 496 |
-
|
| 497 |
-
/* Animations */
|
| 498 |
-
@keyframes float {
|
| 499 |
-
0% { transform: translateY(0); }
|
| 500 |
-
50% { transform: translateY(-5px); }
|
| 501 |
-
100% { transform: translateY(0); }
|
| 502 |
-
}
|
| 503 |
-
|
| 504 |
-
@keyframes spin {
|
| 505 |
-
0% { transform: rotateY(0deg); }
|
| 506 |
-
100% { transform: rotateY(360deg); }
|
| 507 |
-
}
|
| 508 |
-
|
| 509 |
-
@keyframes pulse-glow {
|
| 510 |
-
0% { filter: brightness(1) drop-shadow(0 0 5px var(--neon-cyan)); }
|
| 511 |
-
100% { filter: brightness(1.2) drop-shadow(0 0 10px var(--neon-cyan)); }
|
| 512 |
-
}
|
| 513 |
-
""") as demo:
|
| 514 |
-
gr.Markdown(description)
|
| 515 |
-
|
| 516 |
-
with gr.Row(equal_height=True):
|
| 517 |
-
with gr.Column(scale=1):
|
| 518 |
-
input_image = gr.Image(
|
| 519 |
-
type="numpy",
|
| 520 |
-
label="Upload Your Image",
|
| 521 |
-
elem_id="input-image",
|
| 522 |
-
elem_classes="input-image"
|
| 523 |
-
)
|
| 524 |
-
|
| 525 |
-
with gr.Column(scale=1):
|
| 526 |
-
output_image = gr.Image(
|
| 527 |
-
type="numpy",
|
| 528 |
-
label="Result",
|
| 529 |
-
elem_id="output-image",
|
| 530 |
-
elem_classes="output-image"
|
| 531 |
-
)
|
| 532 |
-
|
| 533 |
-
with gr.Row(elem_classes="download-container"):
|
| 534 |
-
download_file = gr.File(
|
| 535 |
-
label="",
|
| 536 |
-
file_count="single",
|
| 537 |
-
interactive=True,
|
| 538 |
-
visible=False
|
| 539 |
-
)
|
| 540 |
-
|
| 541 |
-
# Process automatically when image is uploaded
|
| 542 |
-
input_image.change(
|
| 543 |
-
fn=process,
|
| 544 |
-
inputs=input_image,
|
| 545 |
-
outputs=[output_image, download_file],
|
| 546 |
-
show_progress="full"
|
| 547 |
-
)
|
| 548 |
-
|
| 549 |
if __name__ == "__main__":
|
| 550 |
demo.launch(share=False)
|
|
|
|
| 319 |
show_progress="full"
|
| 320 |
)
|
| 321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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