Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +39 -40
src/streamlit_app.py
CHANGED
|
@@ -238,47 +238,51 @@ if movie_id:
|
|
| 238 |
elif page:
|
| 239 |
st.title("Rate Random Movies")
|
| 240 |
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
selected_genres = st.multiselect("Select genres:", options=all_genres, placeholder="All genres")
|
| 243 |
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
filtered_df = movie_df[genre_filter].copy()
|
| 248 |
-
else:
|
| 249 |
-
filtered_df = movie_df.copy()
|
| 250 |
-
|
| 251 |
-
if filtered_df.empty:
|
| 252 |
-
st.warning("No movies match the selected filters.")
|
| 253 |
-
st.stop()
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
st.session_state.last_filter_state = tuple(selected_genres)
|
| 259 |
-
st.session_state.current_random_movie = None
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
|
|
|
| 267 |
|
| 268 |
if index >= len(movie_pool):
|
| 269 |
st.info("No more movies to rate.")
|
| 270 |
st.stop()
|
| 271 |
|
| 272 |
-
|
| 273 |
-
movie = movie_pool.iloc[index]
|
| 274 |
-
except IndexError:
|
| 275 |
-
st.error("Could not load a movie. Please try again.")
|
| 276 |
-
st.stop()
|
| 277 |
-
|
| 278 |
-
if movie is None or pd.isna(movie["clean_title"]):
|
| 279 |
-
st.error("Invalid movie data.")
|
| 280 |
-
st.stop()
|
| 281 |
|
|
|
|
| 282 |
poster_url, tmdb_link = get_tmdb_data(movie["clean_title"], movie["year"])
|
| 283 |
|
| 284 |
col1, col2 = st.columns([1, 2])
|
|
@@ -300,30 +304,25 @@ elif page:
|
|
| 300 |
st.markdown(f"<a href='{tmdb_link}' target='_blank'>View on TMDb</a>", unsafe_allow_html=True)
|
| 301 |
|
| 302 |
st.markdown(f"### Movie {index + 1} of {len(movie_pool)}")
|
| 303 |
-
st.markdown("### Your Rating")
|
| 304 |
rating_key = f"rating_{movie['movieId']}"
|
| 305 |
-
|
| 306 |
-
st.session_state[rating_key] = None
|
| 307 |
-
st.session_state[rating_key] = st.radio(
|
| 308 |
-
"Rate this movie:", [1, 2, 3, 4, 5], horizontal=True, index=None, key=f"radio_{movie['movieId']}")
|
| 309 |
|
| 310 |
col_submit, col_skip = st.columns([1, 1])
|
| 311 |
with col_submit:
|
| 312 |
-
rating = st.session_state[rating_key]
|
| 313 |
if st.button("Submit Rating") and rating:
|
| 314 |
save_rating_to_json({
|
| 315 |
"movie_id": int(movie["movieId"]),
|
| 316 |
"rating": rating,
|
| 317 |
"timestamp": datetime.now().isoformat()
|
| 318 |
})
|
| 319 |
-
st.session_state["
|
| 320 |
-
st.
|
| 321 |
-
|
| 322 |
with col_skip:
|
| 323 |
if st.button("Didn't Watch"):
|
| 324 |
-
st.session_state["
|
| 325 |
-
st.
|
| 326 |
-
|
| 327 |
|
| 328 |
elif search_query:
|
| 329 |
st.title(f"Search Results for '{search_query}'")
|
|
|
|
| 238 |
elif page:
|
| 239 |
st.title("Rate Random Movies")
|
| 240 |
|
| 241 |
+
# Genres extrahieren
|
| 242 |
+
all_genres = sorted(set(
|
| 243 |
+
genre.strip()
|
| 244 |
+
for sublist in movie_df["genres"].dropna().str.split("|")
|
| 245 |
+
for genre in sublist
|
| 246 |
+
))
|
| 247 |
selected_genres = st.multiselect("Select genres:", options=all_genres, placeholder="All genres")
|
| 248 |
|
| 249 |
+
# Initialer Zustand
|
| 250 |
+
if "rating_mode_state" not in st.session_state:
|
| 251 |
+
st.session_state.rating_mode_state = {
|
| 252 |
+
"movie_pool": [],
|
| 253 |
+
"index": 0,
|
| 254 |
+
"genre_filter": []
|
| 255 |
+
}
|
| 256 |
|
| 257 |
+
# Filter neu anwenden, wenn sich Genres geändert haben
|
| 258 |
+
if st.session_state.rating_mode_state["genre_filter"] != selected_genres:
|
| 259 |
+
genre_filter = movie_df["genres"].apply(
|
| 260 |
+
lambda g: any(genre in g.split("|") for genre in selected_genres)
|
| 261 |
+
) if selected_genres else pd.Series([True] * len(movie_df))
|
| 262 |
+
|
| 263 |
filtered_df = movie_df[genre_filter].copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
if filtered_df.empty:
|
| 266 |
+
st.warning("No movies match the selected filters.")
|
| 267 |
+
st.stop()
|
|
|
|
|
|
|
| 268 |
|
| 269 |
+
st.session_state.rating_mode_state = {
|
| 270 |
+
"movie_pool": filtered_df.sample(frac=1).reset_index(drop=True),
|
| 271 |
+
"index": 0,
|
| 272 |
+
"genre_filter": selected_genres
|
| 273 |
+
}
|
| 274 |
|
| 275 |
+
# Hole aktuellen Film
|
| 276 |
+
movie_pool = st.session_state.rating_mode_state["movie_pool"]
|
| 277 |
+
index = st.session_state.rating_mode_state["index"]
|
| 278 |
|
| 279 |
if index >= len(movie_pool):
|
| 280 |
st.info("No more movies to rate.")
|
| 281 |
st.stop()
|
| 282 |
|
| 283 |
+
movie = movie_pool.iloc[index]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
+
# Poster abrufen (nutze ggf. gecachte Version)
|
| 286 |
poster_url, tmdb_link = get_tmdb_data(movie["clean_title"], movie["year"])
|
| 287 |
|
| 288 |
col1, col2 = st.columns([1, 2])
|
|
|
|
| 304 |
st.markdown(f"<a href='{tmdb_link}' target='_blank'>View on TMDb</a>", unsafe_allow_html=True)
|
| 305 |
|
| 306 |
st.markdown(f"### Movie {index + 1} of {len(movie_pool)}")
|
|
|
|
| 307 |
rating_key = f"rating_{movie['movieId']}"
|
| 308 |
+
rating = st.radio("Rate this movie:", [1, 2, 3, 4, 5], horizontal=True, key=rating_key)
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
col_submit, col_skip = st.columns([1, 1])
|
| 311 |
with col_submit:
|
|
|
|
| 312 |
if st.button("Submit Rating") and rating:
|
| 313 |
save_rating_to_json({
|
| 314 |
"movie_id": int(movie["movieId"]),
|
| 315 |
"rating": rating,
|
| 316 |
"timestamp": datetime.now().isoformat()
|
| 317 |
})
|
| 318 |
+
st.session_state.rating_mode_state["index"] += 1
|
| 319 |
+
st.rerun()
|
| 320 |
+
|
| 321 |
with col_skip:
|
| 322 |
if st.button("Didn't Watch"):
|
| 323 |
+
st.session_state.rating_mode_state["index"] += 1
|
| 324 |
+
st.rerun()
|
| 325 |
+
|
| 326 |
|
| 327 |
elif search_query:
|
| 328 |
st.title(f"Search Results for '{search_query}'")
|