Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
from rembg.session_factory import new_session
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
session = new_session("u2net")
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
if
|
| 10 |
-
return None
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
output_bytes = remove(
|
| 15 |
input_bytes,
|
| 16 |
session=session,
|
| 17 |
alpha_matting=False
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
with gr.Blocks(
|
|
|
|
| 23 |
css="""
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
background: #0b0f19;
|
| 27 |
-
}
|
| 28 |
-
#drop {
|
| 29 |
-
border: 2px dashed #555;
|
| 30 |
-
height: 85vh;
|
| 31 |
-
display: flex;
|
| 32 |
-
align-items: center;
|
| 33 |
-
justify-content: center;
|
| 34 |
-
font-size: 32px;
|
| 35 |
-
color: white;
|
| 36 |
-
cursor: pointer;
|
| 37 |
-
}
|
| 38 |
"""
|
| 39 |
) as demo:
|
| 40 |
|
| 41 |
-
gr.Markdown(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
elem_id="drop"
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
from rembg.session_factory import new_session
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io, base64, os
|
| 6 |
|
| 7 |
+
PORT = int(os.environ.get("PORT", 7860))
|
| 8 |
+
|
| 9 |
+
# 🧠 FULL MODEL (not lite)
|
| 10 |
session = new_session("u2net")
|
| 11 |
|
| 12 |
+
def process(image):
|
| 13 |
+
if image is None:
|
| 14 |
+
return None, ""
|
| 15 |
|
| 16 |
+
# PIL → bytes
|
| 17 |
+
buf = io.BytesIO()
|
| 18 |
+
image.save(buf, format="PNG")
|
| 19 |
+
input_bytes = buf.getvalue()
|
| 20 |
|
| 21 |
+
# Background removal (FULL U²-Net)
|
| 22 |
output_bytes = remove(
|
| 23 |
input_bytes,
|
| 24 |
session=session,
|
| 25 |
alpha_matting=False
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Bytes → PIL
|
| 29 |
+
out_img = Image.open(io.BytesIO(output_bytes)).convert("RGBA")
|
| 30 |
+
|
| 31 |
+
# PIL → base64 (auto-download)
|
| 32 |
+
out_buf = io.BytesIO()
|
| 33 |
+
out_img.save(out_buf, format="PNG")
|
| 34 |
+
b64 = base64.b64encode(out_buf.getvalue()).decode()
|
| 35 |
+
|
| 36 |
+
html = f"""
|
| 37 |
+
<script>
|
| 38 |
+
const a = document.createElement("a");
|
| 39 |
+
a.href = "data:image/png;base64,{b64}";
|
| 40 |
+
a.download = "xlnk-rmbg.png";
|
| 41 |
+
document.body.appendChild(a);
|
| 42 |
+
a.click();
|
| 43 |
+
a.remove();
|
| 44 |
+
</script>
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
return out_img, html
|
| 48 |
+
|
| 49 |
|
| 50 |
with gr.Blocks(
|
| 51 |
+
title="Xlnk Rmbg – AI Background Remover",
|
| 52 |
css="""
|
| 53 |
+
body { background:#0b0f19; }
|
| 54 |
+
h1, h2, p { color:white; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
"""
|
| 56 |
) as demo:
|
| 57 |
|
| 58 |
+
gr.Markdown("""
|
| 59 |
+
# ✂️ Xlnk Rmbg
|
| 60 |
+
**High-Quality AI Background Removal (U²-Net)**
|
|
|
|
| 61 |
|
| 62 |
+
Drag & drop → preview → auto-download
|
| 63 |
+
No storage. No tracking.
|
| 64 |
+
""")
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
with gr.Row():
|
| 67 |
+
input_img = gr.Image(type="pil", label="Upload image")
|
| 68 |
+
output_img = gr.Image(type="pil", label="Preview (Transparent PNG)")
|
| 69 |
|
| 70 |
+
html = gr.HTML()
|
| 71 |
+
|
| 72 |
+
input_img.change(
|
| 73 |
+
fn=process,
|
| 74 |
+
inputs=input_img,
|
| 75 |
+
outputs=[output_img, html]
|
| 76 |
)
|
| 77 |
|
| 78 |
+
gr.Markdown("""
|
| 79 |
+
### 🔒 Privacy
|
| 80 |
+
- Images processed in memory
|
| 81 |
+
- Nothing saved
|
| 82 |
+
- Open-source (Rembg + U²-Net)
|
| 83 |
+
""")
|
| 84 |
+
|
| 85 |
+
demo.launch(
|
| 86 |
+
server_name="0.0.0.0",
|
| 87 |
+
server_port=PORT,
|
| 88 |
+
show_error=True
|
| 89 |
+
)
|