Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
-
import base64
|
| 3 |
-
import requests
|
| 4 |
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
import torch
|
| 7 |
import numpy as np
|
| 8 |
from transformers import AutoModelForImageSegmentation
|
| 9 |
-
import
|
| 10 |
-
from fastapi import
|
| 11 |
-
|
| 12 |
|
| 13 |
# -------------------------
|
| 14 |
# Model Setup
|
|
@@ -49,55 +47,30 @@ def process_image(image: Image.Image) -> Image.Image:
|
|
| 49 |
image.putalpha(mask)
|
| 50 |
return image
|
| 51 |
|
| 52 |
-
# -------------------------
|
| 53 |
-
# Gradio Function
|
| 54 |
-
# -------------------------
|
| 55 |
-
def remove_background_gradio(img):
|
| 56 |
-
return process_image(img.convert("RGB"))
|
| 57 |
-
|
| 58 |
-
# -------------------------
|
| 59 |
-
# Gradio Interface for UI
|
| 60 |
-
# -------------------------
|
| 61 |
-
demo = gr.Interface(
|
| 62 |
-
fn=remove_background_gradio,
|
| 63 |
-
inputs=gr.Image(type="pil"),
|
| 64 |
-
outputs=gr.Image(type="pil"),
|
| 65 |
-
title="Background Removal Tool",
|
| 66 |
-
description="Upload an image and get a transparent background."
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
# -------------------------
|
| 70 |
# FastAPI App
|
| 71 |
# -------------------------
|
| 72 |
-
app =
|
| 73 |
|
| 74 |
@app.post("/remove-background")
|
| 75 |
-
async def remove_background(
|
| 76 |
"""
|
| 77 |
-
|
| 78 |
-
Returns PNG
|
| 79 |
"""
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
img = Image.open(BytesIO(resp.content)).convert("RGB")
|
| 89 |
-
else:
|
| 90 |
-
return {"error": "Provide file or image_url"}
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
# Optional: Root UI
|
| 100 |
-
# -------------------------
|
| 101 |
-
@app.get("/", response_class=HTMLResponse)
|
| 102 |
-
async def index():
|
| 103 |
-
return demo.launch(share=False, inline=True)[0] # Embed Gradio UI
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from io import BytesIO
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from transformers import AutoModelForImageSegmentation
|
| 7 |
+
from fastapi import FastAPI, File, Form, UploadFile, HTTPException
|
| 8 |
+
from fastapi.responses import StreamingResponse
|
| 9 |
+
import requests
|
| 10 |
|
| 11 |
# -------------------------
|
| 12 |
# Model Setup
|
|
|
|
| 47 |
image.putalpha(mask)
|
| 48 |
return image
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# -------------------------
|
| 51 |
# FastAPI App
|
| 52 |
# -------------------------
|
| 53 |
+
app = FastAPI(title="Background Removal API")
|
| 54 |
|
| 55 |
@app.post("/remove-background")
|
| 56 |
+
async def remove_background(file: UploadFile = File(None), image_url: str = Form(None)):
|
| 57 |
"""
|
| 58 |
+
Accept either an uploaded file or an image URL.
|
| 59 |
+
Returns PNG with transparent background.
|
| 60 |
"""
|
| 61 |
+
try:
|
| 62 |
+
if file:
|
| 63 |
+
img = Image.open(BytesIO(await file.read())).convert("RGB")
|
| 64 |
+
elif image_url:
|
| 65 |
+
resp = requests.get(image_url)
|
| 66 |
+
img = Image.open(BytesIO(resp.content)).convert("RGB")
|
| 67 |
+
else:
|
| 68 |
+
raise HTTPException(status_code=400, detail="Provide file or image_url")
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
result = process_image(img)
|
| 71 |
+
buf = BytesIO()
|
| 72 |
+
result.save(buf, format="PNG")
|
| 73 |
+
buf.seek(0)
|
| 74 |
+
return StreamingResponse(buf, media_type="image/png")
|
| 75 |
+
except Exception as e:
|
| 76 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|