Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,11 @@ import io
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
def remove_bg(image):
|
| 8 |
-
#
|
| 9 |
-
if isinstance(image,
|
| 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 |
-
#
|
| 21 |
iface = gr.Interface(
|
| 22 |
fn=remove_bg,
|
| 23 |
-
inputs=gr.Image(type="filepath",
|
| 24 |
-
outputs=gr.Image(
|
| 25 |
-
title="
|
| 26 |
-
|
| 27 |
)
|
| 28 |
|
| 29 |
-
|
| 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)
|