File size: 13,528 Bytes
16aed1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import os
import re
import numpy as np
import pandas as pd
import streamlit as st
import plotly.express as px

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

st.set_page_config(page_title="RX12 - Netflix Hybrid Recommender", layout="wide")

# -------------------------
# Files expected in Space ROOT
# -------------------------
TITLES_PATH = "netflix_titles.csv"
POPULARITY_PATH = "synthetic_title_popularity.csv"

# -------------------------
# Helpers
# -------------------------
def clean_text(s: str) -> str:
    s = "" if pd.isna(s) else str(s)
    s = s.lower()
    s = re.sub(r"[^a-z0-9\s]", " ", s)
    s = re.sub(r"\s+", " ", s).strip()
    return s

@st.cache_data(show_spinner=False)
def load_titles(path: str) -> pd.DataFrame:
    df = pd.read_csv(path)
    df.columns = [c.strip() for c in df.columns]
    return df

@st.cache_data(show_spinner=False)
def load_popularity(path: str) -> pd.DataFrame:
    df = pd.read_csv(path)
    df.columns = [c.strip() for c in df.columns]
    return df

def build_combined_text(df: pd.DataFrame) -> pd.Series:
    """
    Combine rich metadata fields into a single 'bag of words' representation.
    """
    fields = ["type", "director", "cast", "country", "rating", "listed_in", "description"]
    tmp = df.copy()
    for f in fields:
        if f not in tmp.columns:
            tmp[f] = ""
        tmp[f] = tmp[f].fillna("").astype(str).map(clean_text)

    combined = (
        tmp["type"] + " " +
        tmp["listed_in"] + " " +
        tmp["country"] + " " +
        tmp["rating"] + " " +
        tmp["director"] + " " +
        tmp["cast"] + " " +
        tmp["description"]
    ).str.replace(r"\s+", " ", regex=True).str.strip()

    return combined

@st.cache_data(show_spinner=False)
def build_tfidf_and_similarity(df: pd.DataFrame):
    combined = build_combined_text(df)
    vectorizer = TfidfVectorizer(
        stop_words="english",
        ngram_range=(1, 2),
        min_df=2,
        max_features=50000,
    )
    tfidf = vectorizer.fit_transform(combined)
    sim = cosine_similarity(tfidf, tfidf)
    title_to_idx = {t: i for i, t in enumerate(df["title"].tolist())}
    return sim, title_to_idx

def demand_tier(pop_score: pd.Series) -> pd.Series:
    q1, q2 = pop_score.quantile([0.33, 0.66]).tolist()
    def tier(x):
        if x <= q1: return "Low"
        if x <= q2: return "Mid"
        return "High"
    return pop_score.apply(tier)

def hybrid_recommend(df: pd.DataFrame, sim: np.ndarray, title_to_idx: dict,
                     seed_title: str, top_n: int = 10, alpha: float = 0.7,
                     same_type: bool = True) -> pd.DataFrame:
    """
    Hybrid score = alpha * content_similarity + (1 - alpha) * popularity_norm
    NOTE: This recommendation is computed on the FULL dataset (df),
    not restricted by the Country→Genre selection.
    """
    if seed_title not in title_to_idx:
        return pd.DataFrame()

    idx = title_to_idx[seed_title]
    sim_scores = sim[idx].copy()
    sim_scores[idx] = -1  # drop itself

    pop = df["popularity_norm"].astype(float).to_numpy()
    hybrid = alpha * sim_scores + (1 - alpha) * pop

    seed_type = df.loc[idx, "type"] if "type" in df.columns else None
    candidate_idx = np.arange(len(df))
    if same_type and seed_type is not None:
        candidate_idx = candidate_idx[df["type"].to_numpy() == seed_type]

    ranked = candidate_idx[np.argsort(hybrid[candidate_idx])[::-1]]
    ranked = ranked[:top_n]

    out = df.iloc[ranked].copy()
    out["content_similarity"] = sim_scores[ranked]
    out["hybrid_score"] = hybrid[ranked]

    keep = [
        "title", "type", "release_year", "rating", "listed_in", "country",
        "popularity_score", "popularity_norm", "tier",
        "content_similarity", "hybrid_score"
    ]
    keep = [c for c in keep if c in out.columns]
    return out[keep].sort_values("hybrid_score", ascending=False).reset_index(drop=True)

def split_genres(listed_in: str) -> list:
    if pd.isna(listed_in) or not str(listed_in).strip():
        return []
    return [g.strip() for g in str(listed_in).split(",") if g.strip()]

# -------------------------
# Load + merge
# -------------------------
st.title("RX12 - Netflix Hybrid Recommender")
st.caption("Content-based recommendations (TF‑IDF over metadata & descriptions) blended with popularity priors.")

missing = [p for p in [TITLES_PATH, POPULARITY_PATH] if not os.path.exists(p)]
if missing:
    st.error(
        "Missing required file(s) in Space root directory: "
        + ", ".join(f"`{m}`" for m in missing)
        + ". Upload them under the **Files** tab (root level)."
    )
    st.stop()

titles = load_titles(TITLES_PATH)
pop = load_popularity(POPULARITY_PATH)

if "title" not in titles.columns:
    st.error("`netflix_titles.csv` must contain a `title` column.")
    st.stop()
if not {"title", "popularity_score", "popularity_norm"}.issubset(set(pop.columns)):
    st.error("`synthetic_title_popularity.csv` must contain `title`, `popularity_score`, `popularity_norm`.")
    st.stop()

df = titles.merge(pop, on="title", how="left")
df["popularity_score"] = pd.to_numeric(df["popularity_score"], errors="coerce")
df["popularity_norm"] = pd.to_numeric(df["popularity_norm"], errors="coerce")
df = df.dropna(subset=["popularity_score", "popularity_norm"]).reset_index(drop=True)

df["tier"] = demand_tier(df["popularity_score"])

sim, title_to_idx = build_tfidf_and_similarity(df)

# -------------------------
# Sidebar controls (GLOBAL)
# -------------------------
st.sidebar.header("Controls (Global)")

type_opt = st.sidebar.multiselect(
    "Content type",
    sorted(df["type"].dropna().unique().tolist()),
    default=sorted(df["type"].dropna().unique().tolist())
)

year_min, year_max = int(df["release_year"].min()), int(df["release_year"].max())
year_range = st.sidebar.slider("Release year range", year_min, year_max, (max(year_min, year_max-20), year_max))

rating_opt = st.sidebar.multiselect("Rating", sorted(df["rating"].fillna("Unknown").unique().tolist()), default=[])
tier_opt = st.sidebar.multiselect("Demand tier", ["Low", "Mid", "High"], default=["Low","Mid","High"])

alpha = st.sidebar.slider("Hybrid weight (Similarity → Popularity)", 0.0, 1.0, 0.7, 0.05)
top_n = st.sidebar.slider("Top N recommendations", 5, 30, 10)
same_type = st.sidebar.checkbox("Recommend within same type", value=True)

# Apply global filters for browsing and for seed title selection
f = df.copy()
f = f[f["type"].isin(type_opt)]
f = f[(f["release_year"] >= year_range[0]) & (f["release_year"] <= year_range[1])]
f = f[f["tier"].isin(tier_opt)]
if rating_opt:
    f = f[f["rating"].fillna("Unknown").isin(rating_opt)]
f = f.reset_index(drop=True)

if f.empty:
    st.warning("No titles match your global filters. Adjust filters in the sidebar.")
    st.stop()

seed_title = st.sidebar.selectbox("Pick a seed title", options=sorted(f["title"].unique().tolist()))

tab1, tab2, tab3, tab4 = st.tabs(["Recommend", "Explore Catalog", "Country → Genre", "Explain Method"])

# -------------------------
# Tab 1: Recommendations
# -------------------------
with tab1:
    st.subheader("Recommendations (not affected by Country → Genre selection)")

    seed_row = df[df["title"] == seed_title].head(1)
    if not seed_row.empty:
        sr = seed_row.iloc[0]
        st.markdown(
            f"**Seed title:** `{sr['title']}`  \n"
            f"- Type: **{sr.get('type','')}** | Year: **{sr.get('release_year','')}** | Rating: **{sr.get('rating','')}**  \n"
            f"- Genres: **{sr.get('listed_in','')}**  \n"
            f"- Country: **{sr.get('country','')}**  \n"
        )
        with st.expander("Show description"):
            st.write(sr.get("description", ""))

    recs = hybrid_recommend(df, sim, title_to_idx, seed_title, top_n=top_n, alpha=alpha, same_type=same_type)

    if recs.empty:
        st.info("No recommendations produced. Try another title.")
    else:
        c1, c2 = st.columns([1.25, 1])
        with c1:
            st.dataframe(recs, use_container_width=True, height=420)
        with c2:
            fig = px.scatter(
                recs,
                x="content_similarity",
                y="popularity_norm",
                size="hybrid_score",
                hover_data=["title", "type", "release_year", "tier", "rating"],
            )
            fig.update_layout(
                xaxis_title="Content similarity (cosine TF‑IDF)",
                yaxis_title="Popularity (normalized)",
                height=420
            )
            st.plotly_chart(fig, use_container_width=True)

        st.caption("Tip: move alpha toward 1.0 for 'more similar', toward 0.0 for 'more popular' picks.")

# -------------------------
# Tab 2: Explore catalog (GLOBAL FILTERS ONLY)
# -------------------------
with tab2:
    st.subheader("Interactive catalog exploration (global filters only)")

    c1, c2 = st.columns([1, 1])

    with c1:
        fig = px.histogram(f, x="popularity_score", color="tier", nbins=40, marginal="box")
        fig.update_layout(xaxis_title="Popularity score", yaxis_title="Count", height=420)
        st.plotly_chart(fig, use_container_width=True)

    with c2:
        top = f.sort_values("popularity_score", ascending=False).head(20)
        fig2 = px.bar(top, x="title", y="popularity_score", color="tier")
        fig2.update_layout(xaxis_title="", yaxis_title="Popularity score", height=420)
        st.plotly_chart(fig2, use_container_width=True)

    st.dataframe(
        f[["title","type","release_year","rating","listed_in","country","popularity_score","tier"]].reset_index(drop=True),
        use_container_width=True,
        height=360
    )

# -------------------------
# Tab 3: Country → Genre (COUNTRY SELECTION LIVES HERE ONLY)
# -------------------------
with tab3:
    st.subheader("Country → Genre analysis (affects this tab only)")
    st.caption("Select a production country to see the most common genres and example titles from that market.")

    # Country selector is intentionally inside this tab so it does NOT affect recommendations.
    country_values = (
        df["country"]
        .fillna("")
        .astype(str)
        .str.split(",")
        .explode()
        .str.strip()
    )
    country_values = country_values[country_values != ""]
    all_countries = sorted(country_values.unique().tolist())

    if not all_countries:
        st.warning("No country information found in the dataset.")
    else:
        colA, colB = st.columns([1, 1])
        with colA:
            selected_country = st.selectbox("Select a country", options=all_countries)
        with colB:
            top_genres_n = st.slider("Top N genres", 5, 30, 10)

        # Filter titles that include the selected country
        mask = df["country"].fillna("").astype(str).str.contains(rf"(^|,\s*){re.escape(selected_country)}(\s*,|$)", regex=True)
        dcf = df[mask].copy()

        if dcf.empty:
            st.info("No titles found for this country.")
        else:
            # Genre counts
            genres = dcf["listed_in"].apply(split_genres).explode()
            genres = genres.dropna()
            genre_counts = genres.value_counts().head(top_genres_n).reset_index()
            genre_counts.columns = ["genre", "count"]

            fig = px.bar(genre_counts, x="genre", y="count")
            fig.update_layout(xaxis_title="Genre", yaxis_title="Count", height=420)
            st.plotly_chart(fig, use_container_width=True)

            # Example titles for each top genre (up to 5 per genre)
            examples = []
            top_genres = genre_counts["genre"].tolist()
            for g in top_genres:
                ex = dcf[dcf["listed_in"].fillna("").astype(str).str.contains(re.escape(g), regex=True)]
                ex = ex.sort_values("popularity_score", ascending=False).head(5)
                for _, row in ex.iterrows():
                    examples.append({
                        "genre": g,
                        "title": row.get("title", ""),
                        "type": row.get("type", ""),
                        "release_year": row.get("release_year", ""),
                        "rating": row.get("rating", ""),
                        "popularity_score": row.get("popularity_score", ""),
                    })

            exdf = pd.DataFrame(examples)
            st.markdown("**Example titles (top 5 by popularity within each genre)**")
            st.dataframe(exdf, use_container_width=True, height=360)

# -------------------------
# Tab 4: Explain
# -------------------------
with tab4:
    st.subheader("How recommendations are generated (workshop-aligned)")
    st.markdown(
        """
### 1) Build a content representation
We combine multiple metadata fields into one text document per title:

- type (Movie / TV Show)
- listed_in (genres)
- country
- rating
- director, cast
- description

### 2) TF‑IDF + cosine similarity
We vectorize the combined text using TF‑IDF and compute pairwise cosine similarity between all titles.

### 3) Hybrid ranking
We blend content similarity with a popularity prior:

`hybrid_score = alpha * similarity + (1 - alpha) * popularity_norm`

**Important:** the **Country → Genre** selection is an exploration tool and does **not** restrict the recommender.
        """
    )
    st.code(
        "hybrid_score = alpha * cosine_similarity(TFIDF(combined_text)) + (1 - alpha) * popularity_norm",
        language="python"
    )