gautamnancy commited on
Commit
ae1bad1
·
verified ·
1 Parent(s): 97b27cd

Upload app.py

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