Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,173 +1,3 @@
|
|
1 |
-
|
2 |
-
# import gradio as gr
|
3 |
-
# from load_image import load_img
|
4 |
-
# import spaces
|
5 |
-
# from transformers import AutoModelForImageSegmentation
|
6 |
-
# import torch
|
7 |
-
# from torchvision import transforms
|
8 |
-
# from PIL import Image
|
9 |
-
# import os
|
10 |
-
# import numpy as np
|
11 |
-
|
12 |
-
# torch.set_float32_matmul_precision(["high", "highest"][0])
|
13 |
-
|
14 |
-
# # load 2 models
|
15 |
-
|
16 |
-
# birefnet = AutoModelForImageSegmentation.from_pretrained(
|
17 |
-
# "ZhengPeng7/BiRefNet", trust_remote_code=True
|
18 |
-
# )
|
19 |
-
|
20 |
-
|
21 |
-
# # RMBG2 = AutoModelForImageSegmentation.from_pretrained(
|
22 |
-
# # "briaai/RMBG-2.0", trust_remote_code=True
|
23 |
-
# # )
|
24 |
-
|
25 |
-
# # Keep them in a dict to switch easily
|
26 |
-
# models_dict = {
|
27 |
-
# "BiRefNet": birefnet
|
28 |
-
# # "RMBG-2.0": RMBG2,
|
29 |
-
# }
|
30 |
-
|
31 |
-
# # Transform
|
32 |
-
|
33 |
-
# transform_image = transforms.Compose(
|
34 |
-
# [
|
35 |
-
# transforms.Resize((1024, 1024)),
|
36 |
-
# transforms.ToTensor(),
|
37 |
-
# transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
38 |
-
# ]
|
39 |
-
# )
|
40 |
-
|
41 |
-
# @spaces.GPU
|
42 |
-
# def process(image: Image.Image, model_choice: str):
|
43 |
-
# """
|
44 |
-
# Runs inference to remove the background (adds alpha)
|
45 |
-
# with the chosen segmentation model.
|
46 |
-
# """
|
47 |
-
# # Select the model
|
48 |
-
# current_model = models_dict[model_choice]
|
49 |
-
|
50 |
-
# # Prepare image
|
51 |
-
# image_size = image.size
|
52 |
-
# input_images = transform_image(image).unsqueeze(0)
|
53 |
-
|
54 |
-
# # Inference
|
55 |
-
# with torch.no_grad():
|
56 |
-
# # Each model returns a list of preds in its forward,
|
57 |
-
# # so we take the last element, apply sigmoid, and move to CPU
|
58 |
-
# preds = current_model(input_images)[-1].sigmoid().cpu()
|
59 |
-
|
60 |
-
# # Convert single-channel pred to a PIL mask
|
61 |
-
# pred = preds[0].squeeze()
|
62 |
-
# pred_pil = transforms.ToPILImage()(pred)
|
63 |
-
|
64 |
-
# # Resize the mask back to original image size
|
65 |
-
# mask = pred_pil.resize(image_size)
|
66 |
-
|
67 |
-
# # Add alpha channel to the original
|
68 |
-
# image.putalpha(mask)
|
69 |
-
# return image
|
70 |
-
|
71 |
-
# def fn(source: str, model_choice: str):
|
72 |
-
# """
|
73 |
-
# Used by Tab 1 & Tab 2 to produce a processed image with alpha.
|
74 |
-
# - 'source' is either a file path (type="filepath") or
|
75 |
-
# a URL string (textbox).
|
76 |
-
# - 'model_choice' is the user's selection from the radio.
|
77 |
-
# """
|
78 |
-
# # Load from local path or URL
|
79 |
-
# im = load_img(source, output_type="pil")
|
80 |
-
# im = im.convert("RGB")
|
81 |
-
|
82 |
-
# # Process
|
83 |
-
# processed_image = process(im, model_choice)
|
84 |
-
# return processed_image
|
85 |
-
|
86 |
-
# def process_file(file_path: str, model_choice: str):
|
87 |
-
# """
|
88 |
-
# For Tab 3 (file output).
|
89 |
-
# - Accepts a local path, returns path to a new .png with alpha channel.
|
90 |
-
# - 'model_choice' is also passed in for selecting the model.
|
91 |
-
# """
|
92 |
-
# name_path = file_path.rsplit(".", 1)[0] + ".png"
|
93 |
-
# im = load_img(file_path, output_type="pil")
|
94 |
-
# im = im.convert("RGB")
|
95 |
-
|
96 |
-
# # Run the chosen model
|
97 |
-
# transparent = process(im, model_choice)
|
98 |
-
# transparent.save(name_path)
|
99 |
-
# return name_path
|
100 |
-
|
101 |
-
|
102 |
-
# # GRadio UI
|
103 |
-
|
104 |
-
# model_selector_1 = gr.Radio(
|
105 |
-
# choices=["BiRefNet"], # , "RMBG-2.0"
|
106 |
-
# value="BiRefNet",
|
107 |
-
# label="Select Model"
|
108 |
-
# )
|
109 |
-
# model_selector_2 = gr.Radio(
|
110 |
-
# choices=["BiRefNet"],# "RMBG-2.0"],
|
111 |
-
# value="BiRefNet",
|
112 |
-
# label="Select Model"
|
113 |
-
# )
|
114 |
-
# model_selector_3 = gr.Radio(
|
115 |
-
# choices=["BiRefNet"],# "RMBG-2.0"],
|
116 |
-
# value="BiRefNet",
|
117 |
-
# label="Select Model"
|
118 |
-
# )
|
119 |
-
|
120 |
-
# # Outputs for tabs 1 & 2: single processed image
|
121 |
-
# processed_img_upload = gr.Image(label="Processed Image (Upload)", type="pil")
|
122 |
-
# processed_img_url = gr.Image(label="Processed Image (URL)", type="pil")
|
123 |
-
|
124 |
-
# # For uploading local files
|
125 |
-
# image_upload = gr.Image(label="Upload an image", type="filepath")
|
126 |
-
# image_file_upload = gr.Image(label="Upload an image", type="filepath")
|
127 |
-
|
128 |
-
# # For Tab 2 (URL input)
|
129 |
-
# url_input = gr.Textbox(label="Paste an image URL")
|
130 |
-
|
131 |
-
# # For Tab 3 (file output)
|
132 |
-
# output_file = gr.File(label="Output PNG File")
|
133 |
-
|
134 |
-
# # Tab 1: local image -> processed image
|
135 |
-
# tab1 = gr.Interface(
|
136 |
-
# fn=fn,
|
137 |
-
# inputs=[image_upload, model_selector_1],
|
138 |
-
# outputs=processed_img_upload,
|
139 |
-
# api_name="image",
|
140 |
-
# description="Upload an image and choose your background removal model."
|
141 |
-
# )
|
142 |
-
|
143 |
-
# # Tab 2: URL input -> processed image
|
144 |
-
# tab2 = gr.Interface(
|
145 |
-
# fn=fn,
|
146 |
-
# inputs=[url_input, model_selector_2],
|
147 |
-
# outputs=processed_img_url,
|
148 |
-
# api_name="text",
|
149 |
-
# description="Paste an image URL and choose your background removal model."
|
150 |
-
# )
|
151 |
-
|
152 |
-
# # Tab 3: file output -> returns path to .png
|
153 |
-
# tab3 = gr.Interface(
|
154 |
-
# fn=process_file,
|
155 |
-
# inputs=[image_file_upload, model_selector_3],
|
156 |
-
# outputs=output_file,
|
157 |
-
# api_name="png",
|
158 |
-
# description="Upload an image, choose a model, and get a transparent PNG."
|
159 |
-
# )
|
160 |
-
|
161 |
-
# # Combine all tabs
|
162 |
-
# demo = gr.TabbedInterface(
|
163 |
-
# [tab1, tab2, tab3],
|
164 |
-
# ["Image Upload", "URL Input", "File Output"],
|
165 |
-
# title="Background Removal Tool"
|
166 |
-
# )
|
167 |
-
|
168 |
-
# if __name__ == "__main__":
|
169 |
-
# demo.launch(show_error=True, share=True)
|
170 |
-
|
171 |
import gradio as gr
|
172 |
from load_image import load_img
|
173 |
import spaces
|
@@ -176,66 +6,140 @@ import torch
|
|
176 |
from torchvision import transforms
|
177 |
from PIL import Image
|
178 |
import os
|
|
|
179 |
|
180 |
-
|
181 |
-
torch.set_float32_matmul_precision("high")
|
182 |
|
183 |
-
#
|
184 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
185 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
186 |
)
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
-
# ─── Inference fn ────────────────────────────────────────────────────────────────
|
196 |
@spaces.GPU
|
197 |
-
def
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
return image
|
206 |
|
207 |
-
|
208 |
-
def
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
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import spaces
|
|
|
6 |
from torchvision import transforms
|
7 |
from PIL import Image
|
8 |
import os
|
9 |
+
import numpy as np
|
10 |
|
11 |
+
torch.set_float32_matmul_precision(["high", "highest"][0])
|
|
|
12 |
|
13 |
+
# load model
|
14 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
15 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
16 |
)
|
17 |
|
18 |
+
# Keep model in a dict for easy switching
|
19 |
+
models_dict = {
|
20 |
+
"BiRefNet": birefnet
|
21 |
+
}
|
22 |
+
|
23 |
+
# Transform
|
24 |
+
transform_image = transforms.Compose(
|
25 |
+
[
|
26 |
+
transforms.Resize((1024, 1024)),
|
27 |
+
transforms.ToTensor(),
|
28 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
29 |
+
]
|
30 |
+
)
|
31 |
|
|
|
32 |
@spaces.GPU
|
33 |
+
def process(image: Image.Image, model_choice: str):
|
34 |
+
"""
|
35 |
+
Runs inference to remove the background (adds alpha)
|
36 |
+
with the chosen segmentation model.
|
37 |
+
"""
|
38 |
+
current_model = models_dict[model_choice]
|
39 |
+
|
40 |
+
# Prepare image
|
41 |
+
image_size = image.size
|
42 |
+
input_images = transform_image(image).unsqueeze(0)
|
43 |
+
|
44 |
+
# Inference
|
45 |
+
with torch.no_grad():
|
46 |
+
preds = current_model(input_images)[-1].sigmoid().cpu()
|
47 |
+
|
48 |
+
# Convert single-channel pred to a PIL mask
|
49 |
+
pred = preds[0].squeeze()
|
50 |
+
pred_pil = transforms.ToPILImage()(pred)
|
51 |
+
|
52 |
+
# Resize the mask back to original image size
|
53 |
+
mask = pred_pil.resize(image_size)
|
54 |
+
|
55 |
+
# Add alpha channel to the original
|
56 |
+
image.putalpha(mask)
|
57 |
return image
|
58 |
|
59 |
+
|
60 |
+
def fn(source: str, model_choice: str):
|
61 |
+
"""
|
62 |
+
Used by Tab 1 & Tab 2 to produce a processed image with alpha.
|
63 |
+
- 'source' is either a file path (type="filepath") or
|
64 |
+
a URL string (textbox).
|
65 |
+
- 'model_choice' is the user's selection from the radio.
|
66 |
+
"""
|
67 |
+
im = load_img(source, output_type="pil")
|
68 |
+
im = im.convert("RGB")
|
69 |
+
|
70 |
+
# Process
|
71 |
+
processed_image = process(im, model_choice)
|
72 |
+
return processed_image
|
73 |
+
|
74 |
+
|
75 |
+
def process_file(file_path: str, model_choice: str):
|
76 |
+
"""
|
77 |
+
For Tab 3 (file output).
|
78 |
+
- Accepts a local path, returns path to a new .png with alpha channel.
|
79 |
+
- 'model_choice' is also passed in for selecting the model.
|
80 |
+
"""
|
81 |
+
name_path = file_path.rsplit(".", 1)[0] + ".png"
|
82 |
+
im = load_img(file_path, output_type="pil")
|
83 |
+
im = im.convert("RGB")
|
84 |
+
|
85 |
+
transparent = process(im, model_choice)
|
86 |
+
transparent.save(name_path)
|
87 |
+
return name_path
|
88 |
+
|
89 |
+
|
90 |
+
# Gradio UI\ nmodel_selector = gr.Radio(
|
91 |
+
choices=["BiRefNet"],
|
92 |
+
value="BiRefNet",
|
93 |
+
label="Select Model"
|
94 |
+
)
|
95 |
+
|
96 |
+
# Outputs for tabs 1 & 2: single processed image
|
97 |
+
processed_img_upload = gr.Image(label="Processed Image (Upload)", type="pil")
|
98 |
+
processed_img_url = gr.Image(label="Processed Image (URL)", type="pil")
|
99 |
+
|
100 |
+
# For uploading local files
|
101 |
+
image_upload = gr.Image(label="Upload an image", type="filepath")
|
102 |
+
image_file_upload = gr.Image(label="Upload an image", type="filepath")
|
103 |
+
|
104 |
+
# For Tab 2 (URL input)
|
105 |
+
url_input = gr.Textbox(label="Paste an image URL")
|
106 |
+
|
107 |
+
# For Tab 3 (file output)
|
108 |
+
output_file = gr.File(label="Output PNG File")
|
109 |
+
|
110 |
+
# Tab 1: local image -> processed image
|
111 |
+
tab1 = gr.Interface(
|
112 |
+
fn=fn,
|
113 |
+
inputs=[image_upload, model_selector],
|
114 |
+
outputs=processed_img_upload,
|
115 |
+
api_name="image",
|
116 |
+
description="Upload an image and choose your background removal model."
|
117 |
+
)
|
118 |
+
|
119 |
+
# Tab 2: URL input -> processed image
|
120 |
+
tab2 = gr.Interface(
|
121 |
+
fn=fn,
|
122 |
+
inputs=[url_input, model_selector],
|
123 |
+
outputs=processed_img_url,
|
124 |
+
api_name="text",
|
125 |
+
description="Paste an image URL and choose your background removal model."
|
126 |
+
)
|
127 |
+
|
128 |
+
# Tab 3: file output -> returns path to .png
|
129 |
+
tab3 = gr.Interface(
|
130 |
+
fn=process_file,
|
131 |
+
inputs=[image_file_upload, model_selector],
|
132 |
+
outputs=output_file,
|
133 |
+
api_name="png",
|
134 |
+
description="Upload an image, choose a model, and get a transparent PNG."
|
135 |
+
)
|
136 |
+
|
137 |
+
# Combine all tabs
|
138 |
+
demo = gr.TabbedInterface(
|
139 |
+
[tab1, tab2, tab3],
|
140 |
+
["Image Upload", "URL Input", "File Output"],
|
141 |
+
title="Background Removal Tool"
|
142 |
+
)
|
143 |
|
144 |
if __name__ == "__main__":
|
145 |
demo.launch(show_error=True, share=True)
|
|