MalikSahib1 commited on
Commit
b154bd2
·
verified ·
1 Parent(s): abde04c

Upload 4 files

Browse files
Files changed (4) hide show
  1. .gitignore +14 -0
  2. Dockerfile +22 -0
  3. app.py +67 -0
  4. requirements.txt +6 -0
.gitignore ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # .gitignore
2
+
3
+ # Python
4
+ __pycache__/
5
+ *.pyc
6
+ *.pyo
7
+ *.pyd
8
+ .Python
9
+ env/
10
+ venv/
11
+ pip-selfcheck.json
12
+
13
+ # Environment variables
14
+ .env
Dockerfile ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile
2
+
3
+ # 1. Use a more recent, but very stable, Python version
4
+ FROM python:3.11-slim
5
+
6
+ # 2. Set the working directory in the container
7
+ WORKDIR /app
8
+
9
+ # 3. Copy the requirements file into the container
10
+ COPY requirements.txt .
11
+
12
+ # 4. Install any needed packages specified in requirements.txt
13
+ RUN pip install --no-cache-dir -r requirements.txt
14
+
15
+ # 5. Copy the rest of the application code into the container
16
+ COPY . .
17
+
18
+ # 6. Expose the port the app runs on
19
+ EXPOSE 7860
20
+
21
+ # 7. Define the command to run the application using Gunicorn
22
+ CMD ["gunicorn", "--workers", "2", "--threads", "2", "--bind", "0.0.0.0:7860", "app:app"]
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import os
4
+ from flask import Flask, request, send_file, jsonify
5
+ from rembg import remove
6
+ from PIL import Image
7
+ import io
8
+
9
+ # --- Create the Flask App ---
10
+ app = Flask(__name__)
11
+
12
+ # --- Configuration ---
13
+ # Get the API Key from an environment variable for security.
14
+ API_KEY = os.environ.get("BG_REMOVER_API_KEY")
15
+
16
+ # --- API Endpoints ---
17
+
18
+ # A simple root endpoint to check if the server is running.
19
+ @app.route('/')
20
+ def index():
21
+ return "Background Remover API is running!"
22
+
23
+ # The main endpoint for removing the background.
24
+ @app.route('/remove-bg', methods=['POST'])
25
+ def remove_background_api():
26
+ # 1. --- API Key Authentication ---
27
+ api_key_header = request.headers.get('x-api-key')
28
+ if not api_key_header or api_key_header != API_KEY:
29
+ return jsonify({"error": "Unauthorized. Invalid or missing API Key."}), 401
30
+
31
+ # 2. --- Image Validation ---
32
+ if 'file' not in request.files:
33
+ return jsonify({"error": "No file part in the request"}), 400
34
+
35
+ file = request.files['file']
36
+
37
+ if file.filename == '':
38
+ return jsonify({"error": "No selected file"}), 400
39
+
40
+ # 3. --- Image Processing ---
41
+ if file:
42
+ try:
43
+ input_image_bytes = file.read()
44
+ output_image_bytes = remove(input_image_bytes)
45
+
46
+ # 4. --- Send the Response ---
47
+ return send_file(
48
+ io.BytesIO(output_image_bytes),
49
+ mimetype='image/png',
50
+ as_attachment=True,
51
+ download_name='background_removed.png'
52
+ )
53
+ except Exception as e:
54
+ return jsonify({"error": "Failed to process image", "details": str(e)}), 500
55
+
56
+ return jsonify({"error": "An unknown error occurred"}), 500
57
+
58
+ # --- Run the App ---
59
+ if __name__ == '__main__':
60
+ # For local testing, ensure the environment variable is set.
61
+ if not API_KEY:
62
+ # If you're using the temporary hard-coded key method, you can ignore this error.
63
+ # Otherwise, this reminds you to set the variable.
64
+ print("WARNING: BG_REMOVER_API_KEY is not set. The API will not be secured.")
65
+ print("For local testing, run 'set BG_REMOVER_API_KEY=your-key' (Windows) or 'export BG_REMOVER_API_KEY=your-key' (Mac/Linux) first.")
66
+
67
+ app.run(debug=True, port=5000)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # requirements.txt (Updated Version)
2
+
3
+ Flask
4
+ rembg
5
+ Pillow
6
+ gunicorn