Jonny001 commited on
Commit
95fb114
·
verified ·
1 Parent(s): 4aeaed8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -473
app.py CHANGED
@@ -12,7 +12,7 @@ torch.set_float32_matmul_precision(["high", "highest"][0])
12
  birefnet = AutoModelForImageSegmentation.from_pretrained(
13
  "ZhengPeng7/BiRefNet", trust_remote_code=True
14
  )
15
- birefnet.to("cpu")
16
 
17
  transform_image = transforms.Compose(
18
  [
@@ -32,7 +32,8 @@ def fn(image: Union[Image.Image, str]) -> Tuple[Image.Image, Image.Image]:
32
  @spaces.GPU
33
  def process(image: Image.Image) -> Image.Image:
34
  image_size = image.size
35
- input_images = transform_image(image).unsqueeze(0).to("cpu")
 
36
  with torch.no_grad():
37
  preds = birefnet(input_images)[-1].sigmoid().cpu()
38
  pred = preds[0].squeeze()
@@ -41,478 +42,17 @@ def process(image: Image.Image) -> Image.Image:
41
  image.putalpha(mask)
42
  return image
43
 
44
- custom_css = """
45
- @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&family=Rajdhani:wght@300;400;500;600;700&display=swap');
 
 
46
 
47
- :root {
48
- --neon-blue: #00f3ff;
49
- --neon-pink: #ff00ff;
50
- --neon-green: #00ff9d;
51
- --neon-purple: #8a2be2;
52
- --dark-bg: #0a0a12;
53
- --card-bg: rgba(20, 20, 30, 0.95);
54
- }
55
 
56
- * {
57
- margin: 0;
58
- padding: 0;
59
- box-sizing: border-box;
60
- }
61
-
62
- body {
63
- background: var(--dark-bg) !important;
64
- font-family: 'Rajdhani', sans-serif !important;
65
- overflow-x: hidden;
66
- }
67
-
68
- .gradio-container {
69
- background: linear-gradient(135deg, #0a0a12 0%, #000000 100%) !important;
70
- min-height: 100vh;
71
- color: #ffffff !important;
72
- }
73
-
74
- /* Neon Glow Effects */
75
- .neon-glow {
76
- text-shadow:
77
- 0 0 5px currentColor,
78
- 0 0 10px currentColor,
79
- 0 0 15px currentColor,
80
- 0 0 20px currentColor;
81
- }
82
-
83
- .neon-border {
84
- box-shadow:
85
- 0 0 5px currentColor,
86
- 0 0 10px currentColor,
87
- 0 0 15px currentColor,
88
- inset 0 0 5px currentColor;
89
- }
90
-
91
- /* Header Styles with Neon */
92
- .header-container {
93
- text-align: center;
94
- padding: 50px 20px 40px 20px;
95
- background: linear-gradient(180deg, rgba(10,10,18,0.95) 0%, rgba(10,10,18,0.8) 100%);
96
- border-bottom: 3px solid var(--neon-blue);
97
- margin-bottom: 20px;
98
- position: relative;
99
- }
100
-
101
- .header-container::before {
102
- content: '';
103
- position: absolute;
104
- top: 0;
105
- left: 0;
106
- right: 0;
107
- bottom: 0;
108
- background: linear-gradient(45deg,
109
- transparent 0%,
110
- rgba(0, 243, 255, 0.1) 50%,
111
- transparent 100%);
112
- animation: shimmer 3s ease-in-out infinite;
113
- }
114
-
115
- @keyframes shimmer {
116
- 0%, 100% { opacity: 0.3; }
117
- 50% { opacity: 0.8; }
118
- }
119
-
120
- .main-title {
121
- font-family: 'Orbitron', monospace !important;
122
- font-weight: 900 !important;
123
- font-size: 3.5rem !important;
124
- background: linear-gradient(45deg, var(--neon-blue), var(--neon-pink), var(--neon-green)) !important;
125
- -webkit-background-clip: text !important;
126
- -webkit-text-fill-color: transparent !important;
127
- margin-bottom: 15px !important;
128
- letter-spacing: 3px !important;
129
- text-transform: uppercase;
130
- position: relative;
131
- text-shadow:
132
- 0 0 10px var(--neon-blue),
133
- 0 0 20px var(--neon-pink),
134
- 0 0 30px var(--neon-green) !important;
135
- }
136
-
137
- .subtitle {
138
- font-family: 'Orbitron', monospace !important;
139
- font-size: 1.4rem !important;
140
- color: var(--neon-green) !important;
141
- letter-spacing: 2px !important;
142
- text-shadow:
143
- 0 0 10px var(--neon-green),
144
- 0 0 20px rgba(0, 255, 157, 0.5) !important;
145
- }
146
-
147
- /* Tab Styles with Neon */
148
- .tab-nav {
149
- background: rgba(20, 20, 30, 0.9) !important;
150
- border: 2px solid var(--neon-blue) !important;
151
- border-radius: 15px !important;
152
- padding: 10px !important;
153
- margin: 30px auto !important;
154
- max-width: 550px !important;
155
- box-shadow:
156
- 0 0 20px rgba(0, 243, 255, 0.3),
157
- inset 0 0 20px rgba(0, 243, 255, 0.1) !important;
158
- }
159
-
160
- .tab-nav button {
161
- font-family: 'Orbitron', monospace !important;
162
- font-weight: 700 !important;
163
- font-size: 1rem !important;
164
- background: transparent !important;
165
- color: var(--neon-blue) !important;
166
- border: 2px solid transparent !important;
167
- border-radius: 10px !important;
168
- padding: 15px 25px !important;
169
- margin: 0 5px !important;
170
- transition: all 0.4s ease !important;
171
- text-transform: uppercase;
172
- letter-spacing: 1.5px;
173
- position: relative;
174
- overflow: hidden;
175
- }
176
-
177
- .tab-nav button::before {
178
- content: '';
179
- position: absolute;
180
- top: 0;
181
- left: -100%;
182
- width: 100%;
183
- height: 100%;
184
- background: linear-gradient(90deg, transparent, rgba(0, 243, 255, 0.4), transparent);
185
- transition: left 0.6s ease;
186
- }
187
-
188
- .tab-nav button:hover::before {
189
- left: 100%;
190
- }
191
-
192
- .tab-nav button.selected {
193
- background: rgba(0, 243, 255, 0.15) !important;
194
- border: 2px solid var(--neon-blue) !important;
195
- box-shadow:
196
- 0 0 20px var(--neon-blue),
197
- 0 0 40px rgba(0, 243, 255, 0.3) !important;
198
- color: var(--neon-blue) !important;
199
- text-shadow: 0 0 10px currentColor !important;
200
- }
201
-
202
- .tab-nav button:hover:not(.selected) {
203
- border: 2px solid var(--neon-pink) !important;
204
- color: var(--neon-pink) !important;
205
- box-shadow: 0 0 15px var(--neon-pink) !important;
206
- text-shadow: 0 0 10px currentColor !important;
207
- }
208
-
209
- /* Content Cards with Neon */
210
- .tabitem {
211
- background: var(--card-bg) !important;
212
- border: 2px solid rgba(0, 243, 255, 0.3) !important;
213
- border-radius: 20px !important;
214
- margin: 25px 0 !important;
215
- padding: 30px !important;
216
- box-shadow:
217
- 0 0 30px rgba(0, 243, 255, 0.2),
218
- inset 0 0 30px rgba(0, 243, 255, 0.05) !important;
219
- }
220
-
221
- /* Input Components with Neon */
222
- .gr-image {
223
- background: rgba(0, 0, 0, 0.4) !important;
224
- border: 3px dashed var(--neon-blue) !important;
225
- border-radius: 15px !important;
226
- min-height: 320px !important;
227
- transition: all 0.3s ease !important;
228
- }
229
-
230
- .gr-image:hover {
231
- border-color: var(--neon-pink) !important;
232
- box-shadow: 0 0 25px rgba(255, 0, 255, 0.3) !important;
233
- }
234
-
235
- .gr-textbox {
236
- background: rgba(0, 0, 0, 0.4) !important;
237
- border: 2px solid var(--neon-blue) !important;
238
- border-radius: 12px !important;
239
- color: #ffffff !important;
240
- font-size: 1.1rem !important;
241
- padding: 18px !important;
242
- transition: all 0.3s ease !important;
243
- }
244
-
245
- .gr-textbox:focus {
246
- border-color: var(--neon-pink) !important;
247
- box-shadow: 0 0 20px rgba(255, 0, 255, 0.3) !important;
248
- }
249
-
250
- .gr-textbox::placeholder {
251
- color: rgba(0, 243, 255, 0.7) !important;
252
- font-style: italic;
253
- }
254
-
255
- /* Labels with Neon */
256
- .label {
257
- font-family: 'Orbitron', monospace !important;
258
- font-weight: 700 !important;
259
- font-size: 1.3rem !important;
260
- color: var(--neon-green) !important;
261
- margin-bottom: 20px !important;
262
- letter-spacing: 1px !important;
263
- text-shadow:
264
- 0 0 10px var(--neon-green),
265
- 0 0 20px rgba(0, 255, 157, 0.3) !important;
266
- }
267
-
268
- /* NEON BUTTONS - Improved */
269
- .neon-button {
270
- font-family: 'Orbitron', monospace !important;
271
- font-weight: 800 !important;
272
- font-size: 1.2rem !important;
273
- background: linear-gradient(45deg, var(--neon-blue), var(--neon-green)) !important;
274
- color: #000000 !important;
275
- border: none !important;
276
- border-radius: 12px !important;
277
- padding: 18px 35px !important;
278
- margin: 15px 0 !important;
279
- transition: all 0.4s ease !important;
280
- text-transform: uppercase !important;
281
- letter-spacing: 2px !important;
282
- position: relative !important;
283
- overflow: hidden !important;
284
- cursor: pointer !important;
285
- box-shadow:
286
- 0 0 15px var(--neon-blue),
287
- 0 0 30px rgba(0, 243, 255, 0.4) !important;
288
- }
289
-
290
- .neon-button::before {
291
- content: '';
292
- position: absolute;
293
- top: 0;
294
- left: -100%;
295
- width: 100%;
296
- height: 100%;
297
- background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
298
- transition: left 0.6s ease;
299
- }
300
-
301
- .neon-button:hover::before {
302
- left: 100%;
303
- }
304
-
305
- .neon-button:hover {
306
- transform: translateY(-3px) scale(1.05) !important;
307
- box-shadow:
308
- 0 0 25px var(--neon-blue),
309
- 0 0 50px rgba(0, 243, 255, 0.6),
310
- 0 0 75px rgba(0, 255, 157, 0.3) !important;
311
- background: linear-gradient(45deg, var(--neon-green), var(--neon-pink)) !important;
312
- }
313
-
314
- .neon-button:active {
315
- transform: translateY(-1px) scale(1.02) !important;
316
- }
317
-
318
- /* Image Slider with Neon */
319
- .image-slider-container {
320
- border: 3px solid var(--neon-purple) !important;
321
- border-radius: 15px !important;
322
- padding: 15px !important;
323
- background: rgba(0, 0, 0, 0.3) !important;
324
- box-shadow:
325
- 0 0 20px rgba(138, 43, 226, 0.4),
326
- inset 0 0 20px rgba(138, 43, 226, 0.1) !important;
327
- }
328
-
329
- /* Markdown Text with Neon */
330
- .gr-markdown {
331
- color: #ffffff !important;
332
- font-family: 'Rajdhani', sans-serif !important;
333
- font-size: 1.1rem !important;
334
- line-height: 1.7 !important;
335
- }
336
-
337
- .gr-markdown h3 {
338
- font-family: 'Orbitron', monospace !important;
339
- font-size: 1.5rem !important;
340
- color: var(--neon-blue) !important;
341
- margin: 25px 0 20px 0 !important;
342
- border-left: 4px solid var(--neon-pink) !important;
343
- padding-left: 20px !important;
344
- text-shadow: 0 0 10px rgba(0, 243, 255, 0.5) !important;
345
- }
346
-
347
- /* Footer with Neon */
348
- .footer {
349
- text-align: center;
350
- padding: 40px 20px;
351
- color: var(--neon-blue) !important;
352
- font-family: 'Rajdhani', sans-serif !important;
353
- border-top: 3px solid rgba(0, 243, 255, 0.3);
354
- margin-top: 40px;
355
- background: rgba(10, 10, 18, 0.9) !important;
356
- }
357
-
358
- .footer h3 {
359
- font-family: 'Orbitron', monospace !important;
360
- font-size: 1.4rem !important;
361
- margin-bottom: 15px !important;
362
- color: var(--neon-green) !important;
363
- text-shadow: 0 0 10px rgba(0, 255, 157, 0.5) !important;
364
- }
365
-
366
- .footer p {
367
- margin: 8px 0 !important;
368
- font-size: 1rem !important;
369
- opacity: 0.9;
370
- }
371
-
372
- /* Layout Improvements */
373
- .gr-row {
374
- gap: 30px !important;
375
- margin: 0 !important;
376
- }
377
-
378
- .gr-column {
379
- gap: 25px !important;
380
- }
381
-
382
- /* Scrollbar Neon */
383
- ::-webkit-scrollbar {
384
- width: 10px;
385
- }
386
-
387
- ::-webkit-scrollbar-track {
388
- background: rgba(0, 243, 255, 0.1);
389
- border-radius: 5px;
390
- }
391
-
392
- ::-webkit-scrollbar-thumb {
393
- background: linear-gradient(45deg, var(--neon-blue), var(--neon-pink));
394
- border-radius: 5px;
395
- box-shadow: 0 0 10px rgba(0, 243, 255, 0.5);
396
- }
397
-
398
- ::-webkit-scrollbar-thumb:hover {
399
- background: linear-gradient(45deg, var(--neon-pink), var(--neon-green));
400
- }
401
-
402
- /* Responsive Design */
403
- @media (max-width: 768px) {
404
- .main-title {
405
- font-size: 2.5rem !important;
406
- }
407
-
408
- .subtitle {
409
- font-size: 1.1rem !important;
410
- }
411
-
412
- .tab-nav {
413
- max-width: 100% !important;
414
- margin: 20px 10px !important;
415
- }
416
-
417
- .tab-nav button {
418
- font-size: 0.9rem !important;
419
- padding: 12px 18px !important;
420
- }
421
-
422
- .neon-button {
423
- font-size: 1.1rem !important;
424
- padding: 15px 25px !important;
425
- }
426
- }
427
- """
428
-
429
- with gr.Blocks(css=custom_css, theme=gr.themes.Base(primary_hue="blue")) as demo:
430
-
431
- # Header with Neon
432
- gr.HTML("""
433
- <div class="header-container">
434
- <div class="main-title">⚡ CYBER-BG REMOVER ⚡</div>
435
- <div class="subtitle">NEON-POWERED AI BACKGROUND ERASURE</div>
436
- </div>
437
- """)
438
-
439
- with gr.Tabs(selected=0) as tabs:
440
- with gr.TabItem("🖼️ UPLOAD IMAGE", id=0):
441
- with gr.Row():
442
- with gr.Column(scale=1):
443
- gr.Markdown("### 📤 UPLOAD IMAGE")
444
- image_upload = gr.Image(
445
- label="DRAG & DROP OR CLICK TO UPLOAD",
446
- type="pil",
447
- height=340
448
- )
449
- process_btn1 = gr.Button(
450
- "🚀 REMOVE BACKGROUND",
451
- elem_classes="neon-button"
452
- )
453
-
454
- with gr.Column(scale=1):
455
- gr.Markdown("### 🎯 PROCESSED RESULT")
456
- with gr.Column(elem_classes="image-slider-container"):
457
- slider1 = gr.ImageSlider(
458
- label="ORIGINAL VS PROCESSED",
459
- type="pil",
460
- format="png",
461
- height=360,
462
- show_download_button=True
463
- )
464
-
465
- process_btn1.click(
466
- fn=fn,
467
- inputs=image_upload,
468
- outputs=slider1
469
- )
470
-
471
- with gr.TabItem("🌐 IMAGE URL", id=1):
472
- with gr.Row():
473
- with gr.Column(scale=1):
474
- gr.Markdown("### 🔗 ENTER IMAGE URL")
475
- url_input = gr.Textbox(
476
- label="PASTE IMAGE URL HERE",
477
- placeholder="https://example.com/image.jpg",
478
- lines=1,
479
- max_lines=1
480
- )
481
- process_btn2 = gr.Button(
482
- "🚀 REMOVE BACKGROUND",
483
- elem_classes="neon-button"
484
- )
485
-
486
- with gr.Column(scale=1):
487
- gr.Markdown("### 🎯 PROCESSED RESULT")
488
- with gr.Column(elem_classes="image-slider-container"):
489
- slider2 = gr.ImageSlider(
490
- label="ORIGINAL VS PROCESSED",
491
- type="pil",
492
- format="png",
493
- height=360,
494
- show_download_button=True
495
- )
496
-
497
- process_btn2.click(
498
- fn=fn,
499
- inputs=url_input,
500
- outputs=slider2
501
- )
502
-
503
- # Footer with Neon
504
- gr.HTML("""
505
- <div class="footer">
506
- <h3>🛠️ SYSTEM STATUS: ONLINE</h3>
507
- <p><strong>BiRefNet AI Core:</strong> OPERATIONAL</p>
508
- <p><strong>Neon Processing Unit:</strong> ACTIVE</p>
509
- <p><em>Cyber-BG Remover v3.0 - Ultimate Neon Edition</em></p>
510
- </div>
511
- """)
512
 
513
  if __name__ == "__main__":
514
- demo.launch(
515
- show_error=True,
516
- show_api=False,
517
- share=False
518
- )
 
12
  birefnet = AutoModelForImageSegmentation.from_pretrained(
13
  "ZhengPeng7/BiRefNet", trust_remote_code=True
14
  )
15
+ birefnet.to("cuda")
16
 
17
  transform_image = transforms.Compose(
18
  [
 
32
  @spaces.GPU
33
  def process(image: Image.Image) -> Image.Image:
34
  image_size = image.size
35
+ input_images = transform_image(image).unsqueeze(0).to("cuda")
36
+ # Prediction
37
  with torch.no_grad():
38
  preds = birefnet(input_images)[-1].sigmoid().cpu()
39
  pred = preds[0].squeeze()
 
42
  image.putalpha(mask)
43
  return image
44
 
45
+ slider1 = gr.ImageSlider(label="Processed Image", type="pil", format="png")
46
+ slider2 = gr.ImageSlider(label="Processed Image from URL", type="pil", format="png")
47
+ image_upload = gr.Image(label="Upload an image")
48
+ url_input = gr.Textbox(label="Paste an image URL")
49
 
50
+ tab1 = gr.Interface(fn, inputs=image_upload, outputs=slider1, api_name="image")
51
+ tab2 = gr.Interface(fn, inputs=url_input, outputs=slider2, api_name="text")
 
 
 
 
 
 
52
 
53
+ demo = gr.TabbedInterface(
54
+ [tab1, tab2], ["Image Upload", "URL Input"], title="Background Removal Tool"
55
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  if __name__ == "__main__":
58
+ demo.launch(show_error=True)