Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +29 -5
src/streamlit_app.py
CHANGED
|
@@ -2,15 +2,31 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import random
|
| 4 |
import os
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
# Page config to use full width
|
| 8 |
st.set_page_config(layout="wide")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
BASE_DIR = os.path.dirname(__file__)
|
| 12 |
MOVIES_PATH = os.path.join(BASE_DIR, "movies.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
@st.cache_data
|
| 15 |
def load_movies():
|
| 16 |
df = pd.read_csv(MOVIES_PATH)
|
|
@@ -107,9 +123,11 @@ if movie_id:
|
|
| 107 |
st.title(movie_row["clean_title"])
|
| 108 |
st.caption(f"Genres: {movie_row['genres']}")
|
| 109 |
st.caption(f"Release Year: {movie_row['year']}")
|
| 110 |
-
rating = st.radio("Rate this movie", [1, 2, 3, 4, 5],
|
| 111 |
if st.button("Submit Rating"):
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
st.success(f"Thanks for rating '{movie_row['title']}' with ⭐{rating}")
|
| 114 |
except:
|
| 115 |
st.error("Movie not found.")
|
|
@@ -129,6 +147,8 @@ elif page:
|
|
| 129 |
for i, movie in enumerate(recommendations, 1):
|
| 130 |
st.markdown(f"**#{i}. {movie}** — Match score: `{round(random.uniform(75, 100), 1)}%`")
|
| 131 |
st.session_state.quiz_history.append(st.session_state.rated.copy())
|
|
|
|
|
|
|
| 132 |
if st.button("🔄 Start Over"):
|
| 133 |
del st.session_state.queue
|
| 134 |
st.rerun()
|
|
@@ -138,7 +158,9 @@ elif page:
|
|
| 138 |
st.markdown(f"## **{movie}**")
|
| 139 |
rating = st.radio("Rate this movie", [1, 2, 3, 4, 5], horizontal=True)
|
| 140 |
if st.button("Submit Rating"):
|
| 141 |
-
|
|
|
|
|
|
|
| 142 |
st.session_state.index += 1
|
| 143 |
st.rerun()
|
| 144 |
|
|
@@ -170,7 +192,9 @@ elif search_query:
|
|
| 170 |
st.markdown(f"[➡️ Go to Detail Page](?movie_id={row['movieId']})")
|
| 171 |
rating = st.radio(f"Rate '{row['title']}'", [1, 2, 3, 4, 5], key=row["title"])
|
| 172 |
if st.button(f"Submit rating for {row['title']}", key=f"submit_{row['title']}"):
|
| 173 |
-
|
|
|
|
|
|
|
| 174 |
st.success(f"Thanks for rating '{row['title']}' with ⭐{rating}")
|
| 175 |
|
| 176 |
else:
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
+
import json
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
# Page config to use full width
|
| 9 |
st.set_page_config(layout="wide")
|
| 10 |
|
| 11 |
+
# Constants
|
| 12 |
BASE_DIR = os.path.dirname(__file__)
|
| 13 |
MOVIES_PATH = os.path.join(BASE_DIR, "movies.csv")
|
| 14 |
+
RATINGS_JSON_PATH = os.path.join(BASE_DIR, "ratings.json")
|
| 15 |
+
|
| 16 |
+
# Load or create ratings JSON
|
| 17 |
+
def load_ratings():
|
| 18 |
+
if os.path.exists(RATINGS_JSON_PATH):
|
| 19 |
+
with open(RATINGS_JSON_PATH, "r") as f:
|
| 20 |
+
return json.load(f)
|
| 21 |
+
return []
|
| 22 |
+
|
| 23 |
+
def save_rating_to_json(entry):
|
| 24 |
+
all_ratings = load_ratings()
|
| 25 |
+
all_ratings.append(entry)
|
| 26 |
+
with open(RATINGS_JSON_PATH, "w") as f:
|
| 27 |
+
json.dump(all_ratings, f, indent=2, default=str)
|
| 28 |
|
| 29 |
+
# Load movie data
|
| 30 |
@st.cache_data
|
| 31 |
def load_movies():
|
| 32 |
df = pd.read_csv(MOVIES_PATH)
|
|
|
|
| 123 |
st.title(movie_row["clean_title"])
|
| 124 |
st.caption(f"Genres: {movie_row['genres']}")
|
| 125 |
st.caption(f"Release Year: {movie_row['year']}")
|
| 126 |
+
rating = st.radio("Rate this movie", [1, 2, 3, 4, 5], key=f"detail_{movie_id}")
|
| 127 |
if st.button("Submit Rating"):
|
| 128 |
+
entry = {"movie": movie_row["title"], "rating": rating, "timestamp": datetime.now().isoformat(), "source": "detail"}
|
| 129 |
+
st.session_state.rated.append(entry)
|
| 130 |
+
save_rating_to_json(entry)
|
| 131 |
st.success(f"Thanks for rating '{movie_row['title']}' with ⭐{rating}")
|
| 132 |
except:
|
| 133 |
st.error("Movie not found.")
|
|
|
|
| 147 |
for i, movie in enumerate(recommendations, 1):
|
| 148 |
st.markdown(f"**#{i}. {movie}** — Match score: `{round(random.uniform(75, 100), 1)}%`")
|
| 149 |
st.session_state.quiz_history.append(st.session_state.rated.copy())
|
| 150 |
+
for r in st.session_state.rated:
|
| 151 |
+
save_rating_to_json(r)
|
| 152 |
if st.button("🔄 Start Over"):
|
| 153 |
del st.session_state.queue
|
| 154 |
st.rerun()
|
|
|
|
| 158 |
st.markdown(f"## **{movie}**")
|
| 159 |
rating = st.radio("Rate this movie", [1, 2, 3, 4, 5], horizontal=True)
|
| 160 |
if st.button("Submit Rating"):
|
| 161 |
+
entry = {"movie": movie, "rating": rating, "timestamp": datetime.now().isoformat(), "source": "quiz"}
|
| 162 |
+
st.session_state.rated.append(entry)
|
| 163 |
+
save_rating_to_json(entry)
|
| 164 |
st.session_state.index += 1
|
| 165 |
st.rerun()
|
| 166 |
|
|
|
|
| 192 |
st.markdown(f"[➡️ Go to Detail Page](?movie_id={row['movieId']})")
|
| 193 |
rating = st.radio(f"Rate '{row['title']}'", [1, 2, 3, 4, 5], key=row["title"])
|
| 194 |
if st.button(f"Submit rating for {row['title']}", key=f"submit_{row['title']}"):
|
| 195 |
+
entry = {"movie": row["title"], "rating": rating, "timestamp": datetime.now().isoformat(), "source": "search"}
|
| 196 |
+
st.session_state.rated.append(entry)
|
| 197 |
+
save_rating_to_json(entry)
|
| 198 |
st.success(f"Thanks for rating '{row['title']}' with ⭐{rating}")
|
| 199 |
|
| 200 |
else:
|