Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,17 @@
|
|
| 2 |
Background Removal API using rembg
|
| 3 |
Optimized for Hugging Face Spaces with GPU support
|
| 4 |
|
| 5 |
-
|
| 6 |
-
that can be called from CloudRun Worker or other services.
|
| 7 |
"""
|
| 8 |
|
| 9 |
import gradio as gr
|
|
|
|
|
|
|
| 10 |
from rembg import remove, new_session
|
| 11 |
from PIL import Image
|
| 12 |
import io
|
| 13 |
import time
|
|
|
|
| 14 |
|
| 15 |
# Global session for model reuse (GPU optimized)
|
| 16 |
print("🔥 Loading rembg model...")
|
|
@@ -67,30 +69,67 @@ demo = gr.Interface(
|
|
| 67 |
**Features:**
|
| 68 |
- GPU-accelerated processing (on HF Spaces)
|
| 69 |
- Session reuse for faster processing
|
| 70 |
-
- API endpoint available at `/
|
| 71 |
|
| 72 |
-
**Usage:**
|
| 73 |
-
```
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
"YOUR_SPACE_URL/api/predict",
|
| 78 |
-
files={"data": open("image.png", "rb")}
|
| 79 |
-
)
|
| 80 |
-
result = response.json()
|
| 81 |
```
|
| 82 |
""",
|
| 83 |
-
examples=[
|
| 84 |
-
# You can add example images here if you want
|
| 85 |
-
],
|
| 86 |
-
api_name="predict", # API endpoint name
|
| 87 |
allow_flagging="never"
|
| 88 |
)
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if __name__ == "__main__":
|
| 91 |
-
# Launch with
|
| 92 |
demo.launch(
|
| 93 |
server_name="0.0.0.0",
|
| 94 |
server_port=7860,
|
| 95 |
-
show_api=
|
| 96 |
)
|
|
|
|
| 2 |
Background Removal API using rembg
|
| 3 |
Optimized for Hugging Face Spaces with GPU support
|
| 4 |
|
| 5 |
+
Provides both Gradio UI and FastAPI endpoint for direct HTTP access
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 10 |
+
from fastapi.responses import Response
|
| 11 |
from rembg import remove, new_session
|
| 12 |
from PIL import Image
|
| 13 |
import io
|
| 14 |
import time
|
| 15 |
+
import uvicorn
|
| 16 |
|
| 17 |
# Global session for model reuse (GPU optimized)
|
| 18 |
print("🔥 Loading rembg model...")
|
|
|
|
| 69 |
**Features:**
|
| 70 |
- GPU-accelerated processing (on HF Spaces)
|
| 71 |
- Session reuse for faster processing
|
| 72 |
+
- Direct HTTP API endpoint available at `/remove-bg`
|
| 73 |
|
| 74 |
+
**API Usage:**
|
| 75 |
+
```bash
|
| 76 |
+
curl -X POST "https://YOUR-SPACE.hf.space/remove-bg" \\
|
| 77 |
+
-F "file=@image.png" \\
|
| 78 |
+
-o output.png
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
```
|
| 80 |
""",
|
| 81 |
+
examples=[],
|
|
|
|
|
|
|
|
|
|
| 82 |
allow_flagging="never"
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# Mount Gradio app to FastAPI
|
| 86 |
+
app = demo.app # Get the FastAPI app from Gradio
|
| 87 |
+
|
| 88 |
+
# Add custom FastAPI endpoint for direct HTTP access
|
| 89 |
+
@app.post("/remove-bg")
|
| 90 |
+
async def remove_bg_endpoint(file: UploadFile = File(...)):
|
| 91 |
+
"""
|
| 92 |
+
Direct HTTP endpoint for background removal
|
| 93 |
+
Returns PNG image with transparent background
|
| 94 |
+
"""
|
| 95 |
+
try:
|
| 96 |
+
# Read uploaded file
|
| 97 |
+
contents = await file.read()
|
| 98 |
+
input_image = Image.open(io.BytesIO(contents))
|
| 99 |
+
|
| 100 |
+
# Process with rembg
|
| 101 |
+
output_image = remove_background(input_image)
|
| 102 |
+
|
| 103 |
+
# Convert to bytes
|
| 104 |
+
img_byte_arr = io.BytesIO()
|
| 105 |
+
output_image.save(img_byte_arr, format='PNG')
|
| 106 |
+
img_byte_arr.seek(0)
|
| 107 |
+
|
| 108 |
+
# Return as PNG
|
| 109 |
+
return Response(
|
| 110 |
+
content=img_byte_arr.getvalue(),
|
| 111 |
+
media_type="image/png",
|
| 112 |
+
headers={
|
| 113 |
+
"Content-Disposition": f"attachment; filename=removed_bg.png"
|
| 114 |
+
}
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 119 |
+
|
| 120 |
+
@app.get("/health")
|
| 121 |
+
async def health_check():
|
| 122 |
+
"""Health check endpoint"""
|
| 123 |
+
return {
|
| 124 |
+
"status": "healthy",
|
| 125 |
+
"model": "rembg u2net",
|
| 126 |
+
"session_loaded": rembg_session is not None
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
if __name__ == "__main__":
|
| 130 |
+
# Launch Gradio with custom app
|
| 131 |
demo.launch(
|
| 132 |
server_name="0.0.0.0",
|
| 133 |
server_port=7860,
|
| 134 |
+
show_api=False # We have custom API endpoints
|
| 135 |
)
|