CVAnalyzer / utils.py
Danial7's picture
Update utils.py
b5b5de2 verified
import spacy
from spacy.cli import download
import fitz # PyMuPDF
import re
from transformers import pipeline
def load_models():
# Try loading spaCy model; download if missing
try:
nlp = spacy.load("en_core_web_sm")
except OSError:
download("en_core_web_sm")
nlp = spacy.load("en_core_web_sm")
# Use a fast summarization model instead of GPT-2
llm = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
return nlp, llm
def parse_resume(uploaded_file, nlp):
doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
text = "\n".join(page.get_text() for page in doc)
doc = nlp(text)
name = [ent.text for ent in doc.ents if ent.label_ == "PERSON"]
email = re.findall(r"[\w\.-]+@[\w\.-]+", text)
skills = [token.text.lower() for token in doc if token.pos_ == "NOUN"]
edu_keywords = ["bachelor", "master", "phd", "degree", "certification", "diploma"]
edu = [sent.text for sent in doc.sents if any(k in sent.text.lower() for k in edu_keywords)]
return text, {
"name": name[0] if name else "N/A",
"email": email[0] if email else "N/A",
"skills": list(set(skills)),
"education": edu,
}
def get_recommendations(parsed):
score = 50 + len(parsed["skills"]) % 50
feedback = "Try adding more specific technical skills and quantifiable achievements."
return score, feedback
def generate_career_insights(parsed, llm, suggestion_type="roadmap"):
name = parsed.get("name", "Candidate")
skills = ", ".join(parsed["skills"][:10]) if parsed["skills"] else "unspecified"
education = "; ".join(parsed["education"][:3]) if parsed["education"] else "not mentioned"
input_text_map = {
"certifications": (
f"Candidate has skills in: {skills}. With education: {education}. "
"Summarize relevant certifications they can pursue."
),
"degrees": (
f"Given the education background: {education}, summarize higher education degrees "
"that can help in career advancement."
),
"roadmap": (
f"Create a short 1-year career roadmap for someone with skills: {skills} "
f"and education: {education}. Suggest goals."
),
"counselor": (
f"As a career advisor, suggest top 3 career moves for a person skilled in {skills} "
f"with education in {education}."
)
}
result = llm(input_text_map[suggestion_type], max_length=150, min_length=50, do_sample=False)
return result[0]["summary_text"]