Spaces:
Runtime error
Runtime error
import os | |
import PyPDF2 | |
import re | |
import torch | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM | |
from groq import Groq | |
import gradio as gr | |
from docxtpl import DocxTemplate | |
from datetime import datetime | |
# Set your API key | |
os.environ["GROQ_API_KEY"] = "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" | |
# Initialize Groq client | |
client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
# --- Resume Extraction Functions --- | |
def extract_text_from_pdf(pdf_file_path): | |
"""Extracts text from a PDF file.""" | |
with open(pdf_file_path, 'rb') as pdf_file: | |
pdf_reader = PyPDF2.PdfReader(pdf_file) | |
text = '' | |
for page in range(len(pdf_reader.pages)): | |
text += pdf_reader.pages[page].extract_text() | |
return text | |
def extract_text_from_txt(txt_file_path): | |
"""Extracts text from a .txt file.""" | |
with open(txt_file_path, 'r') as txt_file: | |
text = txt_file.read() | |
return text | |
# --- Skill Extraction with Llama Model --- | |
def extract_skills_llama(text): | |
"""Extracts skills from the text using the Llama model via Groq API.""" | |
chat_completion = client.chat.completions.create( | |
messages=[ | |
{ | |
"role": "user", | |
"content": f"Extract skills from the following text: {text}", | |
} | |
], | |
model="llama3-70b-8192", # Using Llama model | |
) | |
skills = chat_completion.choices[0].message.content.split(', ') # Assuming skills are returned as a comma-separated list | |
return skills | |
# --- Job Description Processing --- | |
def process_job_description(job_description_text): | |
"""Processes the job description text.""" | |
# 1. Preprocess the job description text | |
job_description_text = preprocess_text(job_description_text) | |
# 2. Extract skills from the job description using Llama | |
job_description_skills = extract_skills_llama(job_description_text) | |
return job_description_skills | |
# --- Text Preprocessing --- | |
def preprocess_text(text): | |
"""Preprocesses text for better analysis.""" | |
text = text.lower() # Convert to lowercase | |
text = re.sub(r'[^\w\s]', '', text) # Remove punctuation | |
text = re.sub(r'\s+', ' ', text) # Remove extra whitespace | |
return text | |
# --- Resume Similarity --- | |
def calculate_resume_similarity(resume_text, job_description_text): | |
"""Calculates the similarity between the resume and job description using a Hugging Face model.""" | |
model_name = "cross-encoder/stsb-roberta-base" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
inputs = tokenizer(resume_text, job_description_text, return_tensors="pt", padding=True, truncation=True, max_length=512) | |
with torch.no_grad(): | |
outputs = model(**inputs) | |
similarity_score = torch.sigmoid(outputs.logits).item() | |
return similarity_score | |
# --- Communication Generation --- | |
def communication_generator(message, max_length=100): | |
"""Generates a communication response based on the input message using a Hugging Face model.""" | |
model_name = "google/flan-t5-base" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
inputs = tokenizer(message, return_tensors="pt", padding=True, truncation=True, max_length=512) | |
response = model.generate(**inputs, max_length=max_length, num_beams=4, early_stopping=True) | |
generated_response = tokenizer.batch_decode(response, skip_special_tokens=True)[0] | |
return generated_response + " We look forward to getting in touch with you soon!" | |
# --- Sentiment Analysis --- | |
def sentiment_model(text): | |
"""Analyzes the sentiment of the text using a Hugging Face model.""" | |
model_name = "distilbert-base-uncased-finetuned-sst-3-literal" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512) | |
with torch.no_grad(): | |
outputs = model(**inputs) | |
predicted_class = torch.argmax(outputs.logits).item() | |
sentiment_labels = {0: "Negative", 1: "Neutral", 2: "Positive"} | |
return sentiment_labels[predicted_class] | |
# --- Placeholder Functions for Enhancement --- | |
def enhance_resume(resume_text): | |
"""Placeholder function for enhancing the resume (you can implement your own logic here).""" | |
return resume_text | |
def enhance_job_description(job_description_text): | |
"""Placeholder function for enhancing the job description (you can implement your own logic here).""" | |
return job_description_text | |
# --- Resume Analysis Function --- | |
def analyze_resume(resume_file, job_description_file): | |
"""Analyzes the resume and job description.""" | |
if resume_file.name.endswith(('.pdf', '.txt')): | |
if resume_file.name.endswith('.pdf'): | |
resume_text = extract_text_from_pdf(resume_file.name) | |
else: | |
resume_text = extract_text_from_txt(resume_file.name) | |
else: | |
return "Invalid file type. Please upload a PDF or TXT file for the resume." | |
if job_description_file.name.endswith('.txt'): | |
job_description_text = extract_text_from_txt(job_description_file.name) | |
else: | |
return "Invalid file type. Please upload a TXT file for the job description." | |
job_description_skills = process_job_description(job_description_text) | |
resume_skills = extract_skills_llama(resume_text) | |
similarity_score = calculate_resume_similarity(resume_text, job_description_text) | |
communication_response = communication_generator(f"I am reviewing a resume for a {job_description_text} position. The candidate has the following skills: {', '.join(resume_skills)}") | |
sentiment = sentiment_model(resume_text) | |
enhanced_resume = enhance_resume(resume_text) | |
enhanced_job_description = enhance_job_description(job_description_text) | |
return ( | |
f"## Resume and Job Description Analysis", | |
f"**Similarity Score:** {similarity_score:.2f}", | |
f"**Communication Response:** {communication_response}", | |
f"**Sentiment:** {sentiment}", | |
f"**Resume Skills:** {', '.join(resume_skills)}", | |
f"**Job Description Skills:** {', '.join(job_description_skills)}", | |
f"**Enhanced Resume:**\n{enhanced_resume}", | |
f"**Enhanced Job Description:**\n{enhanced_job_description}", | |
) | |
# --- Offer Letter Generation --- | |
def generate_offer_letter(template_file, candidate_name, role, start_date, hours): | |
"""Generates an offer letter.""" | |
# Parse the start date string | |
try: | |
start_date = datetime.strptime(start_date, "%Y-%m-%d").strftime("%B %d, %Y") # Format for DocxTemplate | |
except ValueError: | |
return "Invalid date format. Please use YYYY-MM-DD." | |
# Define the context variables | |
context = { | |
'candidate_name': candidate_name, | |
'role': role, | |
'start_date': start_date, | |
'hours': hours, | |
} | |
# Load the template document and render it with the context variables | |
tpl = DocxTemplate(template_file.name) | |
tpl.render(context) | |
# Save the generated document | |
script_dir = os.path.dirname(os.path.abspath(__file__)) | |
docx_file_path = os.path.join(script_dir, f"{candidate_name}_offer_letter.docx") | |
tpl.save(docx_file_path) | |
# Return the file object | |
return open(docx_file_path, 'rb') | |
# --- Gradio Interface --- | |
demo = gr.Interface( | |
fn=analyze_resume, | |
inputs=[ | |
gr.File(label="Upload Resume (PDF or TXT)"), | |
gr.File(label="Upload Job Description (TXT)"), | |
], | |
outputs=[ | |
gr.Textbox(label="Similarity Score"), | |
gr.Textbox(label="Communication Response"), | |
gr.Textbox(label="Sentiment Analysis"), | |
gr.Textbox(label="Resume Skills"), | |
gr.Textbox(label="Job Description Skills"), | |
gr.Textbox(label="Enhanced Resume", lines=20), | |
gr.Textbox(label="Enhanced Job Description", lines=10), | |
], | |
title="Resume and Job Description Analyzer", | |
description="Upload your resume (PDF or TXT) and job description (TXT) to analyze their similarity, extract skills, and generate a communication response.", | |
) | |
offer_demo = gr.Interface( | |
fn=generate_offer_letter, | |
inputs=[ | |
gr.File(label="Upload Offer Letter Template (DOCX)"), | |
gr.Textbox(label="Candidate Name"), | |
gr.Textbox(label="Role"), | |
gr.Textbox(label="Start Date (YYYY-MM-DD)"), # Use Textbox for date | |
gr.Number(label="Hours per Week"), | |
], | |
outputs=gr.File(label="Offer Letter"), # Change to gr.File | |
title="Offer Letter Generator", | |
description="Upload an offer letter template and enter candidate information to generate an offer letter.", | |
) | |
# Combine the interfaces using a Tabbed interface | |
demo = gr.TabbedInterface( | |
[demo, offer_demo], | |
["Resume Analyzer", "Offer Letter Generator"], | |
title="HR Assistant", | |
) | |
if __name__ == '__main__': | |
demo.launch(share=True) | |