ihabooe commited on
Commit
5ddafad
·
verified ·
1 Parent(s): 595ca84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -426
app.py CHANGED
@@ -73,280 +73,92 @@ def process(image, progress=gr.Progress()):
73
  # Convert to numpy array for display
74
  output_array = np.array(new_im)
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  progress(1.0, desc="Done!")
77
- return (
78
- output_array,
79
- filepath,
80
- gr.update(value=f"""
81
- <script>
82
- setTimeout(function() {{
83
- const link = document.createElement('a');
84
- link.href = '/file={filepath}';
85
- link.download = '{filename}';
86
- document.body.appendChild(link);
87
- link.click();
88
- document.body.removeChild(link);
89
- }}, 1000);
90
- </script>
91
- """, visible=True)
92
- )
93
 
94
  except Exception as e:
95
  print(f"Error processing image: {str(e)}")
96
  return None, None, None
97
 
98
- css = """
99
- @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
100
-
101
- .container { max-width: 850px; margin: 0 auto; padding: 20px; }
102
-
103
- .title-text {
104
- color: #ff00de;
105
- font-family: 'Orbitron', sans-serif;
106
- font-size: 2.5em;
107
- text-align: center;
108
- margin: 20px 0;
109
- text-shadow: 0 0 10px rgba(255, 0, 222, 0.7);
110
- animation: glow 2s ease-in-out infinite alternate;
111
- }
112
-
113
- .subtitle-text {
114
- color: #00ffff;
115
- text-align: center;
116
- margin-bottom: 30px;
117
- font-size: 1.2em;
118
- text-shadow: 0 0 8px rgba(0, 255, 255, 0.7);
119
- }
120
-
121
- .image-container {
122
- background: rgba(10, 10, 30, 0.3);
123
- border-radius: 15px;
124
- padding: 20px;
125
- margin: 10px 0;
126
- border: 2px solid #00ffff;
127
- box-shadow: 0 0 15px rgba(0, 255, 255, 0.2);
128
- transition: all 0.3s ease;
129
- }
130
-
131
- .image-container img {
132
- max-width: 100%;
133
- height: auto;
134
- display: block;
135
- margin: 0 auto;
136
- }
137
-
138
- .image-container:hover {
139
- box-shadow: 0 0 20px rgba(0, 255, 255, 0.4);
140
- transform: translateY(-2px);
141
- }
142
-
143
- .download-btn {
144
- background: linear-gradient(45deg, #00ffff, #ff00de);
145
- border: none;
146
- padding: 12px 25px;
147
- border-radius: 8px;
148
- color: white;
149
- font-family: 'Orbitron', sans-serif;
150
- cursor: pointer;
151
- transition: all 0.3s ease;
152
- margin-top: 10px;
153
- text-align: center;
154
- text-transform: uppercase;
155
- letter-spacing: 1px;
156
- display: block;
157
- width: 100%;
158
- }
159
-
160
- .download-btn:hover {
161
- transform: translateY(-2px);
162
- box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4);
163
- }
164
-
165
- @keyframes glow {
166
- from {
167
- text-shadow: 0 0 5px #ff00de, 0 0 10px #ff00de, 0 0 15px #ff00de;
168
  }
169
- to {
170
- text-shadow: 0 0 10px #ff00de, 0 0 20px #ff00de, 0 0 30px #ff00de;
 
 
 
 
 
171
  }
172
- }
173
-
174
- @media (max-width: 768px) {
175
- .title-text { font-size: 1.8em; }
176
- .subtitle-text { font-size: 1em; }
177
- .image-container { padding: 10px; }
178
- .download-btn { padding: 10px 20px; }
179
- }
180
- """
181
-
182
- with gr.Blocks(css=css) as demo:
183
- gr.Markdown("""
184
- <h1 class="title-text">AI Background Removal</h1>
185
- <p class="subtitle-text">Remove backgrounds instantly using advanced AI technology</p>
186
- """)
187
 
188
- with gr.Row():
189
- with gr.Column():
190
- input_image = gr.Image(
191
- label="Upload Image",
192
- type="numpy",
193
- elem_classes="image-container"
194
- )
195
-
196
- output_image = gr.Image(
197
- label="Result",
198
- type="numpy",
199
- show_label=True,
200
- elem_classes="image-container"
201
- )
202
-
203
- download_button = gr.File(
204
- label="Download Result",
205
- visible=True,
206
- elem_classes="download-btn"
207
- )
208
-
209
- # Add HTML component for auto-download
210
- auto_download = gr.HTML(visible=False)
211
 
212
- input_image.change(
213
- fn=process,
214
- inputs=input_image,
215
- outputs=[output_image, download_button, auto_download]
216
- )
217
-
218
- if __name__ == "__main__":
219
- demo.launch() w, h = process_image.size
220
-
221
- im_np = np.array(process_image)
222
- im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
223
- im_tensor = torch.unsqueeze(im_tensor, 0)
224
- im_tensor = torch.divide(im_tensor, 255.0)
225
- im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
226
-
227
- progress(0.4, desc="Processing with AI model...")
228
- if torch.cuda.is_available():
229
- im_tensor = im_tensor.cuda()
230
-
231
- with torch.no_grad():
232
- result = net(im_tensor)
233
-
234
- progress(0.6, desc="Post-processing...")
235
- result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
236
- ma = torch.max(result)
237
- mi = torch.min(result)
238
- result = (result - mi) / (ma - mi)
239
-
240
- result_array = (result * 255).cpu().data.numpy().astype(np.uint8)
241
- pil_mask = Image.fromarray(np.squeeze(result_array))
242
-
243
- if pil_mask.size != original_size:
244
- pil_mask = pil_mask.resize(original_size, Image.LANCZOS)
245
-
246
- new_im = orig_image.copy()
247
- new_im.putalpha(pil_mask)
248
-
249
- progress(0.8, desc="Saving result...")
250
- unique_id = str(uuid.uuid4())[:8]
251
- filename = f"background_removed_{unique_id}.png"
252
- filepath = os.path.join(OUTPUT_DIR, filename)
253
- new_im.save(filepath, format='PNG', quality=100)
254
-
255
- # Convert to numpy array for display
256
- output_array = np.array(new_im)
257
-
258
- progress(1.0, desc="Done!")
259
- return output_array, filepath
260
 
261
- except Exception as e:
262
- print(f"Error processing image: {str(e)}")
263
- return None, None
264
-
265
- css = """
266
- @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
267
-
268
- .container { max-width: 850px; margin: 0 auto; padding: 20px; }
269
-
270
- .title-text {
271
- color: #ff00de;
272
- font-family: 'Orbitron', sans-serif;
273
- font-size: 2.5em;
274
- text-align: center;
275
- margin: 20px 0;
276
- text-shadow: 0 0 10px rgba(255, 0, 222, 0.7);
277
- animation: glow 2s ease-in-out infinite alternate;
278
- }
279
-
280
- .subtitle-text {
281
- color: #00ffff;
282
- text-align: center;
283
- margin-bottom: 30px;
284
- font-size: 1.2em;
285
- text-shadow: 0 0 8px rgba(0, 255, 255, 0.7);
286
- }
287
-
288
- .image-container {
289
- background: rgba(10, 10, 30, 0.3);
290
- border-radius: 15px;
291
- padding: 20px;
292
- margin: 10px 0;
293
- border: 2px solid #00ffff;
294
- box-shadow: 0 0 15px rgba(0, 255, 255, 0.2);
295
- transition: all 0.3s ease;
296
- }
297
-
298
- .image-container img {
299
- max-width: 100%;
300
- height: auto;
301
- display: block;
302
- margin: 0 auto;
303
- }
304
-
305
- .image-container:hover {
306
- box-shadow: 0 0 20px rgba(0, 255, 255, 0.4);
307
- transform: translateY(-2px);
308
- }
309
-
310
- .download-btn {
311
- background: linear-gradient(45deg, #00ffff, #ff00de);
312
- border: none;
313
- padding: 12px 25px;
314
- border-radius: 8px;
315
- color: white;
316
- font-family: 'Orbitron', sans-serif;
317
- cursor: pointer;
318
- transition: all 0.3s ease;
319
- margin-top: 10px;
320
- text-align: center;
321
- text-transform: uppercase;
322
- letter-spacing: 1px;
323
- display: block;
324
- width: 100%;
325
- }
326
-
327
- .download-btn:hover {
328
- transform: translateY(-2px);
329
- box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4);
330
- }
331
-
332
- @keyframes glow {
333
- from {
334
- text-shadow: 0 0 5px #ff00de, 0 0 10px #ff00de, 0 0 15px #ff00de;
335
  }
336
- to {
337
- text-shadow: 0 0 10px #ff00de, 0 0 20px #ff00de, 0 0 30px #ff00de;
 
 
338
  }
339
- }
340
-
341
- @media (max-width: 768px) {
342
- .title-text { font-size: 1.8em; }
343
- .subtitle-text { font-size: 1em; }
344
- .image-container { padding: 10px; }
345
- .download-btn { padding: 10px 20px; }
346
- }
347
- """
348
-
349
- with gr.Blocks(css=css) as demo:
350
  gr.Markdown("""
351
  <h1 class="title-text">AI Background Removal</h1>
352
  <p class="subtitle-text">Remove backgrounds instantly using advanced AI technology</p>
@@ -372,176 +184,13 @@ with gr.Blocks(css=css) as demo:
372
  visible=True,
373
  elem_classes="download-btn"
374
  )
375
-
376
- input_image.change(
377
- fn=process,
378
- inputs=input_image,
379
- outputs=[output_image, download_button]
380
- )
381
-
382
- if __name__ == "__main__":
383
- demo.launch()
384
- def process(image, progress=gr.Progress()):
385
- if image is None:
386
- return None, None
387
- try:
388
- progress(0, desc="Starting processing...")
389
- orig_image = Image.fromarray(image)
390
- original_size = orig_image.size
391
-
392
- progress(0.2, desc="Preparing image...")
393
- process_image = resize_image(orig_image)
394
- w, h = process_image.size
395
-
396
- im_np = np.array(process_image)
397
- im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
398
- im_tensor = torch.unsqueeze(im_tensor, 0)
399
- im_tensor = torch.divide(im_tensor, 255.0)
400
- im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
401
-
402
- progress(0.4, desc="Processing with AI model...")
403
- if torch.cuda.is_available():
404
- im_tensor = im_tensor.cuda()
405
-
406
- with torch.no_grad():
407
- result = net(im_tensor)
408
-
409
- progress(0.6, desc="Post-processing...")
410
- result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
411
- ma = torch.max(result)
412
- mi = torch.min(result)
413
- result = (result - mi) / (ma - mi)
414
-
415
- result_array = (result * 255).cpu().data.numpy().astype(np.uint8)
416
- pil_mask = Image.fromarray(np.squeeze(result_array))
417
-
418
- if pil_mask.size != original_size:
419
- pil_mask = pil_mask.resize(original_size, Image.LANCZOS)
420
-
421
- new_im = orig_image.copy()
422
- new_im.putalpha(pil_mask)
423
-
424
- progress(0.8, desc="Saving result...")
425
- unique_id = str(uuid.uuid4())[:8]
426
- filename = f"background_removed_{unique_id}.png"
427
- filepath = os.path.join(OUTPUT_DIR, filename)
428
- new_im.save(filepath, format='PNG', quality=100)
429
-
430
- output_array = np.array(new_im.convert('RGBA'))
431
-
432
- progress(1.0, desc="Done!")
433
- return output_array, gr.File.update(value=filepath, visible=True)
434
-
435
- except Exception as e:
436
- print(f"Error processing image: {str(e)}")
437
- return None, None
438
-
439
- css = """
440
- @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
441
-
442
- .container { max-width: 850px; margin: 0 auto; padding: 20px; }
443
-
444
- .title-text {
445
- color: #ff00de;
446
- font-family: 'Orbitron', sans-serif;
447
- font-size: 2.5em;
448
- text-align: center;
449
- margin: 20px 0;
450
- text-shadow: 0 0 10px rgba(255, 0, 222, 0.7);
451
- animation: glow 2s ease-in-out infinite alternate;
452
- }
453
-
454
- .subtitle-text {
455
- color: #00ffff;
456
- text-align: center;
457
- margin-bottom: 30px;
458
- font-size: 1.2em;
459
- text-shadow: 0 0 8px rgba(0, 255, 255, 0.7);
460
- }
461
-
462
- .image-container {
463
- background: rgba(10, 10, 30, 0.3);
464
- border-radius: 15px;
465
- padding: 20px;
466
- margin: 10px 0;
467
- border: 2px solid #00ffff;
468
- box-shadow: 0 0 15px rgba(0, 255, 255, 0.2);
469
- transition: all 0.3s ease;
470
- }
471
-
472
- .image-container:hover {
473
- box-shadow: 0 0 20px rgba(0, 255, 255, 0.4);
474
- transform: translateY(-2px);
475
- }
476
-
477
- .download-btn {
478
- background: linear-gradient(45deg, #00ffff, #ff00de);
479
- border: none;
480
- padding: 12px 25px;
481
- border-radius: 8px;
482
- color: white;
483
- font-family: 'Orbitron', sans-serif;
484
- cursor: pointer;
485
- transition: all 0.3s ease;
486
- margin-top: 10px;
487
- text-align: center;
488
- text-transform: uppercase;
489
- letter-spacing: 1px;
490
- }
491
-
492
- .download-btn:hover {
493
- transform: translateY(-2px);
494
- box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4);
495
- }
496
-
497
- @keyframes glow {
498
- from {
499
- text-shadow: 0 0 5px #ff00de, 0 0 10px #ff00de, 0 0 15px #ff00de;
500
- }
501
- to {
502
- text-shadow: 0 0 10px #ff00de, 0 0 20px #ff00de, 0 0 30px #ff00de;
503
- }
504
- }
505
-
506
- @media (max-width: 768px) {
507
- .title-text { font-size: 1.8em; }
508
- .subtitle-text { font-size: 1em; }
509
- .image-container { padding: 10px; }
510
- .download-btn { padding: 10px 20px; }
511
- }
512
- """
513
-
514
- with gr.Blocks(css=css) as demo:
515
- gr.Markdown("""
516
- <h1 class="title-text">AI Background Removal</h1>
517
- <p class="subtitle-text">Remove backgrounds instantly using advanced AI technology</p>
518
- """)
519
-
520
- with gr.Row():
521
- with gr.Column():
522
- input_image = gr.Image(
523
- label="Upload Image",
524
- type="numpy",
525
- elem_classes="image-container"
526
- )
527
-
528
- output_image = gr.Image(
529
- label="Result",
530
- type="numpy",
531
- elem_classes="image-container"
532
- )
533
 
534
- download_file = gr.File(
535
- label="Download Processed Image",
536
- visible=False,
537
- elem_classes="download-btn"
538
- )
539
 
540
  input_image.change(
541
  fn=process,
542
  inputs=input_image,
543
- outputs=[output_image, download_file],
544
- api_name="process_image"
545
  )
546
 
547
  if __name__ == "__main__":
 
73
  # Convert to numpy array for display
74
  output_array = np.array(new_im)
75
 
76
+ # Create auto-download HTML
77
+ auto_download_html = f"""
78
+ <script>
79
+ setTimeout(function() {{
80
+ const link = document.createElement('a');
81
+ link.href = '/file={filepath}';
82
+ link.download = '{filename}';
83
+ document.body.appendChild(link);
84
+ link.click();
85
+ document.body.removeChild(link);
86
+ }}, 1000);
87
+ </script>
88
+ """
89
+
90
  progress(1.0, desc="Done!")
91
+ return output_array, filepath, gr.HTML.update(value=auto_download_html, visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  except Exception as e:
94
  print(f"Error processing image: {str(e)}")
95
  return None, None, None
96
 
97
+ # Define the interface
98
+ with gr.Blocks(css="""
99
+ @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
100
+
101
+ .title-text {
102
+ color: #ff00de;
103
+ font-family: 'Orbitron', sans-serif;
104
+ font-size: 2.5em;
105
+ text-align: center;
106
+ margin: 20px 0;
107
+ text-shadow: 0 0 10px rgba(255, 0, 222, 0.7);
108
+ animation: glow 2s ease-in-out infinite alternate;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  }
110
+
111
+ .subtitle-text {
112
+ color: #00ffff;
113
+ text-align: center;
114
+ margin-bottom: 30px;
115
+ font-size: 1.2em;
116
+ text-shadow: 0 0 8px rgba(0, 255, 255, 0.7);
117
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
+ .image-container {
120
+ background: rgba(10, 10, 30, 0.3);
121
+ border-radius: 15px;
122
+ padding: 20px;
123
+ margin: 10px 0;
124
+ border: 2px solid #00ffff;
125
+ box-shadow: 0 0 15px rgba(0, 255, 255, 0.2);
126
+ transition: all 0.3s ease;
127
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
+ .image-container img {
130
+ max-width: 100%;
131
+ height: auto;
132
+ display: block;
133
+ margin: 0 auto;
134
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
+ .download-btn {
137
+ background: linear-gradient(45deg, #00ffff, #ff00de);
138
+ border: none;
139
+ padding: 12px 25px;
140
+ border-radius: 8px;
141
+ color: white;
142
+ font-family: 'Orbitron', sans-serif;
143
+ cursor: pointer;
144
+ transition: all 0.3s ease;
145
+ margin-top: 10px;
146
+ text-align: center;
147
+ text-transform: uppercase;
148
+ letter-spacing: 1px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  }
150
+
151
+ @keyframes glow {
152
+ from { text-shadow: 0 0 5px #ff00de, 0 0 10px #ff00de; }
153
+ to { text-shadow: 0 0 10px #ff00de, 0 0 20px #ff00de; }
154
  }
155
+
156
+ @media (max-width: 768px) {
157
+ .title-text { font-size: 1.8em; }
158
+ .subtitle-text { font-size: 1em; }
159
+ .image-container { padding: 10px; }
160
+ }
161
+ """) as demo:
 
 
 
 
162
  gr.Markdown("""
163
  <h1 class="title-text">AI Background Removal</h1>
164
  <p class="subtitle-text">Remove backgrounds instantly using advanced AI technology</p>
 
184
  visible=True,
185
  elem_classes="download-btn"
186
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
+ auto_download = gr.HTML(visible=False)
 
 
 
 
189
 
190
  input_image.change(
191
  fn=process,
192
  inputs=input_image,
193
+ outputs=[output_image, download_button, auto_download]
 
194
  )
195
 
196
  if __name__ == "__main__":