sidbhasin commited on
Commit
36588be
1 Parent(s): 855a559

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -96
app.py CHANGED
@@ -4,7 +4,6 @@ import torch
4
  import numpy as np
5
  from PIL import Image
6
  import io
7
- import base64
8
 
9
  def remove_background(input_image):
10
  try:
@@ -16,151 +15,115 @@ def remove_background(input_image):
16
 
17
  # Process the image
18
  result = segmentor(input_image)
19
-
20
- # Convert the output image to PNG format
21
- if isinstance(result['output_image'], Image.Image):
22
- output = result['output_image']
23
- else:
24
- output = Image.fromarray(result['output_image'])
25
-
26
- return output, output
27
-
28
  except Exception as e:
29
  raise gr.Error(f"Error processing image: {str(e)}")
30
 
31
- def save_image(image):
32
- if image is None:
33
- return None
34
- try:
35
- # Save image to bytes
36
- buffer = io.BytesIO()
37
- image.save(buffer, format="PNG")
38
- return buffer.getvalue()
39
- except Exception as e:
40
- raise gr.Error(f"Error saving image: {str(e)}")
41
-
42
  css = """
43
  .gradio-container {
44
  font-family: 'Segoe UI', sans-serif;
45
- background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
 
46
  }
47
- .gr-image {
48
- border-radius: 12px;
49
- border: 2px solid rgba(255, 215, 0, 0.3);
50
- transition: transform 0.3s ease;
51
  }
52
- .gr-image:hover {
53
- transform: scale(1.02);
 
 
54
  }
55
  .gr-button {
56
  background: linear-gradient(45deg, #FFD700, #FFA500);
57
- border: none;
58
- color: black;
59
- padding: 10px 20px;
60
- border-radius: 8px;
61
- font-weight: bold;
62
- transition: all 0.3s ease;
63
- }
64
- .gr-button:hover {
65
- transform: translateY(-2px);
66
- box-shadow: 0 4px 12px rgba(255, 215, 0, 0.3);
67
  }
68
- .container {
69
- max-width: 1200px;
70
- margin: 0 auto;
71
- padding: 2rem;
72
- }
73
- .status-msg {
74
- text-align: center;
75
- margin: 1rem 0;
76
- padding: 0.5rem;
77
- border-radius: 8px;
78
- }
79
- .success {
80
- background: rgba(0, 255, 0, 0.1);
81
- color: #00ff00;
82
- }
83
- .error {
84
- background: rgba(255, 0, 0, 0.1);
85
- color: #ff0000;
86
  }
87
  """
88
 
 
89
  with gr.Blocks(css=css) as demo:
90
  gr.HTML(
91
  """
92
  <div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
93
- <h1 style="font-size: 2.5rem; margin-bottom: 1rem; background: linear-gradient(45deg, #FFD700, #FFA500); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
94
  AI Background Remover
95
  </h1>
96
- <p style="color: #cccccc; font-size: 1.1rem; margin-bottom: 2rem;">
97
- Powered by RMBG V1.4 model from BRIA AI
98
  </p>
99
  </div>
100
  """
101
  )
102
 
103
- with gr.Row():
104
  with gr.Column():
 
105
  input_image = gr.Image(
106
  label="Upload Image",
107
  type="pil",
108
  sources=["upload", "clipboard"],
109
- tool="select"
110
  )
111
 
112
  with gr.Column():
113
  output_image = gr.Image(
114
  label="Result",
115
- type="pil"
 
116
  )
117
- download_btn = gr.Button("Download Result", visible=False)
118
 
119
  with gr.Row():
120
- clear_btn = gr.Button("Clear")
121
  process_btn = gr.Button("Remove Background", variant="primary")
122
-
123
- status = gr.Textbox(label="Status", visible=False)
 
124
 
125
  # Event handlers
126
- def process_and_show(image):
127
  if image is None:
128
- raise gr.Error("Please upload an image first")
129
  try:
130
- output, download = remove_background(image)
131
- download_btn.visible = True
132
- status.visible = True
133
- status.value = "✅ Background removed successfully!"
134
- return output, download, True, status
135
  except Exception as e:
136
- status.visible = True
137
- status.value = f"❌ Error: {str(e)}"
138
- return None, None, False, status
139
-
140
- def clear_all():
141
- return {
142
- input_image: None,
143
- output_image: None,
144
- download_btn: gr.Button(visible=False),
145
- status: gr.Textbox(visible=False)
146
- }
147
 
148
  process_btn.click(
149
- fn=process_and_show,
150
  inputs=[input_image],
151
- outputs=[output_image, output_image, download_btn, status]
 
152
  )
153
 
154
  clear_btn.click(
155
- fn=clear_all,
156
- outputs=[input_image, output_image, download_btn, status]
157
- )
158
-
159
- # Download functionality
160
- download_btn.click(
161
- fn=save_image,
162
- inputs=[output_image],
163
- outputs=[gr.File(label="Download")]
164
  )
165
 
166
- demo.queue().launch()
 
 
 
 
 
 
 
 
4
  import numpy as np
5
  from PIL import Image
6
  import io
 
7
 
8
  def remove_background(input_image):
9
  try:
 
15
 
16
  # Process the image
17
  result = segmentor(input_image)
18
+ return result['output_image']
 
 
 
 
 
 
 
 
19
  except Exception as e:
20
  raise gr.Error(f"Error processing image: {str(e)}")
21
 
22
+ # Custom CSS for mobile-friendly design
 
 
 
 
 
 
 
 
 
 
23
  css = """
24
  .gradio-container {
25
  font-family: 'Segoe UI', sans-serif;
26
+ max-width: 100% !important;
27
+ padding: 10px !important;
28
  }
29
+ .container {
30
+ display: flex;
31
+ flex-direction: column;
32
+ gap: 20px;
33
  }
34
+ .image-container {
35
+ width: 100%;
36
+ max-width: 500px;
37
+ margin: 0 auto;
38
  }
39
  .gr-button {
40
  background: linear-gradient(45deg, #FFD700, #FFA500);
41
+ border: none !important;
42
+ color: black !important;
43
+ padding: 12px 20px !important;
44
+ border-radius: 8px !important;
45
+ font-weight: bold !important;
46
+ margin: 10px 0 !important;
47
+ width: 100% !important;
48
+ max-width: 300px !important;
 
 
49
  }
50
+ @media (max-width: 768px) {
51
+ .gradio-container {
52
+ padding: 5px !important;
53
+ }
54
+ .gr-button {
55
+ padding: 10px 15px !important;
56
+ }
 
 
 
 
 
 
 
 
 
 
 
57
  }
58
  """
59
 
60
+ # Create Gradio interface
61
  with gr.Blocks(css=css) as demo:
62
  gr.HTML(
63
  """
64
  <div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
65
+ <h1 style="font-size: 2rem; margin-bottom: 1rem; background: linear-gradient(45deg, #FFD700, #FFA500); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
66
  AI Background Remover
67
  </h1>
68
+ <p style="color: #666; font-size: 1rem; margin-bottom: 2rem;">
69
+ Powered by RMBG V1.4 model
70
  </p>
71
  </div>
72
  """
73
  )
74
 
75
+ with gr.Row(equal_height=True):
76
  with gr.Column():
77
+ # Removed 'tool' parameter and added mobile-friendly settings
78
  input_image = gr.Image(
79
  label="Upload Image",
80
  type="pil",
81
  sources=["upload", "clipboard"],
82
+ interactive=True
83
  )
84
 
85
  with gr.Column():
86
  output_image = gr.Image(
87
  label="Result",
88
+ type="pil",
89
+ interactive=False
90
  )
 
91
 
92
  with gr.Row():
93
+ clear_btn = gr.Button("Clear", variant="secondary")
94
  process_btn = gr.Button("Remove Background", variant="primary")
95
+
96
+ # Status message for feedback
97
+ status_msg = gr.Textbox(label="Status", interactive=False, visible=False)
98
 
99
  # Event handlers
100
+ def process_and_update(image):
101
  if image is None:
102
+ return None, "Please upload an image first"
103
  try:
104
+ result = remove_background(image)
105
+ return result, "Background removed successfully!"
 
 
 
106
  except Exception as e:
107
+ return None, f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
108
 
109
  process_btn.click(
110
+ fn=process_and_update,
111
  inputs=[input_image],
112
+ outputs=[output_image, status_msg],
113
+ api_name="remove_background"
114
  )
115
 
116
  clear_btn.click(
117
+ fn=lambda: (None, None, ""),
118
+ outputs=[input_image, output_image, status_msg],
119
+ api_name="clear"
 
 
 
 
 
 
120
  )
121
 
122
+ # Launch the app
123
+ demo.launch(
124
+ share=True,
125
+ enable_queue=True,
126
+ show_error=True,
127
+ server_port=7860,
128
+ server_name="0.0.0.0"
129
+ )