SynthAIzer commited on
Commit
7842ff6
·
verified ·
1 Parent(s): 74be3e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -7
app.py CHANGED
@@ -6,20 +6,19 @@ from PIL import Image
6
  # Load Hugging Face background removal model
7
  # -------------------------------
8
  pipe = pipeline(
9
- "image-segmentation",
10
  model="briaai/RMBG-1.4",
11
- trust_remote_code=True # <-- IMPORTANT FIX
12
  )
13
 
14
  # -------------------------------
15
  # Function to remove background
16
  # -------------------------------
17
  def remove_bg(image: Image.Image):
18
- # Run model to get mask
19
- result = pipe(image)
20
- mask = result[0]["mask"] # segmentation mask (white=foreground, black=background)
21
 
22
- # Convert mask to binary
23
  mask = mask.convert("L")
24
 
25
  # Apply mask to original image
@@ -37,7 +36,7 @@ demo = gr.Interface(
37
  inputs=gr.Image(type="pil", label="Upload an image"),
38
  outputs=gr.Image(type="pil", label="Background Removed"),
39
  title="Background Remover",
40
- description="Upload an image to remove its background using Hugging Face model.",
41
  )
42
 
43
  # -------------------------------
 
6
  # Load Hugging Face background removal model
7
  # -------------------------------
8
  pipe = pipeline(
9
+ task="image-segmentation",
10
  model="briaai/RMBG-1.4",
11
+ trust_remote_code=True
12
  )
13
 
14
  # -------------------------------
15
  # Function to remove background
16
  # -------------------------------
17
  def remove_bg(image: Image.Image):
18
+ # Run model this returns a PIL mask directly
19
+ mask = pipe(image)
 
20
 
21
+ # Ensure mask is grayscale
22
  mask = mask.convert("L")
23
 
24
  # Apply mask to original image
 
36
  inputs=gr.Image(type="pil", label="Upload an image"),
37
  outputs=gr.Image(type="pil", label="Background Removed"),
38
  title="Background Remover",
39
+ description="Upload an image to remove its background using Hugging Face RMBG model.",
40
  )
41
 
42
  # -------------------------------