Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,55 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from briarmbg import BriaRMBG
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import torch
|
| 5 |
-
import torchvision.transforms as T
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# =====
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
if
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
pixels[x, y] = (255, 255, 255, 0)
|
| 57 |
-
|
| 58 |
-
return image
|
| 59 |
-
|
| 60 |
-
# ===== Launch Gradio app with secure FastAPI request passthrough =====
|
| 61 |
-
gr.Interface(
|
| 62 |
-
fn=remove_background,
|
| 63 |
-
inputs=gr.Image(type="pil"),
|
| 64 |
-
outputs=gr.Image(type="pil"),
|
| 65 |
-
title="Background Remover",
|
| 66 |
-
allow_flagging="never"
|
| 67 |
-
).launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from briarmbg import BriaRMBG
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import torchvision.transforms as T
|
| 6 |
+
|
| 7 |
+
# ===== Load model =====
|
| 8 |
+
model = BriaRMBG.from_pretrained("./")
|
| 9 |
+
model.load_state_dict(torch.load("model.pth", map_location="cpu"))
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
# ===== Define preprocessing =====
|
| 13 |
+
transform = T.Compose([
|
| 14 |
+
T.Resize((512, 512)),
|
| 15 |
+
T.ToTensor()
|
| 16 |
+
])
|
| 17 |
+
|
| 18 |
+
# ===== Background removal function (no auth needed) =====
|
| 19 |
+
def remove_background(image):
|
| 20 |
+
# Preprocess image
|
| 21 |
+
img = transform(image).unsqueeze(0)
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
result = model(img)
|
| 25 |
+
|
| 26 |
+
# Handle different result formats
|
| 27 |
+
if isinstance(result, dict) and "pred" in result:
|
| 28 |
+
result = result["pred"]
|
| 29 |
+
elif isinstance(result, (tuple, list)):
|
| 30 |
+
result = result[0]
|
| 31 |
+
if isinstance(result, list):
|
| 32 |
+
result = result[0]
|
| 33 |
+
|
| 34 |
+
result = result.squeeze().numpy()
|
| 35 |
+
|
| 36 |
+
# Apply transparency mask
|
| 37 |
+
image = image.resize((result.shape[1], result.shape[0]))
|
| 38 |
+
image = image.convert("RGBA")
|
| 39 |
+
pixels = image.load()
|
| 40 |
+
|
| 41 |
+
for y in range(image.height):
|
| 42 |
+
for x in range(image.width):
|
| 43 |
+
if result[y][x] < 0.5:
|
| 44 |
+
pixels[x, y] = (255, 255, 255, 0)
|
| 45 |
+
|
| 46 |
+
return image
|
| 47 |
+
|
| 48 |
+
# ===== Launch Gradio app (public access) =====
|
| 49 |
+
gr.Interface(
|
| 50 |
+
fn=remove_background,
|
| 51 |
+
inputs=gr.Image(type="pil"),
|
| 52 |
+
outputs=gr.Image(type="pil"),
|
| 53 |
+
title="Background Remover",
|
| 54 |
+
allow_flagging="never"
|
| 55 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|