Download app.py from sk5252/bg_api: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/sk5252/bg_api/resolve/main/app.py
- Command line
-
hf download hf://spaces/sk5252/bg_api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sk5252/bg_api/resolve/main/app.py
1.04 kB
| import gradio as gr | |
| from rembg import remove | |
| from PIL import Image | |
| import io | |
| def remove_background(image: Image.Image): | |
| try: | |
| # Convert PIL to bytes | |
| byte_arr = io.BytesIO() | |
| image.save(byte_arr, format="PNG") | |
| input_bytes = byte_arr.getvalue() | |
| # Run background remover | |
| output_bytes = remove(input_bytes) | |
| # Convert back to PIL | |
| output = Image.open(io.BytesIO(output_bytes)).convert("RGBA") | |
| return output | |
| except Exception as e: | |
| # If something goes wrong, return a blank image + print error | |
| print("Error:", e) | |
| return Image.new("RGBA", (512, 512), (255, 0, 0, 0)) # transparent red | |
| iface = gr.Interface( | |
| fn=remove_background, | |
| inputs=gr.Image(type="pil", label="Upload Image"), | |
| outputs=gr.Image(type="pil", label="Background Removed"), | |
| title="Background Remover", | |
| description="Upload an image to remove its background using rembg", | |
| api_name="predict" | |
| ) | |
| iface.launch(show_error=True) # 👈 IMPORTANT: show_error=True | |