Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from
|
| 3 |
from transparent_background import Remover
|
| 4 |
from PIL import Image
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
import io
|
| 7 |
import requests
|
|
|
|
| 8 |
import warnings
|
| 9 |
|
| 10 |
warnings.filterwarnings("ignore")
|
| 11 |
|
| 12 |
app = FastAPI(title="Background Remover API")
|
| 13 |
|
| 14 |
-
# Chargé
|
| 15 |
remover = Remover(mode="base-nightly")
|
| 16 |
|
| 17 |
|
|
@@ -20,40 +20,36 @@ class RemoveBackgroundRequest(BaseModel):
|
|
| 20 |
sod_type: str = "rgba"
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@app.get("/")
|
| 24 |
def welcome():
|
| 25 |
return {"message": "welcome"}
|
| 26 |
|
| 27 |
|
| 28 |
-
@app.post("/remove-background")
|
| 29 |
def remove_background(payload: RemoveBackgroundRequest):
|
| 30 |
-
# Téléchargement de l'image
|
| 31 |
response = requests.get(payload.image_url, timeout=10)
|
| 32 |
response.raise_for_status()
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
io.BytesIO(response.content)
|
| 36 |
-
).convert("RGB")
|
| 37 |
-
|
| 38 |
-
# Traitement
|
| 39 |
result = remover.process(pil_image, type=payload.sod_type)
|
| 40 |
|
| 41 |
-
# PNG en mémoire
|
| 42 |
buf = io.BytesIO()
|
| 43 |
result.save(buf, format="PNG")
|
| 44 |
buf.seek(0)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
import uvicorn
|
| 54 |
-
uvicorn.run(
|
| 55 |
-
"app:app",
|
| 56 |
-
host="0.0.0.0",
|
| 57 |
-
port=7860,
|
| 58 |
-
reload=True
|
| 59 |
-
)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from transparent_background import Remover
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
import io
|
| 6 |
import requests
|
| 7 |
+
import base64
|
| 8 |
import warnings
|
| 9 |
|
| 10 |
warnings.filterwarnings("ignore")
|
| 11 |
|
| 12 |
app = FastAPI(title="Background Remover API")
|
| 13 |
|
| 14 |
+
# Chargé une seule fois
|
| 15 |
remover = Remover(mode="base-nightly")
|
| 16 |
|
| 17 |
|
|
|
|
| 20 |
sod_type: str = "rgba"
|
| 21 |
|
| 22 |
|
| 23 |
+
class RemoveBackgroundResponse(BaseModel):
|
| 24 |
+
image_base64: str
|
| 25 |
+
|
| 26 |
+
|
| 27 |
@app.get("/")
|
| 28 |
def welcome():
|
| 29 |
return {"message": "welcome"}
|
| 30 |
|
| 31 |
|
| 32 |
+
@app.post("/remove-background", response_model=RemoveBackgroundResponse)
|
| 33 |
def remove_background(payload: RemoveBackgroundRequest):
|
| 34 |
+
# Téléchargement de l'image depuis l'URL
|
| 35 |
response = requests.get(payload.image_url, timeout=10)
|
| 36 |
response.raise_for_status()
|
| 37 |
+
pil_image = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 38 |
|
| 39 |
+
# Traitement du fond
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
result = remover.process(pil_image, type=payload.sod_type)
|
| 41 |
|
| 42 |
+
# Conversion en PNG en mémoire
|
| 43 |
buf = io.BytesIO()
|
| 44 |
result.save(buf, format="PNG")
|
| 45 |
buf.seek(0)
|
| 46 |
|
| 47 |
+
# Encodage en Base64
|
| 48 |
+
img_base64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 49 |
+
|
| 50 |
+
return {"image_base64": img_base64}
|
| 51 |
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
import uvicorn
|
| 55 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|