| from flask import Flask, request, send_file, jsonify |
| from flask_cors import CORS |
| from rembg import remove |
| from PIL import Image |
| import io |
| from datetime import datetime, timedelta |
|
|
| app = Flask(__name__) |
| CORS(app) |
|
|
| |
| @app.route('/') |
| def home(): |
| return {'message': 'Background Remover API - Use POST /remove_bg'}, 200 |
|
|
| |
| @app.route('/health', methods=['GET']) |
| def health_check(): |
| return {'status': 'healthy'}, 200 |
|
|
| |
| @app.route('/ping', methods=['GET']) |
| def ping(): |
| """Simple ping endpoint to keep the service alive""" |
| return jsonify({ |
| "status": "alive", |
| "timestamp": datetime.utcnow().isoformat(), |
| "message": "Pong! Service is awake." |
| }), 200 |
|
|
| @app.route('/remove_bg', methods=['POST']) |
| def remove_bg(): |
| if 'file' not in request.files: |
| return {'error': 'No file uploaded'}, 400 |
| |
| file = request.files['file'] |
| |
| try: |
| input_image = Image.open(file.stream) |
| output_image = remove(input_image) |
| |
| img_byte_arr = io.BytesIO() |
| output_image.save(img_byte_arr, format='PNG') |
| img_byte_arr.seek(0) |
| |
| return send_file(img_byte_arr, mimetype='image/png') |
| |
| except Exception as e: |
| return {'error': str(e)}, 500 |
|
|
| if __name__ == '__main__': |
| app.run(host='0.0.0.0', port=5000) |