Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,63 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pickle
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
# Paths
|
| 7 |
-
MODEL_DIR = "models"
|
| 8 |
-
MOVIE_DATA_PATH = "data/
|
| 9 |
-
|
| 10 |
-
# Load models (choose what you want to demo)
|
| 11 |
-
with open(os.path.join(MODEL_DIR, "recommender_svd_mf.pkl"), "rb") as f:
|
| 12 |
-
svd_model = pickle.load(f)
|
| 13 |
-
|
| 14 |
-
# Load movie metadata
|
| 15 |
-
movies_df = pd.read_csv(MOVIE_DATA_PATH) # should include [movieId, title, poster_url, actors]
|
| 16 |
-
|
| 17 |
-
def recommend(user_id, top_k=5):
|
| 18 |
-
"""Generate top-k recommendations using SVD model."""
|
| 19 |
-
# Predict scores for all movies for this user
|
| 20 |
-
all_movie_ids = movies_df["movieId"].unique()
|
| 21 |
-
predictions = []
|
| 22 |
-
for mid in all_movie_ids:
|
| 23 |
-
try:
|
| 24 |
-
est = svd_model.predict(str(user_id), str(mid)).est
|
| 25 |
-
predictions.append((mid, est))
|
| 26 |
-
except Exception:
|
| 27 |
-
continue
|
| 28 |
-
|
| 29 |
-
# Sort and pick top_k
|
| 30 |
-
top_movies = sorted(predictions, key=lambda x: x[1], reverse=True)[:top_k]
|
| 31 |
-
|
| 32 |
-
# Build output
|
| 33 |
-
results = []
|
| 34 |
-
for mid, score in top_movies:
|
| 35 |
-
row = movies_df[movies_df["movieId"] == mid].iloc[0]
|
| 36 |
-
explanation = f"Because you liked movies with {row.get('actors', 'similar style')}."
|
| 37 |
-
results.append((row["title"], row.get("poster_url", None), explanation))
|
| 38 |
-
|
| 39 |
-
return results
|
| 40 |
-
|
| 41 |
-
def format_output(results):
|
| 42 |
-
titles = [r[0] for r in results]
|
| 43 |
-
posters = [r[1] for r in results if r[1] is not None]
|
| 44 |
-
explanations = [r[2] for r in results]
|
| 45 |
-
return titles, posters, explanations
|
| 46 |
-
|
| 47 |
-
demo = gr.Interface(
|
| 48 |
-
fn=lambda user_id, k: format_output(recommend(user_id, k)),
|
| 49 |
-
inputs=[
|
| 50 |
-
gr.Number(label="User ID"),
|
| 51 |
-
gr.Slider(1, 10, value=5, step=1, label="Top-K")
|
| 52 |
-
],
|
| 53 |
-
outputs=[
|
| 54 |
-
gr.Textbox(label="Recommended Movies"),
|
| 55 |
-
gr.Gallery(label="Posters").style(grid=[3], height="auto"),
|
| 56 |
-
gr.Textbox(label="Explanations")
|
| 57 |
-
],
|
| 58 |
-
title="Movie Recommender System",
|
| 59 |
-
description="Enter your User ID to get top-K movie recommendations with posters and explanations."
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Paths
|
| 7 |
+
MODEL_DIR = "models"
|
| 8 |
+
MOVIE_DATA_PATH = "data/movies_metadata.csv" # adjust to your actual metadata file
|
| 9 |
+
|
| 10 |
+
# Load models (choose what you want to demo)
|
| 11 |
+
with open(os.path.join(MODEL_DIR, "recommender_svd_mf.pkl"), "rb") as f:
|
| 12 |
+
svd_model = pickle.load(f)
|
| 13 |
+
|
| 14 |
+
# Load movie metadata
|
| 15 |
+
movies_df = pd.read_csv(MOVIE_DATA_PATH) # should include [movieId, title, poster_url, actors]
|
| 16 |
+
|
| 17 |
+
def recommend(user_id, top_k=5):
|
| 18 |
+
"""Generate top-k recommendations using SVD model."""
|
| 19 |
+
# Predict scores for all movies for this user
|
| 20 |
+
all_movie_ids = movies_df["movieId"].unique()
|
| 21 |
+
predictions = []
|
| 22 |
+
for mid in all_movie_ids:
|
| 23 |
+
try:
|
| 24 |
+
est = svd_model.predict(str(user_id), str(mid)).est
|
| 25 |
+
predictions.append((mid, est))
|
| 26 |
+
except Exception:
|
| 27 |
+
continue
|
| 28 |
+
|
| 29 |
+
# Sort and pick top_k
|
| 30 |
+
top_movies = sorted(predictions, key=lambda x: x[1], reverse=True)[:top_k]
|
| 31 |
+
|
| 32 |
+
# Build output
|
| 33 |
+
results = []
|
| 34 |
+
for mid, score in top_movies:
|
| 35 |
+
row = movies_df[movies_df["movieId"] == mid].iloc[0]
|
| 36 |
+
explanation = f"Because you liked movies with {row.get('actors', 'similar style')}."
|
| 37 |
+
results.append((row["title"], row.get("poster_url", None), explanation))
|
| 38 |
+
|
| 39 |
+
return results
|
| 40 |
+
|
| 41 |
+
def format_output(results):
|
| 42 |
+
titles = [r[0] for r in results]
|
| 43 |
+
posters = [r[1] for r in results if r[1] is not None]
|
| 44 |
+
explanations = [r[2] for r in results]
|
| 45 |
+
return titles, posters, explanations
|
| 46 |
+
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=lambda user_id, k: format_output(recommend(user_id, k)),
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Number(label="User ID"),
|
| 51 |
+
gr.Slider(1, 10, value=5, step=1, label="Top-K")
|
| 52 |
+
],
|
| 53 |
+
outputs=[
|
| 54 |
+
gr.Textbox(label="Recommended Movies"),
|
| 55 |
+
gr.Gallery(label="Posters").style(grid=[3], height="auto"),
|
| 56 |
+
gr.Textbox(label="Explanations")
|
| 57 |
+
],
|
| 58 |
+
title="Movie Recommender System",
|
| 59 |
+
description="Enter your User ID to get top-K movie recommendations with posters and explanations."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|