Kims12 commited on
Commit
bdde7fb
ยท
verified ยท
1 Parent(s): 1834a42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -2
app.py CHANGED
@@ -1,2 +1,76 @@
1
- import os
2
- exec(os.environ.get('APP'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio_imageslider import ImageSlider
3
+ from loadimg import load_img
4
+ import spaces
5
+ from transformers import AutoModelForImageSegmentation
6
+ import torch
7
+ from torchvision import transforms
8
+
9
+ # GPU ์„ค์ •์„ CPU๋กœ ๋ณ€๊ฒฝ
10
+ # GPU ์„ค์ •์„ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ "cuda"๋ฅผ "cpu"๋กœ ๋ณ€๊ฒฝ
11
+ # torch.set_float32_matmul_precision("high")๋Š” CPU์—์„  ํ•„์š” ์—†์Œ.
12
+
13
+ birefnet = AutoModelForImageSegmentation.from_pretrained(
14
+ "ZhengPeng7/BiRefNet", trust_remote_code=True
15
+ )
16
+ birefnet.to("cpu") # GPU -> CPU๋กœ ๋ณ€๊ฒฝ
17
+
18
+ transform_image = transforms.Compose(
19
+ [
20
+ transforms.Resize((1024, 1024)),
21
+ transforms.ToTensor(),
22
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
23
+ ]
24
+ )
25
+
26
+ def fn(image):
27
+ im = load_img(image, output_type="pil")
28
+ im = im.convert("RGB")
29
+ origin = im.copy()
30
+ processed_image = process(im)
31
+ return (processed_image, origin)
32
+
33
+ # @spaces.GPU ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ œ๊ฑฐ
34
+ # CPU ํ™˜๊ฒฝ์—์„œ ๋™์ž‘ํ•˜๋„๋ก ์„ค์ •
35
+
36
+ def process(image):
37
+ image_size = image.size
38
+ input_images = transform_image(image).unsqueeze(0).to("cpu") # GPU -> CPU๋กœ ๋ณ€๊ฒฝ
39
+ # Prediction
40
+ with torch.no_grad():
41
+ preds = birefnet(input_images)[-1].sigmoid().cpu()
42
+ pred = preds[0].squeeze()
43
+ pred_pil = transforms.ToPILImage()(pred)
44
+ mask = pred_pil.resize(image_size)
45
+ image.putalpha(mask)
46
+ return image
47
+
48
+ def process_file(f):
49
+ name_path = f.rsplit(".", 1)[0] + ".png"
50
+ im = load_img(f, output_type="pil")
51
+ im = im.convert("RGB")
52
+ transparent = process(im)
53
+ transparent.save(name_path)
54
+ return name_path
55
+
56
+ slider1 = ImageSlider(label="Processed Image", type="pil")
57
+ slider2 = ImageSlider(label="Processed Image from URL", type="pil")
58
+ image_upload = gr.Image(label="Upload an image")
59
+ image_file_upload = gr.Image(label="Upload an image", type="filepath")
60
+ url_input = gr.Textbox(label="Paste an image URL")
61
+ output_file = gr.File(label="Output PNG File")
62
+
63
+ # Example images
64
+ chameleon = load_img("butterfly.jpg", output_type="pil")
65
+ url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
66
+
67
+ tab1 = gr.Interface(fn, inputs=image_upload, outputs=slider1, examples=[chameleon], api_name="image")
68
+ tab2 = gr.Interface(fn, inputs=url_input, outputs=slider2, examples=[url_example], api_name="text")
69
+ tab3 = gr.Interface(process_file, inputs=image_file_upload, outputs=output_file, examples=["butterfly.jpg"], api_name="png")
70
+
71
+ demo = gr.TabbedInterface(
72
+ [tab1, tab2, tab3], ["Image Upload", "URL Input", "File Output"], title="Background Removal Tool"
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ demo.launch(show_error=True)