mexicanamerican commited on
Commit
3107813
·
verified ·
1 Parent(s): 49ed074

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -66
app.py CHANGED
@@ -12,8 +12,6 @@ birefnet = AutoModelForImageSegmentation.from_pretrained(
12
  "ZhengPeng7/BiRefNet", trust_remote_code=True
13
  )
14
  birefnet.to("cuda")
15
-
16
- # Image transformation pipeline
17
  transform_image = transforms.Compose(
18
  [
19
  transforms.Resize((1024, 1024)),
@@ -22,7 +20,6 @@ transform_image = transforms.Compose(
22
  ]
23
  )
24
 
25
- # Processing function
26
  def fn(image):
27
  im = load_img(image, output_type="pil")
28
  im = im.convert("RGB")
@@ -42,88 +39,37 @@ def process(image):
42
  mask = pred_pil.resize(image_size)
43
  image.putalpha(mask)
44
  return image
45
-
46
- # Process file upload
47
  def process_file(f):
48
- name_path = f.rsplit(".", 1)[0] + ".png"
49
  im = load_img(f, output_type="pil")
50
  im = im.convert("RGB")
51
  transparent = process(im)
52
  transparent.save(name_path)
53
  return name_path
54
 
55
- # Custom CSS Styling
56
- css_style = """
57
- .gradio-container {
58
- background-color: #1b1b1b !important;
59
- color: #f5f5f5 !important;
60
- }
61
-
62
- input, textarea, button {
63
- border-radius: 8px !important;
64
- background-color: #2b2b2b !important;
65
- color: #f5f5f5 !important;
66
- border: 1px solid #3c3c3c !important;
67
- }
68
-
69
- button {
70
- background-color: #9146ff !important;
71
- padding: 10px 24px !important;
72
- color: white !important;
73
- }
74
-
75
- button:hover {
76
- background-color: #732eb5 !important;
77
- }
78
-
79
- .gr-button-primary {
80
- background-color: #9146ff !important;
81
- }
82
-
83
- .gr-image-preview {
84
- background-color: #2b2b2b !important;
85
- border-radius: 8px !important;
86
- }
87
-
88
- .gr-file-preview {
89
- background-color: #2b2b2b !important;
90
- color: white !important;
91
- }
92
-
93
- #output-image img {
94
- border-radius: 8px;
95
- max-width: 100%;
96
- }
97
- """
98
-
99
- # Image sliders
100
  slider1 = ImageSlider(label="birefnet", type="pil")
101
  slider2 = ImageSlider(label="birefnet", type="pil")
102
-
103
- # Image upload areas
104
- image = gr.Image(label="Upload an image", live=True) # Automatically submits on upload
105
- image2 = gr.Image(label="Upload an image", type="filepath", live=True)
106
-
107
- # Other inputs and file outputs
108
  text = gr.Textbox(label="Paste an image URL")
109
- png_file = gr.File(label="Output PNG file")
 
110
 
111
- # Sample images and URLs
112
  chameleon = load_img("butterfly.jpg", output_type="pil")
113
- url = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
114
 
115
- # Define the tabs for each functionality
116
  tab1 = gr.Interface(
117
  fn, inputs=image, outputs=slider1, examples=[chameleon], api_name="image"
118
  )
119
 
120
  tab2 = gr.Interface(fn, inputs=text, outputs=slider2, examples=[url], api_name="text")
121
-
122
  tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["butterfly.jpg"], api_name="png")
123
 
124
- # Create the Gradio app with custom CSS and tabs
125
- with gr.Blocks(css=css_style) as demo:
126
- gr.TabbedInterface([tab1, tab2, tab3], ["image", "text", "png"], title="birefnet for background removal")
 
127
 
128
  if __name__ == "__main__":
129
- demo.launch(show_error=True)
 
12
  "ZhengPeng7/BiRefNet", trust_remote_code=True
13
  )
14
  birefnet.to("cuda")
 
 
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")
 
39
  mask = pred_pil.resize(image_size)
40
  image.putalpha(mask)
41
  return image
42
+
 
43
  def process_file(f):
44
+ name_path = f.rsplit(".",1)[0]+".png"
45
  im = load_img(f, output_type="pil")
46
  im = im.convert("RGB")
47
  transparent = process(im)
48
  transparent.save(name_path)
49
  return name_path
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  slider1 = ImageSlider(label="birefnet", type="pil")
52
  slider2 = ImageSlider(label="birefnet", type="pil")
53
+ image = gr.Image(label="Upload an image")
54
+ image2 = gr.Image(label="Upload an image",type="filepath")
 
 
 
 
55
  text = gr.Textbox(label="Paste an image URL")
56
+ png_file = gr.File(label="output png file")
57
+
58
 
 
59
  chameleon = load_img("butterfly.jpg", output_type="pil")
 
60
 
61
+ url = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
62
  tab1 = gr.Interface(
63
  fn, inputs=image, outputs=slider1, examples=[chameleon], api_name="image"
64
  )
65
 
66
  tab2 = gr.Interface(fn, inputs=text, outputs=slider2, examples=[url], api_name="text")
 
67
  tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["butterfly.jpg"], api_name="png")
68
 
69
+
70
+ demo = gr.TabbedInterface(
71
+ [tab1, tab2,tab3], ["image", "text","png"], title="birefnet for background removal"
72
+ )
73
 
74
  if __name__ == "__main__":
75
+ demo.launch(show_error=True)