Spaces:
Runtime error
Runtime error
Update app/routes.py
Browse files- app/routes.py +25 -30
app/routes.py
CHANGED
|
@@ -9,11 +9,12 @@ from .ranker import rank_resumes_by_semantic_similarity
|
|
| 9 |
import traceback
|
| 10 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 11 |
from .supabase_client import get_user_by_email, create_user
|
| 12 |
-
import os, requests
|
| 13 |
import uuid
|
| 14 |
from .supabase_config import SUPABASE_URL, SUPABASE_API_KEY
|
| 15 |
import secrets
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 19 |
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")
|
|
@@ -40,47 +41,41 @@ def embed():
|
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
def rank_resumes():
|
|
|
|
| 45 |
auth_header = request.headers.get('Authorization')
|
| 46 |
-
if not auth_header or not auth_header.startswith(
|
| 47 |
return jsonify({"error": "Missing or invalid Authorization header"}), 401
|
| 48 |
|
| 49 |
-
api_key = auth_header.split(" ")[1]
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
"apikey": SUPABASE_API_KEY,
|
| 54 |
-
"Authorization": f"Bearer {SUPABASE_API_KEY}"
|
| 55 |
-
}
|
| 56 |
-
user_check = requests.get(
|
| 57 |
f"{SUPABASE_URL}/rest/v1/users?api_key=eq.{api_key}",
|
| 58 |
-
headers=
|
| 59 |
)
|
| 60 |
|
| 61 |
-
if
|
| 62 |
return jsonify({"error": "Invalid API key"}), 403
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
resumes = request.files.getlist('resumes')
|
| 66 |
job_description = request.form.get('job_description')
|
|
|
|
| 67 |
|
| 68 |
-
if not
|
| 69 |
-
return jsonify({"error": "Missing
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return jsonify({
|
| 74 |
-
"job_description": job_description,
|
| 75 |
-
"ranked_resumes": ranked
|
| 76 |
-
})
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
auth = Blueprint('auth', __name__)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
@auth.route('/register', methods=['GET', 'POST'])
|
|
|
|
| 9 |
import traceback
|
| 10 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 11 |
from .supabase_client import get_user_by_email, create_user
|
|
|
|
| 12 |
import uuid
|
| 13 |
from .supabase_config import SUPABASE_URL, SUPABASE_API_KEY
|
| 14 |
import secrets
|
| 15 |
+
import os
|
| 16 |
+
import requests
|
| 17 |
+
api = Blueprint('api', __name__)
|
| 18 |
|
| 19 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 20 |
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@api.route('/rank-resumes', methods=['POST'])
|
| 47 |
def rank_resumes():
|
| 48 |
+
# 1. Extract and validate API key
|
| 49 |
auth_header = request.headers.get('Authorization')
|
| 50 |
+
if not auth_header or not auth_header.startswith("Bearer "):
|
| 51 |
return jsonify({"error": "Missing or invalid Authorization header"}), 401
|
| 52 |
|
| 53 |
+
api_key = auth_header.split(" ")[1]
|
| 54 |
|
| 55 |
+
# 2. Query Supabase to validate API key
|
| 56 |
+
response = requests.get(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
f"{SUPABASE_URL}/rest/v1/users?api_key=eq.{api_key}",
|
| 58 |
+
headers=HEADERS
|
| 59 |
)
|
| 60 |
|
| 61 |
+
if response.status_code != 200 or not response.json():
|
| 62 |
return jsonify({"error": "Invalid API key"}), 403
|
| 63 |
|
| 64 |
+
# 3. Parse form data
|
|
|
|
| 65 |
job_description = request.form.get('job_description')
|
| 66 |
+
resume_files = request.files.getlist('resumes')
|
| 67 |
|
| 68 |
+
if not job_description:
|
| 69 |
+
return jsonify({"error": "Missing job_description field"}), 400
|
| 70 |
+
if not resume_files:
|
| 71 |
+
return jsonify({"error": "No resumes uploaded"}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# 4. Rank resumes
|
| 74 |
+
try:
|
| 75 |
+
results = rank_resumes_by_semantic_similarity(resume_files, job_description)
|
| 76 |
+
return jsonify({"results": results}), 200
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return jsonify({"error": str(e)}), 500
|
| 79 |
|
| 80 |
|
| 81 |
@auth.route('/register', methods=['GET', 'POST'])
|