omar / main.py
zayanomar5's picture
Update main.py
93885cf verified
raw
history blame
2.14 kB
from flask import Flask, request, jsonify
from langchain_community.llms import LlamaCpp
from sentence_transformers import SentenceTransformer
from transformers import AutoModel
import torch
from torch.nn.functional import cosine_similarity
import os
app = Flask(__name__)
n_gpu_layers = 0
n_batch = 1024
llm = LlamaCpp(
model_path="Phi-3-mini-4k-instruct-q4.gguf",
temperature=0.1,
n_gpu_layers=n_gpu_layers,
n_batch=n_batch,
verbose=True,
n_ctx=4096
)
# Initializing sentence transformer model
model = SentenceTransformer('sentence-transformers/paraphrase-TinyBERT-L6-v2')
file_size = os.stat('Phi-3-mini-4k-instruct-q4.gguf')
print("Model size: ", file_size.st_size, "bytes")
@app.route('/cv', methods=['POST'])
def get_skills():
cv_body = request.json.get('cv_body')
output = llm(
f"\n{cv_body}\nCan you list the skills mentioned in the CV?",
max_tokens=256,
stop=[""],
echo=True,
)
return jsonify({'skills': output})
@app.get('/')
def health():
return jsonify({'status': 'Worked'})
@app.route('/compare', methods=['POST'])
def compare():
employee_skills = request.json.get('employee_skills')
jobs_skills = request.json.get('jobs_skills')
# Validation
if not isinstance(jobs_skills, list) or not all(isinstance(skill, str) for skill in jobs_skills):
raise ValueError("jobs_skills must be a list of strings")
# Encoding skills into embeddings
job_embeddings = model.encode(jobs_skills)
employee_embeddings = model.encode(employee_skills)
# Computing cosine similarity between employee skills and each job
similarity_scores = []
employee_embeddings_tensor = torch.from_numpy(employee_embeddings).unsqueeze(0)
for i, job_e in enumerate(job_embeddings):
job_e_tensor = torch.from_numpy(job_e).unsqueeze(0)
similarity_score = cosine_similarity(employee_embeddings_tensor, job_e_tensor, dim=1)
similarity_scores.append({"job": jobs_skills[i], "similarity_score": similarity_score.item()})
return jsonify(similarity_scores)
if __name__ == '__main__':
app.run()