Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,22 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
from transformers import AutoModelForImageSegmentation
|
| 22 |
-
from torchvision import transforms
|
| 23 |
-
|
| 24 |
-
# --- Hardware Setup ---
|
| 25 |
-
# Force CPU if CUDA is not available
|
| 26 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
-
print(f"Current Hardware: {device.upper()}")
|
| 28 |
-
|
| 29 |
-
# --- Model Initialization ---
|
| 30 |
-
# RMBG-2.0 is heavy; we use trust_remote_code=True for the BiRefNet architecture
|
| 31 |
-
print("Loading model... this may take a minute on CPU.")
|
| 32 |
-
model = AutoModelForImageSegmentation.from_pretrained(
|
| 33 |
-
"briaai/RMBG-2.0",
|
| 34 |
-
trust_remote_code=True
|
| 35 |
)
|
| 36 |
-
model.to(device)
|
| 37 |
-
model.eval()
|
| 38 |
-
|
| 39 |
-
# Standard ImageNet normalization used by BiRefNet
|
| 40 |
-
preprocess = transforms.Compose([
|
| 41 |
-
transforms.Resize((1024, 1024)),
|
| 42 |
-
transforms.ToTensor(),
|
| 43 |
-
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
| 44 |
-
])
|
| 45 |
-
|
| 46 |
-
output_folder = 'output_images'
|
| 47 |
-
os.makedirs(output_folder, exist_ok=True)
|
| 48 |
-
|
| 49 |
-
def remove_background(image_input):
|
| 50 |
-
if image_input is None:
|
| 51 |
-
return None, None
|
| 52 |
-
|
| 53 |
-
# Handle both filepath (str) and PIL Image inputs
|
| 54 |
-
if isinstance(image_input, str):
|
| 55 |
-
orig_img = load_img(image_input, output_type="pil").convert("RGB")
|
| 56 |
-
else:
|
| 57 |
-
orig_img = image_input.convert("RGB")
|
| 58 |
-
|
| 59 |
-
w, h = orig_img.size
|
| 60 |
-
|
| 61 |
-
# Preprocess
|
| 62 |
-
input_tensor = preprocess(orig_img).unsqueeze(0).to(device)
|
| 63 |
-
|
| 64 |
-
# Inference
|
| 65 |
-
with torch.no_grad():
|
| 66 |
-
# BiRefNet returns a list of preds; we take the last one
|
| 67 |
-
result = model(input_tensor)[-1].sigmoid().cpu()
|
| 68 |
-
|
| 69 |
-
# Post-process Mask
|
| 70 |
-
mask = transforms.ToPILImage()(result[0].squeeze())
|
| 71 |
-
mask = mask.resize((w, h))
|
| 72 |
-
|
| 73 |
-
# Create Final Transparent Image
|
| 74 |
-
no_bg_img = orig_img.copy()
|
| 75 |
-
no_bg_img.putalpha(mask)
|
| 76 |
-
|
| 77 |
-
save_path = os.path.join(output_folder, "result.png")
|
| 78 |
-
no_bg_img.save(save_path)
|
| 79 |
-
|
| 80 |
-
return (no_bg_img, orig_img), save_path
|
| 81 |
-
|
| 82 |
-
# --- UI Setup ---
|
| 83 |
-
with gr.Blocks(title="RMBG 2.0 CPU") as demo:
|
| 84 |
-
gr.Markdown("# RMBG-2.0 Background Remover")
|
| 85 |
-
gr.Markdown("Optimized for CPU/GPU deployment.")
|
| 86 |
-
|
| 87 |
-
with gr.Tab("Image Upload"):
|
| 88 |
-
with gr.Row():
|
| 89 |
-
in_img = gr.Image(label="Upload Image", type="pil")
|
| 90 |
-
out_slider = ImageSlider(label="Comparison", type="pil")
|
| 91 |
-
|
| 92 |
-
out_file = gr.File(label="Download PNG")
|
| 93 |
-
submit_btn = gr.Button("Remove Background", variant="primary")
|
| 94 |
-
|
| 95 |
-
submit_btn.click(
|
| 96 |
-
fn=remove_background,
|
| 97 |
-
inputs=in_img,
|
| 98 |
-
outputs=[out_slider, out_file]
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
with gr.Tab("URL / Batch"):
|
| 102 |
-
url_input = gr.Textbox(label="Paste Image URL")
|
| 103 |
-
url_slider = ImageSlider(label="Comparison", type="pil")
|
| 104 |
-
url_file = gr.File(label="Download PNG")
|
| 105 |
-
url_btn = gr.Button("Process URL")
|
| 106 |
-
|
| 107 |
-
url_btn.click(
|
| 108 |
-
fn=remove_background,
|
| 109 |
-
inputs=url_input,
|
| 110 |
-
outputs=[url_slider, url_file]
|
| 111 |
-
)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from rembg import remove
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
def fast_process(input_image):
|
| 7 |
+
if input_image is None:
|
| 8 |
+
return None
|
| 9 |
+
# Rembg handles the ONNX optimization internally
|
| 10 |
+
output = remove(input_image)
|
| 11 |
+
return output
|
| 12 |
+
|
| 13 |
+
demo = gr.Interface(
|
| 14 |
+
fn=fast_process,
|
| 15 |
+
inputs=gr.Image(type="pil"),
|
| 16 |
+
outputs=gr.Image(type="pil"),
|
| 17 |
+
title="Ultra-Fast CPU Background Remover",
|
| 18 |
+
description="Using rembg + ONNX for 1-2 second processing on most CPUs."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
if __name__ == "__main__":
|
| 22 |
demo.launch()
|