Spaces:
Sleeping
Sleeping
from flask import Flask, request, jsonify | |
from langchain_community.llms import LlamaCpp | |
from sentence_transformers import SentenceTransformer | |
from transformers import AutoTokenizer, AutoModel | |
# cosine_similarity | |
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", # path to GGUF file | |
temperature=0.1, | |
n_gpu_layers=n_gpu_layers, | |
n_batch=n_batch, | |
verbose=True, | |
n_ctx=4096 | |
) | |
model0 = AutoModel.from_pretrained('sentence-transformers/paraphrase-TinyBERT-L6-v2') | |
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") | |
def get_skills(): | |
cv_body = request.json.get('cv_body') | |
# Simple inference example | |
output = llm( | |
f"<|user|>\n{cv_body}<|end|>\n<|assistant|>Can you list the skills mentioned in the CV?<|end|>", | |
max_tokens=256, # Generate up to 256 tokens | |
stop=["<|end|>"], | |
echo=True, # Whether to echo the prompt | |
) | |
return jsonify({'skills': output}) | |
def health(): | |
return jsonify({'status': 'Worked'}) | |
# we will make here post request to compare between lists of skills one has employee just one text and the other has the of jobs has many texts | |
# the llm will say the most similar job to the cv | |
def compare(): | |
employee_skills = request.json.get('employee_skills') # CV text | |
jobs_skills = request.json.get('jobs_skills') # List of job skills | |
if not isinstance(jobs_skills, list) or not all(isinstance(skill, str) for skill in jobs_skills): | |
raise ValueError("The jobs_skills must be a list of strings") | |
# Convert texts to embeddings arrays | |
employee_embedding = np.array([model.encode(employee_skills)]) | |
job_embeddings = np.array([model.encode(skill) for skill in jobs_skills]) | |
# Calculate similarity using cosine similarity | |
similarities = cosine_similarity(employee_embedding, job_embeddings)[0] | |
# Find the most similar job and its corresponding similarity score | |
max_similarity = np.max(similarities) | |
most_similar_index = np.argmax(similarities) | |
most_similar_job = jobs_skills[most_similar_index] | |
return jsonify({'job': most_similar_job, 'similarity_score': max_similarity}) | |
if __name__ == '__main__': | |
app.run() |