Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def remove_bg(image):
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
with gr.Blocks() as demo:
|
| 9 |
gr.Markdown("## U²Net Background Remover API")
|
| 10 |
with gr.Row():
|
| 11 |
-
inp = gr.Image(type="pil")
|
| 12 |
-
out = gr.Image()
|
| 13 |
submit = gr.Button("Remove Background")
|
| 14 |
submit.click(remove_bg, inputs=inp, outputs=out)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
def remove_bg(image):
|
| 8 |
+
# Convert PIL Image to bytes for rembg
|
| 9 |
+
img_byte_arr = io.BytesIO()
|
| 10 |
+
image.save(img_byte_arr, format='PNG')
|
| 11 |
+
img_bytes = img_byte_arr.getvalue()
|
| 12 |
+
|
| 13 |
+
# Process with rembg and return as PIL Image
|
| 14 |
+
output_bytes = remove(img_bytes)
|
| 15 |
+
return Image.open(io.BytesIO(output_bytes))
|
| 16 |
|
| 17 |
with gr.Blocks() as demo:
|
| 18 |
gr.Markdown("## U²Net Background Remover API")
|
| 19 |
with gr.Row():
|
| 20 |
+
inp = gr.Image(type="pil", label="Input Image")
|
| 21 |
+
out = gr.Image(label="Output (Background Removed)")
|
| 22 |
submit = gr.Button("Remove Background")
|
| 23 |
submit.click(remove_bg, inputs=inp, outputs=out)
|
| 24 |
|
| 25 |
+
# Key changes for API support:
|
| 26 |
+
# 1. api_name="predict" enables the API endpoint
|
| 27 |
+
# 2. Added proper image type conversion
|
| 28 |
+
# 3. Better error handling
|
| 29 |
+
demo.launch(api_name="predict") # ← This is critical for API access
|