sidbhasin commited on
Commit
9ca621b
1 Parent(s): e67f455

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -84
app.py CHANGED
@@ -15,26 +15,17 @@ def remove_background(input_image):
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",
@@ -43,50 +34,16 @@ theme = gr.themes.Soft(
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
  """
@@ -95,23 +52,21 @@ with gr.Blocks(theme=theme, css=css) as demo:
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",
@@ -122,34 +77,22 @@ with gr.Blocks(theme=theme, css=css) as demo:
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
 
15
  device="cpu"
16
  )
17
 
18
+ # Process the image
19
+ result = segmentor(input_image)
20
+ return result['output_image']
 
 
 
 
 
 
 
 
 
 
21
  except Exception as e:
22
  raise gr.Error(f"Error processing image: {str(e)}")
23
 
24
+ # Custom theme with valid colors
25
  theme = gr.themes.Soft(
26
+ primary_hue="orange", # Changed from "gold" to "orange"
27
+ secondary_hue="blue",
28
+ neutral_hue="gray"
29
  ).set(
30
  body_background_fill="linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%)",
31
  body_text_color="#ffffff",
 
34
  border_color_primary="#FFD700"
35
  )
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  # Create Gradio interface
38
+ with gr.Blocks(theme=theme) as demo:
39
  gr.HTML(
40
  """
41
+ <div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
42
+ <h1 style="font-size: 2.5rem; margin-bottom: 1rem; background: linear-gradient(45deg, #FFD700, #FFA500); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
43
+ AI Background Remover
44
  </h1>
45
+ <p style="color: #cccccc; font-size: 1.2rem; margin-bottom: 2rem;">
46
+ Remove backgrounds instantly using RMBG V1.4 model
47
  </p>
48
  </div>
49
  """
 
52
  with gr.Row():
53
  with gr.Column():
54
  input_image = gr.Image(
55
+ label="Upload Image",
56
  type="pil",
57
+ sources=["upload", "clipboard"]
58
  )
59
 
 
 
 
 
 
60
  with gr.Column():
61
  output_image = gr.Image(
62
  label="Result",
63
+ type="pil"
 
64
  )
65
+
66
+ with gr.Row():
67
+ clear_btn = gr.Button("Clear", variant="secondary")
68
+ process_btn = gr.Button("Remove Background", variant="primary")
69
+
70
  # Status message
71
  status_msg = gr.Textbox(
72
  label="Status",
 
77
  # Event handlers
78
  def process_and_update(image):
79
  if image is None:
80
+ return None, "Please upload an image first"
81
  try:
82
  result = remove_background(image)
83
+ return result, "✨ Background removed successfully!"
 
 
 
 
84
  except Exception as e:
85
+ return None, f"❌ Error: {str(e)}"
86
 
87
  process_btn.click(
88
  fn=process_and_update,
89
  inputs=[input_image],
90
+ outputs=[output_image, status_msg],
91
  )
92
 
93
  clear_btn.click(
94
+ fn=lambda: (None, None, "Ready to process your image..."),
95
+ outputs=[input_image, output_image, status_msg],
 
 
 
 
 
 
 
 
96
  )
97
 
98
  # Launch the app