Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,29 +5,33 @@ import io
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
def remove_bg(image):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Define the interface
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=remove_bg,
|
| 25 |
-
inputs=gr.Image(type="filepath", label="Input Image"),
|
| 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)
|