Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,16 +6,12 @@ from transformers import AutoModelForImageSegmentation
|
|
6 |
import torch
|
7 |
from torchvision import transforms
|
8 |
|
|
|
9 |
|
10 |
-
|
11 |
-
torch.set_float32_matmul_precision("high")
|
12 |
-
|
13 |
-
|
14 |
-
model = AutoModelForImageSegmentation.from_pretrained(
|
15 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
16 |
-
)
|
17 |
-
|
18 |
-
|
19 |
transform_image = transforms.Compose(
|
20 |
[
|
21 |
transforms.Resize((1024, 1024)),
|
@@ -24,55 +20,42 @@ transform_image = transforms.Compose(
|
|
24 |
]
|
25 |
)
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
im =
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
|
33 |
-
|
|
|
|
|
34 |
with torch.no_grad():
|
35 |
-
preds =
|
36 |
-
|
37 |
-
|
38 |
-
mask =
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
image_input = gr.Image(label="Resim Yükle")
|
47 |
-
url_input = gr.Textbox(label="Resim URL'si")
|
48 |
-
|
49 |
|
50 |
chameleon = load_img("elon.webp", output_type="pil")
|
51 |
-
url_example = "https://dadanizm.com/wp-content/uploads/2022/11/elon-musk-992x2656.jpeg"
|
52 |
-
|
53 |
|
|
|
54 |
tab1 = gr.Interface(
|
55 |
-
fn=
|
56 |
-
inputs=image_input,
|
57 |
-
outputs=slider,
|
58 |
-
examples=[chameleon],
|
59 |
-
api_name="image"
|
60 |
-
)
|
61 |
-
|
62 |
-
tab2 = gr.Interface(
|
63 |
-
fn=remove_background,
|
64 |
-
inputs=url_input,
|
65 |
-
outputs=slider,
|
66 |
-
examples=[url_example],
|
67 |
-
api_name="url"
|
68 |
)
|
69 |
|
|
|
70 |
|
71 |
demo = gr.TabbedInterface(
|
72 |
-
[tab1, tab2],
|
73 |
-
["Resim", "URL"],
|
74 |
-
title="Resim Arka Planını Kaldır"
|
75 |
)
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
-
demo.launch(show_error=True)
|
|
|
6 |
import torch
|
7 |
from torchvision import transforms
|
8 |
|
9 |
+
torch.set_float32_matmul_precision(["high", "highest"][0])
|
10 |
|
11 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
|
|
|
|
|
|
|
|
12 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
13 |
+
)
|
14 |
+
birefnet.to("cpu")
|
|
|
15 |
transform_image = transforms.Compose(
|
16 |
[
|
17 |
transforms.Resize((1024, 1024)),
|
|
|
20 |
]
|
21 |
)
|
22 |
|
23 |
+
def fn(image):
|
24 |
+
im = load_img(image, output_type="pil")
|
25 |
+
im = im.convert("RGB")
|
26 |
+
origin = im.copy()
|
27 |
+
image = process(im)
|
28 |
+
image = image.resize(origin.size)
|
29 |
+
return (image, origin)
|
30 |
|
31 |
+
@spaces.GPU
|
32 |
+
def process(image):
|
33 |
+
input_images = transform_image(image).unsqueeze(0).to("cpu")
|
34 |
with torch.no_grad():
|
35 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
36 |
+
pred = preds[0].squeeze()
|
37 |
+
pred_pil = transforms.ToPILImage()(pred)
|
38 |
+
mask = pred_pil.resize(image.size)
|
39 |
+
image.putalpha(mask)
|
40 |
+
return image
|
41 |
+
|
42 |
+
slider1 = ImageSlider(label="Birefnet", type="pil")
|
43 |
+
slider2 = ImageSlider(label="Birefnet", type="pil")
|
44 |
+
image = gr.Image(label="Bir resim yükleyin")
|
45 |
+
text = gr.Textbox(label="Bir resim URL'si yapıştırın")
|
|
|
|
|
|
|
46 |
|
47 |
chameleon = load_img("elon.webp", output_type="pil")
|
|
|
|
|
48 |
|
49 |
+
url= "https://dadanizm.com/wp-content/uploads/2022/11/elon-musk-992x2656.jpeg"
|
50 |
tab1 = gr.Interface(
|
51 |
+
fn, inputs=image, outputs=slider1, examples=[chameleon], api_name="image"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
)
|
53 |
|
54 |
+
tab2 = gr.Interface(fn, inputs=text, outputs=slider2, examples=[url], api_name="text")
|
55 |
|
56 |
demo = gr.TabbedInterface(
|
57 |
+
[tab1, tab2], ["Resim", "URL"], title="Birefnet ile Arka Plan Kaldırma"
|
|
|
|
|
58 |
)
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
+
demo.launch(show_error=True)
|