File size: 2,255 Bytes
e4a31a6
3f32aed
688350d
 
 
3f32aed
688350d
 
3f32aed
688350d
 
 
3f32aed
688350d
3f32aed
688350d
 
 
3f32aed
688350d
3f32aed
688350d
3f32aed
688350d
3f32aed
688350d
 
e4a31a6
688350d
 
e4a31a6
 
3f32aed
688350d
 
 
 
 
 
 
 
 
 
 
 
 
3f32aed
688350d
 
 
 
 
 
e4a31a6
688350d
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import tempfile
import gradio as gr
from rembg import remove
from PIL import Image

# Force ONNXRuntime to CPU only
os.environ["ORT_PROVIDERS"] = "CPUExecutionProvider"

def remove_background(input_image: Image.Image) -> Image.Image:
    """Your existing remover."""
    return remove(input_image)

def remove_and_save(img: Image.Image):
    """
    1) remove background,
    2) save to a temp PNG,
    3) return (PIL.Image for preview, file path for DownloadButton).
    """
    result = remove_background(img)
    tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
    result.save(tmp.name, format="PNG")
    tmp.close()
    return result, tmp.name

with gr.Blocks() as demo:
    gr.Markdown("## 🖼️ Background Remover")
    gr.Markdown(
        "Upload an image and click **Remove Background** to get a transparent PNG.  \n"
        "**Note:** CPU-only ONNX Runtime (no GPU needed)."
    )

    with gr.Row():
        original = gr.Image(type="pil", label="Original")
        result   = gr.Image(type="pil", label="Result")

    # Stack Remove + Download vertically
    with gr.Column(elem_id="controls"):
        remove_btn   = gr.Button("✂️ Remove Background", variant="primary")
        download_btn = gr.DownloadButton("⬇️ Download PNG")

    remove_btn.click(
        fn=remove_and_save,
        inputs=original,
        outputs=[result, download_btn],
    )

    # Centered footer
    demo.css = """
      #controls { align-items: start; gap: 0.5rem; }
      .footer { text-align: center; margin-top: 2rem; color: #666; font-size:0.9rem; }
      .social-icons a { margin: 0 0.5rem; text-decoration: none; color: inherit; }
    """

    gr.HTML("""
      <div class="footer">
        Made by Salman Alfarisi • 2025<br>
        <span class="social-icons">
          <a href="https://github.com/salmanalfarisi11" target="_blank">GitHub</a> |
          <a href="https://www.instagram.com/faris.salman111/" target="_blank">Instagram</a> |
          <a href="https://id.linkedin.com/in/salmanalfarisi11" target="_blank">LinkedIn</a>
        </span><br>
        Source code on <a href="https://github.com/salmanalfarisi11" target="_blank">GitHub</a>
      </div>
    """)

if __name__ == "__main__":
    demo.launch()