| import streamlit as st |
| import pandas as pd |
| import joblib |
|
|
| |
| model = joblib.load("src/book_recommender_model.pkl") |
| scaler = joblib.load("src/scaler.pkl") |
| df = joblib.load("src/books_df.pkl") |
|
|
| |
| features = pd.concat([ |
| pd.get_dummies(df["rating_between"]), |
| pd.get_dummies(df["language_code"]), |
| df[["average_rating","ratings_count"]] |
| ], axis=1) |
| features_scaled = scaler.transform(features) |
|
|
| |
| indices = pd.Series(df.index, index=df["title"]) |
|
|
| |
| st.title("📚 Book Recommendation App") |
| st.write("Content-Based Recommendation System using pre-trained model") |
|
|
| title = st.selectbox("Select a book", df["title"].values) |
|
|
| def recommend(title, n=5): |
| if title not in indices: |
| return pd.DataFrame(columns=["Title","Authors","Average_Rating","Ratings_Count"]) |
| |
| idx = indices[title] |
| distances, neighbors_idx = model.kneighbors([features_scaled[idx]], n_neighbors=n+1) |
| neighbors_idx = neighbors_idx[0][1:] |
| return df.iloc[neighbors_idx][["title","authors","average_rating","ratings_count"]] |
|
|
| if st.button("Recommend"): |
| recommendations = recommend(title) |
| if recommendations.empty: |
| st.error("❌ Book not found") |
| else: |
| st.subheader("Recommended Books") |
| st.dataframe(recommendations.reset_index(drop=True)) |
|
|