Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModel
|
| 2 |
-
import pdfplumber
|
| 3 |
import torch
|
| 4 |
-
|
| 5 |
-
import re
|
| 6 |
|
| 7 |
-
|
| 8 |
-
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
-
model = AutoModel.from_pretrained(model_name)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return text
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
text =
|
| 24 |
-
|
|
|
|
| 25 |
return text
|
| 26 |
|
| 27 |
-
# Function to
|
| 28 |
-
def
|
| 29 |
-
inputs = tokenizer(
|
| 30 |
with torch.no_grad():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def lic_profile_matcher(job_description, resume_pdf):
|
| 44 |
-
# Extract text from PDF resume
|
| 45 |
-
resume_text = extract_text_from_pdf(resume_pdf)
|
| 46 |
-
|
| 47 |
-
# Preprocess the text (clean and standardize)
|
| 48 |
-
processed_resume = preprocess_text(resume_text)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
if similarity_score > 0.7:
|
| 55 |
-
return f"Candidate is a good fit with a similarity score of {similarity_score:.2f}."
|
| 56 |
else:
|
| 57 |
-
return
|
| 58 |
-
|
| 59 |
-
# Example job description for LIC role
|
| 60 |
-
job_description = """
|
| 61 |
-
We are looking for a motivated sales agent with experience in selling life insurance products.
|
| 62 |
-
Experience in customer service, understanding of insurance policies, and excellent communication skills are required.
|
| 63 |
-
"""
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
print(result)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import docx
|
| 3 |
+
import fitz # PyMuPDF for PDF extraction
|
| 4 |
from transformers import AutoTokenizer, AutoModel
|
|
|
|
| 5 |
import torch
|
| 6 |
+
import os
|
|
|
|
| 7 |
|
| 8 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Load the Hugging Face tokenizer and model for semantic textual similarity
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
| 12 |
+
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
| 13 |
+
|
| 14 |
+
# Function to extract text from PDF
|
| 15 |
+
def extract_text_from_pdf(pdf_path):
|
| 16 |
+
doc = fitz.open(pdf_path)
|
| 17 |
+
text = ""
|
| 18 |
+
for page in doc:
|
| 19 |
+
text += page.get_text()
|
| 20 |
return text
|
| 21 |
|
| 22 |
+
# Function to extract text from DOCX
|
| 23 |
+
def extract_text_from_docx(docx_path):
|
| 24 |
+
doc = docx.Document(docx_path)
|
| 25 |
+
text = ""
|
| 26 |
+
for para in doc.paragraphs:
|
| 27 |
+
text += para.text + "\n"
|
| 28 |
return text
|
| 29 |
|
| 30 |
+
# Function to calculate semantic similarity score
|
| 31 |
+
def get_similarity_score(text1, text2):
|
| 32 |
+
inputs = tokenizer([text1, text2], padding=True, truncation=True, return_tensors='pt')
|
| 33 |
with torch.no_grad():
|
| 34 |
+
embeddings = model(**inputs)
|
| 35 |
+
sentence_embeddings = embeddings.last_hidden_state.mean(dim=1)
|
| 36 |
+
similarity_score = torch.nn.functional.cosine_similarity(sentence_embeddings[0], sentence_embeddings[1], dim=0)
|
| 37 |
+
return similarity_score.item()
|
| 38 |
+
|
| 39 |
+
# API endpoint to process the resume and calculate similarity with LIC profile
|
| 40 |
+
@app.route('/score_resume', methods=['POST'])
|
| 41 |
+
def score_resume():
|
| 42 |
+
if 'file' not in request.files:
|
| 43 |
+
return jsonify({"error": "No file part"}), 400
|
| 44 |
+
file = request.files['file']
|
| 45 |
+
lic_profile = request.form.get('lic_profile', '') # LIC profile text to compare against
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
if file.filename.endswith('.pdf'):
|
| 48 |
+
resume_text = extract_text_from_pdf(file)
|
| 49 |
+
elif file.filename.endswith('.docx'):
|
| 50 |
+
resume_text = extract_text_from_docx(file)
|
|
|
|
|
|
|
| 51 |
else:
|
| 52 |
+
return jsonify({"error": "Invalid file type. Please upload a PDF or DOCX file."}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
if not lic_profile:
|
| 55 |
+
return jsonify({"error": "LIC profile text is required."}), 400
|
| 56 |
+
|
| 57 |
+
# Calculate the similarity score between resume and LIC profile
|
| 58 |
+
score = get_similarity_score(resume_text, lic_profile)
|
| 59 |
+
|
| 60 |
+
return jsonify({"similarity_score": score})
|
| 61 |
|
| 62 |
+
if __name__ == '__main__':
|
| 63 |
+
app.run(debug=True)
|
|
|