Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,171 +1,241 @@
|
|
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 |
-
#
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
|
|
20 |
|
21 |
-
#
|
22 |
-
#
|
|
|
|
|
|
|
|
|
23 |
# )
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
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 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
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"
|
111 |
# value="BiRefNet",
|
112 |
# label="Select Model"
|
113 |
# )
|
114 |
# model_selector_3 = gr.Radio(
|
115 |
-
# choices=["BiRefNet"
|
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 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
)
|
142 |
|
143 |
-
# Tab 2: URL input -> processed image
|
144 |
-
tab2 = gr.Interface(
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
)
|
151 |
|
152 |
-
# Tab 3: file output -> returns path to .png
|
153 |
-
tab3 = gr.Interface(
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
-
#
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
166 |
)
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
if __name__ == "__main__":
|
169 |
demo.launch(show_error=True, share=True)
|
170 |
|
171 |
-
|
|
|
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
|
174 |
+
from transformers import AutoModelForImageSegmentation
|
175 |
+
import torch
|
176 |
+
from torchvision import transforms
|
177 |
+
from PIL import Image
|
178 |
+
import os
|
179 |
|
180 |
+
# precision tweak
|
181 |
+
torch.set_float32_matmul_precision("high")
|
182 |
+
|
183 |
+
# βββ Model & Transforms βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
184 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
185 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
186 |
)
|
187 |
|
188 |
+
transform_image = transforms.Compose([
|
189 |
+
transforms.Resize((1024, 1024)),
|
190 |
+
transforms.ToTensor(),
|
191 |
+
transforms.Normalize([0.485, 0.456, 0.406],
|
192 |
+
[0.229, 0.224, 0.225]),
|
193 |
+
])
|
194 |
+
|
195 |
+
# βββ Inference fn ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
196 |
+
@spaces.GPU
|
197 |
+
def remove_bg(image: Image.Image):
|
198 |
+
orig_size = image.size
|
199 |
+
x = transform_image(image).unsqueeze(0)
|
200 |
+
out = birefnet(x)
|
201 |
+
logits = out.logits
|
202 |
+
mask = logits.sigmoid().cpu().squeeze(0)
|
203 |
+
mask_pil = transforms.ToPILImage()(mask).resize(orig_size)
|
204 |
+
image.putalpha(mask_pil)
|
205 |
+
return image
|
206 |
+
|
207 |
+
# βββ URL wrapper βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
208 |
+
def remove_bg_url(url: str):
|
209 |
+
img = load_img(url, output_type="pil").convert("RGB")
|
210 |
+
return remove_bg(img)
|
211 |
+
|
212 |
+
# βββ Fileβbased version ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
213 |
+
def remove_bg_file(path: str):
|
214 |
+
img = load_img(path, output_type="pil").convert("RGB")
|
215 |
+
out = remove_bg(img)
|
216 |
+
out_path = os.path.splitext(path)[0] + ".png"
|
217 |
+
out.save(out_path)
|
218 |
+
return out_path
|
219 |
+
|
220 |
+
# βββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
221 |
+
with gr.Blocks() as demo:
|
222 |
+
gr.Markdown("## Background Removal (BiRefNet only)")
|
223 |
+
|
224 |
+
with gr.Tab("Upload"):
|
225 |
+
inp = gr.Image(type="filepath", label="Upload Image")
|
226 |
+
out = gr.Image(type="pil", label="Result")
|
227 |
+
inp.change(remove_bg, inp, out)
|
228 |
+
|
229 |
+
with gr.Tab("URL"):
|
230 |
+
url_in = gr.Textbox(label="Image URL")
|
231 |
+
out_url = gr.Image(type="pil", label="Result")
|
232 |
+
url_in.submit(remove_bg_url, url_in, out_url)
|
233 |
+
|
234 |
+
with gr.Tab("Save PNG"):
|
235 |
+
inp2 = gr.File(label="Image File")
|
236 |
+
out2 = gr.File(label="Transparent PNG")
|
237 |
+
inp2.change(remove_bg_file, inp2, out2)
|
238 |
+
|
239 |
if __name__ == "__main__":
|
240 |
demo.launch(show_error=True, share=True)
|
241 |
|
|