Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ except:
|
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
-
from flask import Flask, render_template, Response, request, jsonify, send_file
|
| 19 |
import cv2
|
| 20 |
import numpy as np
|
| 21 |
import base64
|
|
@@ -26,6 +26,25 @@ import shutil
|
|
| 26 |
|
| 27 |
app = Flask(__name__)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Initialize the face detection and swapping models
|
| 30 |
app_model = FaceAnalysis(name='buffalo_l')
|
| 31 |
app_model.prepare(ctx_id=0, det_size=(640, 640))
|
|
@@ -50,11 +69,13 @@ default_face = faces_in_default_img[0]
|
|
| 50 |
|
| 51 |
|
| 52 |
@app.route('/')
|
|
|
|
| 53 |
def index():
|
| 54 |
return render_template('index.html')
|
| 55 |
|
| 56 |
|
| 57 |
@app.route('/process_frame', methods=['POST'])
|
|
|
|
| 58 |
def process_frame():
|
| 59 |
try:
|
| 60 |
data = request.json['image']
|
|
@@ -83,11 +104,13 @@ def process_frame():
|
|
| 83 |
|
| 84 |
|
| 85 |
@app.route('/get_current_face')
|
|
|
|
| 86 |
def get_current_face():
|
| 87 |
return send_file(default_img_path, mimetype='image/jpeg')
|
| 88 |
|
| 89 |
|
| 90 |
@app.route('/upload_face', methods=['POST'])
|
|
|
|
| 91 |
def upload_face():
|
| 92 |
if 'face' not in request.files:
|
| 93 |
return jsonify({'success': False, 'message': 'No file part'})
|
|
@@ -122,6 +145,7 @@ def upload_face():
|
|
| 122 |
|
| 123 |
|
| 124 |
@app.route('/reset_face', methods=['POST'])
|
|
|
|
| 125 |
def reset_face():
|
| 126 |
backup_path = 'assets/face.jpg'
|
| 127 |
if os.path.exists(backup_path):
|
|
@@ -138,5 +162,10 @@ def reset_face():
|
|
| 138 |
return jsonify({'success': False, 'message': 'No backup found'})
|
| 139 |
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
if __name__ == "__main__":
|
| 142 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
+
from flask import Flask, render_template, Response, request, jsonify, send_file, abort
|
| 19 |
import cv2
|
| 20 |
import numpy as np
|
| 21 |
import base64
|
|
|
|
| 26 |
|
| 27 |
app = Flask(__name__)
|
| 28 |
|
| 29 |
+
# Get the access key from Hugging Face environment variable
|
| 30 |
+
ACCESS_KEY = os.environ.get('KEY', 'default_access_key')
|
| 31 |
+
|
| 32 |
+
def check_auth():
|
| 33 |
+
"""Check if the request is authenticated with the correct key"""
|
| 34 |
+
provided_key = request.args.get('key')
|
| 35 |
+
if provided_key != ACCESS_KEY:
|
| 36 |
+
return False
|
| 37 |
+
return True
|
| 38 |
+
|
| 39 |
+
def require_auth(f):
|
| 40 |
+
"""Decorator to require authentication for routes"""
|
| 41 |
+
def decorated_function(*args, **kwargs):
|
| 42 |
+
if not check_auth():
|
| 43 |
+
abort(401) # Unauthorized
|
| 44 |
+
return f(*args, **kwargs)
|
| 45 |
+
decorated_function.__name__ = f.__name__
|
| 46 |
+
return decorated_function
|
| 47 |
+
|
| 48 |
# Initialize the face detection and swapping models
|
| 49 |
app_model = FaceAnalysis(name='buffalo_l')
|
| 50 |
app_model.prepare(ctx_id=0, det_size=(640, 640))
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
@app.route('/')
|
| 72 |
+
@require_auth
|
| 73 |
def index():
|
| 74 |
return render_template('index.html')
|
| 75 |
|
| 76 |
|
| 77 |
@app.route('/process_frame', methods=['POST'])
|
| 78 |
+
@require_auth
|
| 79 |
def process_frame():
|
| 80 |
try:
|
| 81 |
data = request.json['image']
|
|
|
|
| 104 |
|
| 105 |
|
| 106 |
@app.route('/get_current_face')
|
| 107 |
+
@require_auth
|
| 108 |
def get_current_face():
|
| 109 |
return send_file(default_img_path, mimetype='image/jpeg')
|
| 110 |
|
| 111 |
|
| 112 |
@app.route('/upload_face', methods=['POST'])
|
| 113 |
+
@require_auth
|
| 114 |
def upload_face():
|
| 115 |
if 'face' not in request.files:
|
| 116 |
return jsonify({'success': False, 'message': 'No file part'})
|
|
|
|
| 145 |
|
| 146 |
|
| 147 |
@app.route('/reset_face', methods=['POST'])
|
| 148 |
+
@require_auth
|
| 149 |
def reset_face():
|
| 150 |
backup_path = 'assets/face.jpg'
|
| 151 |
if os.path.exists(backup_path):
|
|
|
|
| 162 |
return jsonify({'success': False, 'message': 'No backup found'})
|
| 163 |
|
| 164 |
|
| 165 |
+
@app.errorhandler(401)
|
| 166 |
+
def unauthorized(error):
|
| 167 |
+
return jsonify({'error': 'Unauthorized access. Valid key required.'}), 401
|
| 168 |
+
|
| 169 |
+
|
| 170 |
if __name__ == "__main__":
|
| 171 |
app.run(host="0.0.0.0", port=7860)
|