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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -5,11 +5,11 @@ import io
5
  import numpy as np
6
 
7
  def remove_bg(image):
8
- # Convert input (could be numpy array from Gradio or file path from API)
9
- if isinstance(image, np.ndarray):
10
- image = Image.fromarray(image)
11
- elif isinstance(image, str):
12
  image = Image.open(image)
 
 
13
 
14
  # Convert to bytes for rembg
15
  img_byte_arr = io.BytesIO()
@@ -17,14 +17,14 @@ def remove_bg(image):
17
  output_bytes = remove(img_byte_arr.getvalue())
18
  return Image.open(io.BytesIO(output_bytes))
19
 
20
- # Create interface - this works better for APIs than Blocks
21
  iface = gr.Interface(
22
  fn=remove_bg,
23
- inputs=gr.Image(type="filepath", label="Input Image"),
24
- outputs=gr.Image(label="Output"),
25
- title="U²Net Background Remover API",
26
- description="Upload an image to remove the background"
27
  )
28
 
29
- # For Hugging Face Spaces, use this launch configuration
30
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
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()
 
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)