Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +26 -0
- requirements.txt +5 -0
- server.py +38 -0
Dockerfile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Install basic system dependencies
|
| 4 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 5 |
+
build-essential \
|
| 6 |
+
&& rm -rf /var/lib/apt/clean/var/lib/apt/lists/*
|
| 7 |
+
|
| 8 |
+
# Set up user permissions using Debian standard useradd
|
| 9 |
+
RUN useradd -m -u 1000 user
|
| 10 |
+
USER user
|
| 11 |
+
ENV HOME=/home/user \
|
| 12 |
+
PATH=/home/user/.local/bin:$PATH
|
| 13 |
+
WORKDIR $HOME/app
|
| 14 |
+
|
| 15 |
+
# Copy python dependencies and install
|
| 16 |
+
COPY --chown=user requirements.txt .
|
| 17 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 18 |
+
|
| 19 |
+
# Copy server code
|
| 20 |
+
COPY --chown=user . .
|
| 21 |
+
|
| 22 |
+
# Hugging Face Spaces routes traffic natively through port 7860
|
| 23 |
+
EXPOSE 7860
|
| 24 |
+
ENV PORT=7860
|
| 25 |
+
|
| 26 |
+
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
rembg
|
| 4 |
+
Pillow
|
| 5 |
+
python-multipart
|
server.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Response
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from rembg import remove
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
import uvicorn
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
# Enable CORS configurations to allow the internal node proxy to make pipeline hops
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
@app.post("/remove-bg")
|
| 21 |
+
async def remove_background(file: UploadFile = File(...)):
|
| 22 |
+
"""
|
| 23 |
+
Removes the background from an uploaded image file.
|
| 24 |
+
Expects a multipart/form-data upload with an image file.
|
| 25 |
+
Returns a PNG image with a transparent background.
|
| 26 |
+
"""
|
| 27 |
+
contents = await file.read()
|
| 28 |
+
input_image = Image.open(io.BytesIO(contents)).convert("RGBA")
|
| 29 |
+
output_image = remove(input_image)
|
| 30 |
+
|
| 31 |
+
output_buffer = io.BytesIO()
|
| 32 |
+
output_image.save(output_buffer, format="PNG")
|
| 33 |
+
return Response(content=output_buffer.getvalue(), media_type="image/png")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
port = int(os.environ.get("PORT", 8080))
|
| 38 |
+
uvicorn.run("server:app", host="0.0.0.0", port=port)
|