-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
131 lines (102 loc) · 4.28 KB
/
Copy pathapp.py
File metadata and controls
131 lines (102 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import streamlit as st
from db import get_user, create_user, add_interaction, get_user_history, get_user_likes, db_ok
from recommender import (
get_all_items,
get_personalized_recommendations,
get_trending_items,
get_similar_items,
get_items_by_ids,
)
from ui.components import load_css, render_card, render_section_title
st.set_page_config(page_title="TrendMatrix – Personalized Recommendations", layout="wide")
def init_state():
if "user" not in st.session_state:
st.session_state.user = None
if "selected_item_id" not in st.session_state:
st.session_state.selected_item_id = None
def login_sidebar():
st.sidebar.markdown("## 👤 Login")
st.sidebar.caption("Use any name. This just helps remember your picks.")
# Theme toggle
st.sidebar.toggle("Light Theme", value=False, key="light_theme")
username = st.sidebar.text_input("Username", key="username_input")
if st.sidebar.button("Continue", type="primary"):
if not username.strip():
st.sidebar.error("Please enter a username.")
return
try:
user = get_user(username.strip()) or create_user(username.strip())
st.session_state.user = user
st.sidebar.success(f"Hi {user['username']} 👋")
except Exception as e:
st.sidebar.error("Could not connect to the database. You can still browse demo picks.")
st.sidebar.code(str(e))
def show_item_grid(items, section_key, user_id):
if not items:
st.write("Nothing to show here yet.")
return
cols = st.columns(4, gap="large")
for i, item in enumerate(items):
col = cols[i % 4]
with col:
render_card(item)
if user_id is not None and db_ok():
like_key = f"{section_key}_like_{item['id']}"
if st.button("❤️ Like", key=like_key):
add_interaction(user_id, item["id"], "liked")
st.toast("Saved to your vibe ✨")
def main():
init_state()
theme = 'light' if st.session_state.get('light_theme', False) else 'dark'
load_css(theme)
items_df = get_all_items()
col1, col2 = st.columns([0.12, 0.88])
with col1:
st.image("assets/logo.png", width=60)
with col2:
st.markdown("### TrendMatrix")
st.markdown('<div class="app-caption">A small personalized recommendation space.</div>', unsafe_allow_html=True)
login_sidebar()
user = st.session_state.user
if user and db_ok():
history_ids = get_user_history(user["id"])
user_id = user["id"]
st.write(f"Logged in as **{user['username']}**")
# Navigation
nav = st.sidebar.selectbox("View", ["Recommendations", "Saved Items"], key="nav")
else:
history_ids = []
user_id = None
nav = "Recommendations"
if not db_ok():
st.warning("Database secrets not configured. App will show demo recommendations only.")
if nav == "Saved Items":
if user and db_ok():
liked_ids = get_user_likes(user_id)
if liked_ids:
liked_items = get_items_by_ids(liked_ids)
st.markdown("### 💖 Your Saved Items")
show_item_grid(liked_items, "likes", user_id)
else:
st.write("No saved items yet. Start liking products to see them here!")
else:
st.write("Please log in to view saved items.")
else:
# Normal recommendations flow
if history_ids:
recs = get_personalized_recommendations(history_ids, top_n=8)
render_section_title("🎯 Recommended for you")
show_item_grid(recs, "rec", user_id)
else:
render_section_title("✨ Fresh picks to get you started")
show_item_grid(get_trending_items(8), "fresh", user_id)
if history_ids:
last_id = history_ids[0]
similar = get_similar_items(last_id, top_n=8)
if similar:
render_section_title("📺 Because you viewed something like this")
show_item_grid(similar, "similar", user_id)
render_section_title("🔥 Trending now")
show_item_grid(get_trending_items(8), "trending", user_id)
if __name__ == "__main__":
main()