Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -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:
|
@@ -145,26 +145,30 @@ def recommend_books(target_book: str, num_recommendations: int = 10) -> str:
|
|
145 |
|
146 |
correlations = compute_correlations_faiss(faiss_index, list(dataset["Book-Title"]), closest_match, num_recommendations)
|
147 |
|
148 |
-
recommendations = dataset[dataset["Book-Title"] != target_book].head(num_recommendations)
|
149 |
|
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=
|
160 |
inputs=[
|
161 |
gr.Textbox(label="Enter a book title"),
|
162 |
-
gr.Slider(minimum=1, maximum=20, step=1, label="Number of recommendations", value=10)
|
|
|
|
|
|
|
|
|
|
|
163 |
],
|
164 |
-
|
165 |
-
|
166 |
-
description="Enter a book title to get recommendations based on user ratings and book similarities."
|
167 |
)
|
168 |
|
169 |
-
# Launch the app
|
170 |
iface.launch()
|
|
|
132 |
book_titles = dataset["Book-Title"]
|
133 |
|
134 |
|
135 |
+
def recommend_books_with_theme(target_book: str, num_recommendations: int = 10, theme= None):
|
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:
|
|
|
145 |
|
146 |
correlations = compute_correlations_faiss(faiss_index, list(dataset["Book-Title"]), closest_match, num_recommendations)
|
147 |
|
148 |
+
recommendations = dataset[dataset["Book-Title"][correlations['book']] != target_book].head(num_recommendations)
|
149 |
|
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 |
+
# Set theme based on user selection
|
154 |
+
theme_mode = "light" if theme == "Light" else "dark"
|
155 |
+
return result, theme_mode
|
156 |
|
|
|
157 |
|
158 |
+
# Gradio interface
|
|
|
159 |
iface = gr.Interface(
|
160 |
+
fn=recommend_books_with_theme,
|
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 |
+
title="Book Recommender with Theme Toggle",
|
171 |
+
description="Enter a book title to get recommendations and select a theme (Light/Dark)."
|
|
|
172 |
)
|
173 |
|
|
|
174 |
iface.launch()
|