benixryr commited on
Commit
17b5d19
·
1 Parent(s): 27d8c3c

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +85 -0
main.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import SentenceTransformer, util
2
+ from flask import Flask, request
3
+ import json
4
+ import logging
5
+ class JobOffer:
6
+ def __init__(self, title, description, skills):
7
+ self.title = title
8
+ self.description = description
9
+ self.skills = skills
10
+
11
+ class JobApplicant:
12
+ def __init__(self, id, skills):
13
+ self.id = id
14
+ self.skills = skills
15
+ self.score = 0
16
+
17
+ class JobApplicantEncoder(json.JSONEncoder):
18
+ def default(self, o):
19
+ return o.__dict__
20
+
21
+ app = Flask(__name__)
22
+
23
+ model_name = "Sahajtomar/french_semantic"
24
+ model_name = "dangvantuan/sentence-camembert-base"
25
+ model_name = "dangvantuan/sentence-camembert-large"
26
+ model = SentenceTransformer(model_name)
27
+
28
+
29
+ def get_cosine_score(job_applicant, job_offer):
30
+ embeddings1 = model.encode(job_applicant, convert_to_tensor=True)
31
+ embeddings2 = model.encode(job_offer, convert_to_tensor=True)
32
+ cosine_scores = util.pytorch_cos_sim(embeddings1, embeddings2)
33
+ return cosine_scores[0][0].item()
34
+
35
+ @app.route('/process/', methods=['POST'])
36
+ def get_cosinos():
37
+ #Map the input strings to object.
38
+ data = request.get_json()
39
+ temp_job_offer = data["job_offer"]
40
+ temp_job_applicant = data["job_applicant"]
41
+ assert temp_job_offer is not None
42
+ assert temp_job_applicant is not None
43
+ job_offer = JobOffer(temp_job_offer["title"], temp_job_offer["description"], temp_job_offer["skills"])
44
+ job_applicant_list = []
45
+ for job_applicant in temp_job_applicant:
46
+ job_applicant_list.append(JobApplicant(job_applicant["id"], job_applicant["skills"]))
47
+
48
+ #Compute the cosine score between the job offer and the job applicant
49
+ for job_applicant in job_applicant_list:
50
+ job_applicant.score = get_cosine_score(job_applicant.skills, job_offer.skills)
51
+
52
+ return json.dumps(job_applicant_list, cls=JobApplicantEncoder)
53
+
54
+
55
+ # embeddings1 = model.encode(job_applicant, convert_to_tensor=True)
56
+ # embeddings2 = model.encode(job_offer, convert_to_tensor=True)
57
+ # cosine_scores = util.pytorch_cos_sim(embeddings1, embeddings2)
58
+
59
+ # # Create a list of tuples containing sentence and score
60
+ # results = []
61
+ # for i in range(len(job_applicant)):
62
+ # for j in range(len(job_offer)):
63
+ # results.append((job_applicant[i], job_offer[j], cosine_scores[i][j]))
64
+
65
+ # # Sort the list based on the scores in descending order
66
+ # results.sort(key=lambda x: x[2], reverse=True)
67
+
68
+ # # Print the sentences in the sorted order
69
+ # for result in results:
70
+ # print("Sentence 1:", result[0])
71
+ # print("Sentence 2:", result[1])
72
+ # print("Similarité cosinus:", result[2])
73
+ # print("-" * 50)
74
+
75
+ if __name__ == '__main__':
76
+ logging.info("Starting server...")
77
+ resp = get_cosine_score("Je ne suis pas un développeur web", "Nous recherchons un développeur web qui sache faire du React, du NodeJS et du MongoDB")
78
+ print(f"0 : {resp}")
79
+ resp = get_cosine_score("Je ne suis pas un développeur web, j'ai aucune compétences en React", "Nous recherchons un développeur web qui sache faire du React, du NodeJS et du MongoDB")
80
+ print(f"1 : {resp}")
81
+ resp = get_cosine_score("Je ne suis pas un développeur web, j'ai aucune compétences en React et MongoDB", "Nous recherchons un développeur web qui sache faire du React, du NodeJS et du MongoDB")
82
+ print(f"2 : {resp}")
83
+ resp = get_cosine_score("Je ne suis pas un développeur web, j'ai aucune compétences en React, MongoDB et NodeJS", "Nous recherchons un développeur web qui sache faire du React, du NodeJS et du MongoDB")
84
+ print(f"3 : {resp}")
85
+ # app.run(host='0.0.0.0', port=5000, debug=True)