Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +19 -0
- README.md +19 -5
- app.py +34 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y --no-install-recommends libgl1 libglib2.0-0 \
|
| 6 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 7 |
+
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Descarga el modelo de rembg durante el build, para que el primer
|
| 12 |
+
# pedido de un usuario no tenga que esperar la descarga.
|
| 13 |
+
RUN python -c "import rembg; rembg.new_session('u2net')"
|
| 14 |
+
|
| 15 |
+
COPY app.py .
|
| 16 |
+
|
| 17 |
+
EXPOSE 7860
|
| 18 |
+
|
| 19 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,24 @@
|
|
| 1 |
---
|
| 2 |
-
title: Remove
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Remove Background API
|
| 3 |
+
emoji: ✂️
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Remove Background API
|
| 12 |
+
|
| 13 |
+
Servidor simple que quita el fondo de imágenes usando [rembg](https://github.com/danielgatis/rembg).
|
| 14 |
+
Gratis, sin límite de imágenes, corre en el CPU gratuito de Hugging Face Spaces.
|
| 15 |
+
|
| 16 |
+
## Endpoint
|
| 17 |
+
|
| 18 |
+
`POST /remove-bg` — recibe un campo `file` (multipart/form-data) con la imagen,
|
| 19 |
+
devuelve un PNG con fondo transparente.
|
| 20 |
+
|
| 21 |
+
## Nota sobre el plan gratuito
|
| 22 |
+
|
| 23 |
+
Los Spaces gratuitos se "duermen" tras un rato sin uso. El primer pedido después
|
| 24 |
+
de estar dormido puede tardar 30–60 segundos en despertar; los siguientes son rápidos.
|
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import Response
|
| 4 |
+
from rembg import remove
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="Remove Background API")
|
| 7 |
+
|
| 8 |
+
# Permite que tu página en GitHub Pages (u otro origen) llame a este servidor.
|
| 9 |
+
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
+
allow_methods=["*"],
|
| 13 |
+
allow_headers=["*"],
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def health():
|
| 19 |
+
return {"status": "ok"}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@app.post("/remove-bg")
|
| 23 |
+
async def remove_bg(file: UploadFile = File(...)):
|
| 24 |
+
if not file.content_type or not file.content_type.startswith("image/"):
|
| 25 |
+
raise HTTPException(status_code=400, detail="El archivo debe ser una imagen.")
|
| 26 |
+
|
| 27 |
+
input_bytes = await file.read()
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
output_bytes = remove(input_bytes)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
raise HTTPException(status_code=500, detail=f"No se pudo procesar la imagen: {e}")
|
| 33 |
+
|
| 34 |
+
return Response(content=output_bytes, media_type="image/png")
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.0
|
| 2 |
+
uvicorn[standard]==0.30.6
|
| 3 |
+
python-multipart==0.0.9
|
| 4 |
+
rembg==2.0.59
|
| 5 |
+
onnxruntime==1.19.2
|
| 6 |
+
pillow==10.4.0
|