Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +42 -4
src/streamlit_app.py
CHANGED
|
@@ -4,12 +4,16 @@ import random
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Load movie data
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
@st.cache_data
|
| 10 |
def load_movies():
|
| 11 |
df = pd.read_csv(MOVIES_PATH)
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
movie_df = load_movies()
|
| 15 |
movie_titles = movie_df["title"].unique().tolist()
|
|
@@ -55,8 +59,24 @@ st.markdown("""
|
|
| 55 |
query_params = st.query_params
|
| 56 |
page = query_params.get("recommend")
|
| 57 |
search_query = query_params.get("search")
|
|
|
|
| 58 |
|
| 59 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# Quiz Flow
|
| 61 |
if "queue" not in st.session_state:
|
| 62 |
random.shuffle(movie_titles)
|
|
@@ -87,16 +107,34 @@ if page:
|
|
| 87 |
elif search_query:
|
| 88 |
# Movie Search Page
|
| 89 |
st.title(f"🔍 Search Results for '{search_query}'")
|
| 90 |
-
matches = movie_df[movie_df["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
if matches.empty:
|
| 92 |
st.warning("No matching movies found.")
|
| 93 |
for _, row in matches.iterrows():
|
| 94 |
with st.expander(row["title"]):
|
| 95 |
st.write(f"**Genres:** {row['genres']}")
|
|
|
|
|
|
|
| 96 |
rating = st.radio(f"Rate '{row['title']}'", [1, 2, 3, 4, 5], key=row["title"])
|
| 97 |
if st.button(f"Submit rating for {row['title']}", key=f"submit_{row['title']}"):
|
| 98 |
st.session_state.rated.append({"movie": row["title"], "rating": rating})
|
| 99 |
st.success(f"Thanks for rating '{row['title']}' with ⭐{rating}")
|
|
|
|
| 100 |
else:
|
| 101 |
# Home Page
|
| 102 |
st.title("🏠 Welcome to MovieMatch")
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Load movie data
|
| 7 |
+
BASE_DIR = os.path.dirname(__file__)
|
| 8 |
+
MOVIES_PATH = os.path.join(BASE_DIR, "src", "movies.csv")
|
| 9 |
|
| 10 |
@st.cache_data
|
| 11 |
def load_movies():
|
| 12 |
df = pd.read_csv(MOVIES_PATH)
|
| 13 |
+
df["year"] = df["title"].str.extract(r'\((\d{4})\)').fillna("Unknown")
|
| 14 |
+
df["clean_title"] = df["title"].str.replace(r'\(\d{4}\)', '', regex=True).str.strip()
|
| 15 |
+
df["genres"] = df["genres"].fillna("Unknown")
|
| 16 |
+
return df
|
| 17 |
|
| 18 |
movie_df = load_movies()
|
| 19 |
movie_titles = movie_df["title"].unique().tolist()
|
|
|
|
| 59 |
query_params = st.query_params
|
| 60 |
page = query_params.get("recommend")
|
| 61 |
search_query = query_params.get("search")
|
| 62 |
+
movie_id = query_params.get("movie_id")
|
| 63 |
|
| 64 |
+
if movie_id:
|
| 65 |
+
# Detail Page
|
| 66 |
+
try:
|
| 67 |
+
movie_id = int(movie_id)
|
| 68 |
+
movie_row = movie_df[movie_df["movieId"] == movie_id].iloc[0]
|
| 69 |
+
st.title(movie_row["clean_title"])
|
| 70 |
+
st.caption(f"Genres: {movie_row['genres']}")
|
| 71 |
+
st.caption(f"Release Year: {movie_row['year']}")
|
| 72 |
+
rating = st.radio("Rate this movie", [1, 2, 3, 4, 5], key=f"detail_{movie_id}")
|
| 73 |
+
if st.button("Submit Rating"):
|
| 74 |
+
st.session_state.rated.append({"movie": movie_row["title"], "rating": rating})
|
| 75 |
+
st.success(f"Thanks for rating '{movie_row['title']}' with ⭐{rating}")
|
| 76 |
+
except:
|
| 77 |
+
st.error("Movie not found.")
|
| 78 |
+
|
| 79 |
+
elif page:
|
| 80 |
# Quiz Flow
|
| 81 |
if "queue" not in st.session_state:
|
| 82 |
random.shuffle(movie_titles)
|
|
|
|
| 107 |
elif search_query:
|
| 108 |
# Movie Search Page
|
| 109 |
st.title(f"🔍 Search Results for '{search_query}'")
|
| 110 |
+
matches = movie_df[movie_df["clean_title"].str.contains(search_query, case=False, na=False)]
|
| 111 |
+
|
| 112 |
+
# Filters
|
| 113 |
+
genres_all = sorted(set("|".join(movie_df["genres"]).split("|")))
|
| 114 |
+
selected_genre = st.selectbox("Filter by genre", ["All"] + genres_all)
|
| 115 |
+
if selected_genre != "All":
|
| 116 |
+
matches = matches[matches["genres"].str.contains(selected_genre)]
|
| 117 |
+
|
| 118 |
+
sort_order = st.selectbox("Sort by", ["Title A-Z", "Year Desc", "Year Asc"])
|
| 119 |
+
if sort_order == "Year Desc":
|
| 120 |
+
matches = matches.sort_values(by="year", ascending=False)
|
| 121 |
+
elif sort_order == "Year Asc":
|
| 122 |
+
matches = matches.sort_values(by="year", ascending=True)
|
| 123 |
+
else:
|
| 124 |
+
matches = matches.sort_values(by="clean_title")
|
| 125 |
+
|
| 126 |
if matches.empty:
|
| 127 |
st.warning("No matching movies found.")
|
| 128 |
for _, row in matches.iterrows():
|
| 129 |
with st.expander(row["title"]):
|
| 130 |
st.write(f"**Genres:** {row['genres']}")
|
| 131 |
+
st.write(f"**Year:** {row['year']}")
|
| 132 |
+
st.markdown(f"[➡️ Go to Detail Page](?movie_id={row['movieId']})")
|
| 133 |
rating = st.radio(f"Rate '{row['title']}'", [1, 2, 3, 4, 5], key=row["title"])
|
| 134 |
if st.button(f"Submit rating for {row['title']}", key=f"submit_{row['title']}"):
|
| 135 |
st.session_state.rated.append({"movie": row["title"], "rating": rating})
|
| 136 |
st.success(f"Thanks for rating '{row['title']}' with ⭐{rating}")
|
| 137 |
+
|
| 138 |
else:
|
| 139 |
# Home Page
|
| 140 |
st.title("🏠 Welcome to MovieMatch")
|