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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -5,29 +5,33 @@ import io
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)
 
5
  import numpy as np
6
 
7
  def remove_bg(image):
8
+ try:
9
+ # Convert input to PIL Image
10
+ if isinstance(image, str): # file path
11
+ img = Image.open(image)
12
+ elif isinstance(image, np.ndarray): # numpy array from Gradio
13
+ img = Image.fromarray(image)
14
+ else:
15
+ raise ValueError("Unsupported input type")
16
+
17
+ # Process with rembg
18
+ img_byte_arr = io.BytesIO()
19
+ img.save(img_byte_arr, format='PNG')
20
+ output_bytes = remove(img_byte_arr.getvalue())
21
+ return Image.open(io.BytesIO(output_bytes))
22
+ except Exception as e:
23
+ raise gr.Error(f"Error processing image: {str(e)}")
24
 
25
+ # Define the interface with explicit API enabled
26
  iface = gr.Interface(
27
  fn=remove_bg,
28
+ inputs=gr.Image(type="filepath", label="Input Image"),
29
+ outputs=gr.Image(label="Output Image", type="pil"),
30
  title="Background Remover",
31
+ description="Upload an image to remove its background",
32
+ allow_flagging="never" # Disable the flagging feature
33
  )
34
 
35
+ # Launch the app with API enabled
36
  if __name__ == "__main__":
37
+ iface.launch(server_name="0.0.0.0", server_port=7860, enable_api=True)