File size: 3,343 Bytes
56325dc
 
 
 
 
 
19ea0c5
 
 
 
56325dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19ea0c5
 
56325dc
 
 
 
 
 
 
 
 
 
 
19ea0c5
 
56325dc
19ea0c5
 
 
 
 
56325dc
19ea0c5
 
 
56325dc
 
 
 
 
 
 
19ea0c5
 
 
 
 
56325dc
 
 
 
 
 
 
 
 
 
 
19ea0c5
 
 
 
56325dc
19ea0c5
 
 
 
 
56325dc
19ea0c5
 
 
 
 
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
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, supabase
#from config import supabase

# These functions will be called in the main.py file

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, json=payload)

    # 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, email, summary):
    """Stores candidate data in Supabase"""
    data = {
        "name": candidate_name,
        "resume": resume_text,
        "score": score,
        "email": email,
        "summary": summary  # Store summary in Supabase
    }

    response = supabase.table("candidates").insert(data).execute()
    print("Inserted into Supabase:", response)  # Debugging output

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()
        summary = candidate.get("summary", "No summary available")  # Avoid KeyError
        page.insert_text(
            (50, 50),
            f"Candidate: {candidate['name']}\nEmail: {candidate['email']}\nScore: {candidate['score']}\nSummary: {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)

        # Generate summary (replace with actual summarization logic later)
        summary = f"{pdf_file.name} has a score of {score} for this job."

        candidates.append({
            "name": pdf_file.name,
            "resume": resume_text,
            "score": score,
            "email": email,
            "summary": summary
        })

        # Store all details including summary in Supabase
        store_in_supabase(resume_text, score, pdf_file.name, email, summary)

    return sorted(candidates, key=lambda x: x["score"], reverse=True)[:5]  # Return top 5 candidates