File size: 2,810 Bytes
56325dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fitz  # PyMuPDF for PDF processing
import requests
import json
import re
from io import BytesIO
import supabase
from config import SUPABASE_URL, SUPABASE_KEY, HF_API_TOKEN, HF_API_URL, HF_HEADERS

def parse_resume(pdf_file):
    """Extracts text from a resume PDF."""
    doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
    text = "\n".join([page.get_text("text") for page in doc])
    return text

def extract_email(resume_text):
    """Extracts an email address from resume text."""
    match = re.search(r"[\w\.-]+@[\w\.-]+", resume_text)
    return match.group(0) if match else None

def score_candidate(resume_text, job_description):
    """Sends resume and job description to Hugging Face for scoring."""
    payload = {"inputs": f"Resume: {resume_text}\nJob Description: {job_description}"}
    response = requests.post(HF_API_URL, headers=HF_HEADERS, data=json.dumps(payload))
    return response.json().get("score", 0)
    # Debugging: Print response
    if response.status_code != 200:
        print(f"Error: {response.status_code}, {response.text}")  # Log any errors
        return 0  # Return default score if API fails

    try:
        return response.json().get("score", 0)
    except requests.exceptions.JSONDecodeError:
        print("Failed to decode JSON response:", response.text)  # Debugging output
        return 0  # Return default score if JSON decoding fails

def store_in_supabase(resume_text, score, candidate_name):
    """Stores resume data in Supabase."""
    email = extract_email(resume_text)
    data = {
        "name": candidate_name, 
        "resume": resume_text, 
        "score": score, 
        "email": email
    }
    supabase.table("candidates").insert(data).execute()

def generate_pdf_report(shortlisted_candidates):
    """Generates a PDF summary of shortlisted candidates."""
    pdf = BytesIO()
    doc = fitz.open()
    for candidate in shortlisted_candidates:
        page = doc.new_page()
        page.insert_text((50, 50), f"Candidate: {candidate['name']}\nEmail: {candidate['email']}\nScore: {candidate['score']}\nSummary: {candidate['summary']}")
    doc.save(pdf)
    pdf.seek(0)
    return pdf

def process_resumes(uploaded_files, job_description):
    """Processes uploaded resumes and returns shortlisted candidates."""
    candidates = []
    for pdf_file in uploaded_files:
        resume_text = parse_resume(pdf_file)
        score = score_candidate(resume_text, job_description)
        email = extract_email(resume_text)
        candidates.append({
            "name": pdf_file.name, 
            "resume": resume_text, 
            "score": score, 
            "email": email
        })
        store_in_supabase(resume_text, score, pdf_file.name, email)
    return sorted(candidates, key=lambda x: x["score"], reverse=True)[:5]