Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +44 -65
src/streamlit_app.py
CHANGED
|
@@ -13,8 +13,8 @@ RATINGS_JSON_PATH = "/tmp/ratings.json"
|
|
| 13 |
POSTER_PLACEHOLDER = "https://via.placeholder.com/300x450.png?text=No+Poster"
|
| 14 |
TMDB_API_KEY = "d15fc170483ad01d6b3d59561432fefc"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def
|
| 18 |
url = "https://api.themoviedb.org/3/search/movie"
|
| 19 |
params = {
|
| 20 |
"api_key": TMDB_API_KEY,
|
|
@@ -25,12 +25,15 @@ def get_poster_url(title, year=None):
|
|
| 25 |
try:
|
| 26 |
response = requests.get(url, params=params)
|
| 27 |
if response.status_code == 200 and response.json()["results"]:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception:
|
| 32 |
pass
|
| 33 |
-
return POSTER_PLACEHOLDER
|
| 34 |
|
| 35 |
def load_ratings():
|
| 36 |
if os.path.exists(RATINGS_JSON_PATH):
|
|
@@ -51,7 +54,6 @@ def load_movies():
|
|
| 51 |
df["year"] = df["title"].str.extract(r'\((\d{4})\)').fillna("0").astype(int)
|
| 52 |
df["clean_title"] = df["title"].str.replace(r'\(\d{4}\)', '', regex=True).str.strip()
|
| 53 |
df["genres"] = df["genres"].fillna("Unknown")
|
| 54 |
-
df["poster"] = df.apply(lambda row: get_poster_url(row["clean_title"], row["year"]), axis=1)
|
| 55 |
return df
|
| 56 |
|
| 57 |
movie_df = load_movies()
|
|
@@ -167,13 +169,16 @@ if movie_id:
|
|
| 167 |
movie_id = int(movie_id)
|
| 168 |
movie_info = movie_df[movie_df["movieId"] == movie_id].iloc[0]
|
| 169 |
st.title(movie_info["clean_title"])
|
|
|
|
| 170 |
col1, col2 = st.columns([1, 3])
|
| 171 |
with col1:
|
| 172 |
-
st.image(
|
| 173 |
with col2:
|
| 174 |
st.subheader("Details")
|
| 175 |
st.write(f"**Genres:** {movie_info['genres']}")
|
| 176 |
st.write(f"**Year:** {movie_info['year']}")
|
|
|
|
|
|
|
| 177 |
|
| 178 |
user_rating = next((r["rating"] for r in all_ratings_data if r["movie_id"] == movie_id), None)
|
| 179 |
if user_rating:
|
|
@@ -184,63 +189,37 @@ if movie_id:
|
|
| 184 |
st.error("Could not load movie details.")
|
| 185 |
|
| 186 |
elif page:
|
| 187 |
-
st.title("
|
| 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 |
-
else:
|
| 220 |
-
movie = st.session_state.queue[st.session_state.index]
|
| 221 |
-
movie_id_val = title_to_movie_id.get(movie)
|
| 222 |
-
movie_row = movie_df[movie_df["movieId"] == movie_id_val].iloc[0]
|
| 223 |
-
|
| 224 |
-
st.markdown(f"## **{movie_row['clean_title']}**")
|
| 225 |
-
st.caption(f"Genres: {movie_row['genres']} | Year: {movie_row['year']}")
|
| 226 |
-
|
| 227 |
-
col1, col2, col3, col4, col5 = st.columns(5)
|
| 228 |
-
selected = None
|
| 229 |
-
for i, col in enumerate([col1, col2, col3, col4, col5], 1):
|
| 230 |
-
if col.button("β
" * i, key=f"rate_{i}"):
|
| 231 |
-
selected = i
|
| 232 |
-
|
| 233 |
-
if selected:
|
| 234 |
-
entry = {"movie_id": movie_id_val, "rating": selected, "timestamp": datetime.now().isoformat(), "source": "quiz"}
|
| 235 |
-
st.session_state.rated.append(entry)
|
| 236 |
-
save_rating_to_json(entry)
|
| 237 |
-
st.session_state.index += 1
|
| 238 |
-
st.rerun()
|
| 239 |
-
|
| 240 |
-
st.markdown("<br>", unsafe_allow_html=True)
|
| 241 |
-
if st.button("Didn't Watch"):
|
| 242 |
-
st.session_state.index += 1
|
| 243 |
-
st.rerun()
|
| 244 |
|
| 245 |
elif search_query:
|
| 246 |
# Movie Search Page
|
|
|
|
| 13 |
POSTER_PLACEHOLDER = "https://via.placeholder.com/300x450.png?text=No+Poster"
|
| 14 |
TMDB_API_KEY = "d15fc170483ad01d6b3d59561432fefc"
|
| 15 |
|
| 16 |
+
@st.cache_data(show_spinner=False)
|
| 17 |
+
def get_tmdb_data(title, year=None):
|
| 18 |
url = "https://api.themoviedb.org/3/search/movie"
|
| 19 |
params = {
|
| 20 |
"api_key": TMDB_API_KEY,
|
|
|
|
| 25 |
try:
|
| 26 |
response = requests.get(url, params=params)
|
| 27 |
if response.status_code == 200 and response.json()["results"]:
|
| 28 |
+
result = response.json()["results"][0]
|
| 29 |
+
poster_path = result.get("poster_path")
|
| 30 |
+
movie_id = result.get("id")
|
| 31 |
+
poster_url = f"https://image.tmdb.org/t/p/w500{poster_path}" if poster_path else POSTER_PLACEHOLDER
|
| 32 |
+
tmdb_link = f"https://www.themoviedb.org/movie/{movie_id}" if movie_id else None
|
| 33 |
+
return poster_url, tmdb_link
|
| 34 |
except Exception:
|
| 35 |
pass
|
| 36 |
+
return POSTER_PLACEHOLDER, None
|
| 37 |
|
| 38 |
def load_ratings():
|
| 39 |
if os.path.exists(RATINGS_JSON_PATH):
|
|
|
|
| 54 |
df["year"] = df["title"].str.extract(r'\((\d{4})\)').fillna("0").astype(int)
|
| 55 |
df["clean_title"] = df["title"].str.replace(r'\(\d{4}\)', '', regex=True).str.strip()
|
| 56 |
df["genres"] = df["genres"].fillna("Unknown")
|
|
|
|
| 57 |
return df
|
| 58 |
|
| 59 |
movie_df = load_movies()
|
|
|
|
| 169 |
movie_id = int(movie_id)
|
| 170 |
movie_info = movie_df[movie_df["movieId"] == movie_id].iloc[0]
|
| 171 |
st.title(movie_info["clean_title"])
|
| 172 |
+
poster_url, tmdb_link = get_tmdb_data(movie_info["clean_title"], movie_info["year"])
|
| 173 |
col1, col2 = st.columns([1, 3])
|
| 174 |
with col1:
|
| 175 |
+
st.image(poster_url, use_column_width=True)
|
| 176 |
with col2:
|
| 177 |
st.subheader("Details")
|
| 178 |
st.write(f"**Genres:** {movie_info['genres']}")
|
| 179 |
st.write(f"**Year:** {movie_info['year']}")
|
| 180 |
+
if tmdb_link:
|
| 181 |
+
st.markdown(f"[π View on TMDb]({tmdb_link})")
|
| 182 |
|
| 183 |
user_rating = next((r["rating"] for r in all_ratings_data if r["movie_id"] == movie_id), None)
|
| 184 |
if user_rating:
|
|
|
|
| 189 |
st.error("Could not load movie details.")
|
| 190 |
|
| 191 |
elif page:
|
| 192 |
+
st.title("πΏ Rate Random Movies")
|
| 193 |
+
num_questions = 1
|
| 194 |
+
movie_pool = movie_df.sample(frac=1).reset_index(drop=True)
|
| 195 |
+
index = st.session_state.get("random_index", 0)
|
| 196 |
+
|
| 197 |
+
if index < len(movie_pool):
|
| 198 |
+
movie = movie_pool.iloc[index]
|
| 199 |
+
st.session_state["random_index"] = index + 1
|
| 200 |
+
poster_url, tmdb_link = get_tmdb_data(movie["clean_title"], movie["year"])
|
| 201 |
+
|
| 202 |
+
st.image(poster_url, width=300)
|
| 203 |
+
st.subheader(movie["clean_title"])
|
| 204 |
+
st.markdown(f"**Genres:** {movie['genres']}")
|
| 205 |
+
st.markdown(f"**Year:** {movie['year']}")
|
| 206 |
+
if tmdb_link:
|
| 207 |
+
st.markdown(f"[π View on TMDb]({tmdb_link})")
|
| 208 |
+
|
| 209 |
+
st.markdown("### Your Rating")
|
| 210 |
+
rating = st.radio("Rate this movie:", [1, 2, 3, 4, 5], horizontal=True, key=f"rating_{movie['movieId']}")
|
| 211 |
+
col1, col2 = st.columns([1, 1])
|
| 212 |
+
with col1:
|
| 213 |
+
if st.button("Submit Rating"):
|
| 214 |
+
save_rating_to_json({
|
| 215 |
+
"movie_id": int(movie["movieId"]),
|
| 216 |
+
"rating": rating,
|
| 217 |
+
"timestamp": datetime.now().isoformat()
|
| 218 |
+
})
|
| 219 |
+
st.rerun()
|
| 220 |
+
with col2:
|
| 221 |
+
if st.button("Didn't Watch"):
|
| 222 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
elif search_query:
|
| 225 |
# Movie Search Page
|