OriLib commited on
Commit
664c62d
·
verified ·
1 Parent(s): 8a8b950

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -106
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import spaces
2
  import gradio as gr
3
- from gradio_imageslider import ImageSlider
4
  import torch
5
  from diffusers import DiffusionPipeline
6
  from PIL import Image
@@ -11,99 +10,59 @@ print("Loading Fibo-Edit-RMBG model...")
11
  pipe = DiffusionPipeline.from_pretrained(
12
  "briaai/Fibo-Edit-RMBG",
13
  torch_dtype=torch.bfloat16,
14
- trust_remote_code=True
15
  )
16
 
17
- # Check if CUDA is available
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
19
- pipe.to(device)
20
  print(f"Model loaded on {device}")
21
 
22
- @spaces.GPU
23
- def process_image(image, num_steps=10, guidance_scale=1.0):
24
- """
25
- Process an image with Fibo-Edit-RMBG
26
- """
27
- if image is None:
28
- return None, None, "Please upload an image"
29
-
30
- # Fixed instruction for background removal
31
- 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.'}\""
32
-
33
- try:
34
- # Run the pipeline to get the grayscale alpha matte
35
- mask = pipe(
36
- image=image,
37
- prompt=instruction,
38
- num_inference_steps=num_steps,
39
- guidance_scale=guidance_scale
40
- ).images[0]
41
-
42
- # Convert original image to RGBA
43
- original_rgba = image.convert("RGBA")
44
-
45
- # Convert mask to grayscale (in case it's not already)
46
- mask_gray = mask.convert("L")
47
-
48
- # Resize mask to match original image dimensions
49
- if mask_gray.size != original_rgba.size:
50
- mask_gray = mask_gray.resize(original_rgba.size, Image.LANCZOS)
51
 
52
- # Use the mask as the alpha channel
53
- original_rgba.putalpha(mask_gray)
54
 
55
- tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
56
- original_rgba.save(tmp.name, format="PNG")
57
-
58
- return (original_rgba, image), tmp.name, "✅ Image processed successfully!"
59
 
60
- except Exception as e:
61
- return None, None, f"❌ Error: {str(e)}"
 
 
 
 
62
 
63
- # css = """
64
- # .image-container img {
65
- # object-fit: contain !important;
66
- # }
67
- # """
 
68
 
69
- # css = """
70
- # .image-container img,
71
- # .image-container .fixed img,
72
- # .image-container .fixed {
73
- # object-fit: contain !important;
74
- # max-height: 400px !important;
75
- # }
76
- # """
77
 
78
- css = """
79
- #output-slider img {
80
- object-fit: contain !important;
81
- }
82
- """
83
 
84
 
85
- # Create Gradio interface
86
- with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal", css=css) as demo:
87
  gr.Markdown("""
88
- # Fibo-Edit-RMBG - Background Removal
89
 
90
- Powered by [Bria AI's Fibo-Edit-RMBG model](https://huggingface.co/briaai/Fibo-Edit-RMBG)
91
 
92
- This model performs background removal on any image
93
 
94
- ### How to use:
95
- 1. Upload your image
96
- 2. Adjust settings if needed (optional)
97
- 3. Click Process!
98
  """)
99
 
100
  with gr.Row():
101
  with gr.Column(scale=1):
102
- input_image = gr.Image(
103
- label="Input Image",
104
- type="pil",
105
- height=400
106
- )
107
 
108
  with gr.Accordion("Advanced Settings", open=False):
109
  num_steps = gr.Slider(
@@ -111,57 +70,55 @@ with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal", css=css) as demo:
111
  maximum=50,
112
  value=10,
113
  step=1,
114
- label="Number of Steps"
115
  )
116
-
117
  guidance_scale = gr.Slider(
118
  minimum=1.0,
119
  maximum=15.0,
120
  value=1.0,
121
  step=0.5,
122
- label="Guidance Scale"
123
  )
124
 
125
- process_btn = gr.Button("🚀 Process Image", variant="primary")
126
 
127
  with gr.Column(scale=1):
128
- output_image = ImageSlider(
129
- label="Result vs Original",
130
  type="pil",
131
- height=400,
132
- elem_id="output-slider"
 
 
 
133
  )
 
134
 
135
- download_file = gr.File(label="Download PNG (with transparency)")
 
 
136
 
137
- status_text = gr.Textbox(
138
- label="Status",
139
- interactive=False,
140
- lines=2
141
- )
 
 
 
 
142
 
143
- # Connect the button
144
  process_btn.click(
145
- fn=process_image,
146
  inputs=[input_image, num_steps, guidance_scale],
147
- outputs=[output_image, download_file, status_text]
148
  )
149
 
150
- gr.Markdown("""
151
- ---
152
- ### About Fibo-Edit-RMBG
153
-
154
- Fibo-Edit-RMBG is built on [Fibo-Edit](https://huggingface.co/briaai/Fibo-Edit), an 8B parameter image editing model
155
- that uses structured prompts for precise, deterministic editing.
156
-
157
- - **Model**: [briaai/Fibo-Edit-RMBG](https://huggingface.co/briaai/Fibo-Edit-RMBG)
158
- - **Architecture**: Based on FIBO with 8B parameters
159
- - **License**: Non-commercial use ([CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/))
160
-
161
- For commercial use, please [contact Bria AI](https://bria.ai/contact-us).
162
- """)
163
 
164
- # Launch the app
165
  if __name__ == "__main__":
166
- demo.launch(show_api=False)
167
-
 
1
  import spaces
2
  import gradio as gr
 
3
  import torch
4
  from diffusers import DiffusionPipeline
5
  from PIL import Image
 
10
  pipe = DiffusionPipeline.from_pretrained(
11
  "briaai/Fibo-Edit-RMBG",
12
  torch_dtype=torch.bfloat16,
13
+ trust_remote_code=True,
14
  )
15
 
 
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ # pipe.to(device)
18
  print(f"Model loaded on {device}")
19
 
20
+ 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.'}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
 
 
22
 
23
+ @spaces.GPU
24
+ def process(image, num_steps=10, guidance_scale=1.0):
25
+ if image is None:
26
+ return None, None
27
 
28
+ mask = pipe(
29
+ image=image,
30
+ prompt=INSTRUCTION,
31
+ num_inference_steps=int(num_steps),
32
+ guidance_scale=guidance_scale,
33
+ ).images[0]
34
 
35
+ original_rgba = image.convert("RGBA")
36
+ mask_gray = mask.convert("L")
37
+ if mask_gray.size != original_rgba.size:
38
+ mask_gray = mask_gray.resize(original_rgba.size, Image.Resampling.LANCZOS)
39
+ original_rgba.putalpha(mask_gray)
40
+ result = original_rgba
41
 
42
+ # Save full-res result for download
43
+ tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
44
+ result.save(tmp.name, format="PNG")
 
 
 
 
 
45
 
46
+ return (image.convert("RGBA"), result), tmp.name
 
 
 
 
47
 
48
 
49
+ with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal", theme=gr.themes.Soft(primary_hue="sky")) as demo:
 
50
  gr.Markdown("""
51
+ # Fibo-Edit-RMBG - Background Removal
52
 
53
+ Powered by [Bria AI's Fibo-Edit-RMBG model](https://huggingface.co/briaai/Fibo-Edit-RMBG)
54
 
55
+ This model performs background removal on any image
56
 
57
+ ### How to use:
58
+ 1. Upload your image
59
+ 2. Adjust settings if needed (optional)
60
+ 3. Click Process!
61
  """)
62
 
63
  with gr.Row():
64
  with gr.Column(scale=1):
65
+ input_image = gr.Image(label="Input Image", type="pil", height=400)
 
 
 
 
66
 
67
  with gr.Accordion("Advanced Settings", open=False):
68
  num_steps = gr.Slider(
 
70
  maximum=50,
71
  value=10,
72
  step=1,
73
+ label="Number of Steps",
74
  )
 
75
  guidance_scale = gr.Slider(
76
  minimum=1.0,
77
  maximum=15.0,
78
  value=1.0,
79
  step=0.5,
80
+ label="Guidance Scale",
81
  )
82
 
83
+ process_btn = gr.Button("Remove Background", variant="primary", size="lg")
84
 
85
  with gr.Column(scale=1):
86
+ slider = gr.ImageSlider(
87
+ label="Original vs Background Removed",
88
  type="pil",
89
+ image_mode="RGBA",
90
+ format="png",
91
+ slider_position=50,
92
+ max_height=500,
93
+ container=True,
94
  )
95
+ download_file = gr.File(label="Download Result (PNG with transparency)")
96
 
97
+ gr.Markdown("""
98
+ ---
99
+ ### About Fibo-Edit-RMBG
100
 
101
+ Fibo-Edit-RMBG is built on [Fibo-Edit](https://huggingface.co/briaai/Fibo-Edit), an 8B parameter image editing model
102
+ that uses structured prompts for precise, deterministic editing.
103
+
104
+ - **Model**: [briaai/Fibo-Edit-RMBG](https://huggingface.co/briaai/Fibo-Edit-RMBG)
105
+ - **Architecture**: Based on FIBO with 8B parameters
106
+ - **License**: Non-commercial use ([CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/))
107
+
108
+ For commercial use, please [contact Bria AI](https://bria.ai/contact-us).
109
+ """)
110
 
 
111
  process_btn.click(
112
+ fn=process,
113
  inputs=[input_image, num_steps, guidance_scale],
114
+ outputs=[slider, download_file],
115
  )
116
 
117
+ input_image.change(
118
+ fn=process,
119
+ inputs=[input_image],
120
+ outputs=[slider, download_file],
121
+ )
 
 
 
 
 
 
 
 
122
 
 
123
  if __name__ == "__main__":
124
+ demo.launch()