Articles-RecSys / recommend.py
Mohamed-BC's picture
Update recommend.py
0c2b108 verified
raw
history blame contribute delete
752 Bytes
from sentence_transformers import SentenceTransformer
from scipy.spatial.distance import cosine
import numpy as np
import pandas as pd
from datasets import load_dataset
import pickle as pkl
def recommend(query, n=5):
# Load the model
model = SentenceTransformer('all-MiniLM-L6-v2', device='cpu')
data = load_dataset('Mohamed-BC/Articles')['train'].to_pandas()
# get the embeddings
a_embeddings = pkl.load(open('articles_embeddings.pkl', 'rb'))
# Encode the query
q_embedding = model.encode(query)
# Calculate the cosine similarity
cos_sim = np.array([1 - cosine(q_embedding, emb) for emb in a_embeddings])
# Get the top n recommendations
top_n = np.argsort(cos_sim)[-n:]
return data.iloc[top_n]['title']