HR-1 / app.py
DreamStream-1's picture
Update app.py
2080e4d verified
raw
history blame
10.4 kB
import os
import re
from datetime import datetime
import PyPDF2
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM
from sentence_transformers import SentenceTransformer, util
import gradio as gr
# --- Model Loading with Caching --- #
class ModelCache:
_tokenizers = {}
_models = {}
@classmethod
def get_model(cls, model_name):
if model_name not in cls._models:
cls._models[model_name] = AutoModelForSeq2SeqLM.from_pretrained(model_name)
return cls._models[model_name]
@classmethod
def get_tokenizer(cls, model_name):
if model_name not in cls._tokenizers:
cls._tokenizers[model_name] = AutoTokenizer.from_pretrained(model_name)
return cls._tokenizers[model_name]
# --- PDF/Text Extraction Functions --- #
def extract_text_from_file(file_path):
"""Extracts text from PDF or TXT files based on file extension."""
if file_path.endswith('.pdf'):
return extract_text_from_pdf(file_path)
elif file_path.endswith('.txt'):
return extract_text_from_txt(file_path)
else:
raise ValueError("Unsupported file type. Only PDF and TXT files are accepted.")
def extract_text_from_pdf(pdf_file_path):
"""Extracts text from a PDF file with logging for page extraction."""
text = []
with open(pdf_file_path, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
for i, page in enumerate(pdf_reader.pages):
page_text = page.extract_text()
if page_text:
text.append(page_text)
else:
print(f"Warning: Page {i} could not be extracted.")
return ''.join(text)
def extract_text_from_txt(txt_file_path):
"""Extracts text from a .txt file."""
with open(txt_file_path, 'r', encoding='utf-8') as txt_file:
return txt_file.read()
# --- Skill Extraction with Hugging Face --- #
def extract_skills_huggingface(text):
"""Extracts skills from the text using a Hugging Face model."""
model_name = "google/flan-t5-base"
tokenizer = ModelCache.get_tokenizer(model_name)
model = ModelCache.get_model(model_name)
input_text = f"Extract skills from the following text: {text}"
inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True)
outputs = model.generate(**inputs)
skills = tokenizer.decode(outputs[0], skip_special_tokens=True).split(', ') # Expecting a comma-separated list
return skills
# --- Job Description Processing Function --- #
def process_job_description(text):
"""Extracts skills or relevant keywords from the job description."""
return extract_skills_huggingface(text)
# --- Qualification and Experience Extraction --- #
def extract_qualifications(text):
"""Extracts qualifications from text (e.g., degrees, certifications)."""
qualifications = re.findall(r'\b(bachelor|master|phd|certified|degree|diploma|qualification|certification)\b', text, re.IGNORECASE)
return qualifications if qualifications else ['No specific qualifications found']
def extract_experience(text):
"""Extracts years of experience from the text."""
experience_years = re.findall(r'(\d+)\s*(years|year) of experience', text, re.IGNORECASE)
job_titles = re.findall(r'\b(software engineer|developer|manager|analyst)\b', text, re.IGNORECASE)
experience_years = [int(year[0]) for year in experience_years]
return experience_years, job_titles
# --- Semantic Similarity Calculation --- #
def calculate_semantic_similarity(text1, text2):
"""Calculates semantic similarity using a sentence transformer model and returns the score as a percentage."""
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
embeddings1 = model.encode(text1, convert_to_tensor=True)
embeddings2 = model.encode(text2, convert_to_tensor=True)
similarity_score = util.pytorch_cos_sim(embeddings1, embeddings2).item()
# Convert similarity score to percentage
similarity_percentage = similarity_score * 100
return similarity_percentage
# --- Thresholds --- #
def categorize_similarity(score):
"""Categorizes the similarity score into thresholds for better insights."""
if score >= 80:
return "High Match"
elif score >= 50:
return "Moderate Match"
else:
return "Low Match"
# --- Communication Generation with Enhanced Response --- #
def communication_generator(resume_skills, job_description_skills, skills_similarity, qualifications_similarity, experience_similarity, max_length=200):
"""Generates a more detailed communication response based on similarity scores."""
model_name = "google/flan-t5-base"
tokenizer = ModelCache.get_tokenizer(model_name)
model = ModelCache.get_model(model_name)
# Assess candidate fit based on similarity scores
fit_status = "strong fit" if skills_similarity >= 80 and qualifications_similarity >= 80 and experience_similarity >= 80 else \
"moderate fit" if skills_similarity >= 50 else "weak fit"
# Create a detailed communication message based on match levels
message = (
f"After a detailed analysis of the candidate's resume, we found the following insights:\n\n"
f"- **Skills Match**: {skills_similarity:.2f}% ({categorize_similarity(skills_similarity)})\n"
f"- **Qualifications Match**: {qualifications_similarity:.2f}% ({categorize_similarity(qualifications_similarity)})\n"
f"- **Experience Match**: {experience_similarity:.2f}% ({categorize_similarity(experience_similarity)})\n\n"
f"The overall assessment indicates that the candidate is a {fit_status} for the role. "
f"Skills such as {', '.join(resume_skills)} align {categorize_similarity(skills_similarity).lower()} with the job's requirements of {', '.join(job_description_skills)}. "
f"In terms of qualifications and experience, the candidate shows a {categorize_similarity(qualifications_similarity).lower()} match with the role's needs. "
f"Based on these findings, we believe the candidate could potentially excel in the role, "
f"but additional evaluation or interviews are recommended for further clarification."
)
inputs = tokenizer(message, return_tensors="pt", padding=True, truncation=True)
response = model.generate(**inputs, max_length=max_length, num_beams=4, early_stopping=True)
return tokenizer.decode(response[0], skip_special_tokens=True)
# --- Sentiment Analysis --- #
def sentiment_analysis(text):
"""Analyzes the sentiment of the text using a Hugging Face model."""
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = ModelCache.get_tokenizer(model_name)
model = ModelCache.get_model(model_name)
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
with torch.no_grad():
outputs = model(**inputs)
predicted_sentiment = torch.argmax(outputs.logits).item()
return ["Negative", "Neutral", "Positive"][predicted_sentiment]
# --- Updated Resume Analysis Function --- #
def analyze_resume(resume_file, job_description_file):
"""Analyzes the resume and job description, returning similarity score, skills, qualifications, and experience matching."""
# Extract resume and job description text
try:
resume_text = extract_text_from_file(resume_file.name)
job_description_text = extract_text_from_file(job_description_file.name)
except ValueError as ve:
return str(ve)
# Extract skills, qualifications, and experience
resume_skills = extract_skills_huggingface(resume_text)
job_description_skills = process_job_description(job_description_text)
resume_qualifications = extract_qualifications(resume_text)
job_description_qualifications = extract_qualifications(job_description_text)
resume_experience, resume_job_titles = extract_experience(resume_text)
job_description_experience, job_description_titles = extract_experience(job_description_text)
# Calculate semantic similarity for different sections in percentages
skills_similarity = calculate_semantic_similarity(' '.join(resume_skills), ' '.join(job_description_skills))
qualifications_similarity = calculate_semantic_similarity(' '.join(resume_qualifications), ' '.join(job_description_qualifications))
experience_similarity = calculate_semantic_similarity(' '.join([str(e) for e in resume_experience]), ' '.join([str(e) for e in job_description_experience]))
# Generate a communication response based on the similarity percentages
communication_response = communication_generator(
resume_skills, job_description_skills,
skills_similarity, qualifications_similarity, experience_similarity
)
# Perform Sentiment Analysis
sentiment = sentiment_analysis(resume_text)
# Return the results including thresholds and percentage scores
return (
f"Skills Similarity: {skills_similarity:.2f}% ({categorize_similarity(skills_similarity)})",
f"Qualifications Similarity: {qualifications_similarity:.2f}% ({categorize_similarity(qualifications_similarity)})",
f"Experience Similarity: {experience_similarity:.2f}% ({categorize_similarity(experience_similarity)})",
communication_response,
f"Sentiment Analysis: {sentiment}",
f"Resume Skills: {', '.join(resume_skills)}",
f"Job Description Skills: {', '.join(job_description_skills)}",
f"Resume Qualifications: {', '.join(resume_qualifications)}",
f"Job Description Qualifications: {', '.join(job_description_qualifications)}",
f"Resume Experience: {', '.join(map(str, resume_experience))} years, Titles: {', '.join(resume_job_titles)}",
f"Job Description Experience: {', '.join(map(str, job_description_experience))} years, Titles: {', '.join(job_description_titles)}"
)
# --- Gradio Interface --- #
iface = gr.Interface(
fn=analyze_resume,
inputs=["file", "file"],
outputs=[
"text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text"
],
title="Resume Analysis Tool",
description="Analyze a resume against a job description to evaluate skills, qualifications, experience, and sentiment."
)
if __name__ == "__main__":
iface.launch()