Spaces:
Paused
Paused
VINU NAYAK commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,30 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
from rembg import remove
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
output_image = remove(input_image)
|
| 11 |
-
return output_image
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
title="Background Remover",
|
| 18 |
-
description="Upload an image to remove its background",
|
| 19 |
-
examples=[],
|
| 20 |
-
theme=gr.themes.Soft()
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
demo.launch()
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Header
|
| 2 |
+
from fastapi.responses import StreamingResponse
|
| 3 |
from rembg import remove
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Replace this with your secret
|
| 10 |
+
API_KEY = "your_super_secret_key"
|
| 11 |
+
|
| 12 |
+
@app.post("/remove-bg")
|
| 13 |
+
async def remove_bg(file: UploadFile = File(...), authorization: str = Header(None)):
|
| 14 |
+
# Check API key in Authorization header
|
| 15 |
+
if authorization != f"Bearer {API_KEY}":
|
| 16 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 17 |
+
|
| 18 |
+
# Read image
|
| 19 |
+
contents = await file.read()
|
| 20 |
+
input_image = Image.open(io.BytesIO(contents))
|
| 21 |
+
|
| 22 |
+
# Remove background
|
| 23 |
output_image = remove(input_image)
|
|
|
|
| 24 |
|
| 25 |
+
# Prepare image response
|
| 26 |
+
img_byte_arr = io.BytesIO()
|
| 27 |
+
output_image.save(img_byte_arr, format="PNG")
|
| 28 |
+
img_byte_arr.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
return StreamingResponse(img_byte_arr, media_type="image/png")
|
|
|