Spaces:
Running
Running
allow search on just filters without query
Browse files
app.py
CHANGED
@@ -90,13 +90,10 @@ result_table = pn.widgets.Tabulator(
|
|
90 |
# 4) Semantic Search with Filtering
|
91 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
92 |
def semantic_search(event=None):
|
|
|
93 |
query = w_semquery.value.strip()
|
94 |
-
if not query:
|
95 |
-
return
|
96 |
-
|
97 |
-
model, ids_list, emb_tensor = get_semantic_resources()
|
98 |
|
99 |
-
# Apply filters first
|
100 |
filt = df.copy()
|
101 |
if w_countries.value:
|
102 |
filt = filt[filt["country"].isin(w_countries.value)]
|
@@ -109,9 +106,21 @@ def semantic_search(event=None):
|
|
109 |
filt["question_code"].astype(str).str.contains(w_keyword.value, case=False, na=False)
|
110 |
]
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
filtered_ids = filt["id"].tolist()
|
113 |
id_to_index = {id_: i for i, id_ in enumerate(ids_list)}
|
114 |
filtered_indices = [id_to_index[id_] for id_ in filtered_ids if id_ in id_to_index]
|
|
|
115 |
if not filtered_indices:
|
116 |
result_table.value = pd.DataFrame(columns=["Score", "country", "year", "question_text", "answer_text"])
|
117 |
return
|
@@ -130,6 +139,7 @@ def semantic_search(event=None):
|
|
130 |
|
131 |
result_table.value = sem_rows[["Score", "country", "year", "question_text", "answer_text"]]
|
132 |
|
|
|
133 |
def clear_filters(event=None):
|
134 |
w_countries.value = []
|
135 |
w_years.value = []
|
|
|
90 |
# 4) Semantic Search with Filtering
|
91 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
92 |
def semantic_search(event=None):
|
93 |
+
"""Run filtered view if no semantic query; otherwise do semantic within filtered subset."""
|
94 |
query = w_semquery.value.strip()
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
# 1) Apply filters first (country/year/keyword)
|
97 |
filt = df.copy()
|
98 |
if w_countries.value:
|
99 |
filt = filt[filt["country"].isin(w_countries.value)]
|
|
|
106 |
filt["question_code"].astype(str).str.contains(w_keyword.value, case=False, na=False)
|
107 |
]
|
108 |
|
109 |
+
# 2) If no semantic query, just show the filtered data (no Score column)
|
110 |
+
if not query:
|
111 |
+
if filt.empty:
|
112 |
+
result_table.value = pd.DataFrame(columns=["country", "year", "question_text", "answer_text"])
|
113 |
+
else:
|
114 |
+
result_table.value = filt[["country", "year", "question_text", "answer_text"]]
|
115 |
+
return
|
116 |
+
|
117 |
+
# 3) Otherwise, do semantic search *within* the filtered subset
|
118 |
+
model, ids_list, emb_tensor = get_semantic_resources()
|
119 |
+
|
120 |
filtered_ids = filt["id"].tolist()
|
121 |
id_to_index = {id_: i for i, id_ in enumerate(ids_list)}
|
122 |
filtered_indices = [id_to_index[id_] for id_ in filtered_ids if id_ in id_to_index]
|
123 |
+
|
124 |
if not filtered_indices:
|
125 |
result_table.value = pd.DataFrame(columns=["Score", "country", "year", "question_text", "answer_text"])
|
126 |
return
|
|
|
139 |
|
140 |
result_table.value = sem_rows[["Score", "country", "year", "question_text", "answer_text"]]
|
141 |
|
142 |
+
|
143 |
def clear_filters(event=None):
|
144 |
w_countries.value = []
|
145 |
w_years.value = []
|