Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
|
| 7 |
+
# -----------------------
|
| 8 |
+
# 1. Load and prepare data
|
| 9 |
+
# -----------------------
|
| 10 |
+
@st.cache_data
|
| 11 |
+
def load_data():
|
| 12 |
+
# Replace with your file path or relative path
|
| 13 |
+
df1 = pd.read_csv("tmdb_5000_credits.csv")
|
| 14 |
+
df2 = pd.read_csv("tmdb_5000_movies.csv")
|
| 15 |
+
df = pd.merge(df1, df2, left_on="movie_id", right_on="id")
|
| 16 |
+
df["overview"] = df["overview"].fillna(" ")
|
| 17 |
+
return df, df2
|
| 18 |
+
|
| 19 |
+
df, df2 = load_data()
|
| 20 |
+
|
| 21 |
+
# -----------------------
|
| 22 |
+
# 2. Build TF-IDF and cosine similarity
|
| 23 |
+
# -----------------------
|
| 24 |
+
@st.cache_resource
|
| 25 |
+
def build_model(df):
|
| 26 |
+
tfidf = TfidfVectorizer(stop_words="english")
|
| 27 |
+
tfidf_matrix = tfidf.fit_transform(df["overview"])
|
| 28 |
+
cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)
|
| 29 |
+
return cosine_sim
|
| 30 |
+
|
| 31 |
+
cosine_sim = build_model(df)
|
| 32 |
+
|
| 33 |
+
# -----------------------
|
| 34 |
+
# 3. Build reverse index mapping (title -> index)
|
| 35 |
+
# -----------------------
|
| 36 |
+
indices = pd.Series(df2.index, index=df2["title"]).drop_duplicates()
|
| 37 |
+
|
| 38 |
+
# -----------------------
|
| 39 |
+
# 4. Recommendation function
|
| 40 |
+
# -----------------------
|
| 41 |
+
def get_recommendations(title, cosine_sim=cosine_sim):
|
| 42 |
+
if title not in indices:
|
| 43 |
+
return []
|
| 44 |
+
idx = indices[title]
|
| 45 |
+
sim_scores = list(enumerate(cosine_sim[idx]))
|
| 46 |
+
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
| 47 |
+
sim_scores = sim_scores[1:11] # skip the movie itself
|
| 48 |
+
movie_indices = [i[0] for i in sim_scores]
|
| 49 |
+
return df2["title"].iloc[movie_indices]
|
| 50 |
+
|
| 51 |
+
# -----------------------
|
| 52 |
+
# 5. Streamlit App UI
|
| 53 |
+
# -----------------------
|
| 54 |
+
st.title("🎬 Movie Recommendation Engine")
|
| 55 |
+
st.markdown("Get recommendations based on similar movie plots!")
|
| 56 |
+
|
| 57 |
+
movie_list = df2["title"].values
|
| 58 |
+
selected_movie = st.selectbox("Choose a movie to get recommendations", movie_list)
|
| 59 |
+
|
| 60 |
+
if st.button("Recommend"):
|
| 61 |
+
recommendations = get_recommendations(selected_movie)
|
| 62 |
+
if len(recommendations) == 0:
|
| 63 |
+
st.warning("Movie not found in the database.")
|
| 64 |
+
else:
|
| 65 |
+
st.success("You might also like:")
|
| 66 |
+
for rec in recommendations:
|
| 67 |
+
st.write(f"- {rec}")
|