Spaces:
Paused
Paused
Arena Agent commited on
Commit ·
45dafaf
1
Parent(s): 04f1e0a
Fix Space configuration and Docker deployment
Browse files- .env +0 -2
- .gitignore +4 -0
- Dockerfile +23 -0
- README.md +29 -40
- app.py +37 -40
- requirements.txt +4 -7
.env
DELETED
|
@@ -1,2 +0,0 @@
|
|
| 1 |
-
# Your API key for authentication
|
| 2 |
-
API_KEY=your-secret-api-key-here
|
|
|
|
|
|
|
|
|
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
.venv/
|
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
+
PYTHONUNBUFFERED=1 \
|
| 5 |
+
PORT=7860
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
RUN apt-get update \
|
| 10 |
+
&& apt-get install -y --no-install-recommends \
|
| 11 |
+
ca-certificates \
|
| 12 |
+
libglib2.0-0 \
|
| 13 |
+
libgl1 \
|
| 14 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
+
|
| 16 |
+
COPY requirements.txt .
|
| 17 |
+
RUN pip install --no-cache-dir --upgrade pip \
|
| 18 |
+
&& pip install --no-cache-dir -r requirements.txt
|
| 19 |
+
|
| 20 |
+
COPY app.py .
|
| 21 |
+
|
| 22 |
+
EXPOSE 7860
|
| 23 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,55 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# RemBG API Server
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
##
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
```
|
| 11 |
|
| 12 |
-
|
| 13 |
-
- Copy `.env.example` to `.env`
|
| 14 |
-
- Set your desired API key in the `.env` file
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
python app.py
|
| 19 |
```
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
-
|
| 25 |
-
- Headers:
|
| 26 |
-
- `X-API-Key`: Your API key
|
| 27 |
-
- Body: Form-data with an 'image' file
|
| 28 |
-
- Returns: PNG image with background removed
|
| 29 |
|
| 30 |
-
|
| 31 |
-
- Endpoint: `GET /health`
|
| 32 |
-
- Returns: Health status of the server
|
| 33 |
|
| 34 |
-
## Example Usage with cURL
|
| 35 |
```bash
|
| 36 |
-
curl -X POST
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
```
|
| 38 |
|
| 39 |
-
|
| 40 |
-
```python
|
| 41 |
-
import requests
|
| 42 |
-
|
| 43 |
-
url = "http://localhost:5000/remove-bg"
|
| 44 |
-
headers = {
|
| 45 |
-
"X-API-Key": "your-api-key"
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
with open("input.jpg", "rb") as f:
|
| 49 |
-
files = {"image": f}
|
| 50 |
-
response = requests.post(url, headers=headers, files=files)
|
| 51 |
-
|
| 52 |
-
if response.status_code == 200:
|
| 53 |
-
with open("output.png", "wb") as f:
|
| 54 |
-
f.write(response.content)
|
| 55 |
-
```
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: RemBG API
|
| 3 |
+
emoji: 🖼️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
# RemBG API Server
|
| 12 |
|
| 13 |
+
A Flask API that removes image backgrounds with `rembg`.
|
| 14 |
|
| 15 |
+
## Endpoints
|
| 16 |
|
| 17 |
+
### Health check
|
| 18 |
+
|
| 19 |
+
```text
|
| 20 |
+
GET /health
|
| 21 |
```
|
| 22 |
|
| 23 |
+
### Remove a background
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
```text
|
| 26 |
+
POST /remove-bg
|
|
|
|
| 27 |
```
|
| 28 |
|
| 29 |
+
Required:
|
| 30 |
|
| 31 |
+
- Header: `X-API-Key`
|
| 32 |
+
- Multipart form field: `image`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
Example:
|
|
|
|
|
|
|
| 35 |
|
|
|
|
| 36 |
```bash
|
| 37 |
+
curl -X POST \
|
| 38 |
+
-H "X-API-Key: your-api-key" \
|
| 39 |
+
-F "image=@input.jpg" \
|
| 40 |
+
https://vinaynayak-rembg-api.hf.space/remove-bg \
|
| 41 |
+
-o removed_bg.png
|
| 42 |
```
|
| 43 |
|
| 44 |
+
Set `API_KEY` in the Hugging Face Space Settings → Secrets. Do not commit a `.env` file or any real API key.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,63 +1,60 @@
|
|
| 1 |
-
|
| 2 |
-
from rembg import remove
|
| 3 |
-
from PIL import Image
|
| 4 |
import io
|
| 5 |
import os
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
|
|
|
| 12 |
|
| 13 |
-
# Get API key from environment variable
|
| 14 |
-
API_KEY = os.getenv('API_KEY')
|
| 15 |
|
| 16 |
-
def check_api_key():
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return True
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
def remove_background():
|
| 24 |
-
# Check API key
|
| 25 |
if not check_api_key():
|
| 26 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
file = request.files['image']
|
| 32 |
-
|
| 33 |
-
if file.filename == '':
|
| 34 |
-
return jsonify({'error': 'No selected file'}), 400
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
# Read the image
|
| 38 |
input_image = Image.open(file.stream)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
output.save(img_byte_arr, format='PNG')
|
| 46 |
-
img_byte_arr.seek(0)
|
| 47 |
-
|
| 48 |
return send_file(
|
| 49 |
-
|
| 50 |
-
mimetype=
|
| 51 |
as_attachment=True,
|
| 52 |
-
download_name=
|
| 53 |
)
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
except Exception as e:
|
| 56 |
-
return jsonify({'error': str(e)}), 500
|
| 57 |
|
| 58 |
-
@app.route(
|
| 59 |
def health_check():
|
| 60 |
-
return jsonify({
|
|
|
|
| 61 |
|
| 62 |
-
if __name__ ==
|
| 63 |
-
|
|
|
|
|
|
| 1 |
+
import hmac
|
|
|
|
|
|
|
| 2 |
import io
|
| 3 |
import os
|
| 4 |
+
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
from flask import Flask, jsonify, request, send_file
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from rembg import remove
|
| 9 |
|
|
|
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
+
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024 # 16 MB
|
| 14 |
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def check_api_key() -> bool:
|
| 17 |
+
supplied_key = request.headers.get("X-API-Key", "")
|
| 18 |
+
configured_key = os.getenv("API_KEY", "")
|
| 19 |
+
return bool(configured_key) and hmac.compare_digest(supplied_key, configured_key)
|
|
|
|
| 20 |
|
| 21 |
+
|
| 22 |
+
@app.route("/remove-bg", methods=["POST"])
|
| 23 |
def remove_background():
|
|
|
|
| 24 |
if not check_api_key():
|
| 25 |
+
return jsonify({"error": "Invalid or missing API key"}), 401
|
| 26 |
+
|
| 27 |
+
if "image" not in request.files:
|
| 28 |
+
return jsonify({"error": "No image file provided"}), 400
|
| 29 |
|
| 30 |
+
file = request.files["image"]
|
| 31 |
+
if not file.filename:
|
| 32 |
+
return jsonify({"error": "No selected file"}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
try:
|
|
|
|
| 35 |
input_image = Image.open(file.stream)
|
| 36 |
+
output_image = remove(input_image)
|
| 37 |
+
|
| 38 |
+
output_bytes = io.BytesIO()
|
| 39 |
+
output_image.save(output_bytes, format="PNG")
|
| 40 |
+
output_bytes.seek(0)
|
| 41 |
+
|
|
|
|
|
|
|
|
|
|
| 42 |
return send_file(
|
| 43 |
+
output_bytes,
|
| 44 |
+
mimetype="image/png",
|
| 45 |
as_attachment=True,
|
| 46 |
+
download_name="removed_bg.png",
|
| 47 |
)
|
| 48 |
+
except Exception as exc:
|
| 49 |
+
app.logger.exception("Background removal failed")
|
| 50 |
+
return jsonify({"error": str(exc)}), 500
|
| 51 |
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
@app.route("/health", methods=["GET"])
|
| 54 |
def health_check():
|
| 55 |
+
return jsonify({"status": "healthy"}), 200
|
| 56 |
+
|
| 57 |
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
port = int(os.getenv("PORT", "7860"))
|
| 60 |
+
app.run(host="0.0.0.0", port=port)
|
requirements.txt
CHANGED
|
@@ -1,7 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
python-dotenv=
|
| 4 |
-
|
| 5 |
-
huggingface-hub==0.16.4
|
| 6 |
-
transformers==4.30.2
|
| 7 |
-
rembg==2.0.50
|
|
|
|
| 1 |
+
Flask>=3.0,<4
|
| 2 |
+
Pillow>=10,<12
|
| 3 |
+
python-dotenv>=1,<2
|
| 4 |
+
rembg[cpu]>=2.0.50,<3
|
|
|
|
|
|
|
|
|