File size: 1,362 Bytes
8c10814
e612aac
97d91b0
 
 
 
 
 
 
 
 
 
 
 
e612aac
 
8c10814
e612aac
8c10814
97d91b0
8c10814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Hugging Face Spaces entry point for BiRefNet background removal."""

# ZeroGPU requires this import and decorator. The fallback keeps local and
# ordinary CPU Spaces usable without the Hugging Face-only ``spaces`` package.
try:
    import spaces
except ImportError:
    class _LocalSpaces:
        @staticmethod
        def GPU(**_kwargs):
            return lambda function: function

    spaces = _LocalSpaces()

import gradio as gr

from remove_background import remove_background


@spaces.GPU(duration=90)
def process(image):
    """Adapt the reusable image function to Gradio's callback interface."""
    return remove_background(image)


with gr.Blocks(title="BiRefNet Background Remover") as demo:
    gr.Markdown(
        """
        # BiRefNet Background Remover
        Upload an image to create a PNG with a transparent background.

        The first request may take longer while the BiRefNet model is downloaded.
        """
    )
    with gr.Row():
        source = gr.Image(label="Image", type="pil", sources=["upload"])
        result = gr.Image(label="Transparent PNG", type="pil", format="png")
    run_button = gr.Button("Remove background", variant="primary")
    run_button.click(process, inputs=source, outputs=result, api_name="remove_background")

demo.queue(default_concurrency_limit=1)

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