sidbhasin commited on
Commit
e67f455
·
verified ·
1 Parent(s): 6647c19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -70
app.py CHANGED
@@ -8,121 +8,149 @@ import io
8
  def remove_background(input_image):
9
  try:
10
  # Initialize the pipeline with trust_remote_code=True
11
- segmentor = pipeline("image-segmentation",
12
- model="briaai/RMBG-1.4",
13
- device=-1,
14
- trust_remote_code=True)
 
 
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
  input_image = gr.Image(
78
- label="Upload Image",
79
  type="pil",
80
- sources=["upload", "clipboard"],
81
- interactive=True
82
  )
83
 
 
 
 
 
 
84
  with gr.Column():
85
  output_image = gr.Image(
86
  label="Result",
87
  type="pil",
88
- interactive=False
89
  )
90
-
91
- with gr.Row():
92
- clear_btn = gr.Button("Clear", variant="secondary")
93
- process_btn = gr.Button("Remove Background", variant="primary")
94
-
95
- # Status message for feedback
96
- status_msg = gr.Textbox(label="Status", interactive=False, visible=False)
97
 
98
- # Event handlers with concurrency limit
99
  def process_and_update(image):
100
  if image is None:
101
- return None, "Please upload an image first"
102
  try:
103
  result = remove_background(image)
104
- return result, "Background removed successfully!"
 
 
 
 
105
  except Exception as e:
106
- return None, f"Error: {str(e)}"
107
 
108
  process_btn.click(
109
  fn=process_and_update,
110
  inputs=[input_image],
111
- outputs=[output_image, status_msg],
112
- concurrency_limit=1, # Limit concurrent processing
113
- show_progress=True # Show processing progress
114
  )
115
 
116
  clear_btn.click(
117
- fn=lambda: (None, None, ""),
118
- outputs=[input_image, output_image, status_msg],
119
- queue=False # No need for queue in clear operation
120
  )
121
 
122
- # Launch the app with updated parameters
123
- demo.launch(
124
- share=True,
125
- max_threads=1, # Control maximum worker threads
126
- show_error=True,
127
- server_name="0.0.0.0"
128
- )
 
 
 
 
8
  def remove_background(input_image):
9
  try:
10
  # Initialize the pipeline with trust_remote_code=True
11
+ segmentor = pipeline(
12
+ "image-segmentation",
13
+ model="briaai/RMBG-1.4",
14
+ trust_remote_code=True,
15
+ device="cpu"
16
+ )
17
 
18
+ # Process the image and get result
19
+ result = segmentor(input_image, return_mask=True)
20
+
21
+ # Convert result to RGBA
22
+ if isinstance(result, Image.Image):
23
+ # Create transparent background
24
+ output = Image.new('RGBA', result.size, (0, 0, 0, 0))
25
+ output.paste(input_image, mask=result)
26
+ else:
27
+ output = result['output_image']
28
+
29
+ return output
30
+
31
  except Exception as e:
32
  raise gr.Error(f"Error processing image: {str(e)}")
33
 
34
+ # Custom theme and styling
35
+ theme = gr.themes.Soft(
36
+ primary_hue="gold",
37
+ secondary_hue="orange",
38
+ ).set(
39
+ body_background_fill="linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%)",
40
+ body_text_color="#ffffff",
41
+ button_primary_background_fill="linear-gradient(45deg, #FFD700, #FFA500)",
42
+ button_primary_text_color="#000000",
43
+ border_color_primary="#FFD700"
44
+ )
45
+
46
  css = """
47
  .gradio-container {
48
+ max-width: 1200px !important;
49
+ margin: 0 auto !important;
50
+ padding: 20px !important;
 
 
 
 
 
51
  }
52
  .image-container {
53
+ border-radius: 15px !important;
54
+ border: 2px solid rgba(255, 215, 0, 0.3) !important;
55
+ padding: 10px !important;
56
+ background: rgba(255, 255, 255, 0.1) !important;
57
+ transition: transform 0.3s ease !important;
58
+ }
59
+ .image-container:hover {
60
+ transform: translateY(-5px) !important;
61
  }
62
  .gr-button {
63
+ min-width: 200px !important;
64
+ height: 45px !important;
65
+ font-size: 16px !important;
66
+ margin: 10px !important;
67
+ transition: all 0.3s ease !important;
68
+ }
69
+ .gr-button:hover {
70
+ transform: translateY(-2px) !important;
71
+ box-shadow: 0 5px 15px rgba(255, 215, 0, 0.3) !important;
72
  }
73
+ .footer {
74
+ text-align: center;
75
+ margin-top: 20px;
76
+ color: #666;
 
 
 
77
  }
78
  """
79
 
80
  # Create Gradio interface
81
+ with gr.Blocks(theme=theme, css=css) as demo:
82
  gr.HTML(
83
  """
84
+ <div style="text-align: center; margin-bottom: 2rem;">
85
+ <h1 style="font-size: 3rem; margin-bottom: 1rem; background: linear-gradient(45deg, #FFD700, #FFA500); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
86
+ AI Background Remover Pro
87
  </h1>
88
+ <p style="color: #cccccc; font-size: 1.2rem;">
89
+ Remove backgrounds instantly using advanced AI technology
90
  </p>
91
  </div>
92
  """
93
  )
94
 
95
+ with gr.Row():
96
  with gr.Column():
97
  input_image = gr.Image(
98
+ label="Upload Your Image",
99
  type="pil",
100
+ elem_classes="image-container"
 
101
  )
102
 
103
+ with gr.Row():
104
+ clear_btn = gr.Button("Clear", variant="secondary")
105
+ process_btn = gr.Button("Remove Background", variant="primary")
106
+ download_btn = gr.Button("Download", variant="primary", visible=False)
107
+
108
  with gr.Column():
109
  output_image = gr.Image(
110
  label="Result",
111
  type="pil",
112
+ elem_classes="image-container"
113
  )
114
+
115
+ # Status message
116
+ status_msg = gr.Textbox(
117
+ label="Status",
118
+ placeholder="Ready to process your image...",
119
+ interactive=False
120
+ )
121
 
122
+ # Event handlers
123
  def process_and_update(image):
124
  if image is None:
125
+ return None, "Please upload an image first", gr.Button.update(visible=False)
126
  try:
127
  result = remove_background(image)
128
+ return (
129
+ result,
130
+ "✨ Background removed successfully!",
131
+ gr.Button.update(visible=True)
132
+ )
133
  except Exception as e:
134
+ return None, f"Error: {str(e)}", gr.Button.update(visible=False)
135
 
136
  process_btn.click(
137
  fn=process_and_update,
138
  inputs=[input_image],
139
+ outputs=[output_image, status_msg, download_btn],
 
 
140
  )
141
 
142
  clear_btn.click(
143
+ fn=lambda: (None, None, "Ready to process your image...", gr.Button.update(visible=False)),
144
+ outputs=[input_image, output_image, status_msg, download_btn],
 
145
  )
146
 
147
+ gr.HTML(
148
+ """
149
+ <div class="footer">
150
+ <p>Powered by BRIA AI's RMBG V1.4 Model</p>
151
+ </div>
152
+ """
153
+ )
154
+
155
+ # Launch the app
156
+ demo.launch()