myshirk commited on
Commit
3d8de76
Β·
verified Β·
1 Parent(s): 5ec65d6

add columns to show

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -66,6 +66,13 @@ def get_semantic_resources():
66
  country_opts = sorted(df["country"].dropna().unique())
67
  year_opts = sorted(df["year"].dropna().unique())
68
 
 
 
 
 
 
 
 
69
  w_countries = pn.widgets.MultiSelect(name="Countries", options=country_opts)
70
  w_years = pn.widgets.MultiSelect(name="Years", options=year_opts)
71
  w_keyword = pn.widgets.TextInput(name="Keyword Search", placeholder="Search questions or answers with exact string matching")
@@ -107,6 +114,18 @@ def _group_by_question(df_in: pd.DataFrame) -> pd.DataFrame:
107
  .rename(columns={"country": "Countries", "year": "Years", "answer_text": "Sample Answers"})
108
  )
109
  return grouped
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  def search(event=None):
112
  query = w_semquery.value.strip()
@@ -123,7 +142,7 @@ def search(event=None):
123
  ]
124
 
125
  if not query:
126
- result_table.value = _group_by_question(filt) if w_group.value else filt[["country", "year", "question_text", "answer_text"]]
127
  return
128
 
129
  model, ids_list, emb_tensor = get_semantic_resources()
@@ -131,7 +150,7 @@ def search(event=None):
131
  id_to_index = {id_: i for i, id_ in enumerate(ids_list)}
132
  filtered_indices = [id_to_index[id_] for id_ in filtered_ids if id_ in id_to_index]
133
  if not filtered_indices:
134
- result_table.value = _group_by_question(filt.iloc[0:0]) if w_group.value else pd.DataFrame(columns=["Score", "country", "year", "question_text", "answer_text"])
135
  return
136
 
137
  top_k = min(int(w_topk.value), len(filtered_indices))
@@ -146,8 +165,7 @@ def search(event=None):
146
  sem_rows["Score"] = sem_rows["id"].map(score_map)
147
  sem_rows = sem_rows.sort_values("Score", ascending=False)
148
 
149
- result_table.value = _group_by_question(sem_rows.drop(columns=["Score"])) if w_group.value else sem_rows[["Score", "country", "year", "question_text", "answer_text"]]
150
-
151
 
152
 
153
  def clear_filters(event=None):
@@ -184,7 +202,7 @@ result_table.value = df[["country", "year", "question_text", "answer_text"]].cop
184
  # ──────────────────────────────────────────────────────────────────────
185
  sidebar = pn.Column(
186
  "## πŸ”Ž Filters",
187
- w_countries, w_years, w_keyword, w_group,
188
  pn.Spacer(height=20),
189
  "## 🧠 Semantic Search",
190
  w_semquery,
 
66
  country_opts = sorted(df["country"].dropna().unique())
67
  year_opts = sorted(df["year"].dropna().unique())
68
 
69
+ ALL_COLUMNS = ["country","year","section","question_code","question_text","answer_code","answer_text","Score"]
70
+ w_columns = pn.widgets.MultiChoice(
71
+ name="Columns to show",
72
+ options=ALL_COLUMNS,
73
+ value=["country","year","question_text","answer_text"]
74
+ )
75
+
76
  w_countries = pn.widgets.MultiSelect(name="Countries", options=country_opts)
77
  w_years = pn.widgets.MultiSelect(name="Years", options=year_opts)
78
  w_keyword = pn.widgets.TextInput(name="Keyword Search", placeholder="Search questions or answers with exact string matching")
 
114
  .rename(columns={"country": "Countries", "year": "Years", "answer_text": "Sample Answers"})
115
  )
116
  return grouped
117
+
118
+ def _selected_cols(has_score: bool = False):
119
+ allowed = set(df.columns.tolist())
120
+ cols = [c for c in w_columns.value if c in allowed]
121
+ if has_score and "Score" in w_columns.value:
122
+ cols = ["Score"] + cols
123
+ if not cols:
124
+ cols = ["country","year","question_text","answer_text"]
125
+ if has_score and "Score" in w_columns.options:
126
+ cols = ["Score"] + cols
127
+ return cols
128
+
129
 
130
  def search(event=None):
131
  query = w_semquery.value.strip()
 
142
  ]
143
 
144
  if not query:
145
+ result_table.value = _group_by_question(filt) if w_group.value else filt[_selected_cols(False)]
146
  return
147
 
148
  model, ids_list, emb_tensor = get_semantic_resources()
 
150
  id_to_index = {id_: i for i, id_ in enumerate(ids_list)}
151
  filtered_indices = [id_to_index[id_] for id_ in filtered_ids if id_ in id_to_index]
152
  if not filtered_indices:
153
+ result_table.value = _group_by_question(filt.iloc[0:0]) if w_group.value else pd.DataFrame(columns=_selected_cols(True))
154
  return
155
 
156
  top_k = min(int(w_topk.value), len(filtered_indices))
 
165
  sem_rows["Score"] = sem_rows["id"].map(score_map)
166
  sem_rows = sem_rows.sort_values("Score", ascending=False)
167
 
168
+ result_table.value = _group_by_question(sem_rows.drop(columns=["Score"])) if w_group.value else sem_rows[_selected_cols(True)]
 
169
 
170
 
171
  def clear_filters(event=None):
 
202
  # ──────────────────────────────────────────────────────────────────────
203
  sidebar = pn.Column(
204
  "## πŸ”Ž Filters",
205
+ w_countries, w_years, w_keyword, w_group, w_columns,
206
  pn.Spacer(height=20),
207
  "## 🧠 Semantic Search",
208
  w_semquery,