ihabooe commited on
Commit
116136a
·
verified ·
1 Parent(s): 1cbc077

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -108
app.py CHANGED
@@ -2,7 +2,7 @@ import numpy as np
2
  import torch
3
  import torch.nn.functional as F
4
  from torchvision.transforms.functional import normalize
5
- import gradio as gr # Make sure this import is present
6
  from briarmbg import BriaRMBG
7
  import PIL
8
  from PIL import Image
@@ -25,7 +25,6 @@ OUTPUT_DIR = "output_images"
25
  os.makedirs(OUTPUT_DIR, exist_ok=True)
26
 
27
  def resize_image(image, max_size=1024):
28
- """Resize image while maintaining aspect ratio and quality"""
29
  width, height = image.size
30
  aspect_ratio = width / height
31
 
@@ -42,13 +41,13 @@ def resize_image(image, max_size=1024):
42
 
43
  def process(image, progress=gr.Progress()):
44
  if image is None:
45
- return None, gr.update(visible=False)
46
 
47
  progress(0, desc="Starting processing...")
48
  orig_image = Image.fromarray(image)
49
  original_size = orig_image.size
50
 
51
- progress(0.1, desc="Preparing image...")
52
  process_image = resize_image(orig_image)
53
  w, h = process_image.size
54
 
@@ -58,7 +57,7 @@ def process(image, progress=gr.Progress()):
58
  im_tensor = torch.divide(im_tensor, 255.0)
59
  im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
60
 
61
- progress(0.3, desc="Processing with AI model...")
62
  if torch.cuda.is_available():
63
  im_tensor = im_tensor.cuda()
64
 
@@ -80,95 +79,67 @@ def process(image, progress=gr.Progress()):
80
  new_im = orig_image.copy()
81
  new_im.putalpha(pil_mask)
82
 
83
- progress(0.8, desc="Preparing download...")
84
  unique_id = str(uuid.uuid4())[:8]
85
  filename = f"background_removed_{unique_id}.png"
86
  filepath = os.path.join(OUTPUT_DIR, filename)
87
-
88
  new_im.save(filepath, format='PNG', quality=100)
89
- output_array = np.array(new_im.convert("RGBA"))
90
 
91
  progress(1.0, desc="Done!")
92
- return output_array, gr.update(visible=True, value=filepath, interactive=True)
93
-
94
- def update_comparison(image, result):
95
- if result is None:
96
- return None
97
- orig_image = Image.fromarray(image)
98
- result_image = Image.fromarray(result)
99
-
100
- width = max(orig_image.width, result_image.width)
101
- height = max(orig_image.height, result_image.height)
102
-
103
- comparison = Image.new('RGBA', (width * 2, height))
104
- comparison.paste(orig_image, (0, 0))
105
- comparison.paste(result_image, (width, 0))
106
-
107
- return np.array(comparison)
108
 
109
  # Gradio interface
110
  with gr.Blocks(css="""
111
- @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&family=Roboto+Mono:wght@300;400;700&display=swap');
 
 
112
 
113
- :root {
114
- --neon-cyan: #00ffff;
115
- --neon-pink: #ff00de;
116
- --dark-background: #0a0a1e;
117
- }
118
-
119
- body {
120
- font-family: 'Roboto Mono', monospace;
121
- background: linear-gradient(135deg, var(--dark-background) 0%, #121238 100%);
122
- color: #ffffff;
123
- }
124
-
125
- .container {
126
- max-width: 800px;
127
- margin: 0 auto;
128
- padding: 20px;
129
- }
130
-
131
- .image-comparison {
132
- position: relative;
133
- width: 100% !important;
134
- max-width: 800px !important;
135
- margin: 0 auto !important;
136
- border-radius: 12px !important;
137
- border: 2px solid var(--neon-cyan) !important;
138
- background: rgba(18, 18, 56, 0.7) !important;
139
- overflow: hidden !important;
140
- }
141
-
142
  .title-text {
143
- color: var(--neon-pink);
144
  font-family: 'Orbitron', sans-serif;
145
- font-size: 2em;
146
  text-align: center;
147
  margin: 20px 0;
148
- text-shadow: 0 0 10px rgba(255, 0, 222, 0.5);
149
  }
150
-
151
  .subtitle-text {
152
- color: var(--neon-cyan);
153
  text-align: center;
154
- margin-bottom: 20px;
155
- text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
 
156
  }
157
-
158
- .download-button {
159
- background: linear-gradient(45deg, var(--neon-cyan), var(--neon-pink)) !important;
160
- border: none !important;
161
- padding: 10px 20px !important;
162
- border-radius: 8px !important;
163
- color: white !important;
164
- font-family: 'Orbitron', sans-serif !important;
165
- cursor: pointer !important;
166
- transition: all 0.3s ease !important;
167
  }
168
-
169
- .download-button:hover {
170
- transform: translateY(-2px) !important;
171
- box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4) !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  }
173
  """) as demo:
174
  gr.Markdown("""
@@ -176,41 +147,27 @@ with gr.Blocks(css="""
176
  <p class="subtitle-text">Remove backgrounds instantly using advanced AI technology</p>
177
  """)
178
 
179
- with gr.Column():
180
- input_image = gr.Image(
181
- type="numpy",
182
- label="Upload Your Image",
183
- elem_classes="image-comparison"
184
- )
185
-
186
- output_image = gr.Image(
187
- type="numpy",
188
- visible=False
189
- )
190
-
191
- image_comparison = gr.Image(
192
- type="numpy",
193
- label="Before / After Comparison",
194
- elem_classes="image-comparison"
195
- )
196
-
197
- download_file = gr.File(
198
- label="",
199
- file_count="single",
200
- interactive=True,
201
- visible=False,
202
- elem_classes="download-button"
203
- )
204
-
205
- input_image.change(
206
  fn=process,
207
  inputs=input_image,
208
- outputs=[output_image, download_file]
209
- ).then(
210
- fn=update_comparison,
211
- inputs=[input_image, output_image],
212
- outputs=image_comparison
213
  )
214
 
215
  if __name__ == "__main__":
216
- demo.launch(share=False)
 
2
  import torch
3
  import torch.nn.functional as F
4
  from torchvision.transforms.functional import normalize
5
+ import gradio as gr
6
  from briarmbg import BriaRMBG
7
  import PIL
8
  from PIL import Image
 
25
  os.makedirs(OUTPUT_DIR, exist_ok=True)
26
 
27
  def resize_image(image, max_size=1024):
 
28
  width, height = image.size
29
  aspect_ratio = width / height
30
 
 
41
 
42
  def process(image, progress=gr.Progress()):
43
  if image is None:
44
+ return None
45
 
46
  progress(0, desc="Starting processing...")
47
  orig_image = Image.fromarray(image)
48
  original_size = orig_image.size
49
 
50
+ progress(0.2, desc="Preparing image...")
51
  process_image = resize_image(orig_image)
52
  w, h = process_image.size
53
 
 
57
  im_tensor = torch.divide(im_tensor, 255.0)
58
  im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
59
 
60
+ progress(0.4, desc="Processing with AI model...")
61
  if torch.cuda.is_available():
62
  im_tensor = im_tensor.cuda()
63
 
 
79
  new_im = orig_image.copy()
80
  new_im.putalpha(pil_mask)
81
 
82
+ progress(0.8, desc="Saving result...")
83
  unique_id = str(uuid.uuid4())[:8]
84
  filename = f"background_removed_{unique_id}.png"
85
  filepath = os.path.join(OUTPUT_DIR, filename)
 
86
  new_im.save(filepath, format='PNG', quality=100)
 
87
 
88
  progress(1.0, desc="Done!")
89
+ return gr.Image.update(value=filepath, visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  # Gradio interface
92
  with gr.Blocks(css="""
93
+ @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
94
+
95
+ .container { max-width: 850px; margin: 0 auto; padding: 20px; }
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  .title-text {
98
+ color: #ff00de;
99
  font-family: 'Orbitron', sans-serif;
100
+ font-size: 2.5em;
101
  text-align: center;
102
  margin: 20px 0;
103
+ text-shadow: 0 0 10px rgba(255, 0, 222, 0.7);
104
  }
105
+
106
  .subtitle-text {
107
+ color: #00ffff;
108
  text-align: center;
109
+ margin-bottom: 30px;
110
+ font-size: 1.2em;
111
+ text-shadow: 0 0 8px rgba(0, 255, 255, 0.7);
112
  }
113
+
114
+ .image-container {
115
+ background: rgba(10, 10, 30, 0.3);
116
+ border-radius: 15px;
117
+ padding: 20px;
118
+ margin: 10px 0;
119
+ border: 2px solid #00ffff;
120
+ box-shadow: 0 0 15px rgba(0, 255, 255, 0.2);
 
 
121
  }
122
+
123
+ .download-btn {
124
+ background: linear-gradient(45deg, #00ffff, #ff00de);
125
+ border: none;
126
+ padding: 12px 25px;
127
+ border-radius: 8px;
128
+ color: white;
129
+ font-family: 'Orbitron', sans-serif;
130
+ cursor: pointer;
131
+ transition: all 0.3s ease;
132
+ }
133
+
134
+ .download-btn:hover {
135
+ transform: translateY(-2px);
136
+ box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4);
137
+ }
138
+
139
+ @media (max-width: 768px) {
140
+ .title-text { font-size: 1.8em; }
141
+ .subtitle-text { font-size: 1em; }
142
+ .image-container { padding: 10px; }
143
  }
144
  """) as demo:
145
  gr.Markdown("""
 
147
  <p class="subtitle-text">Remove backgrounds instantly using advanced AI technology</p>
148
  """)
149
 
150
+ with gr.Row():
151
+ with gr.Column():
152
+ input_image = gr.Image(
153
+ label="Upload Image",
154
+ type="numpy",
155
+ elem_classes="image-container"
156
+ )
157
+
158
+ output_image = gr.Image(
159
+ label="Result",
160
+ type="filepath",
161
+ elem_classes="image-container"
162
+ )
163
+
164
+ process_btn = gr.Button("Remove Background", variant="primary")
165
+
166
+ process_btn.click(
 
 
 
 
 
 
 
 
 
 
167
  fn=process,
168
  inputs=input_image,
169
+ outputs=output_image
 
 
 
 
170
  )
171
 
172
  if __name__ == "__main__":
173
+ demo.launch()