Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import faiss
|
|
9 |
from faiss import write_index, read_index
|
10 |
import gradio as gr
|
11 |
from fuzzywuzzy import process
|
|
|
12 |
from tqdm import tqdm
|
13 |
from transformers import BertTokenizerFast, BertModel, AutoTokenizer, AutoModel
|
14 |
|
@@ -131,7 +132,7 @@ def load_and_prepare_data():
|
|
131 |
book_titles = dataset["Book-Title"]
|
132 |
|
133 |
|
134 |
-
def recommend_books(target_book: str, num_recommendations: int = 10)
|
135 |
global dataset, faiss_index, normalized_data, book_titles, ratings_by_isbn
|
136 |
|
137 |
if dataset is None or faiss_index is None or normalized_data is None or book_titles is None:
|
@@ -158,20 +159,7 @@ def recommend_books(target_book: str, num_recommendations: int = 10) -> str:
|
|
158 |
|
159 |
recommendations = recommendations.head(num_recommendations)
|
160 |
|
161 |
-
result = f"Top {num_recommendations} recommendations for '{target_book}':\n\n"
|
162 |
dups = []
|
163 |
-
result += "\n\n".join([
|
164 |
-
f"{idx, dups.append(dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[0])}. "
|
165 |
-
f"Title: {dataset.loc[dataset['Book-Title'] == row['book'], 'Book-Title'].values[0]}, "
|
166 |
-
f"Author: {dataset.loc[dataset['Book-Title'] == row['book'], 'Book-Author'].values[0]}, "
|
167 |
-
f"Year: {dataset.loc[dataset['Book-Title'] == row['book'], 'Year-Of-Publication'].values[0]}, "
|
168 |
-
f"Publisher: {dataset.loc[dataset['Book-Title'] == row['book'], 'Publisher'].values[0]}, "
|
169 |
-
f"ISBN: {dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[0]}, "
|
170 |
-
f"Rating: {ratings_by_isbn.loc[ratings_by_isbn['ISBN'] == dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[0], 'Book-Rating'].values[0]}"
|
171 |
-
for idx, (_, row) in enumerate(recommendations.iterrows(), 1) if dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[0] not in dups
|
172 |
-
])
|
173 |
-
# "ISBN";"Book-Title";"Book-Author";"Year-Of-Publication";"Publisher";"Image-URL-S"
|
174 |
-
|
175 |
result_df = pd.DataFrame([
|
176 |
{
|
177 |
"Rank": idx,
|
@@ -182,9 +170,11 @@ def recommend_books(target_book: str, num_recommendations: int = 10) -> str:
|
|
182 |
"ISBN": dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[0],
|
183 |
"Rating": ratings_by_isbn.loc[
|
184 |
ratings_by_isbn['ISBN'] == dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[
|
185 |
-
0], 'Book-Rating'].values[0]
|
|
|
186 |
}
|
187 |
for idx, (_, row) in enumerate(recommendations.iterrows(), 1)
|
|
|
188 |
])
|
189 |
|
190 |
return result_df
|
@@ -197,14 +187,17 @@ iface = gr.Interface(
|
|
197 |
gr.Textbox(label="Enter a book title"),
|
198 |
gr.Slider(minimum=1, maximum=20, step=1, label="Number of recommendations", value=10)
|
199 |
],
|
200 |
-
outputs=
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
204 |
title="Book Recommender",
|
205 |
description="Enter a book title to get recommendations based on user ratings and book similarities."
|
206 |
)
|
207 |
|
208 |
-
|
209 |
# Launch the app
|
210 |
iface.launch()
|
|
|
9 |
from faiss import write_index, read_index
|
10 |
import gradio as gr
|
11 |
from fuzzywuzzy import process
|
12 |
+
from pandas import DataFrame
|
13 |
from tqdm import tqdm
|
14 |
from transformers import BertTokenizerFast, BertModel, AutoTokenizer, AutoModel
|
15 |
|
|
|
132 |
book_titles = dataset["Book-Title"]
|
133 |
|
134 |
|
135 |
+
def recommend_books(target_book: str, num_recommendations: int = 10):
|
136 |
global dataset, faiss_index, normalized_data, book_titles, ratings_by_isbn
|
137 |
|
138 |
if dataset is None or faiss_index is None or normalized_data is None or book_titles is None:
|
|
|
159 |
|
160 |
recommendations = recommendations.head(num_recommendations)
|
161 |
|
|
|
162 |
dups = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
result_df = pd.DataFrame([
|
164 |
{
|
165 |
"Rank": idx,
|
|
|
170 |
"ISBN": dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[0],
|
171 |
"Rating": ratings_by_isbn.loc[
|
172 |
ratings_by_isbn['ISBN'] == dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[
|
173 |
+
0], 'Book-Rating'].values[0],
|
174 |
+
"none": dups.append(dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[0])
|
175 |
}
|
176 |
for idx, (_, row) in enumerate(recommendations.iterrows(), 1)
|
177 |
+
if dataset.loc[dataset['Book-Title'] == row['book'], 'ISBN'].values[0] not in dups
|
178 |
])
|
179 |
|
180 |
return result_df
|
|
|
187 |
gr.Textbox(label="Enter a book title"),
|
188 |
gr.Slider(minimum=1, maximum=20, step=1, label="Number of recommendations", value=10)
|
189 |
],
|
190 |
+
outputs=[
|
191 |
+
gr.Dataframe(
|
192 |
+
headers=["Rank", "Title", "Author", "Year", "Publisher", "ISBN", "Rating"],
|
193 |
+
type="pandas",
|
194 |
+
|
195 |
+
),
|
196 |
+
gr.JSON(label="Detailed Recommendations")
|
197 |
+
],
|
198 |
title="Book Recommender",
|
199 |
description="Enter a book title to get recommendations based on user ratings and book similarities."
|
200 |
)
|
201 |
|
|
|
202 |
# Launch the app
|
203 |
iface.launch()
|