Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,16 +16,16 @@ st.set_page_config(
|
|
16 |
layout="wide"
|
17 |
)
|
18 |
|
19 |
-
# App title and description
|
20 |
-
st.title("π Book Recommendation System")
|
21 |
-
st.markdown("Enter a book summary and genres to get personalized book recommendations!")
|
22 |
-
|
23 |
# GitHub URLs for model files and dataset
|
24 |
GITHUB_CSV_URL = "https://media.githubusercontent.com/media/Manithj/bookRecEngine/refs/heads/main/goodreadsV2.csv"
|
25 |
GITHUB_KNN_URL = "https://media.githubusercontent.com/media/Manithj/bookRecEngine/refs/heads/main/knn_model.pkl"
|
26 |
GITHUB_TFIDF_URL = "https://raw.githubusercontent.com/Manithj/bookRecEngine/main/tfidf_vectorizer.pkl"
|
27 |
|
28 |
-
#
|
|
|
|
|
|
|
|
|
29 |
@st.cache_resource
|
30 |
def load_models_from_github():
|
31 |
try:
|
@@ -42,7 +42,7 @@ def load_models_from_github():
|
|
42 |
st.error(f"Error loading models: {e}")
|
43 |
return None, None
|
44 |
|
45 |
-
# Load the dataset from GitHub
|
46 |
@st.cache_data
|
47 |
def load_data_from_github():
|
48 |
try:
|
@@ -67,9 +67,24 @@ def load_data_from_github():
|
|
67 |
st.error(f"Error loading dataset: {e}")
|
68 |
return None
|
69 |
|
70 |
-
#
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
# Recommendation function for out-of-dataset books
|
75 |
def recommend_books_knn_out_of_dataset(input_summary, input_genres, top_n=5):
|
@@ -96,19 +111,6 @@ def recommend_books_knn_out_of_dataset(input_summary, input_genres, top_n=5):
|
|
96 |
|
97 |
return recommendations
|
98 |
|
99 |
-
# Status indicator for loading data
|
100 |
-
with st.spinner("Loading models and data from GitHub..."):
|
101 |
-
# Load models and data
|
102 |
-
tfidf, knn_model = load_models_from_github()
|
103 |
-
df_cleaned = load_data_from_github()
|
104 |
-
|
105 |
-
if tfidf is not None and knn_model is not None and df_cleaned is not None:
|
106 |
-
st.success("Models and data loaded successfully!")
|
107 |
-
models_loaded = True
|
108 |
-
else:
|
109 |
-
st.error("Failed to load models or data. Please check the GitHub URLs.")
|
110 |
-
models_loaded = False
|
111 |
-
|
112 |
# Sidebar for inputs
|
113 |
st.sidebar.header("Input Parameters")
|
114 |
|
@@ -186,6 +188,22 @@ st.sidebar.info(
|
|
186 |
"""
|
187 |
)
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
# Add a footer
|
190 |
st.markdown("---")
|
191 |
st.markdown("π Book Recommendation System | Created with Streamlit")
|
|
|
16 |
layout="wide"
|
17 |
)
|
18 |
|
|
|
|
|
|
|
|
|
19 |
# GitHub URLs for model files and dataset
|
20 |
GITHUB_CSV_URL = "https://media.githubusercontent.com/media/Manithj/bookRecEngine/refs/heads/main/goodreadsV2.csv"
|
21 |
GITHUB_KNN_URL = "https://media.githubusercontent.com/media/Manithj/bookRecEngine/refs/heads/main/knn_model.pkl"
|
22 |
GITHUB_TFIDF_URL = "https://raw.githubusercontent.com/Manithj/bookRecEngine/main/tfidf_vectorizer.pkl"
|
23 |
|
24 |
+
# Define the preprocessing function
|
25 |
+
def preprocess_text(text):
|
26 |
+
return re.sub(r'[^a-zA-Z0-9\s]', '', text.lower())
|
27 |
+
|
28 |
+
# Load models from GitHub - using st.cache_resource to load only once
|
29 |
@st.cache_resource
|
30 |
def load_models_from_github():
|
31 |
try:
|
|
|
42 |
st.error(f"Error loading models: {e}")
|
43 |
return None, None
|
44 |
|
45 |
+
# Load the dataset from GitHub - using st.cache_data to load only once
|
46 |
@st.cache_data
|
47 |
def load_data_from_github():
|
48 |
try:
|
|
|
67 |
st.error(f"Error loading dataset: {e}")
|
68 |
return None
|
69 |
|
70 |
+
# Load models and data at startup - this happens only once due to caching
|
71 |
+
with st.spinner("Loading models and data (this will only happen once)..."):
|
72 |
+
tfidf, knn_model = load_models_from_github()
|
73 |
+
df_cleaned = load_data_from_github()
|
74 |
+
|
75 |
+
if tfidf is not None and knn_model is not None and df_cleaned is not None:
|
76 |
+
models_loaded = True
|
77 |
+
else:
|
78 |
+
models_loaded = False
|
79 |
+
|
80 |
+
# App title and description
|
81 |
+
st.title("π Book Recommendation System")
|
82 |
+
st.markdown("Enter a book summary and genres to get personalized book recommendations!")
|
83 |
+
|
84 |
+
if not models_loaded:
|
85 |
+
st.error("Failed to load models or data. Please check the GitHub URLs.")
|
86 |
+
else:
|
87 |
+
st.success("Models and data loaded successfully!")
|
88 |
|
89 |
# Recommendation function for out-of-dataset books
|
90 |
def recommend_books_knn_out_of_dataset(input_summary, input_genres, top_n=5):
|
|
|
111 |
|
112 |
return recommendations
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
# Sidebar for inputs
|
115 |
st.sidebar.header("Input Parameters")
|
116 |
|
|
|
188 |
"""
|
189 |
)
|
190 |
|
191 |
+
# Add example inputs for quick testing
|
192 |
+
st.sidebar.markdown("---")
|
193 |
+
st.sidebar.header("Try these examples")
|
194 |
+
|
195 |
+
if st.sidebar.button("Example 1: Fantasy Adventure"):
|
196 |
+
st.sidebar.text_area("Book Summary",
|
197 |
+
value="A young wizard discovers his magical powers and embarks on a journey to defeat a dark lord threatening the world.",
|
198 |
+
height=150, key="example1_summary")
|
199 |
+
st.sidebar.text_input("Genres", value="fantasy, adventure, magic", key="example1_genres")
|
200 |
+
|
201 |
+
if st.sidebar.button("Example 2: Mystery Thriller"):
|
202 |
+
st.sidebar.text_area("Book Summary",
|
203 |
+
value="A detective investigates a series of murders that seem to be connected to an unsolved case from decades ago.",
|
204 |
+
height=150, key="example2_summary")
|
205 |
+
st.sidebar.text_input("Genres", value="mystery, thriller, crime", key="example2_genres")
|
206 |
+
|
207 |
# Add a footer
|
208 |
st.markdown("---")
|
209 |
st.markdown("π Book Recommendation System | Created with Streamlit")
|