danschnurp commited on
Commit
aeafac5
·
verified ·
1 Parent(s): b9c4193

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -74,17 +74,16 @@ def build_faiss_index(dataset: pd.DataFrame) -> Tuple[faiss.IndexFlatIP, np.ndar
74
  return index
75
 
76
 
77
- def compute_correlations_faiss(index, book_titles: List[str],
78
- target_book, num_recommendations) -> pd.DataFrame:
79
  print(target_book, type(target_book))
80
- emb = create_embedding([target_book])
81
  # target_vector = book_titles.index(emb)
82
 
83
 
84
  # Perform the search
85
- k = num_recommendations
86
  similarities, I = index.search(emb.astype('float16'), k)
87
- print(similarities, I)
88
 
89
  # # Reduce database and query vectors to 2D for visualization
90
  # pca = PCA(n_components=2)
@@ -132,7 +131,7 @@ def load_and_prepare_data():
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
137
 
138
  if dataset is None or faiss_index is None or normalized_data is None or book_titles is None:
@@ -140,23 +139,22 @@ def recommend_books(target_book: str, num_recommendations: int = 10):
140
 
141
  target_book = target_book.lower()
142
  # Fuzzy match the input to the closest book title
143
- closest_match, score, _ = process.extractOne(target_book, book_titles)
144
 
145
- if score < 50: # You can adjust this threshold
146
- return f"No close match found for '{target_book}'. Please try a different title."
147
 
148
- correlations = compute_correlations_faiss(faiss_index, list(dataset["Book-Title"]), closest_match, num_recommendations)
149
 
150
- recommendations = dataset[dataset["Book-Title"][correlations['book']] != target_book].head(num_recommendations)
151
 
 
152
  result = f"Top {num_recommendations} recommendations for '{target_book}':\n\n"
153
  for i, (_, row) in enumerate(recommendations.iterrows(), 1):
154
  result += f"{i}. {row['book']} (Correlation: {row['corr']:.2f})\n"
155
- return result
156
 
 
157
 
158
- import gradio as gr
159
 
 
160
  iface = gr.Interface(
161
  fn=recommend_books,
162
  inputs=[
@@ -165,8 +163,9 @@ iface = gr.Interface(
165
  ],
166
  outputs=gr.Textbox(label="Recommendations"),
167
  title="Book Recommender",
168
- description="Enter a book title to get recommendations based on user ratings and book similarities.",
169
- theme="light" # Force light mode
170
  )
171
 
172
- iface.launch()
 
 
74
  return index
75
 
76
 
77
+ def compute_correlations_faiss(index: faiss.IndexFlatIP, book_titles: List[str],
78
+ target_book, ) -> pd.DataFrame:
79
  print(target_book, type(target_book))
80
+ emb = create_embedding([target_book[0]])
81
  # target_vector = book_titles.index(emb)
82
 
83
 
84
  # Perform the search
85
+ k = len(book_titles) # Search for all books
86
  similarities, I = index.search(emb.astype('float16'), k)
 
87
 
88
  # # Reduce database and query vectors to 2D for visualization
89
  # pca = PCA(n_components=2)
 
131
  book_titles = dataset["Book-Title"]
132
 
133
 
134
+ def recommend_books(target_book: str, num_recommendations: int = 10) -> str:
135
  global dataset, faiss_index, normalized_data, book_titles
136
 
137
  if dataset is None or faiss_index is None or normalized_data is None or book_titles is None:
 
139
 
140
  target_book = target_book.lower()
141
  # Fuzzy match the input to the closest book title
142
+ closest_match = process.extractOne(target_book, book_titles)
143
 
 
 
144
 
145
+ correlations = compute_correlations_faiss(faiss_index, book_titles, closest_match)
146
 
147
+ recommendations = correlations[correlations['book'] != target_book].head(num_recommendations)
148
 
149
+ print(recommendations['book'])
150
  result = f"Top {num_recommendations} recommendations for '{target_book}':\n\n"
151
  for i, (_, row) in enumerate(recommendations.iterrows(), 1):
152
  result += f"{i}. {row['book']} (Correlation: {row['corr']:.2f})\n"
 
153
 
154
+ return result
155
 
 
156
 
157
+ # Create Gradio interface
158
  iface = gr.Interface(
159
  fn=recommend_books,
160
  inputs=[
 
163
  ],
164
  outputs=gr.Textbox(label="Recommendations"),
165
  title="Book Recommender",
166
+ description="Enter a book title to get recommendations based on user ratings and book similarities."
167
+ , theme=gr.themes.Base()
168
  )
169
 
170
+ # Launch the app
171
+ iface.launch()