cv_job / app.py
saifeddinemk's picture
Init Commit
3efbf71
raw
history blame
2.5 kB
from sentence_transformers import SentenceTransformer, util
import gradio as gr
# Load the SentenceTransformer model for sentence similarity
try:
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
except Exception as e:
print(f"Error loading SentenceTransformer model: {e}")
def match_cv_to_job(cv_text, job_description):
debug_info = "Debug Info:\n"
# Encode the entire CV text
try:
cv_embedding = model.encode(cv_text, convert_to_tensor=True)
debug_info += f"CV Embedding: {cv_embedding}\n"
except Exception as e:
debug_info += f"Error encoding CV text: {e}\n"
return None, debug_info
# Encode the entire job description
try:
job_description_embedding = model.encode(job_description, convert_to_tensor=True)
debug_info += f"Job Description Embedding: {job_description_embedding}\n"
except Exception as e:
debug_info += f"Error encoding job description: {e}\n"
return None, debug_info
# Compute similarity score between the entire CV and the entire job description
try:
similarity_score = util.pytorch_cos_sim(cv_embedding, job_description_embedding).item()
debug_info += f"Overall Similarity Score: {similarity_score}\n"
# Convert similarity score to a percentage for easier interpretation
match_percentage = similarity_score * 100
debug_info += f"Overall Match Percentage: {match_percentage:.2f}%\n"
except Exception as e:
debug_info += f"Error calculating similarity score: {e}\n"
return None, debug_info
return {"Match Percentage": f"{match_percentage:.2f}%"}, debug_info
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# CV and Job Description Matcher with Overall Similarity Score")
# Input fields for CV and job description
cv_text = gr.Textbox(label="CV Text", placeholder="Enter the CV text here", lines=10)
job_description = gr.Textbox(label="Job Description", placeholder="Enter the entire job description text here", lines=10)
# Button and output area
match_button = gr.Button("Calculate Match Percentage")
output = gr.JSON(label="Match Result")
debug_output = gr.Textbox(label="Debug Info", lines=10) # Debug box for detailed output
# Set button click to run the function
match_button.click(fn=match_cv_to_job, inputs=[cv_text, job_description], outputs=[output, debug_output])
demo.launch()