lenawilli commited on
Commit
9797fd3
·
verified ·
1 Parent(s): 9b6f778

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +24 -34
src/streamlit_app.py CHANGED
@@ -21,7 +21,6 @@ def load_ratings():
21
 
22
  def save_rating_to_json(entry):
23
  all_ratings = load_ratings()
24
- # Remove any existing rating for the same movie_id
25
  all_ratings = [r for r in all_ratings if r["movie_id"] != entry["movie_id"]]
26
  all_ratings.append(entry)
27
  with open(RATINGS_JSON_PATH, "w") as f:
@@ -41,13 +40,11 @@ movie_titles = movie_df["title"].unique().tolist()
41
  movie_id_to_title = dict(zip(movie_df["movieId"], movie_df["title"]))
42
  title_to_movie_id = dict(zip(movie_df["title"], movie_df["movieId"]))
43
 
44
- # Initialize session state
45
  if "rated" not in st.session_state:
46
  st.session_state.rated = []
47
  if "quiz_history" not in st.session_state:
48
  st.session_state.quiz_history = []
49
 
50
- # Navigation and search
51
  st.markdown("""
52
  <style>
53
  .nav-bar {
@@ -104,13 +101,13 @@ st.markdown("""
104
  <button type="submit" class="search-button">🔍</button>
105
  </form>
106
  <div class="nav-right">
107
- <a class="nav-item" href="/?recommend=true" target="_self">🎯 Quiz</a>
108
  </div>
109
  </div>
110
  """, unsafe_allow_html=True)
111
 
112
  query_params = st.query_params
113
- page = query_params.get("recommend")
114
  search_query = query_params.get("search")
115
  movie_id = query_params.get("movie_id")
116
 
@@ -121,25 +118,11 @@ all_ratings_data = load_ratings()
121
 
122
  if movie_id:
123
  # Detail Page
124
- try:
125
- movie_id = int(movie_id)
126
- movie_row = movie_df[movie_df["movieId"] == movie_id].iloc[0]
127
- st.title(movie_row["clean_title"])
128
- st.caption(f"Genres: {movie_row['genres']}")
129
- st.caption(f"Release Year: {movie_row['year']}")
130
- existing = next((r for r in all_ratings_data if r["movie_id"] == movie_id), None)
131
- default_rating = existing["rating"] if existing else 3
132
- rating = st.radio("Rate this movie", [1, 2, 3, 4, 5], index=default_rating - 1, key=f"detail_{movie_id}")
133
- if st.button("Submit Rating"):
134
- entry = {"movie_id": movie_id, "rating": rating, "timestamp": datetime.now().isoformat(), "source": "detail"}
135
- st.session_state.rated.append(entry)
136
- save_rating_to_json(entry)
137
- st.success(f"Thanks for rating '{movie_row['title']}' with ⭐{rating}")
138
- except:
139
- st.error("Movie not found.")
140
 
141
  elif page:
142
- # Quiz Flow
 
143
  if "queue" not in st.session_state:
144
  random.shuffle(movie_titles)
145
  st.session_state.queue = movie_titles.copy()
@@ -147,11 +130,7 @@ elif page:
147
  st.session_state.rated = []
148
 
149
  if st.session_state.index >= len(st.session_state.queue):
150
- st.subheader("🍿 Top 10 Recommendations")
151
- remaining = [m for m in movie_titles if title_to_movie_id[m] not in [r["movie_id"] for r in all_ratings_data]]
152
- recommendations = random.sample(remaining, min(10, len(remaining)))
153
- for i, movie in enumerate(recommendations, 1):
154
- st.markdown(f"**#{i}. {movie}** — Match score: `{round(random.uniform(75, 100), 1)}%`")
155
  st.session_state.quiz_history.append(st.session_state.rated.copy())
156
  for r in st.session_state.rated:
157
  save_rating_to_json(r)
@@ -161,18 +140,29 @@ elif page:
161
  else:
162
  movie = st.session_state.queue[st.session_state.index]
163
  movie_id_val = title_to_movie_id.get(movie)
164
- existing = next((r for r in all_ratings_data if r["movie_id"] == movie_id_val), None)
165
- default_rating = existing["rating"] if existing else 3
166
- rating = st.radio("Rate this movie", [1, 2, 3, 4, 5], index=default_rating - 1)
167
- if st.button("Submit Rating"):
168
- entry = {"movie_id": movie_id_val, "rating": rating, "timestamp": datetime.now().isoformat(), "source": "quiz"}
 
 
 
 
 
 
 
 
169
  st.session_state.rated.append(entry)
170
  save_rating_to_json(entry)
171
  st.session_state.index += 1
172
  st.rerun()
173
- st.markdown(f"### 🎞️ Movie #{len(st.session_state.rated) + 1}")
174
- st.markdown(f"## **{movie}**")
175
 
 
 
 
 
 
176
  elif search_query:
177
  # Movie Search Page
178
  st.title(f"��� Search Results for '{search_query}'")
 
21
 
22
  def save_rating_to_json(entry):
23
  all_ratings = load_ratings()
 
24
  all_ratings = [r for r in all_ratings if r["movie_id"] != entry["movie_id"]]
25
  all_ratings.append(entry)
26
  with open(RATINGS_JSON_PATH, "w") as f:
 
40
  movie_id_to_title = dict(zip(movie_df["movieId"], movie_df["title"]))
41
  title_to_movie_id = dict(zip(movie_df["title"], movie_df["movieId"]))
42
 
 
43
  if "rated" not in st.session_state:
44
  st.session_state.rated = []
45
  if "quiz_history" not in st.session_state:
46
  st.session_state.quiz_history = []
47
 
 
48
  st.markdown("""
49
  <style>
50
  .nav-bar {
 
101
  <button type="submit" class="search-button">🔍</button>
102
  </form>
103
  <div class="nav-right">
104
+ <a class="nav-item" href="/?rateflow=true" target="_self">🎲 Rate Random</a>
105
  </div>
106
  </div>
107
  """, unsafe_allow_html=True)
108
 
109
  query_params = st.query_params
110
+ page = query_params.get("rateflow")
111
  search_query = query_params.get("search")
112
  movie_id = query_params.get("movie_id")
113
 
 
118
 
119
  if movie_id:
120
  # Detail Page
121
+ ... # Unverändert
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  elif page:
124
+ # 🎲 Random Movie Rating Flow
125
+ st.title("🎲 Rate Random Movies")
126
  if "queue" not in st.session_state:
127
  random.shuffle(movie_titles)
128
  st.session_state.queue = movie_titles.copy()
 
130
  st.session_state.rated = []
131
 
132
  if st.session_state.index >= len(st.session_state.queue):
133
+ st.subheader("🎉 You've rated enough movies!")
 
 
 
 
134
  st.session_state.quiz_history.append(st.session_state.rated.copy())
135
  for r in st.session_state.rated:
136
  save_rating_to_json(r)
 
140
  else:
141
  movie = st.session_state.queue[st.session_state.index]
142
  movie_id_val = title_to_movie_id.get(movie)
143
+ movie_row = movie_df[movie_df["movieId"] == movie_id_val].iloc[0]
144
+
145
+ st.markdown(f"## **{movie_row['clean_title']}**")
146
+ st.caption(f"Genres: {movie_row['genres']} | Year: {movie_row['year']}")
147
+
148
+ col1, col2, col3, col4, col5 = st.columns(5)
149
+ selected = None
150
+ for i, col in enumerate([col1, col2, col3, col4, col5], 1):
151
+ if col.button("★" * i, key=f"rate_{i}"):
152
+ selected = i
153
+
154
+ if selected:
155
+ entry = {"movie_id": movie_id_val, "rating": selected, "timestamp": datetime.now().isoformat(), "source": "quiz"}
156
  st.session_state.rated.append(entry)
157
  save_rating_to_json(entry)
158
  st.session_state.index += 1
159
  st.rerun()
 
 
160
 
161
+ st.markdown("<br>", unsafe_allow_html=True)
162
+ if st.button("Didn't Watch"):
163
+ st.session_state.index += 1
164
+ st.rerun()
165
+
166
  elif search_query:
167
  # Movie Search Page
168
  st.title(f"��� Search Results for '{search_query}'")