Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +54 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,55 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import streamlit as st
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import joblib
|
| 7 |
+
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model():
|
| 10 |
+
return tf.keras.models.load_model("recommender_model.keras")
|
| 11 |
+
|
| 12 |
+
@st.cache_data
|
| 13 |
+
def load_assets():
|
| 14 |
+
df_movies = pd.read_csv("movies.csv")
|
| 15 |
+
user_map, movie_map = joblib.load("encodings.pkl")
|
| 16 |
+
return df_movies, user_map, movie_map
|
| 17 |
+
|
| 18 |
+
model = load_model()
|
| 19 |
+
movies_df, user2idx, movie2idx = load_assets()
|
| 20 |
+
reverse_movie_map = {v: k for k, v in movie2idx.items()}
|
| 21 |
+
|
| 22 |
+
st.title("TensorFlow Movie Recommender")
|
| 23 |
+
st.write("Select some movies you've liked to get recommendations:")
|
| 24 |
+
|
| 25 |
+
movie_titles = movies_df.set_index("movieId")["title"].to_dict()
|
| 26 |
+
movie_choices = [movie_titles[mid] for mid in movie2idx.keys() if mid in movie_titles]
|
| 27 |
+
selected_titles = st.multiselect("Liked movies", sorted(movie_choices))
|
| 28 |
+
|
| 29 |
+
user_ratings = {}
|
| 30 |
+
for title in selected_titles:
|
| 31 |
+
movie_id = [k for k, v in movie_titles.items() if v == title][0]
|
| 32 |
+
user_ratings[movie_id] = 5.0
|
| 33 |
+
|
| 34 |
+
if st.button("Get Recommendations"):
|
| 35 |
+
if not user_ratings:
|
| 36 |
+
st.warning("Please select at least one movie.")
|
| 37 |
+
else:
|
| 38 |
+
liked_indices = [movie2idx[m] for m in user_ratings if m in movie2idx]
|
| 39 |
+
avg_embedding = tf.reduce_mean(model.layers[2](tf.constant(liked_indices)), axis=0, keepdims=True)
|
| 40 |
+
all_movie_indices = tf.range(len(movie2idx))
|
| 41 |
+
movie_embeddings = model.layers[3](all_movie_indices)
|
| 42 |
+
scores = tf.reduce_sum(avg_embedding * movie_embeddings, axis=1).numpy()
|
| 43 |
+
top_indices = np.argsort(scores)[::-1]
|
| 44 |
+
|
| 45 |
+
recommended = []
|
| 46 |
+
for idx in top_indices:
|
| 47 |
+
mid = reverse_movie_map[idx]
|
| 48 |
+
if mid not in user_ratings and mid in movie_titles:
|
| 49 |
+
recommended.append((movie_titles[mid], scores[idx]))
|
| 50 |
+
if len(recommended) >= 10:
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
st.subheader("Top Recommendations")
|
| 54 |
+
for title, score in recommended:
|
| 55 |
+
st.write(f"{title} — Score: {score:.3f}")
|