BasitAliii commited on
Commit
95965a1
Β·
verified Β·
1 Parent(s): 91eaf66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -45
app.py CHANGED
@@ -30,7 +30,7 @@ store = ProfileStore()
30
  col_logo, col_title = st.columns([1, 6])
31
  with col_logo:
32
  try:
33
- st.image("logo.jpg", width=90) # Uploaded logo
34
  except:
35
  st.image(
36
  "https://via.placeholder.com/100x100/4F46E5/FFFFFF?text=🀝",
@@ -99,7 +99,7 @@ with st.sidebar:
99
  # ---------- Avatar Preview ----------
100
  if avatar_file:
101
  st.image(avatar_file, width=150, caption="Preview of avatar")
102
- if avatar_file.size > 800 * 1024:
103
  st.error("File exceeds maximum size of 800KB!")
104
  avatar_file = None
105
 
@@ -137,15 +137,13 @@ with st.sidebar:
137
  ok, msg = store.add_or_update(profile)
138
  if ok:
139
  st.success(msg)
140
- # Clear all fields
141
- for key in ["offers_text", "wants_text", "availability", "preferences"]:
142
  if key in st.session_state:
143
  del st.session_state[key]
144
  if "avatar_uploader" in st.session_state:
145
  del st.session_state["avatar_uploader"]
146
- # Clear previous matches
147
- st.session_state["matches"] = []
148
- st.rerun()
149
  else:
150
  st.error(msg)
151
 
@@ -175,9 +173,25 @@ with st.sidebar:
175
  st.success("Profile updated")
176
  if "avatar_uploader" in st.session_state:
177
  del st.session_state["avatar_uploader"]
178
- # Clear previous matches
179
- st.session_state["matches"] = []
180
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
  # ---------- MAIN CONTENT ----------
183
  left, right = st.columns([2, 3])
@@ -224,40 +238,54 @@ with right:
224
  )
225
 
226
  if st.button("✨ Find Best Matches", key="find_matches_button"):
227
- current = store.find_by_username(pick)
228
- try:
229
- sys_msg, user_msg = make_prompt_for_matching(current, profiles, 3)
230
- matches = ask_groq_for_matches(sys_msg, user_msg)
231
- except Exception:
232
- matches = calculate_local_matches(
233
- current,
234
- [p for p in profiles if p.id != current.id],
235
- 3,
236
- )
237
-
238
- # Save matches in session state to persist
239
- st.session_state["matches"] = matches
240
-
241
- # Display matches from session state
242
- matches_to_show = st.session_state.get("matches", [])
243
- if matches_to_show:
244
- for m in matches_to_show:
245
- st.markdown("<div class='card'>", unsafe_allow_html=True)
246
- cols = st.columns([1, 4])
247
-
248
- with cols[0]:
249
- avatar_path = m.get("avatar")
250
- if avatar_path and Path(avatar_path).exists():
251
- st.image(avatar_path, width=120)
252
- else:
253
- st.image("https://via.placeholder.com/120", width=120)
254
-
255
- with cols[1]:
256
- st.markdown(f"### {m.get('username')}")
257
- st.caption(f"🧠 Offers: {', '.join(m.get('offers', []))}")
258
- st.caption(f"🎯 Wants: {', '.join(m.get('wants', []))}")
259
- st.caption(f"Match Reason: {m.get('reason', 'AI Match')}")
260
- st.markdown("</div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
 
262
  # ---------- FEEDBACK ----------
263
  with st.sidebar:
@@ -276,6 +304,6 @@ with st.sidebar:
276
  st.success("Thank you!")
277
  if "feedback_text" in st.session_state:
278
  del st.session_state["feedback_text"]
279
- st.rerun()
280
  else:
281
  st.warning("Please write feedback.")
 
30
  col_logo, col_title = st.columns([1, 6])
31
  with col_logo:
32
  try:
33
+ st.image("logo.jpg", width=90) # Use uploaded logo
34
  except:
35
  st.image(
36
  "https://via.placeholder.com/100x100/4F46E5/FFFFFF?text=🀝",
 
99
  # ---------- Avatar Preview ----------
100
  if avatar_file:
101
  st.image(avatar_file, width=150, caption="Preview of avatar")
102
+ if avatar_file.size > 800 * 1024: # 800KB max
103
  st.error("File exceeds maximum size of 800KB!")
104
  avatar_file = None
105
 
 
137
  ok, msg = store.add_or_update(profile)
138
  if ok:
139
  st.success(msg)
140
+ # Clear fields
141
+ for key in ["offers_text", "wants_text", "availability", "preferences", "username_input"]:
142
  if key in st.session_state:
143
  del st.session_state[key]
144
  if "avatar_uploader" in st.session_state:
145
  del st.session_state["avatar_uploader"]
146
+ st.experimental_rerun()
 
 
147
  else:
148
  st.error(msg)
149
 
 
173
  st.success("Profile updated")
174
  if "avatar_uploader" in st.session_state:
175
  del st.session_state["avatar_uploader"]
176
+ st.experimental_rerun()
177
+
178
+ # ---------- DELETE PROFILE ----------
179
+ if selected_user != "β€” Create New β€”":
180
+ if st.button("πŸ—‘οΈ Delete Profile", type="secondary", key="delete_button"):
181
+ profiles = store.load_all()
182
+ profile_to_delete = next((p for p in profiles if p.username == selected_user), None)
183
+ if profile_to_delete:
184
+ profiles.remove(profile_to_delete)
185
+ store.save_all(profiles)
186
+ st.warning(f"Profile '{selected_user}' deleted")
187
+ for key in ["offers_text", "wants_text", "availability", "preferences", "username_input"]:
188
+ if key in st.session_state:
189
+ del st.session_state[key]
190
+ if "avatar_uploader" in st.session_state:
191
+ del st.session_state["avatar_uploader"]
192
+ st.experimental_rerun()
193
+ else:
194
+ st.error("Profile not found")
195
 
196
  # ---------- MAIN CONTENT ----------
197
  left, right = st.columns([2, 3])
 
238
  )
239
 
240
  if st.button("✨ Find Best Matches", key="find_matches_button"):
241
+ with st.spinner("Analyzing profiles..."):
242
+ current = store.find_by_username(pick)
243
+ matches = []
244
+
245
+ try:
246
+ sys_msg, user_msg = make_prompt_for_matching(current, profiles, 3)
247
+ raw_matches = ask_groq_for_matches(sys_msg, user_msg)
248
+ # Enrich Groq matches with full profile info
249
+ for rm in raw_matches:
250
+ profile = store.find_by_username(rm.get("username", ""))
251
+ if profile:
252
+ matches.append({
253
+ "username": profile.username,
254
+ "offers": profile.offers,
255
+ "wants": profile.wants,
256
+ "avatar": profile.avatar,
257
+ "score": int(float(rm.get("score", 0)) * 100),
258
+ "reason": rm.get("reason", "AI Match")
259
+ })
260
+ except Exception:
261
+ matches = calculate_local_matches(
262
+ current,
263
+ [p for p in profiles if p.id != current.id],
264
+ 3,
265
+ )
266
+ # Convert score 0-1 to 0-100%
267
+ for m in matches:
268
+ m["score"] = int(m["score"] * 100)
269
+
270
+ # Display matches
271
+ for m in matches:
272
+ st.markdown("<div class='card'>", unsafe_allow_html=True)
273
+ cols = st.columns([1, 4])
274
+ with cols[0]:
275
+ if m.get("avatar") and Path(m["avatar"]).exists():
276
+ st.image(m["avatar"], width=100)
277
+ else:
278
+ st.image(
279
+ "https://via.placeholder.com/100",
280
+ width=100
281
+ )
282
+ with cols[1]:
283
+ st.markdown(f"### {m.get('username')}")
284
+ st.markdown(f"🧠 **Offers:** {', '.join(m.get('offers', [])) or 'β€”'}")
285
+ st.markdown(f"🎯 **Wants:** {', '.join(m.get('wants', [])) or 'β€”'}")
286
+ st.markdown(f"**Match Score:** {m.get('score', 0)}%")
287
+ st.markdown(f"Match Reason: {m.get('reason', 'AI Match')}")
288
+ st.markdown("</div>", unsafe_allow_html=True)
289
 
290
  # ---------- FEEDBACK ----------
291
  with st.sidebar:
 
304
  st.success("Thank you!")
305
  if "feedback_text" in st.session_state:
306
  del st.session_state["feedback_text"]
307
+ st.experimental_rerun()
308
  else:
309
  st.warning("Please write feedback.")