sidbhasin commited on
Commit
6128b5a
·
verified ·
1 Parent(s): 9f15a49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -7,42 +7,38 @@ import io
7
 
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
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",
32
- button_primary_background_fill="linear-gradient(45deg, #FFD700, #FFA500)",
33
- button_primary_text_color="#000000",
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>
 
7
 
8
  def remove_background(input_image):
9
  try:
10
+ # Initialize the pipeline with correct parameters
11
  segmentor = pipeline(
12
+ task="image-segmentation",
13
  model="briaai/RMBG-1.4",
14
+ trust_remote_code=True
 
15
  )
16
 
17
+ # Convert input to PIL Image if it's not already
18
+ if not isinstance(input_image, Image.Image):
19
+ input_image = Image.fromarray(input_image)
20
+
21
  # Process the image
22
+ result = segmentor(input_image, return_mask=True)
23
+
24
+ # Create transparent background image
25
+ output_image = Image.new('RGBA', input_image.size, (0, 0, 0, 0))
26
+ output_image.paste(input_image, mask=result)
27
+
28
+ return output_image
29
+
30
  except Exception as e:
31
  raise gr.Error(f"Error processing image: {str(e)}")
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # Create Gradio interface
34
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
35
  gr.HTML(
36
  """
37
  <div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
38
+ <h1 style="font-size: 2.5rem; margin-bottom: 1rem;">
39
  AI Background Remover
40
  </h1>
41
+ <p style="color: #666; font-size: 1.1rem;">
42
  Remove backgrounds instantly using RMBG V1.4 model
43
  </p>
44
  </div>