Bla1r commited on
Commit
54e77cb
·
verified ·
1 Parent(s): b9b3f14

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import uuid
9
+ import os
10
+
11
+ torch.set_float32_matmul_precision(["high", "highest"][0])
12
+
13
+ birefnet = AutoModelForImageSegmentation.from_pretrained(
14
+ "ZhengPeng7/BiRefNet", trust_remote_code=True
15
+ )
16
+ birefnet.to("cuda")
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
+ @spaces.GPU
27
+ def process(image):
28
+ image_size = image.size
29
+ input_images = transform_image(image).unsqueeze(0).to("cuda")
30
+ # Prediction
31
+ with torch.no_grad():
32
+ preds = birefnet(input_images)[-1].sigmoid().cpu()
33
+ pred = preds[0].squeeze()
34
+ pred_pil = transforms.ToPILImage()(pred)
35
+ mask = pred_pil.resize(image_size)
36
+ image.putalpha(mask)
37
+ return image
38
+
39
+ def fn(image):
40
+ im = load_img(image, output_type="pil")
41
+ im = im.convert("RGB")
42
+ processed_image = process(im)
43
+
44
+ # Save to a temp file
45
+ filename = f"/tmp/processed_{uuid.uuid4().hex}.png"
46
+ processed_image.save(filename)
47
+
48
+ return processed_image, filename # Return both preview and downloadable file
49
+
50
+ def process_file(f):
51
+ name_path = f.rsplit(".", 1)[0] + ".png"
52
+ im = load_img(f, output_type="pil")
53
+ im = im.convert("RGB")
54
+ transparent = process(im)
55
+ transparent.save(name_path)
56
+ return name_path
57
+
58
+ # Components
59
+ slider1 = gr.Image(label="Preview") # Changed from ImageSlider to Image
60
+ output_file1 = gr.File(label="Download Processed Image")
61
+ slider2 = ImageSlider(label="Processed Image from URL", type="pil")
62
+ image_upload = gr.Image(label="Upload an image")
63
+ image_file_upload = gr.Image(label="Upload an image", type="filepath")
64
+ url_input = gr.Textbox(label="Paste an image URL")
65
+ output_file = gr.File(label="Output PNG File")
66
+
67
+ # Example images
68
+ chameleon = load_img("butterfly.jpg", output_type="pil")
69
+ url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
70
+
71
+ # Tabs
72
+ tab1 = gr.Interface(fn, inputs=image_upload, outputs=[slider1, output_file1], examples=[chameleon], api_name="image")
73
+ tab2 = gr.Interface(fn, inputs=url_input, outputs=slider2, examples=[url_example], api_name="text")
74
+ tab3 = gr.Interface(process_file, inputs=image_file_upload, outputs=output_file, examples=["butterfly.jpg"], api_name="png")
75
+
76
+ demo = gr.TabbedInterface(
77
+ [tab1, tab2, tab3], ["Image Upload", "URL Input", "File Output"], title="Background Removal Tool"
78
+ )
79
+
80
+ if __name__ == "__main__":
81
+ demo.launch(show_error=True)