shemayons commited on
Commit
4ad1442
·
verified ·
1 Parent(s): 2daf4de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -220
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
- # 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
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from load_image import load_img
3
  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)