Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +12 -57
src/streamlit_app.py
CHANGED
|
@@ -1,27 +1,13 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import tensorflow as tf
|
| 5 |
import joblib
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
# Define file paths (assuming
|
| 9 |
BASE_DIR = os.path.dirname(__file__)
|
| 10 |
-
KERAS_MODEL_PATH = os.path.join(BASE_DIR, "recommender_model.keras")
|
| 11 |
MOVIES_PATH = os.path.join(BASE_DIR, "movies.csv")
|
| 12 |
ENCODINGS_PATH = os.path.join(BASE_DIR, "encodings.pkl")
|
| 13 |
|
| 14 |
-
@st.cache_resource
|
| 15 |
-
def load_model():
|
| 16 |
-
if not os.path.exists(KERAS_MODEL_PATH):
|
| 17 |
-
st.error(f"β Model file not found at: {KERAS_MODEL_PATH}")
|
| 18 |
-
st.stop()
|
| 19 |
-
try:
|
| 20 |
-
return tf.keras.models.load_model(KERAS_MODEL_PATH)
|
| 21 |
-
except Exception as e:
|
| 22 |
-
st.error(f"β Failed to load model:\n\n{e}")
|
| 23 |
-
st.stop()
|
| 24 |
-
|
| 25 |
@st.cache_data
|
| 26 |
def load_assets():
|
| 27 |
if not os.path.exists(MOVIES_PATH):
|
|
@@ -38,56 +24,25 @@ def load_assets():
|
|
| 38 |
st.error(f"β Failed to load assets:\n\n{e}")
|
| 39 |
st.stop()
|
| 40 |
|
| 41 |
-
# Load
|
| 42 |
-
model = load_model()
|
| 43 |
movies_df, user2idx, movie2idx = load_assets()
|
| 44 |
-
reverse_movie_map = {v: k for k, v in movie2idx.items()}
|
| 45 |
|
| 46 |
# UI
|
| 47 |
-
st.title("π¬ TensorFlow Movie Recommender")
|
| 48 |
-
st.write("
|
| 49 |
|
| 50 |
# Movie title selection
|
| 51 |
movie_titles = movies_df.set_index("movieId")["title"].to_dict()
|
| 52 |
movie_choices = [movie_titles[mid] for mid in movie2idx if mid in movie_titles]
|
| 53 |
selected_titles = st.multiselect("ποΈ Liked movies", sorted(movie_choices))
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
movie_id = next((k for k, v in movie_titles.items() if v == title), None)
|
| 59 |
-
if movie_id:
|
| 60 |
-
user_ratings[movie_id] = 5.0
|
| 61 |
-
|
| 62 |
-
# Generate recommendations
|
| 63 |
-
if st.button("π― Get Recommendations"):
|
| 64 |
-
if not user_ratings:
|
| 65 |
st.warning("Please select at least one movie.")
|
| 66 |
else:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
try:
|
| 73 |
-
# Calculate average embedding and similarity scores
|
| 74 |
-
avg_embedding = tf.reduce_mean(model.layers[2](tf.constant(liked_indices)), axis=0, keepdims=True)
|
| 75 |
-
all_movie_indices = tf.range(len(movie2idx))
|
| 76 |
-
movie_embeddings = model.layers[3](all_movie_indices)
|
| 77 |
-
scores = tf.reduce_sum(avg_embedding * movie_embeddings, axis=1).numpy()
|
| 78 |
-
top_indices = np.argsort(scores)[::-1]
|
| 79 |
-
|
| 80 |
-
# Top 10 recommendations excluding already liked
|
| 81 |
-
recommended = []
|
| 82 |
-
for idx in top_indices:
|
| 83 |
-
mid = reverse_movie_map.get(idx)
|
| 84 |
-
if mid not in user_ratings and mid in movie_titles:
|
| 85 |
-
recommended.append((movie_titles[mid], scores[idx]))
|
| 86 |
-
if len(recommended) >= 10:
|
| 87 |
-
break
|
| 88 |
-
|
| 89 |
-
st.subheader("πΏ Top 10 Recommendations")
|
| 90 |
-
for title, score in recommended:
|
| 91 |
-
st.write(f"**{title}** β Score: `{score:.3f}`")
|
| 92 |
-
except Exception as e:
|
| 93 |
-
st.error(f"β Error generating recommendations:\n\n{e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
| 3 |
import joblib
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Define file paths (assuming files are in the same folder as this script)
|
| 7 |
BASE_DIR = os.path.dirname(__file__)
|
|
|
|
| 8 |
MOVIES_PATH = os.path.join(BASE_DIR, "movies.csv")
|
| 9 |
ENCODINGS_PATH = os.path.join(BASE_DIR, "encodings.pkl")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@st.cache_data
|
| 12 |
def load_assets():
|
| 13 |
if not os.path.exists(MOVIES_PATH):
|
|
|
|
| 24 |
st.error(f"β Failed to load assets:\n\n{e}")
|
| 25 |
st.stop()
|
| 26 |
|
| 27 |
+
# Load only static data
|
|
|
|
| 28 |
movies_df, user2idx, movie2idx = load_assets()
|
|
|
|
| 29 |
|
| 30 |
# UI
|
| 31 |
+
st.title("π¬ TensorFlow Movie Recommender (Mock Version)")
|
| 32 |
+
st.write("This is a dummy version of the app without model logic. Select movies to simulate recommendations.")
|
| 33 |
|
| 34 |
# Movie title selection
|
| 35 |
movie_titles = movies_df.set_index("movieId")["title"].to_dict()
|
| 36 |
movie_choices = [movie_titles[mid] for mid in movie2idx if mid in movie_titles]
|
| 37 |
selected_titles = st.multiselect("ποΈ Liked movies", sorted(movie_choices))
|
| 38 |
|
| 39 |
+
# Simulate recommendations
|
| 40 |
+
if st.button("π― Get Recommendations (Dummy)"):
|
| 41 |
+
if not selected_titles:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
st.warning("Please select at least one movie.")
|
| 43 |
else:
|
| 44 |
+
st.subheader("πΏ Mock Recommendations")
|
| 45 |
+
st.write("Here are some movies we 'think' you might like... π")
|
| 46 |
+
mock_recommendations = sorted(set(movie_choices) - set(selected_titles))[:10]
|
| 47 |
+
for i, title in enumerate(mock_recommendations, 1):
|
| 48 |
+
st.write(f"**#{i}** β {title}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|