Spaces:
Running
Running
Salman Alfarisi commited on
Commit ·
e4a31a6
1
Parent(s): 536551c
Initial commit: Remove-Background Gradio app
Browse files- .gitignore +6 -0
- README.md +27 -0
- app.py +68 -0
- requirements.txt +5 -0
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
| 3 |
+
.env/
|
| 4 |
+
venv/
|
| 5 |
+
*.onnx
|
| 6 |
+
*.png
|
README.md
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Background Remover
|
| 3 |
emoji: 📊
|
|
@@ -12,3 +13,29 @@ short_description: 'CPU-only background remover '
|
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
HEAD
|
| 2 |
---
|
| 3 |
title: Background Remover
|
| 4 |
emoji: 📊
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 16 |
+
|
| 17 |
+
# 🖼️ Remove Background AI
|
| 18 |
+
|
| 19 |
+
A lightweight Gradio application that removes image backgrounds using ONNX-based U²-Net inference entirely on CPU. Upload your photos, click **Remove Background**, and download a transparent-PNG in seconds.
|
| 20 |
+
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
## 🚀 Features
|
| 24 |
+
|
| 25 |
+
- **Instant background removal** with U²-Net (ONNX)
|
| 26 |
+
- **CPU-only** inference — no GPU required
|
| 27 |
+
- **Preview** original vs. result side-by-side
|
| 28 |
+
- **Download** your result as a PNG with transparency
|
| 29 |
+
- **Self-hostable**: runs locally or on any cloud/CI that supports Python
|
| 30 |
+
- **Ready for Hugging Face Spaces** via a single `app.py`
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## 📋 Prerequisites
|
| 35 |
+
|
| 36 |
+
- Python 3.8 or higher
|
| 37 |
+
- `pip` package manager
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
723bdcf (Initial commit: Remove-Background Gradio app)
|
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from rembg import remove
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Force ONNXRuntime to CPU only
|
| 8 |
+
os.environ["ORT_PROVIDERS"] = "CPUExecutionProvider"
|
| 9 |
+
|
| 10 |
+
def remove_background(input_image: Image.Image) -> Image.Image:
|
| 11 |
+
"""Your existing remover."""
|
| 12 |
+
return remove(input_image)
|
| 13 |
+
|
| 14 |
+
def remove_and_save(img: Image.Image):
|
| 15 |
+
"""
|
| 16 |
+
1) remove background,
|
| 17 |
+
2) save to a temp PNG,
|
| 18 |
+
3) return (PIL.Image for preview, file path for DownloadButton).
|
| 19 |
+
"""
|
| 20 |
+
result = remove_background(img)
|
| 21 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 22 |
+
result.save(tmp.name, format="PNG")
|
| 23 |
+
tmp.close()
|
| 24 |
+
return result, tmp.name
|
| 25 |
+
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("## 🖼️ Background Remover")
|
| 28 |
+
gr.Markdown(
|
| 29 |
+
"Upload an image and click **Remove Background** to get a transparent PNG. \n"
|
| 30 |
+
"**Note:** CPU-only ONNX Runtime (no GPU needed)."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
original = gr.Image(type="pil", label="Original")
|
| 35 |
+
result = gr.Image(type="pil", label="Result")
|
| 36 |
+
|
| 37 |
+
# Stack Remove + Download vertically
|
| 38 |
+
with gr.Column(elem_id="controls"):
|
| 39 |
+
remove_btn = gr.Button("✂️ Remove Background", variant="primary")
|
| 40 |
+
download_btn = gr.DownloadButton("⬇️ Download PNG")
|
| 41 |
+
|
| 42 |
+
remove_btn.click(
|
| 43 |
+
fn=remove_and_save,
|
| 44 |
+
inputs=original,
|
| 45 |
+
outputs=[result, download_btn],
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Centered footer
|
| 49 |
+
demo.css = """
|
| 50 |
+
#controls { align-items: start; gap: 0.5rem; }
|
| 51 |
+
.footer { text-align: center; margin-top: 2rem; color: #666; font-size:0.9rem; }
|
| 52 |
+
.social-icons a { margin: 0 0.5rem; text-decoration: none; color: inherit; }
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
gr.HTML("""
|
| 56 |
+
<div class="footer">
|
| 57 |
+
Made by Salman Alfarisi • 2025<br>
|
| 58 |
+
<span class="social-icons">
|
| 59 |
+
<a href="https://github.com/salmanalfarisi11" target="_blank">GitHub</a> |
|
| 60 |
+
<a href="https://www.instagram.com/faris.salman111/" target="_blank">Instagram</a> |
|
| 61 |
+
<a href="https://id.linkedin.com/in/salmanalfarisi11" target="_blank">LinkedIn</a>
|
| 62 |
+
</span><br>
|
| 63 |
+
Source code on <a href="https://github.com/salmanalfarisi11" target="_blank">GitHub</a>
|
| 64 |
+
</div>
|
| 65 |
+
""")
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.0
|
| 2 |
+
rembg>=2.0
|
| 3 |
+
pillow>=9.0
|
| 4 |
+
onnxruntime>=1.18
|
| 5 |
+
|