Spaces:
Running
on
Zero
Running
on
Zero
Update Gradio app with multiple files
Browse files- app.py +26 -91
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -2,25 +2,25 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
import spaces
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
-
device =
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
rembg_session = new_session('u2net')
|
| 21 |
|
| 22 |
@spaces.GPU(duration=30)
|
| 23 |
-
def
|
| 24 |
"""
|
| 25 |
Remove background from image using RMBG-2.0 model.
|
| 26 |
|
|
@@ -49,53 +49,6 @@ def remove_background_u2net(image):
|
|
| 49 |
|
| 50 |
return image_rgba
|
| 51 |
|
| 52 |
-
@spaces.GPU(duration=20)
|
| 53 |
-
def remove_background_rembg(image):
|
| 54 |
-
"""
|
| 55 |
-
Remove background from image using rembg library.
|
| 56 |
-
|
| 57 |
-
Args:
|
| 58 |
-
image (PIL.Image): Input image to process
|
| 59 |
-
|
| 60 |
-
Returns:
|
| 61 |
-
PIL.Image: Image with background removed
|
| 62 |
-
"""
|
| 63 |
-
if image is None:
|
| 64 |
-
return None
|
| 65 |
-
|
| 66 |
-
# Convert PIL to bytes for rembg
|
| 67 |
-
import io
|
| 68 |
-
img_byte_arr = io.BytesIO()
|
| 69 |
-
image.save(img_byte_arr, format='PNG')
|
| 70 |
-
img_bytes = img_byte_arr.getvalue()
|
| 71 |
-
|
| 72 |
-
# Remove background
|
| 73 |
-
output_bytes = remove(img_bytes, session=rembg_session)
|
| 74 |
-
|
| 75 |
-
# Convert back to PIL
|
| 76 |
-
output_image = Image.open(io.BytesIO(output_bytes))
|
| 77 |
-
|
| 78 |
-
return output_rgba
|
| 79 |
-
|
| 80 |
-
def process_image(image, method):
|
| 81 |
-
"""
|
| 82 |
-
Process image background removal based on selected method.
|
| 83 |
-
|
| 84 |
-
Args:
|
| 85 |
-
image (PIL.Image): Input image
|
| 86 |
-
method (str): Background removal method ('u2net' or 'rembg')
|
| 87 |
-
|
| 88 |
-
Returns:
|
| 89 |
-
PIL.Image: Processed image
|
| 90 |
-
"""
|
| 91 |
-
if image is None:
|
| 92 |
-
return None
|
| 93 |
-
|
| 94 |
-
if method == "u2net":
|
| 95 |
-
return remove_background_u2net(image)
|
| 96 |
-
else:
|
| 97 |
-
return remove_background_rembg(image)
|
| 98 |
-
|
| 99 |
def create_collage(original, processed):
|
| 100 |
"""
|
| 101 |
Create a side-by-side comparison of original and processed images.
|
|
@@ -152,7 +105,7 @@ with gr.Blocks(title="Background Removal App", theme=gr.themes.Soft()) as demo:
|
|
| 152 |
# Background Removal App
|
| 153 |
Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 154 |
|
| 155 |
-
Upload an image to remove its background using
|
| 156 |
"""
|
| 157 |
)
|
| 158 |
|
|
@@ -165,16 +118,6 @@ with gr.Blocks(title="Background Removal App", theme=gr.themes.Soft()) as demo:
|
|
| 165 |
sources=["upload", "webcam", "clipboard"]
|
| 166 |
)
|
| 167 |
|
| 168 |
-
method = gr.Radio(
|
| 169 |
-
choices=[
|
| 170 |
-
("RMBG-2.0 (BRIA AI)", "u2net"),
|
| 171 |
-
("RMBG (Rembg)", "rembg")
|
| 172 |
-
],
|
| 173 |
-
value="u2net",
|
| 174 |
-
label="Background Removal Method",
|
| 175 |
-
info="Choose the AI model for background removal"
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
process_btn = gr.Button("Remove Background", variant="primary", size="lg")
|
| 179 |
|
| 180 |
with gr.Accordion("Advanced Options", open=False):
|
|
@@ -205,19 +148,19 @@ with gr.Blocks(title="Background Removal App", theme=gr.themes.Soft()) as demo:
|
|
| 205 |
# Example images
|
| 206 |
gr.Examples(
|
| 207 |
examples=[
|
| 208 |
-
["https://gradio-builds.s3.amazonaws.com/assets/cheetah-003.jpg"
|
| 209 |
-
["https://gradio-builds.s3.amazonaws.com/assets/TheCheethcat.jpg"
|
| 210 |
],
|
| 211 |
-
inputs=
|
| 212 |
outputs=output_image,
|
| 213 |
-
fn=
|
| 214 |
cache_examples=True
|
| 215 |
)
|
| 216 |
|
| 217 |
# Event handlers
|
| 218 |
process_btn.click(
|
| 219 |
-
fn=
|
| 220 |
-
inputs=
|
| 221 |
outputs=output_image,
|
| 222 |
show_progress=True
|
| 223 |
).then(
|
|
@@ -238,13 +181,12 @@ with gr.Blocks(title="Background Removal App", theme=gr.themes.Soft()) as demo:
|
|
| 238 |
)
|
| 239 |
|
| 240 |
# MCP Server Functions
|
| 241 |
-
def remove_background_mcp(image_path: str
|
| 242 |
"""
|
| 243 |
Remove background from an image file and save the result.
|
| 244 |
|
| 245 |
Args:
|
| 246 |
image_path (str): Path to the input image file
|
| 247 |
-
method (str): Background removal method ('u2net' or 'rembg')
|
| 248 |
|
| 249 |
Returns:
|
| 250 |
str: Path to the output image file with background removed
|
|
@@ -253,11 +195,8 @@ def remove_background_mcp(image_path: str, method: str = "u2net") -> str:
|
|
| 253 |
# Load image
|
| 254 |
image = Image.open(image_path)
|
| 255 |
|
| 256 |
-
# Process
|
| 257 |
-
|
| 258 |
-
result = remove_background_u2net(image)
|
| 259 |
-
else:
|
| 260 |
-
result = remove_background_rembg(image)
|
| 261 |
|
| 262 |
# Save result
|
| 263 |
output_path = image_path.replace('.', '_no_bg.')
|
|
@@ -267,13 +206,12 @@ def remove_background_mcp(image_path: str, method: str = "u2net") -> str:
|
|
| 267 |
except Exception as e:
|
| 268 |
raise Exception(f"Error processing image: {str(e)}")
|
| 269 |
|
| 270 |
-
def remove_background_base64(image_data: str
|
| 271 |
"""
|
| 272 |
Remove background from base64 encoded image data.
|
| 273 |
|
| 274 |
Args:
|
| 275 |
image_data (str): Base64 encoded image data
|
| 276 |
-
method (str): Background removal method ('u2net' or 'rembg')
|
| 277 |
|
| 278 |
Returns:
|
| 279 |
str: Base64 encoded image with background removed
|
|
@@ -286,11 +224,8 @@ def remove_background_base64(image_data: str, method: str = "u2net") -> str:
|
|
| 286 |
image_bytes = base64.b64decode(image_data)
|
| 287 |
image = Image.open(io.BytesIO(image_bytes))
|
| 288 |
|
| 289 |
-
# Process
|
| 290 |
-
|
| 291 |
-
result = remove_background_u2net(image)
|
| 292 |
-
else:
|
| 293 |
-
result = remove_background_rembg(image)
|
| 294 |
|
| 295 |
# Encode result back to base64
|
| 296 |
output_buffer = io.BytesIO()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
from transformers import AutoModelForImageSegmentation
|
| 7 |
import spaces
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# Load RMBG-2.0 model
|
| 11 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 12 |
+
model = AutoModelForImageSegmentation.from_pretrained('briaai/RMBG-2.0', trust_remote_code=True).eval().to(device)
|
| 13 |
|
| 14 |
+
# Data settings
|
| 15 |
+
image_size = (1024, 1024)
|
| 16 |
+
transform_image = transforms.Compose([
|
| 17 |
+
transforms.Resize(image_size),
|
| 18 |
+
transforms.ToTensor(),
|
| 19 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 20 |
+
])
|
|
|
|
| 21 |
|
| 22 |
@spaces.GPU(duration=30)
|
| 23 |
+
def remove_background(image):
|
| 24 |
"""
|
| 25 |
Remove background from image using RMBG-2.0 model.
|
| 26 |
|
|
|
|
| 49 |
|
| 50 |
return image_rgba
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def create_collage(original, processed):
|
| 53 |
"""
|
| 54 |
Create a side-by-side comparison of original and processed images.
|
|
|
|
| 105 |
# Background Removal App
|
| 106 |
Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 107 |
|
| 108 |
+
Upload an image to remove its background using the advanced RMBG-2.0 AI model from BRIA AI.
|
| 109 |
"""
|
| 110 |
)
|
| 111 |
|
|
|
|
| 118 |
sources=["upload", "webcam", "clipboard"]
|
| 119 |
)
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
process_btn = gr.Button("Remove Background", variant="primary", size="lg")
|
| 122 |
|
| 123 |
with gr.Accordion("Advanced Options", open=False):
|
|
|
|
| 148 |
# Example images
|
| 149 |
gr.Examples(
|
| 150 |
examples=[
|
| 151 |
+
["https://gradio-builds.s3.amazonaws.com/assets/cheetah-003.jpg"],
|
| 152 |
+
["https://gradio-builds.s3.amazonaws.com/assets/TheCheethcat.jpg"],
|
| 153 |
],
|
| 154 |
+
inputs=input_image,
|
| 155 |
outputs=output_image,
|
| 156 |
+
fn=remove_background,
|
| 157 |
cache_examples=True
|
| 158 |
)
|
| 159 |
|
| 160 |
# Event handlers
|
| 161 |
process_btn.click(
|
| 162 |
+
fn=remove_background,
|
| 163 |
+
inputs=input_image,
|
| 164 |
outputs=output_image,
|
| 165 |
show_progress=True
|
| 166 |
).then(
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
# MCP Server Functions
|
| 184 |
+
def remove_background_mcp(image_path: str) -> str:
|
| 185 |
"""
|
| 186 |
Remove background from an image file and save the result.
|
| 187 |
|
| 188 |
Args:
|
| 189 |
image_path (str): Path to the input image file
|
|
|
|
| 190 |
|
| 191 |
Returns:
|
| 192 |
str: Path to the output image file with background removed
|
|
|
|
| 195 |
# Load image
|
| 196 |
image = Image.open(image_path)
|
| 197 |
|
| 198 |
+
# Process image
|
| 199 |
+
result = remove_background(image)
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
# Save result
|
| 202 |
output_path = image_path.replace('.', '_no_bg.')
|
|
|
|
| 206 |
except Exception as e:
|
| 207 |
raise Exception(f"Error processing image: {str(e)}")
|
| 208 |
|
| 209 |
+
def remove_background_base64(image_data: str) -> str:
|
| 210 |
"""
|
| 211 |
Remove background from base64 encoded image data.
|
| 212 |
|
| 213 |
Args:
|
| 214 |
image_data (str): Base64 encoded image data
|
|
|
|
| 215 |
|
| 216 |
Returns:
|
| 217 |
str: Base64 encoded image with background removed
|
|
|
|
| 224 |
image_bytes = base64.b64decode(image_data)
|
| 225 |
image = Image.open(io.BytesIO(image_bytes))
|
| 226 |
|
| 227 |
+
# Process image
|
| 228 |
+
result = remove_background(image)
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
# Encode result back to base64
|
| 231 |
output_buffer = io.BytesIO()
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
gradio[mcp]
|
| 2 |
torch
|
| 3 |
transformers
|
| 4 |
-
|
| 5 |
Pillow
|
| 6 |
numpy
|
| 7 |
spaces
|
|
|
|
| 1 |
gradio[mcp]
|
| 2 |
torch
|
| 3 |
transformers
|
| 4 |
+
torchvision
|
| 5 |
Pillow
|
| 6 |
numpy
|
| 7 |
spaces
|