Update app.py
Browse files
app.py
CHANGED
@@ -40,52 +40,56 @@ def translate_to_english(cv_text):
|
|
40 |
else:
|
41 |
return cv_text, detected_lang
|
42 |
|
43 |
-
#if
|
44 |
def translate_job_description_if_needed(job_description, target_lang):
|
45 |
-
if target_lang != '
|
46 |
return translator(job_description, src_lang="eng_Latn", tgt_lang=target_lang)[0]['translation_text']
|
47 |
-
return job_description
|
48 |
-
|
49 |
-
# Function to find top 3 job descriptions matching the CV
|
50 |
def find_top_matches(cv_text):
|
51 |
if not cv_text:
|
52 |
return "Error: CV is empty", None
|
53 |
|
54 |
-
# Translate CV to
|
55 |
cv_text, detected_lang = translate_to_english(cv_text)
|
56 |
-
|
|
|
57 |
descriptions = df['Job_Description'].tolist()
|
58 |
|
59 |
-
# Encode both the CV and job descriptions
|
60 |
descriptions_embeddings = model.encode(descriptions, convert_to_tensor=True)
|
61 |
cv_embedding = model.encode([cv_text], convert_to_tensor=True)
|
62 |
-
# Calculate cosine similarities between the CV and all job descriptions
|
63 |
similarities = util.pytorch_cos_sim(cv_embedding, descriptions_embeddings)[0]
|
64 |
|
65 |
# Get the top 3 matches based on similarity scores
|
66 |
-
top_3_indices = similarities.argsort(descending=True)[:3] # Get the indices of the top 3 matches
|
67 |
top_3_matches = df.iloc[top_3_indices]
|
68 |
top_3_similarities = similarities[top_3_indices].numpy()
|
69 |
|
70 |
-
#create vertical bar
|
71 |
plt.bar(top_3_matches['Company'], top_3_similarities, color='skyblue')
|
72 |
-
|
73 |
-
# Set the labels and title
|
74 |
plt.ylabel('Similarity Score')
|
75 |
plt.xlabel('Company')
|
76 |
plt.title('Top 3 Job Description Matches')
|
|
|
77 |
# Create a detailed summary for the top 3 job descriptions
|
78 |
job_summaries = ""
|
79 |
for _, row in top_3_matches.iterrows():
|
80 |
# Translate job description if the detected language is not English
|
81 |
job_desc_translated = translate_job_description_if_needed(row['Job_Description'], detected_lang)
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
return job_summaries, plt
|
88 |
-
|
89 |
# Define Gradio interface
|
90 |
demo = gr.Interface(
|
91 |
fn=find_top_matches,
|
|
|
40 |
else:
|
41 |
return cv_text, detected_lang
|
42 |
|
43 |
+
#if entered cv is not in english return the job description in the entered cv langauge
|
44 |
def translate_job_description_if_needed(job_description, target_lang):
|
45 |
+
if target_lang != 'eng_Latn':
|
46 |
return translator(job_description, src_lang="eng_Latn", tgt_lang=target_lang)[0]['translation_text']
|
47 |
+
return job_description
|
48 |
+
|
49 |
+
# Function to find top 3 job descriptions matching the CV to job descriptions
|
50 |
def find_top_matches(cv_text):
|
51 |
if not cv_text:
|
52 |
return "Error: CV is empty", None
|
53 |
|
54 |
+
# Translate CV to english if not in english
|
55 |
cv_text, detected_lang = translate_to_english(cv_text)
|
56 |
+
|
57 |
+
# Get job descriptions from the DataFrame as list
|
58 |
descriptions = df['Job_Description'].tolist()
|
59 |
|
60 |
+
# Encode both the CV and job descriptions to calcuate cosine similarities
|
61 |
descriptions_embeddings = model.encode(descriptions, convert_to_tensor=True)
|
62 |
cv_embedding = model.encode([cv_text], convert_to_tensor=True)
|
|
|
63 |
similarities = util.pytorch_cos_sim(cv_embedding, descriptions_embeddings)[0]
|
64 |
|
65 |
# Get the top 3 matches based on similarity scores
|
66 |
+
top_3_indices = similarities.argsort(descending=True)[:3] # Get the indices of the top 3 matches
|
67 |
top_3_matches = df.iloc[top_3_indices]
|
68 |
top_3_similarities = similarities[top_3_indices].numpy()
|
69 |
|
70 |
+
#create vertical bar of top 3 match jobs to cv
|
71 |
plt.bar(top_3_matches['Company'], top_3_similarities, color='skyblue')
|
|
|
|
|
72 |
plt.ylabel('Similarity Score')
|
73 |
plt.xlabel('Company')
|
74 |
plt.title('Top 3 Job Description Matches')
|
75 |
+
|
76 |
# Create a detailed summary for the top 3 job descriptions
|
77 |
job_summaries = ""
|
78 |
for _, row in top_3_matches.iterrows():
|
79 |
# Translate job description if the detected language is not English
|
80 |
job_desc_translated = translate_job_description_if_needed(row['Job_Description'], detected_lang)
|
81 |
|
82 |
+
if detected_lang == 'arb_Arab':
|
83 |
+
# Use dir="rtl" for right-to-left languages
|
84 |
+
job_summaries += f'<div dir="rtl"><strong>الشركة</strong>: {row["Company"]} <br>'
|
85 |
+
job_summaries += f'<strong>وصف الوظيفه :</strong> {job_desc_translated}<br><br></div>'
|
86 |
+
else:
|
87 |
+
# Normal left-to-right display
|
88 |
+
job_summaries += f"<strong>Company:</strong> {row['Company']}<br>"
|
89 |
+
job_summaries += f"<strong>Job Description :</strong> {job_desc_translated}<br><br>"
|
90 |
|
91 |
return job_summaries, plt
|
92 |
+
|
93 |
# Define Gradio interface
|
94 |
demo = gr.Interface(
|
95 |
fn=find_top_matches,
|