Janeka commited on
Commit
3b7a756
·
verified ·
1 Parent(s): ad587f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -5,26 +5,29 @@ import io
5
  import numpy as np
6
 
7
  def remove_bg(image):
8
- # Handle both file paths (API) and numpy arrays (UI)
9
- if isinstance(image, str): # API sends file path
10
- image = Image.open(image)
11
- elif isinstance(image, np.ndarray): # UI sends numpy array
12
- image = Image.fromarray(image)
 
 
13
 
14
- # Convert to bytes for rembg
15
  img_byte_arr = io.BytesIO()
16
- image.save(img_byte_arr, format='PNG')
17
  output_bytes = remove(img_byte_arr.getvalue())
18
  return Image.open(io.BytesIO(output_bytes))
19
 
20
- # Use Interface instead of Blocks for better API support
21
  iface = gr.Interface(
22
  fn=remove_bg,
23
- inputs=gr.Image(type="filepath"), # Important for API
24
- outputs=gr.Image(),
25
  title="Background Remover",
26
- allow_flagging="never"
27
  )
28
 
 
29
  if __name__ == "__main__":
30
  iface.launch(server_name="0.0.0.0", server_port=7860)
 
5
  import numpy as np
6
 
7
  def remove_bg(image):
8
+ # Convert input to PIL Image
9
+ if isinstance(image, str): # file path
10
+ img = Image.open(image)
11
+ elif isinstance(image, np.ndarray): # numpy array from Gradio
12
+ img = Image.fromarray(image)
13
+ else:
14
+ raise ValueError("Unsupported input type")
15
 
16
+ # Process with rembg
17
  img_byte_arr = io.BytesIO()
18
+ img.save(img_byte_arr, format='PNG')
19
  output_bytes = remove(img_byte_arr.getvalue())
20
  return Image.open(io.BytesIO(output_bytes))
21
 
22
+ # Define the interface
23
  iface = gr.Interface(
24
  fn=remove_bg,
25
+ inputs=gr.Image(type="filepath", label="Input Image"), # Important: use "filepath" for API compatibility
26
+ outputs=gr.Image(label="Output Image"),
27
  title="Background Remover",
28
+ description="Upload an image to remove its background"
29
  )
30
 
31
+ # Launch the app
32
  if __name__ == "__main__":
33
  iface.launch(server_name="0.0.0.0", server_port=7860)