Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -77,7 +77,7 @@ def build_faiss_index(dataset: pd.DataFrame) -> Tuple[faiss.IndexFlatIP, np.ndar
|
|
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 |
|
@@ -132,7 +132,7 @@ def load_and_prepare_data():
|
|
132 |
book_titles = dataset["Book-Title"]
|
133 |
|
134 |
|
135 |
-
def
|
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,8 +140,10 @@ def recommend_books_with_theme(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 = process.extractOne(target_book, book_titles)
|
144 |
|
|
|
|
|
145 |
|
146 |
correlations = compute_correlations_faiss(faiss_index, list(dataset["Book-Title"]), closest_match, num_recommendations)
|
147 |
|
@@ -150,25 +152,24 @@ def recommend_books_with_theme(target_book: str, num_recommendations: int = 10,
|
|
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 |
-
theme_mode = "light" if theme == "Light" else "dark"
|
155 |
-
return result, theme_mode
|
156 |
|
157 |
|
158 |
-
|
|
|
159 |
iface = gr.Interface(
|
160 |
-
fn=
|
161 |
inputs=[
|
162 |
gr.Textbox(label="Enter a book title"),
|
163 |
-
gr.Slider(minimum=1, maximum=20, step=1, label="Number of recommendations", value=10)
|
164 |
-
gr.Dropdown(["Light", "Dark"], label="Theme", value="Light") # Theme toggle
|
165 |
-
],
|
166 |
-
outputs=[
|
167 |
-
gr.Textbox(label="Recommendations"),
|
168 |
-
gr.Text(label="Current Theme"), # Show selected theme
|
169 |
],
|
170 |
-
|
171 |
-
|
|
|
|
|
172 |
)
|
173 |
|
|
|
|
|
|
|
174 |
iface.launch()
|
|
|
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 |
|
|
|
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 |
|
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 |
|
|
|
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=[
|
163 |
gr.Textbox(label="Enter a book title"),
|
164 |
+
gr.Slider(minimum=1, maximum=20, step=1, label="Number of recommendations", value=10)
|
|
|
|
|
|
|
|
|
|
|
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()
|
173 |
+
|
174 |
+
|
175 |
iface.launch()
|