npc0 commited on
Commit
c005b55
·
verified ·
1 Parent(s): 98ebb33

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +23 -14
src/streamlit_app.py CHANGED
@@ -562,7 +562,6 @@ def get_top_k_polarized_comments_for_users(user_ids, k=5):
562
  local_con.close()
563
 
564
 
565
- @lru_cache()
566
  def estimate_group_voting_diversity(user_ids, topic_id):
567
  """
568
  Estimates the diversity of voting within a group of users for a specific topic.
@@ -580,7 +579,10 @@ def estimate_group_voting_diversity(user_ids, topic_id):
580
  Returns 0.0 if the group has less than 2 users or if no comments
581
  were voted on by at least two users in the group.
582
  """
583
- if not user_ids or len(user_ids) < 2:
 
 
 
584
  return 0.0
585
 
586
  local_con = None
@@ -598,8 +600,8 @@ def estimate_group_voting_diversity(user_ids, topic_id):
598
  JOIN comments c ON v.comment_id = c.id
599
  WHERE c.topic_id = ? AND v.user_id IN (?)
600
  """
601
- # DuckDB's Python API handles lists for IN clauses
602
- results = local_con.execute(query, [topic_id, user_ids]).fetchall()
603
 
604
  if not results:
605
  return 0.0 # No votes found for this group on this topic
@@ -635,7 +637,7 @@ def estimate_group_voting_diversity(user_ids, topic_id):
635
 
636
  except Exception as e:
637
  # st.error is not available here, just print or log
638
- print(f"Error estimating group voting diversity for topic {topic_id} and users {user_ids}: {e}")
639
  return 0.0 # Return 0.0 on error
640
  finally:
641
  if local_con:
@@ -669,8 +671,9 @@ def name_user_group(user_ids, topic_id):
669
  local_con = duckdb.connect(database=DB_PATH, read_only=True)
670
 
671
  # 1. Get total unique users who voted in the topic
 
672
  total_voters_result = local_con.execute("""
673
- SELECT COUNT(DISTINCT user_id)
674
  FROM votes v
675
  JOIN comments c ON v.comment_id = c.id
676
  WHERE c.topic_id = ?
@@ -679,13 +682,17 @@ def name_user_group(user_ids, topic_id):
679
 
680
  # 2. Get unique users from the input list who voted in the topic
681
  # Filter user_ids to only those present in the votes table for this topic
682
- # DuckDB IN clause handles lists directly
683
- group_voters_result = local_con.execute("""
684
- SELECT COUNT(DISTINCT user_id)
 
 
685
  FROM votes v
686
  JOIN comments c ON v.comment_id = c.id
687
- WHERE c.topic_id = ? AND v.user_id IN (?)
688
- """, [topic_id, user_ids]).fetchone()
 
 
689
  group_voters_count = group_voters_result[0] if group_voters_result else 0
690
 
691
  # Handle case where no one in the group has voted on this topic
@@ -747,7 +754,7 @@ def name_user_group(user_ids, topic_id):
747
  return "Whispering Gallery", "A small group where many different ideas are quietly shared."
748
 
749
  except Exception as e:
750
- print(f"Error naming user group for topic {topic_id} and users {user_ids}: {e}")
751
  return "Mysterious Gathering", "An error occurred while trying to name this group." # Default name and description on error
752
  finally:
753
  if local_con:
@@ -1082,6 +1089,7 @@ def view_topic_page():
1082
  if current_comment_id: # Only show voting if there's a comment to vote on
1083
  # Display comment history and the current comment with the random intro
1084
  if st.session_state.get('_show_new_area_message', True):
 
1085
  _, user_ids = get_user_cluster_label(user_id, get_ttl_hash(10))
1086
  new_area_name, desc = name_user_group(user_ids, topic_id)
1087
  for statm in [
@@ -1089,7 +1097,6 @@ def view_topic_page():
1089
  f"🗺️ And yet, your journey leads you to a new place: **{new_area_name}**! {desc}"]:
1090
  st.markdown(statm)
1091
  st.session_state.comment_history += f"\n\n{statm}"
1092
- st.session_state._show_new_area_message = False
1093
  st.markdown(f"[Collected new insight, {random_phrase}]:\n* {current_comment_content}")
1094
 
1095
  # Handle vote logic
@@ -1291,7 +1298,9 @@ if 'processed_url_params' not in st.session_state:
1291
 
1292
  # Initialize the database on first run
1293
  initialize_database()
1294
- add_dummy_topic()
 
 
1295
 
1296
  # Handle initial load from URL query parameters
1297
  # Process only once per session load using the flag
 
562
  local_con.close()
563
 
564
 
 
565
  def estimate_group_voting_diversity(user_ids, topic_id):
566
  """
567
  Estimates the diversity of voting within a group of users for a specific topic.
 
579
  Returns 0.0 if the group has less than 2 users or if no comments
580
  were voted on by at least two users in the group.
581
  """
582
+ # Convert list to tuple for caching purposes (tuples are hashable)
583
+ user_ids_tuple = tuple(user_ids)
584
+
585
+ if not user_ids_tuple or len(user_ids_tuple) < 2:
586
  return 0.0
587
 
588
  local_con = None
 
600
  JOIN comments c ON v.comment_id = c.id
601
  WHERE c.topic_id = ? AND v.user_id IN (?)
602
  """
603
+ # DuckDB's Python API handles lists/tuples for IN clauses
604
+ results = local_con.execute(query, [topic_id, user_ids_tuple]).fetchall()
605
 
606
  if not results:
607
  return 0.0 # No votes found for this group on this topic
 
637
 
638
  except Exception as e:
639
  # st.error is not available here, just print or log
640
+ print(f"Error estimating group voting diversity for topic {topic_id} and users {user_ids_tuple}: {e}")
641
  return 0.0 # Return 0.0 on error
642
  finally:
643
  if local_con:
 
671
  local_con = duckdb.connect(database=DB_PATH, read_only=True)
672
 
673
  # 1. Get total unique users who voted in the topic
674
+ # Specify v.user_id to avoid ambiguity
675
  total_voters_result = local_con.execute("""
676
+ SELECT COUNT(DISTINCT v.user_id)
677
  FROM votes v
678
  JOIN comments c ON v.comment_id = c.id
679
  WHERE c.topic_id = ?
 
682
 
683
  # 2. Get unique users from the input list who voted in the topic
684
  # Filter user_ids to only those present in the votes table for this topic
685
+ # Construct the IN clause dynamically to avoid casting issues
686
+ # This part correctly uses the list by expanding placeholders
687
+ placeholders = ', '.join(['?'] * len(user_ids))
688
+ group_voters_query = f"""
689
+ SELECT COUNT(DISTINCT v.user_id)
690
  FROM votes v
691
  JOIN comments c ON v.comment_id = c.id
692
+ WHERE c.topic_id = ? AND v.user_id IN ({placeholders})
693
+ """
694
+ # Pass topic_id and then all user_ids as separate parameters
695
+ group_voters_result = local_con.execute(group_voters_query, [topic_id] + user_ids).fetchone()
696
  group_voters_count = group_voters_result[0] if group_voters_result else 0
697
 
698
  # Handle case where no one in the group has voted on this topic
 
754
  return "Whispering Gallery", "A small group where many different ideas are quietly shared."
755
 
756
  except Exception as e:
757
+ st.error(f"Error naming user group for topic {topic_id} and users {user_ids}: {e}")
758
  return "Mysterious Gathering", "An error occurred while trying to name this group." # Default name and description on error
759
  finally:
760
  if local_con:
 
1089
  if current_comment_id: # Only show voting if there's a comment to vote on
1090
  # Display comment history and the current comment with the random intro
1091
  if st.session_state.get('_show_new_area_message', True):
1092
+ st.session_state._show_new_area_message = False
1093
  _, user_ids = get_user_cluster_label(user_id, get_ttl_hash(10))
1094
  new_area_name, desc = name_user_group(user_ids, topic_id)
1095
  for statm in [
 
1097
  f"🗺️ And yet, your journey leads you to a new place: **{new_area_name}**! {desc}"]:
1098
  st.markdown(statm)
1099
  st.session_state.comment_history += f"\n\n{statm}"
 
1100
  st.markdown(f"[Collected new insight, {random_phrase}]:\n* {current_comment_content}")
1101
 
1102
  # Handle vote logic
 
1298
 
1299
  # Initialize the database on first run
1300
  initialize_database()
1301
+ if st.session_state.get('_add_dummy', True):
1302
+ add_dummy_topic()
1303
+ st.session_state._add_dummy = False
1304
 
1305
  # Handle initial load from URL query parameters
1306
  # Process only once per session load using the flag