LamRhm commited on
Commit
71199f8
·
verified ·
1 Parent(s): aa2acfc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+
4
+ from langchain_community.document_loaders import TextLoader
5
+ from langchain_community.embeddings import HuggingFaceEmbeddings # changed here
6
+ from langchain_text_splitters import CharacterTextSplitter
7
+ from langchain_chroma import Chroma
8
+
9
+ import gradio as gr
10
+
11
+
12
+ books = pd.read_csv("books_with_emotions.csv")
13
+ books["large_thumbnail"] = books["thumbnail"] + "&fife=w800"
14
+ books["large_thumbnail"] = np.where(
15
+ books["large_thumbnail"].isna(),
16
+ "cover-not-found.jpg",
17
+ books["large_thumbnail"],
18
+ )
19
+
20
+ raw_documents = TextLoader("tagged_description.txt",encoding="utf-8").load()
21
+ text_splitter = CharacterTextSplitter(separator="\n", chunk_size=500, chunk_overlap=50)
22
+ documents = text_splitter.split_documents(raw_documents)
23
+
24
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
25
+ db_books = Chroma.from_documents(documents, embeddings)
26
+
27
+
28
+ def retrieve_semantic_recommendations(
29
+ query: str,
30
+ category: str = None,
31
+ tone: str = None,
32
+ initial_top_k: int = 50,
33
+ final_top_k: int = 16,
34
+ ) -> pd.DataFrame:
35
+
36
+ recs = db_books.similarity_search(query, k=initial_top_k)
37
+ books_list = [int(rec.page_content.strip('"').split()[0]) for rec in recs]
38
+ book_recs = books[books["isbn13"].isin(books_list)].head(initial_top_k)
39
+
40
+ if category != "All":
41
+ book_recs = book_recs[book_recs["simple_categories"] == category].head(final_top_k)
42
+ else:
43
+ book_recs = book_recs.head(final_top_k)
44
+
45
+ if tone == "Happy":
46
+ book_recs.sort_values(by="joy", ascending=False, inplace=True)
47
+ elif tone == "Surprising":
48
+ book_recs.sort_values(by="surprise", ascending=False, inplace=True)
49
+ elif tone == "Angry":
50
+ book_recs.sort_values(by="anger", ascending=False, inplace=True)
51
+ elif tone == "Suspenseful":
52
+ book_recs.sort_values(by="fear", ascending=False, inplace=True)
53
+ elif tone == "Sad":
54
+ book_recs.sort_values(by="sadness", ascending=False, inplace=True)
55
+
56
+ return book_recs
57
+
58
+
59
+ def recommend_books(
60
+ query: str,
61
+ category: str,
62
+ tone: str
63
+ ):
64
+ recommendations = retrieve_semantic_recommendations(query, category, tone)
65
+ results = []
66
+
67
+ for _, row in recommendations.iterrows():
68
+ description = row["description"]
69
+ truncated_desc_split = description.split()
70
+ truncated_description = " ".join(truncated_desc_split[:30]) + "..."
71
+
72
+ authors_split = row["authors"].split(";")
73
+ if len(authors_split) == 2:
74
+ authors_str = f"{authors_split[0]} and {authors_split[1]}"
75
+ elif len(authors_split) > 2:
76
+ authors_str = f"{', '.join(authors_split[:-1])}, and {authors_split[-1]}"
77
+ else:
78
+ authors_str = row["authors"]
79
+
80
+ caption = f"{row['title']} by {authors_str}: {truncated_description}"
81
+ results.append((row["large_thumbnail"], caption))
82
+ return results
83
+
84
+ categories = ["All"] + sorted(books["simple_categories"].unique())
85
+ tones = ["All"] + ["Happy", "Surprising", "Angry", "Suspenseful", "Sad"]
86
+
87
+ with gr.Blocks(theme = gr.themes.Glass()) as dashboard:
88
+ gr.Markdown("# Semantic book recommender")
89
+
90
+ with gr.Row():
91
+ user_query = gr.Textbox(label = "Please enter a description of a book:",
92
+ placeholder = "e.g., A story about forgiveness")
93
+ category_dropdown = gr.Dropdown(choices = categories, label = "Select a category:", value = "All")
94
+ tone_dropdown = gr.Dropdown(choices = tones, label = "Select an emotional tone:", value = "All")
95
+ submit_button = gr.Button("Find recommendations")
96
+
97
+ gr.Markdown("## Recommendations")
98
+ output = gr.Gallery(label = "Recommended books", columns = 8, rows = 2)
99
+
100
+ submit_button.click(fn = recommend_books,
101
+ inputs = [user_query, category_dropdown, tone_dropdown],
102
+ outputs = output)
103
+
104
+
105
+ if __name__ == "__main__":
106
+ dashboard.launch()