File size: 1,312 Bytes
8807f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import streamlit as st

class Recommender:
    def __init__(self, df, embeddings, search_engine, embedder):
        self.df = df
        self.embeddings = embeddings
        self.search_engine = search_engine
        self.embedder = embedder

    def recommend(self, book_title: str, top_k=5):
        if book_title not in set(self.df["book_name"].values):
            raise ValueError(f"Book '{book_title}' not found in dataset.")
        # if book_tile == "The Power Of Full Engagement":
        #     raise ValueError(f"Book '{book_title}' has no summary found in dataset.")

        idx = self.df[self.df["book_name"] == book_title].index[0]
        # st.write(idx)
        query_vec = self.embeddings[idx].reshape(1, -1)

        labels, distances = self.search_engine.search(query_vec, top_k=top_k + 1)
        # st.write(labels)

        results = []
        for label, dist in zip(labels, distances):
            if label == idx:
                continue
            results.append({
                "book_name": self.df.iloc[label]["book_name"],
                "summaries": self.df.iloc[label]["summaries"],
                "categories": self.df.iloc[label]["categories"],
                "score": round(1 - dist, 3)
            })

        return self.df.iloc[idx], results[:top_k]