140 lines
5.8 KiB
Python
140 lines
5.8 KiB
Python
from django.db.models.signals import post_save
|
||
from django.dispatch import receiver
|
||
from . import models
|
||
|
||
# @receiver(post_save, sender=models.Candidate)
|
||
# def parse_resume(sender, instance, created, **kwargs):
|
||
# if instance.resume and not instance.summary:
|
||
# from .utils import extract_summary_from_pdf,match_resume_with_job_description
|
||
# summary = extract_summary_from_pdf(instance.resume.path)
|
||
# if 'error' not in summary:
|
||
# instance.summary = summary
|
||
# instance.save()
|
||
|
||
# match_resume_with_job_description
|
||
|
||
import logging
|
||
logger = logging.getLogger(__name__)
|
||
import os
|
||
from .utils import extract_text_from_pdf,score_resume_with_openrouter
|
||
import asyncio
|
||
|
||
|
||
|
||
@receiver(post_save, sender=models.Candidate)
|
||
def score_candidate_resume(sender, instance, created, **kwargs):
|
||
# Skip if no resume or OpenRouter not configured
|
||
if instance.resume:
|
||
return
|
||
if kwargs.get('update_fields') is not None:
|
||
return
|
||
|
||
# Optional: Only re-score if resume changed (advanced: track file hash)
|
||
# For simplicity, we score on every save with a resume
|
||
|
||
try:
|
||
# Get absolute file path
|
||
file_path = instance.resume.path
|
||
if not os.path.exists(file_path):
|
||
logger.warning(f"Resume file not found: {file_path}")
|
||
return
|
||
|
||
resume_text = extract_text_from_pdf(file_path)
|
||
# if not resume_text:
|
||
# instance.scoring_error = "Could not extract text from resume."
|
||
# instance.save(update_fields=['scoring_error'])
|
||
# return
|
||
job_detail=str(instance.job.description)+str(instance.job.qualifications)
|
||
prompt1 = f"""
|
||
You are an expert resume parser and summarizer. Given a resume in plain text format, extract and organize the following key-value information into a clean, valid JSON object:
|
||
|
||
full_name: Full name of the candidate
|
||
current_title: Most recent or current job title
|
||
location: City and state (or country if outside the U.S.)
|
||
contact: Phone number and email (as a single string or separate fields)
|
||
linkedin: LinkedIn profile URL (if present)
|
||
github: GitHub or portfolio URL (if present)
|
||
summary: Brief professional profile or summary (1–2 sentences)
|
||
education: List of degrees, each with:
|
||
institution
|
||
degree
|
||
year
|
||
gpa (if provided)
|
||
relevant_courses (as a list, if mentioned)
|
||
skills: Grouped by category if possible (e.g., programming, big data, visualization), otherwise as a flat list of technologies/tools
|
||
experience: List of roles, each with:
|
||
company
|
||
job_title
|
||
location
|
||
start_date and end_date (or "Present" if applicable)
|
||
key_achievements (as a list of concise bullet points)
|
||
projects: List of notable projects (if clearly labeled), each with:
|
||
name
|
||
year
|
||
technologies_used
|
||
brief_description
|
||
Instructions:
|
||
|
||
Be concise but preserve key details.
|
||
Normalize formatting (e.g., “Jun. 2014” → “2014-06”).
|
||
Omit redundant or promotional language.
|
||
If a section is missing, omit the key or set it to null/empty list as appropriate.
|
||
Output only valid JSON—no markdown, no extra text.
|
||
Now, process the following resume text:
|
||
{resume_text}
|
||
"""
|
||
result = score_resume_with_openrouter(prompt1)
|
||
prompt = f"""
|
||
You are an expert technical recruiter. Your task is to score the following candidate for the role of a Senior Data Analyst based on the provided job criteria.
|
||
|
||
**Job Criteria:**
|
||
{job_detail}
|
||
|
||
**Candidate's Extracted Resume Json:**
|
||
\"\"\"
|
||
{result}
|
||
\"\"\"
|
||
|
||
**Your Task:**
|
||
Provide a response in strict JSON format with the following keys:
|
||
1. 'match_score': A score from 0 to 100 representing how well the candidate fits the role.
|
||
2. 'strengths': A brief summary of why the candidate is a strong fit, referencing specific criteria.
|
||
3. 'weaknesses': A brief summary of where the candidate falls short or what criteria are missing.
|
||
4. 'criteria_checklist': An object where you rate the candidate's match for each specific criterion (e.g., {{'Python': 'Met', 'AWS': 'Not Mentioned'}}).
|
||
|
||
|
||
Only output valid JSON. Do not include any other text.
|
||
"""
|
||
|
||
result1 = score_resume_with_openrouter(prompt)
|
||
|
||
instance.parsed_summary = str(result)
|
||
|
||
# Update candidate with scoring results
|
||
instance.match_score = result1.get('match_score')
|
||
instance.strengths = result1.get('strengths', '')
|
||
instance.weaknesses = result1.get('weaknesses', '')
|
||
instance.criteria_checklist = result1.get('criteria_checklist', {})
|
||
|
||
|
||
|
||
# Save only scoring-related fields to avoid recursion
|
||
instance.save(update_fields=[
|
||
'match_score', 'strengths', 'weaknesses',
|
||
'criteria_checklist','parsed_summary'
|
||
])
|
||
|
||
logger.info(f"Successfully scored resume for candidate {instance.id}")
|
||
|
||
except Exception as e:
|
||
# error_msg = str(e)[:500] # Truncate to fit TextField
|
||
# instance.scoring_error = error_msg
|
||
# instance.save(update_fields=['scoring_error'])
|
||
logger.error(f"Failed to score resume for candidate {instance.id}: {e}")
|
||
|
||
|
||
# @receiver(post_save,sender=models.Candidate)
|
||
# def trigger_scoring(sender,intance,created,**kwargs):
|
||
|
||
|
||
|