Geek7 commited on
Commit
47cb3a7
·
verified ·
1 Parent(s): 7a2c85b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -1,11 +1,33 @@
1
- # app.py
 
 
 
 
2
 
3
- import os
4
- import subprocess
 
5
 
6
- if __name__ == "__main__":
7
- # Run awake.py in the background
8
- subprocess.Popen(["python", "wk.py"]) # Start awake.py
 
 
 
 
 
 
 
 
9
 
10
- # Run the Flask app using Gunicorn
11
- os.system("gunicorn -w 4 -b 0.0.0.0:5000 myapp:myapp") # 4 worker processes
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify, request, send_file
2
+ from flask_cors import CORS
3
+ from rembg import remove
4
+ from PIL import Image
5
+ import io
6
 
7
+ # Initialize the Flask app
8
+ myapp = Flask(__name__)
9
+ CORS(myapp) # Enable CORS if needed
10
 
11
+ @myapp.route('/')
12
+ def home():
13
+ return "Welcome to the Image Background Remover!" # Basic home response
14
+
15
+ @myapp.route('/remove_background', methods=['POST'])
16
+ def remove_background():
17
+ if 'image' not in request.files:
18
+ return jsonify({"error": "No image provided"}), 400
19
+
20
+ input_image = request.files['image'].read() # Read the uploaded image
21
+ output_bytes = remove(input_image) # Process the image with rembg
22
 
23
+ # Convert the output bytes back into a PIL image
24
+ output_image = Image.open(io.BytesIO(output_bytes))
25
+ img_byte_arr = io.BytesIO()
26
+ output_image.save(img_byte_arr, format='PNG')
27
+ img_byte_arr.seek(0)
28
+
29
+ return send_file(img_byte_arr, mimetype='image/png')
30
+
31
+ # Add this block to make sure your app runs when called
32
+ if __name__ == "__main__":
33
+ myapp.run(host='0.0.0.0', port=7860) # Run directly if needed for testing