"""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()