File size: 3,786 Bytes
a5f4f9e
fe4deef
 
e01c218
fe4deef
5ffd6e4
fe4deef
e01c218
1c276c6
e01c218
1c276c6
fe4deef
 
e01c218
fe4deef
8bca7b3
2586906
4ef44ad
e01c218
664c62d
 
 
fe4deef
e01c218
664c62d
 
 
 
 
 
fe4deef
664c62d
 
 
 
 
 
5a5bf1e
664c62d
 
 
8a8b950
664c62d
98b8b30
8a8b950
e01c218
fe4deef
664c62d
fe4deef
664c62d
fe4deef
664c62d
fe4deef
664c62d
 
 
 
fe4deef
 
 
 
664c62d
fe4deef
 
 
4ef44ad
 
1c276c6
fe4deef
664c62d
fe4deef
 
 
 
1c276c6
fe4deef
664c62d
fe4deef
 
664c62d
fe4deef
 
664c62d
 
2586906
664c62d
 
 
 
 
2586906
664c62d
2586906
664c62d
 
 
5ffd6e4
664c62d
 
 
 
 
 
 
 
 
fe4deef
 
664c62d
1afd42b
664c62d
fe4deef
 
 
e01c218
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import spaces
import gradio as gr
import torch
from diffusers import BriaFiboEditPipeline
from PIL import Image
import tempfile

# Load the model on CPU at module level (ZeroGPU moves to CUDA inside @spaces.GPU)
print("Loading Fibo-Edit-RMBG model...")
pipe = BriaFiboEditPipeline.from_pretrained(
    "briaai/Fibo-Edit-RMBG",
    torch_dtype=torch.bfloat16,
)
print("Model loaded on CPU")

INSTRUCTION = {'edit_instruction':'Generate a detailed grayscale alpha matte. Map the opaque foreground to white and the background to black. Produce soft, anti-aliased grayscale gradients at the edges of the subject to represent fine details and transparency.'}


@spaces.GPU(duration=300)
def process(image, num_steps=10, guidance_scale=1.0):
    if image is None:
        return None, None

    pipe.to("cuda")
    mask = pipe(
        image=image,
        prompt=INSTRUCTION,
        num_inference_steps=int(num_steps),
        guidance_scale=guidance_scale,
    ).images[0]

    original_rgba = image.convert("RGBA")
    mask_gray = mask.convert("L")
    if mask_gray.size != original_rgba.size:
        mask_gray = mask_gray.resize(original_rgba.size, Image.Resampling.LANCZOS)
    original_rgba.putalpha(mask_gray)
    result = original_rgba

    # Save full-res result for download
    tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
    result.save(tmp.name, format="PNG")

    return (image.convert("RGBA"), result), tmp.name


with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
    gr.Markdown("""
# Fibo-Edit-RMBG - Background Removal

Powered by [Bria AI's Fibo-Edit-RMBG model](https://huggingface.co/briaai/Fibo-Edit-RMBG)

This model performs background removal on any image

### How to use:
1. Upload your image
2. Adjust settings if needed (optional)
3. Click Process!
    """)

    with gr.Row():
        with gr.Column(scale=1):
            input_image = gr.Image(label="Input Image", type="pil", height=400)

            with gr.Accordion("Advanced Settings", open=False):
                num_steps = gr.Slider(
                    minimum=1,
                    maximum=50,
                    value=10,
                    step=1,
                    label="Number of Steps",
                )
                guidance_scale = gr.Slider(
                    minimum=1.0,
                    maximum=15.0,
                    value=1.0,
                    step=0.5,
                    label="Guidance Scale",
                )

            process_btn = gr.Button("Remove Background", variant="primary", size="lg")

        with gr.Column(scale=1):
            slider = gr.ImageSlider(
                label="Original vs Background Removed",
                type="pil",
                image_mode="RGBA",
                format="png",
                slider_position=50,
                max_height=500,
                container=True,
            )
            download_file = gr.File(label="Download Result (PNG with transparency)")

    gr.Markdown("""
---
### About Fibo-Edit-RMBG

Fibo-Edit-RMBG is built on [Fibo-Edit](https://huggingface.co/briaai/Fibo-Edit), an 8B parameter image editing model
that uses structured prompts for precise, deterministic editing.

- **Model**: [briaai/Fibo-Edit-RMBG](https://huggingface.co/briaai/Fibo-Edit-RMBG)
- **Architecture**: Based on FIBO with 8B parameters
- **License**: Non-commercial use ([CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/))

For commercial use, please [contact Bria AI](https://bria.ai/contact-us).
    """)

    process_btn.click(
        fn=process,
        inputs=[input_image, num_steps, guidance_scale],
        outputs=[slider, download_file],
    )

if __name__ == "__main__":
    demo.launch(show_error=True, theme=gr.themes.Soft(primary_hue="sky"))