Spaces:
Running on Zero
Running on Zero
Bria commited on
Commit ·
5ffd6e4
1
Parent(s): a734730
Add RGBA PNG download button
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from gradio_imageslider import ImageSlider
|
|
| 4 |
import torch
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
from PIL import Image
|
| 7 |
-
import
|
| 8 |
|
| 9 |
# Load the model
|
| 10 |
print("Loading Fibo-Edit-RMBG model...")
|
|
@@ -25,7 +25,7 @@ def process_image(image, num_steps=10, guidance_scale=1.0):
|
|
| 25 |
Process an image with Fibo-Edit-RMBG
|
| 26 |
"""
|
| 27 |
if image is None:
|
| 28 |
-
return 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.'}\""
|
|
@@ -52,10 +52,13 @@ def process_image(image, num_steps=10, guidance_scale=1.0):
|
|
| 52 |
# Use the mask as the alpha channel
|
| 53 |
original_rgba.putalpha(mask_gray)
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
-
return None, f"❌ Error: {str(e)}"
|
| 59 |
|
| 60 |
# Create Gradio interface
|
| 61 |
with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
|
@@ -106,6 +109,8 @@ with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
|
| 106 |
height=400
|
| 107 |
)
|
| 108 |
|
|
|
|
|
|
|
| 109 |
status_text = gr.Textbox(
|
| 110 |
label="Status",
|
| 111 |
interactive=False,
|
|
@@ -116,7 +121,7 @@ with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
|
| 116 |
process_btn.click(
|
| 117 |
fn=process_image,
|
| 118 |
inputs=[input_image, num_steps, guidance_scale],
|
| 119 |
-
outputs=[output_image, status_text]
|
| 120 |
)
|
| 121 |
|
| 122 |
gr.Markdown("""
|
|
|
|
| 4 |
import torch
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
from PIL import Image
|
| 7 |
+
import tempfile
|
| 8 |
|
| 9 |
# Load the model
|
| 10 |
print("Loading Fibo-Edit-RMBG model...")
|
|
|
|
| 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.'}\""
|
|
|
|
| 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 |
# Create Gradio interface
|
| 64 |
with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
|
|
|
| 109 |
height=400
|
| 110 |
)
|
| 111 |
|
| 112 |
+
download_file = gr.File(label="Download PNG (with transparency)")
|
| 113 |
+
|
| 114 |
status_text = gr.Textbox(
|
| 115 |
label="Status",
|
| 116 |
interactive=False,
|
|
|
|
| 121 |
process_btn.click(
|
| 122 |
fn=process_image,
|
| 123 |
inputs=[input_image, num_steps, guidance_scale],
|
| 124 |
+
outputs=[output_image, download_file, status_text]
|
| 125 |
)
|
| 126 |
|
| 127 |
gr.Markdown("""
|