BasitAliii commited on
Commit
2453133
·
verified ·
1 Parent(s): bb2d1c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -49
app.py CHANGED
@@ -245,57 +245,116 @@ with right:
245
  if st.button("✨ Find Best Matches", key="find_matches_button"):
246
  with st.spinner("Finding best matches..."):
247
  current = store.find_by_username(pick)
248
-
249
- try:
250
- sys_msg, user_msg = make_prompt_for_matching(current, profiles, 3)
251
- raw_matches = ask_groq_for_matches(sys_msg, user_msg)
252
- except Exception as e:
253
- st.info(f"Using local matching due to: {str(e)[:100]}...")
254
- raw_matches = calculate_local_matches(
255
- current,
256
- [p for p in profiles if p.id != current.id],
257
- 3,
258
- )
259
-
260
- # Enrich returned matches
261
- enriched = []
262
- for rm in raw_matches:
263
- prof = store.find_by_username(rm.get("username", ""))
264
- if prof:
265
- enriched.append({
266
- "username": prof.username,
267
- "offers": prof.offers,
268
- "wants": prof.wants,
269
- "avatar": prof.avatar,
270
- "score": int(float(rm.get("score", 0)) * 100),
271
- "reason": rm.get("reason", "AI Match")
272
- })
273
- st.session_state["matches"] = enriched
274
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  matches_list = st.session_state.get("matches", [])
276
- if matches_list:
277
- st.markdown(f"### 🎯 Top {len(matches_list)} Matches for {pick}")
 
 
278
 
279
- for m in matches_list:
280
- st.markdown("<div class='card'>", unsafe_allow_html=True)
281
- cols = st.columns([1, 4])
282
-
283
- with cols[0]:
284
- if m.get("avatar") and Path(m["avatar"]).exists():
285
- st.image(m["avatar"], width=120)
286
- else:
287
- st.image("https://via.placeholder.com/120", width=120)
288
-
289
- with cols[1]:
290
- st.markdown(f"### {m.get('username')}")
291
- score = m.get('score', 0)
292
- st.progress(score / 100)
293
- st.caption(f"Match Score: {score}%")
294
- st.markdown(f"🧠 **Offers:** {', '.join(m.get('offers', [])) or '—'}")
295
- st.markdown(f"🎯 **Wants:** {', '.join(m.get('wants', [])) or '—'}")
296
- st.markdown(f"*{m.get('reason', 'AI Match')}*")
297
-
298
- st.markdown("</div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
 
300
  # ----------------------------------------------
301
  # Feedback
 
245
  if st.button("✨ Find Best Matches", key="find_matches_button"):
246
  with st.spinner("Finding best matches..."):
247
  current = store.find_by_username(pick)
248
+
249
+ if not current:
250
+ st.error(f"Profile '{pick}' not found!")
251
+ else:
252
+ try:
253
+ sys_msg, user_msg = make_prompt_for_matching(current, profiles, 3)
254
+ raw_matches = ask_groq_for_matches(sys_msg, user_msg)
255
+ st.info("Using AI-powered matching...")
256
+ except Exception as e:
257
+ st.info(f"Using local matching...")
258
+ raw_matches = calculate_local_matches(
259
+ current,
260
+ [p for p in profiles if p.id != current.id],
261
+ 3,
262
+ )
263
+
264
+ # Debug: Show raw matches
265
+ st.write(f"Found {len(raw_matches)} raw matches")
266
+
267
+ # Enrich returned matches
268
+ enriched = []
269
+ for rm in raw_matches:
270
+ match_username = rm.get("username", "")
271
+ prof = store.find_by_username(match_username)
272
+ if prof:
273
+ # Handle score properly - ensure it's between 0-100
274
+ score_value = rm.get("score", 0)
275
+ if isinstance(score_value, str):
276
+ # Try to extract numeric value from string
277
+ import re
278
+ numbers = re.findall(r'\d+\.?\d*', score_value)
279
+ if numbers:
280
+ score_value = float(numbers[0])
281
+ else:
282
+ score_value = 50.0 # Default
283
+ else:
284
+ score_value = float(score_value)
285
+
286
+ # Ensure score is between 0-100
287
+ score_value = max(0, min(100, score_value))
288
+
289
+ enriched.append({
290
+ "username": prof.username,
291
+ "offers": prof.offers,
292
+ "wants": prof.wants,
293
+ "avatar": prof.avatar,
294
+ "score": score_value,
295
+ "reason": rm.get("reason", "AI Match")
296
+ })
297
+
298
+ # Sort by score (highest first)
299
+ enriched.sort(key=lambda x: x.get("score", 0), reverse=True)
300
+
301
+ st.session_state["matches"] = enriched
302
+ st.session_state["current_profile"] = pick
303
+
304
+ # Display matches
305
  matches_list = st.session_state.get("matches", [])
306
+ current_profile = st.session_state.get("current_profile", "")
307
+
308
+ if matches_list and current_profile:
309
+ st.markdown(f"### 🎯 Top {len(matches_list)} Matches for {current_profile}")
310
 
311
+ for idx, m in enumerate(matches_list):
312
+ st.markdown("<div class='card'>", unsafe_allow_html=True)
313
+ cols = st.columns([1, 4])
314
+
315
+ with cols[0]:
316
+ if m.get("avatar") and Path(m["avatar"]).exists():
317
+ st.image(m["avatar"], width=120)
318
+ else:
319
+ st.image("https://via.placeholder.com/120", width=120)
320
+
321
+ with cols[1]:
322
+ st.markdown(f"### {m.get('username')}")
323
+
324
+ # Get and validate score
325
+ score = float(m.get('score', 0))
326
+ score = max(0, min(100, score)) # Ensure between 0-100
327
+
328
+ # Display score and progress bar
329
+ st.write(f"**Match Score:** {score:.1f}%")
330
+
331
+ # Progress bar with color based on score
332
+ if score >= 80:
333
+ progress_color = "green"
334
+ elif score >= 60:
335
+ progress_color = "blue"
336
+ elif score >= 40:
337
+ progress_color = "orange"
338
+ else:
339
+ progress_color = "red"
340
+
341
+ # Create a custom progress bar with HTML/CSS
342
+ progress_html = f"""
343
+ <div style="width: 100%; background-color: #f0f0f0; border-radius: 10px; margin: 5px 0;">
344
+ <div style="width: {score}%; background-color: {progress_color}; height: 10px; border-radius: 10px;"></div>
345
+ </div>
346
+ """
347
+ st.markdown(progress_html, unsafe_allow_html=True)
348
+
349
+ st.markdown(f"🧠 **Offers:** {', '.join(m.get('offers', [])) or '—'}")
350
+ st.markdown(f"🎯 **Wants:** {', '.join(m.get('wants', [])) or '—'}")
351
+ st.markdown(f"*{m.get('reason', 'AI Match')}*")
352
+
353
+ st.markdown("</div>", unsafe_allow_html=True)
354
+
355
+ # Add some spacing between matches
356
+ if idx < len(matches_list) - 1:
357
+ st.markdown("<br>", unsafe_allow_html=True)
358
 
359
  # ----------------------------------------------
360
  # Feedback