ghadaAlmuaikel commited on
Commit
1ca713e
1 Parent(s): 9d939dd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #information about job descriptions
2
+ data = {
3
+ 'Company': ['Google', 'Amazon', 'Microsoft', 'Facebook', 'Tesla'],
4
+ 'Job_Description': [
5
+ "We are looking for a Senior Software Engineer with extensive experience in Python, Java, and cloud computing. The candidate should have experience working in an Agile environment and a deep understanding of machine learning.",
6
+ "The Data Analyst will analyze large datasets to uncover trends, patterns, and insights. Proficiency in SQL, Python, and data visualization tools like PowerBI or Tableau is required.",
7
+ "Hiring a Cloud Architect with experience in Azure, AWS, and cloud infrastructure design. The ideal candidate should have experience with Docker, Kubernetes, and network security.",
8
+ "AI Research Scientist with expertise in machine learning, deep learning, and natural language processing (NLP). Experience with TensorFlow, PyTorch, and data-driven research.",
9
+ "Looking for an Electrical Engineer with experience in circuit design, power electronics, and embedded systems. Proficiency in CAD tools and simulation software is a must."
10
+ ]
11
+ }
12
+
13
+ # Load the job descriptions into a pandas DataFrame
14
+ df = pd.DataFrame(data)
15
+
16
+ lang_code_mapping = {
17
+ 'ar': 'arb_Arab', # Arabic
18
+ 'fr': 'fra_Latn', # French
19
+ 'es': 'spa_Latn', # Spanish
20
+ 'de': 'deu_Latn', # German
21
+ }
22
+
23
+ # Take CV and translate it to English if not in English to compare to job descriptions and return English CV and detected language
24
+ def translate_to_english(cv_text):
25
+ detected_lang = lang_code_mapping.get(detect(cv_text), "eng_Latn")
26
+
27
+ # Translate to English if not already in English
28
+ if detected_lang != 'eng_Latn':
29
+ translation = translator(cv_text, src_lang=detected_lang, tgt_lang="eng_Latn")[0]['translation_text']
30
+ return translation, detected_lang
31
+ else:
32
+ return cv_text, detected_lang
33
+
34
+ #if an entered cv is not in English return the job description in the entered cv language
35
+ def translate_job_description_if_needed(job_description, target_lang):
36
+ if target_lang != 'en':
37
+ return translator(job_description, src_lang="eng_Latn", tgt_lang=target_lang)[0]['translation_text']
38
+ return job_description
39
+
40
+ # Function to find top 3 job descriptions matching the CV using semantic similarity
41
+ def find_top_matches(cv_text):
42
+ if not cv_text:
43
+ return "Error: CV is empty", None
44
+
45
+ # Translate CV to English if it contains Arabic text
46
+ cv_text, detected_lang = translate_to_english(cv_text)
47
+ # Get job descriptions from the DataFrame
48
+ descriptions = df['Job_Description'].tolist()
49
+
50
+ # Encode both the CV and job descriptions
51
+ descriptions_embeddings = model.encode(descriptions, convert_to_tensor=True)
52
+ cv_embedding = model.encode([cv_text], convert_to_tensor=True)
53
+ # Calculate cosine similarities between the CV and all job descriptions
54
+ similarities = util.pytorch_cos_sim(cv_embedding, descriptions_embeddings)[0]
55
+
56
+ # Get the top 3 matches based on similarity scores
57
+ top_3_indices = similarities.argsort(descending=True)[:3] # Get the indices of the top 3 matches
58
+ top_3_matches = df.iloc[top_3_indices]
59
+ top_3_similarities = similarities[top_3_indices].numpy()
60
+
61
+ #create vertical bar
62
+ plt.bar(top_3_matches['Company'], top_3_similarities, color='skyblue')
63
+
64
+ # Set the labels and title
65
+ plt.ylabel('Similarity Score')
66
+ plt.xlabel('Company')
67
+ plt.title('Top 3 Job Description Matches')
68
+ # Create a detailed summary for the top 3 job descriptions
69
+ job_summaries = ""
70
+ for _, row in top_3_matches.iterrows():
71
+ # Translate job description if the detected language is not English
72
+ job_desc_translated = translate_job_description_if_needed(row['Job_Description'], detected_lang)
73
+
74
+ # Show job description only in the detected language if it's not English
75
+ job_summaries += f"<strong>Company:</strong> {row['Company']}<br>"
76
+ job_summaries += f"<strong>Job Description :</strong> {job_desc_translated}<br><br>"
77
+
78
+ return job_summaries, plt
79
+
80
+ # Define Gradio interface
81
+ demo = gr.Interface(
82
+ fn=find_top_matches,
83
+ inputs=gr.Textbox(lines=15, placeholder="Enter your CV text here...", label="CV Text"),
84
+ outputs=[
85
+ gr.HTML(label="Job Summaries"),
86
+ gr.Plot(label="Top 3 Matching Job Descriptions")
87
+ ],
88
+ title="'Match CV to Job Description",
89
+ description="Upload your CV to find the top 3 job descriptions that match from the available companies using semantic similarity."
90
+ )
91
+
92
+ # Launch the Gradio interface in Colab
93
+ demo.launch()