Spaces:
Running on Zero
Running on Zero
Bria commited on
Commit Β·
952f6e6
1
Parent(s): 9131e14
Replace gradio_imageslider with custom HTML/JS slider
Browse files
app.py
CHANGED
|
@@ -1,10 +1,48 @@
|
|
| 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
|
| 7 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Load the model
|
| 10 |
print("Loading Fibo-Edit-RMBG model...")
|
|
@@ -14,24 +52,19 @@ pipe = DiffusionPipeline.from_pretrained(
|
|
| 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
|
| 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,
|
|
@@ -39,25 +72,18 @@ def process_image(image, num_steps=10, guidance_scale=1.0):
|
|
| 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 |
-
return (original_rgba, image), "β
Image processed successfully!"
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
-
return
|
|
|
|
| 59 |
|
| 60 |
-
# Create Gradio interface
|
| 61 |
with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
| 62 |
gr.Markdown("""
|
| 63 |
# Fibo-Edit-RMBG - Background Removal
|
|
@@ -88,7 +114,6 @@ with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
|
| 88 |
step=1,
|
| 89 |
label="Number of Steps"
|
| 90 |
)
|
| 91 |
-
|
| 92 |
guidance_scale = gr.Slider(
|
| 93 |
minimum=1.0,
|
| 94 |
maximum=15.0,
|
|
@@ -100,23 +125,17 @@ with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
|
| 100 |
process_btn = gr.Button("π Process Image", variant="primary")
|
| 101 |
|
| 102 |
with gr.Column(scale=1):
|
| 103 |
-
|
| 104 |
-
label="Result vs Original",
|
| 105 |
-
type="pil",
|
| 106 |
-
height=400
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
status_text = gr.Textbox(
|
| 110 |
label="Status",
|
| 111 |
interactive=False,
|
| 112 |
lines=2
|
| 113 |
)
|
| 114 |
|
| 115 |
-
# Connect the button
|
| 116 |
process_btn.click(
|
| 117 |
fn=process_image,
|
| 118 |
inputs=[input_image, num_steps, guidance_scale],
|
| 119 |
-
outputs=[
|
| 120 |
)
|
| 121 |
|
| 122 |
gr.Markdown("""
|
|
@@ -133,7 +152,6 @@ with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
|
| 133 |
For commercial use, please [contact Bria AI](https://bria.ai/contact-us).
|
| 134 |
""")
|
| 135 |
|
| 136 |
-
# Launch the app
|
| 137 |
if __name__ == "__main__":
|
| 138 |
demo.launch(show_api=False)
|
| 139 |
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import torch
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
from PIL import Image
|
| 6 |
+
import base64
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _img_to_b64(img: Image.Image) -> str:
|
| 11 |
+
buf = BytesIO()
|
| 12 |
+
img.save(buf, format="PNG")
|
| 13 |
+
return base64.b64encode(buf.getvalue()).decode()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def make_slider_html(result: Image.Image, original: Image.Image) -> str:
|
| 17 |
+
r, o = _img_to_b64(result), _img_to_b64(original)
|
| 18 |
+
return f"""
|
| 19 |
+
<div id="cmp" style="position:relative;width:100%;cursor:col-resize;user-select:none;line-height:0">
|
| 20 |
+
<img src="data:image/png;base64,{o}" style="width:100%;display:block">
|
| 21 |
+
<div id="cmp-clip" style="position:absolute;top:0;left:0;width:50%;overflow:hidden">
|
| 22 |
+
<img src="data:image/png;base64,{r}" style="width:200%;max-width:none;display:block">
|
| 23 |
+
</div>
|
| 24 |
+
<div id="cmp-line" style="position:absolute;top:0;left:50%;width:3px;height:100%;background:#fff;box-shadow:0 0 4px rgba(0,0,0,.8)"></div>
|
| 25 |
+
<div id="cmp-handle" style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:36px;height:36px;border-radius:50%;background:#fff;box-shadow:0 0 6px rgba(0,0,0,.6);display:flex;align-items:center;justify-content:center;font-size:14px;pointer-events:none">⇆</div>
|
| 26 |
+
</div>
|
| 27 |
+
<script>
|
| 28 |
+
(function(){{
|
| 29 |
+
const el=document.getElementById('cmp'),
|
| 30 |
+
clip=document.getElementById('cmp-clip'),
|
| 31 |
+
line=document.getElementById('cmp-line'),
|
| 32 |
+
handle=document.getElementById('cmp-handle');
|
| 33 |
+
function setPos(x){{
|
| 34 |
+
const r=el.getBoundingClientRect(),p=Math.max(0,Math.min((x-r.left)/r.width,1))*100+'%';
|
| 35 |
+
clip.style.width=p; line.style.left=p; handle.style.left=p;
|
| 36 |
+
}}
|
| 37 |
+
let drag=false;
|
| 38 |
+
el.addEventListener('mousedown',e=>{{drag=true;setPos(e.clientX);}});
|
| 39 |
+
document.addEventListener('mouseup',()=>drag=false);
|
| 40 |
+
document.addEventListener('mousemove',e=>{{if(drag)setPos(e.clientX);}});
|
| 41 |
+
el.addEventListener('touchstart',e=>setPos(e.touches[0].clientX),{{passive:true}});
|
| 42 |
+
el.addEventListener('touchmove',e=>setPos(e.touches[0].clientX),{{passive:true}});
|
| 43 |
+
}})();
|
| 44 |
+
</script>"""
|
| 45 |
+
|
| 46 |
|
| 47 |
# Load the model
|
| 48 |
print("Loading Fibo-Edit-RMBG model...")
|
|
|
|
| 52 |
trust_remote_code=True
|
| 53 |
)
|
| 54 |
|
|
|
|
| 55 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 56 |
pipe.to(device)
|
| 57 |
print(f"Model loaded on {device}")
|
| 58 |
|
| 59 |
+
|
| 60 |
@spaces.GPU
|
| 61 |
def process_image(image, num_steps=10, guidance_scale=1.0):
|
|
|
|
|
|
|
|
|
|
| 62 |
if image is None:
|
| 63 |
+
return "", "Please upload an image"
|
| 64 |
|
|
|
|
| 65 |
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.'}\""
|
| 66 |
|
| 67 |
try:
|
|
|
|
| 68 |
mask = pipe(
|
| 69 |
image=image,
|
| 70 |
prompt=instruction,
|
|
|
|
| 72 |
guidance_scale=guidance_scale
|
| 73 |
).images[0]
|
| 74 |
|
|
|
|
| 75 |
original_rgba = image.convert("RGBA")
|
|
|
|
|
|
|
| 76 |
mask_gray = mask.convert("L")
|
|
|
|
|
|
|
| 77 |
if mask_gray.size != original_rgba.size:
|
| 78 |
mask_gray = mask_gray.resize(original_rgba.size, Image.LANCZOS)
|
|
|
|
|
|
|
| 79 |
original_rgba.putalpha(mask_gray)
|
| 80 |
|
| 81 |
+
return make_slider_html(original_rgba, image), "β
Image processed successfully!"
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
+
return "", f"β Error: {str(e)}"
|
| 85 |
+
|
| 86 |
|
|
|
|
| 87 |
with gr.Blocks(title="Fibo-Edit-RMBG - Background Removal") as demo:
|
| 88 |
gr.Markdown("""
|
| 89 |
# Fibo-Edit-RMBG - Background Removal
|
|
|
|
| 114 |
step=1,
|
| 115 |
label="Number of Steps"
|
| 116 |
)
|
|
|
|
| 117 |
guidance_scale = gr.Slider(
|
| 118 |
minimum=1.0,
|
| 119 |
maximum=15.0,
|
|
|
|
| 125 |
process_btn = gr.Button("π Process Image", variant="primary")
|
| 126 |
|
| 127 |
with gr.Column(scale=1):
|
| 128 |
+
output_slider = gr.HTML(label="Result vs Original")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
status_text = gr.Textbox(
|
| 130 |
label="Status",
|
| 131 |
interactive=False,
|
| 132 |
lines=2
|
| 133 |
)
|
| 134 |
|
|
|
|
| 135 |
process_btn.click(
|
| 136 |
fn=process_image,
|
| 137 |
inputs=[input_image, num_steps, guidance_scale],
|
| 138 |
+
outputs=[output_slider, status_text]
|
| 139 |
)
|
| 140 |
|
| 141 |
gr.Markdown("""
|
|
|
|
| 152 |
For commercial use, please [contact Bria AI](https://bria.ai/contact-us).
|
| 153 |
""")
|
| 154 |
|
|
|
|
| 155 |
if __name__ == "__main__":
|
| 156 |
demo.launch(show_api=False)
|
| 157 |
|