Spaces:
Running
Running
File size: 4,943 Bytes
4b10b93 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | from flask import Flask, request, jsonify
from flask_cors import CORS
import os
import tempfile
import shutil
from pathlib import Path
from uuid import uuid4
import fitz # PDF
from deepface import DeepFace
app = Flask(__name__)
CORS(app)
UPLOADS_DIR = Path("uploads")
UPLOADS_DIR.mkdir(exist_ok=True)
session_store = {}
resume_store = {}
# ========================
# SESSION
# ========================
def create_session():
return {
"reference_image": None,
"verified_frames": 0,
"total_frames": 0,
"terminated": False
}
def get_session(session_id):
if session_id not in session_store:
session_store[session_id] = create_session()
return session_store[session_id]
# ========================
# EXTRACT IMAGE FROM PDF
# ========================
def extract_image_from_pdf(file_path, resume_id):
doc = fitz.open(file_path)
for page_index in range(len(doc)):
page = doc[page_index]
images = page.get_images(full=True)
for img in images:
xref = img[0]
base = doc.extract_image(xref)
image_bytes = base["image"]
img_path = UPLOADS_DIR / f"{resume_id}.jpg"
with open(img_path, "wb") as f:
f.write(image_bytes)
return str(img_path)
return None
# ========================
# UPLOAD RESUME
# ========================
@app.route("/api/resumes/upload", methods=["POST"])
def upload_resume():
file = request.files.get("file")
name = request.form.get("name")
email = request.form.get("email")
if not file:
return jsonify({"error": "No file"}), 400
resume_id = str(uuid4())
file_path = UPLOADS_DIR / f"{resume_id}.pdf"
file.save(file_path)
# Extract image
image_path = extract_image_from_pdf(file_path, resume_id)
if not image_path:
return jsonify({
"error": "Resume must contain a clear photo"
}), 400
resume_store[resume_id] = {
"image_path": image_path
}
return jsonify({
"message": "Resume uploaded",
"resume_id": resume_id
})
# ========================
# START SESSION
# ========================
@app.route("/start", methods=["POST"])
def start():
data = request.json
session_id = data.get("session_id")
resume_id = data.get("resume_id")
if resume_id not in resume_store:
return jsonify({"error": "Invalid resume"}), 400
session = create_session()
session["reference_image"] = resume_store[resume_id]["image_path"]
session_store[session_id] = session
return jsonify({"message": "Session started"})
# ========================
# VERIFY FACE
# ========================
@app.route("/verify-face", methods=["POST"])
def verify_face():
session_id = request.form.get("session_id")
image = request.files.get("image")
session = get_session(session_id)
if session.get("terminated"):
return jsonify({
"terminated": True,
"reason": "Already terminated"
}), 403
if not image:
return jsonify({"error": "No image"}), 400
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp:
path = temp.name
image.save(path)
try:
result = DeepFace.verify(
img1_path=session["reference_image"],
img2_path=path,
enforce_detection=False
)
verified = result["verified"]
distance = result["distance"]
session["total_frames"] += 1
if verified:
session["verified_frames"] += 1
# 🚨 CHEATING DETECTION
if not verified:
session["terminated"] = True
return jsonify({
"terminated": True,
"reason": "CHEATING DETECTED",
"verified": False
}), 403
return jsonify({
"verified": True,
"distance": distance
})
except Exception as e:
return jsonify({"error": str(e)}), 500
finally:
os.remove(path)
# ========================
# END SESSION
# ========================
@app.route("/end", methods=["POST"])
def end():
data = request.json
session_id = data.get("session_id")
session = get_session(session_id)
total_frames = session.get("total_frames", 0)
verified_frames = session.get("verified_frames", 0)
verification_rate = (
(verified_frames / total_frames) * 100
if total_frames else 0
)
return jsonify({
"verification_rate": round(verification_rate, 2),
"status": "verified" if verification_rate > 60 else "suspicious",
"terminated": session.get("terminated", False),
"total_frames": total_frames,
"verified_frames": verified_frames,
})
# ========================
# RUN
# ========================
if __name__ == "__main__":
app.run(port=5000, debug=True) |