zayanomar5 commited on
Commit
f7cd617
·
verified ·
1 Parent(s): 02113e3

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +30 -19
main.py CHANGED
@@ -51,32 +51,43 @@ def health():
51
  # the llm will say the most similar job to the cv
52
  @app.route('/compare', methods=['POST'])
53
  def compare():
54
- jobs_skills = request.json.get('jobs_skills')
55
- employee_skills = request.json.get('employee_skills')
 
56
 
 
57
  if not isinstance(jobs_skills, list) or not all(isinstance(skill, str) for skill in jobs_skills):
58
- raise ValueError("jobs_skills must be a list of strings")
 
 
59
 
60
- # Encode job and employee skills
61
  job_embeddings = [model.encode(skill) for skill in jobs_skills]
62
- employee_embeddings = model.encode(employee_skills)
63
 
64
- # Calculate cosine similarity
65
- similarities = []
66
- employee_embedding_tensor = torch.tensor(employee_embeddings).unsqueeze(0)
67
- for job_embedding in job_embeddings:
68
- job_embedding_tensor = torch.tensor(job_embedding).unsqueeze(0)
69
- similarity = cosine_similarity(employee_embedding_tensor, job_embedding_tensor)
70
- similarities.append(similarity.item())
71
 
72
- # Find the job with highest similarity
73
- max_similarity_index = similarities.index(max(similarities))
74
- max_similarity_job = jobs_skills[max_similarity_index]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- # Return similarities for all jobs
77
- similarities_dict = {job: similarity for job, similarity in zip(jobs_skills, similarities)}
78
-
79
- return jsonify({'job': max_similarity_job, 'similarities': similarities_dict})
80
 
81
  if __name__ == '__main__':
82
  app.run()
 
51
  # the llm will say the most similar job to the cv
52
  @app.route('/compare', methods=['POST'])
53
  def compare():
54
+ data = request.json
55
+ jobs_skills = data.get('jobs_skills')
56
+ employees_skills = data.get('employees_skills')
57
 
58
+ # Validate input
59
  if not isinstance(jobs_skills, list) or not all(isinstance(skill, str) for skill in jobs_skills):
60
+ return jsonify({"error": "jobs_skills must be a list of strings"}), 400
61
+ if not isinstance(employees_skills, list) or not all(isinstance(skills, str) for skills in employees_skills):
62
+ return jsonify({"error": "employees_skills must be a list of strings"}), 400
63
 
64
+ # Encode job skills
65
  job_embeddings = [model.encode(skill) for skill in jobs_skills]
66
+ job_embeddings_tensor = torch.tensor(job_embeddings)
67
 
68
+ # Initialize a dictionary to store similarities for each employee
69
+ all_similarities = {}
 
 
 
 
 
70
 
71
+ for idx, employee_skills in enumerate(employees_skills):
72
+ # Encode employee skills
73
+ employee_embedding = model.encode(employee_skills)
74
+ employee_embedding_tensor = torch.tensor(employee_embedding).unsqueeze(0)
75
+
76
+ # Calculate cosine similarity
77
+ similarities = cosine_similarity(employee_embedding_tensor, job_embeddings_tensor)[0]
78
+
79
+ # Find the job with highest similarity for this employee
80
+ max_similarity_index = similarities.argmax()
81
+ max_similarity_job = jobs_skills[max_similarity_index]
82
+
83
+ # Store similarities in the dictionary
84
+ similarities_dict = {job: similarity for job, similarity in zip(jobs_skills, similarities)}
85
+ all_similarities[f'employee_{idx+1}'] = {
86
+ 'job': max_similarity_job,
87
+ 'similarities': similarities_dict
88
+ }
89
 
90
+ return jsonify(all_similarities)
 
 
 
91
 
92
  if __name__ == '__main__':
93
  app.run()