from sentence_transformers import SentenceTransformer, util from flask import Flask, request import json import logging class JobOffer: def __init__(self, title, description, skills): self.title = title self.description = description self.skills = skills class JobApplicant: def __init__(self, id, skills): self.id = id self.skills = skills self.score = 0 class JobApplicantEncoder(json.JSONEncoder): def default(self, o): return o.__dict__ app = Flask(__name__) model_name = "Sahajtomar/french_semantic" model_name = "dangvantuan/sentence-camembert-base" model_name = "dangvantuan/sentence-camembert-large" model = SentenceTransformer(model_name) def get_cosine_score(job_applicant, job_offer): embeddings1 = model.encode(job_applicant, convert_to_tensor=True) embeddings2 = model.encode(job_offer, convert_to_tensor=True) cosine_scores = util.pytorch_cos_sim(embeddings1, embeddings2) return cosine_scores[0][0].item() @app.route('/process/', methods=['POST']) def get_cosinos(): #Map the input strings to object. data = request.get_json() temp_job_offer = data["job_offer"] temp_job_applicant = data["job_applicant"] assert temp_job_offer is not None assert temp_job_applicant is not None job_offer = JobOffer(temp_job_offer["title"], temp_job_offer["description"], temp_job_offer["skills"]) job_applicant_list = [] for job_applicant in temp_job_applicant: job_applicant_list.append(JobApplicant(job_applicant["id"], job_applicant["skills"])) #Compute the cosine score between the job offer and the job applicant for job_applicant in job_applicant_list: job_applicant.score = get_cosine_score(job_applicant.skills, job_offer.skills) return json.dumps(job_applicant_list, cls=JobApplicantEncoder) # embeddings1 = model.encode(job_applicant, convert_to_tensor=True) # embeddings2 = model.encode(job_offer, convert_to_tensor=True) # cosine_scores = util.pytorch_cos_sim(embeddings1, embeddings2) # # Create a list of tuples containing sentence and score # results = [] # for i in range(len(job_applicant)): # for j in range(len(job_offer)): # results.append((job_applicant[i], job_offer[j], cosine_scores[i][j])) # # Sort the list based on the scores in descending order # results.sort(key=lambda x: x[2], reverse=True) # # Print the sentences in the sorted order # for result in results: # print("Sentence 1:", result[0]) # print("Sentence 2:", result[1]) # print("Similarité cosinus:", result[2]) # print("-" * 50) if __name__ == '__main__': logging.info("Starting server...") 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") print(f"0 : {resp}") 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") print(f"1 : {resp}") 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") print(f"2 : {resp}") 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") print(f"3 : {resp}") # app.run(host='0.0.0.0', port=5000, debug=True)