Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse filesadd region filter
app.py
CHANGED
@@ -4,7 +4,7 @@ from appStore.prep_data import process_giz_worldwide, remove_duplicates
|
|
4 |
from appStore.prep_utils import create_documents, get_client
|
5 |
from appStore.embed import hybrid_embed_chunks
|
6 |
from appStore.search import hybrid_search
|
7 |
-
from appStore.region_utils import load_region_data, get_country_name
|
8 |
from appStore.tfidf_extraction import extract_top_keywords
|
9 |
from torch import cuda
|
10 |
import json
|
@@ -36,45 +36,53 @@ collection_name = "giz_worldwide"
|
|
36 |
client = get_client()
|
37 |
print(client.get_collections())
|
38 |
|
|
|
|
|
|
|
39 |
# Fetch unique country codes and map to country names
|
40 |
@st.cache_data
|
41 |
-
def
|
42 |
results = hybrid_search(_client, "", collection_name)
|
43 |
country_set = set()
|
44 |
for res in results[0] + results[1]:
|
45 |
countries = res.payload.get('metadata', {}).get('countries', "[]")
|
46 |
try:
|
47 |
country_list = json.loads(countries.replace("'", '"'))
|
48 |
-
#
|
49 |
two_digit_codes = [code.upper() for code in country_list if len(code) == 2]
|
50 |
country_set.update(two_digit_codes)
|
51 |
except json.JSONDecodeError:
|
52 |
pass
|
53 |
-
|
54 |
-
# Create a mapping of {CountryName -> ISO2Code}
|
55 |
-
# so you can display the name in the selectbox but store the 2-digit code
|
56 |
country_name_to_code = {}
|
|
|
|
|
57 |
for code in country_set:
|
58 |
name = get_country_name(code, region_df)
|
|
|
|
|
59 |
country_name_to_code[name] = code
|
|
|
60 |
|
61 |
-
return country_name_to_code
|
62 |
|
63 |
-
|
64 |
-
# Get country name mapping
|
65 |
client = get_client()
|
66 |
-
country_name_mapping =
|
67 |
unique_country_names = sorted(country_name_mapping.keys()) # List of country names
|
68 |
|
69 |
# Layout filters in columns
|
70 |
col1, col2, col3 = st.columns([1, 1, 4])
|
71 |
|
72 |
with col1:
|
73 |
-
|
74 |
with col2:
|
|
|
|
|
75 |
current_year = datetime.now().year
|
76 |
default_start_year = current_year - 5 # Default to 5 years ago
|
77 |
-
|
78 |
end_year_range = st.slider(
|
79 |
"Project End Year",
|
80 |
min_value=2010,
|
@@ -86,7 +94,7 @@ with col2:
|
|
86 |
show_exact_matches = st.checkbox("Show only exact matches", value=False)
|
87 |
button = st.button("Search")
|
88 |
|
89 |
-
def filter_results(results, country_filter, end_year_range):
|
90 |
filtered = []
|
91 |
for r in results:
|
92 |
metadata = r.payload.get('metadata', {})
|
@@ -103,15 +111,21 @@ def filter_results(results, country_filter, end_year_range):
|
|
103 |
# Translate selected country name to iso2
|
104 |
selected_iso_code = country_name_mapping.get(country_filter, None)
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
# Filtering
|
107 |
if (
|
108 |
(country_filter == "All/Not allocated" or selected_iso_code in c_list)
|
|
|
109 |
and (end_year_range[0] <= end_year_val <= end_year_range[1])
|
110 |
):
|
111 |
filtered.append(r)
|
112 |
return filtered
|
113 |
|
114 |
-
|
115 |
if button:
|
116 |
# 1) Use a bigger limit so we get more than 10 results
|
117 |
# We'll filter them first, then slice the top 10 from the filtered set.
|
@@ -122,8 +136,8 @@ if button:
|
|
122 |
lexical_all = results[1]
|
123 |
|
124 |
# 2) Filter the entire sets
|
125 |
-
filtered_semantic = filter_results(semantic_all, country_filter, end_year_range)
|
126 |
-
filtered_lexical = filter_results(lexical_all, country_filter, end_year_range)
|
127 |
|
128 |
filtered_semantic_no_dupe = remove_duplicates(filtered_semantic)
|
129 |
filtered_lexical_no_dupe = remove_duplicates(filtered_lexical)
|
|
|
4 |
from appStore.prep_utils import create_documents, get_client
|
5 |
from appStore.embed import hybrid_embed_chunks
|
6 |
from appStore.search import hybrid_search
|
7 |
+
from appStore.region_utils import load_region_data, get_country_name, get_regions
|
8 |
from appStore.tfidf_extraction import extract_top_keywords
|
9 |
from torch import cuda
|
10 |
import json
|
|
|
36 |
client = get_client()
|
37 |
print(client.get_collections())
|
38 |
|
39 |
+
# Get all unique sub-regions
|
40 |
+
_, unique_sub_regions = get_regions(region_df)
|
41 |
+
|
42 |
# Fetch unique country codes and map to country names
|
43 |
@st.cache_data
|
44 |
+
def get_country_name_and_region_mapping(_client, collection_name, region_df):
|
45 |
results = hybrid_search(_client, "", collection_name)
|
46 |
country_set = set()
|
47 |
for res in results[0] + results[1]:
|
48 |
countries = res.payload.get('metadata', {}).get('countries', "[]")
|
49 |
try:
|
50 |
country_list = json.loads(countries.replace("'", '"'))
|
51 |
+
# Only add codes of length 2
|
52 |
two_digit_codes = [code.upper() for code in country_list if len(code) == 2]
|
53 |
country_set.update(two_digit_codes)
|
54 |
except json.JSONDecodeError:
|
55 |
pass
|
56 |
+
|
57 |
+
# Create a mapping of {CountryName -> ISO2Code} and {ISO2Code -> SubRegion}
|
|
|
58 |
country_name_to_code = {}
|
59 |
+
iso_code_to_sub_region = {}
|
60 |
+
|
61 |
for code in country_set:
|
62 |
name = get_country_name(code, region_df)
|
63 |
+
sub_region_row = region_df[region_df['alpha-2'] == code]
|
64 |
+
sub_region = sub_region_row['sub-region'].values[0] if not sub_region_row.empty else "Not allocated"
|
65 |
country_name_to_code[name] = code
|
66 |
+
iso_code_to_sub_region[code] = sub_region
|
67 |
|
68 |
+
return country_name_to_code, iso_code_to_sub_region
|
69 |
|
70 |
+
# Get country name and region mappings
|
|
|
71 |
client = get_client()
|
72 |
+
country_name_mapping, iso_code_to_sub_region = get_country_name_and_region_mapping(client, collection_name, region_df)
|
73 |
unique_country_names = sorted(country_name_mapping.keys()) # List of country names
|
74 |
|
75 |
# Layout filters in columns
|
76 |
col1, col2, col3 = st.columns([1, 1, 4])
|
77 |
|
78 |
with col1:
|
79 |
+
region_filter = st.selectbox("Region", ["All/Not allocated"] + sorted(unique_sub_regions)) # Display region names
|
80 |
with col2:
|
81 |
+
country_filter = st.selectbox("Country", ["All/Not allocated"] + unique_country_names) # Display country names
|
82 |
+
with col3:
|
83 |
current_year = datetime.now().year
|
84 |
default_start_year = current_year - 5 # Default to 5 years ago
|
85 |
+
|
86 |
end_year_range = st.slider(
|
87 |
"Project End Year",
|
88 |
min_value=2010,
|
|
|
94 |
show_exact_matches = st.checkbox("Show only exact matches", value=False)
|
95 |
button = st.button("Search")
|
96 |
|
97 |
+
def filter_results(results, country_filter, region_filter, end_year_range):
|
98 |
filtered = []
|
99 |
for r in results:
|
100 |
metadata = r.payload.get('metadata', {})
|
|
|
111 |
# Translate selected country name to iso2
|
112 |
selected_iso_code = country_name_mapping.get(country_filter, None)
|
113 |
|
114 |
+
# Check if any country in the metadata matches the selected region
|
115 |
+
if region_filter != "All/Not allocated":
|
116 |
+
countries_in_region = [code for code in c_list if iso_code_to_sub_region.get(code) == region_filter]
|
117 |
+
else:
|
118 |
+
countries_in_region = c_list
|
119 |
+
|
120 |
# Filtering
|
121 |
if (
|
122 |
(country_filter == "All/Not allocated" or selected_iso_code in c_list)
|
123 |
+
and (region_filter == "All/Not allocated" or countries_in_region)
|
124 |
and (end_year_range[0] <= end_year_val <= end_year_range[1])
|
125 |
):
|
126 |
filtered.append(r)
|
127 |
return filtered
|
128 |
|
|
|
129 |
if button:
|
130 |
# 1) Use a bigger limit so we get more than 10 results
|
131 |
# We'll filter them first, then slice the top 10 from the filtered set.
|
|
|
136 |
lexical_all = results[1]
|
137 |
|
138 |
# 2) Filter the entire sets
|
139 |
+
filtered_semantic = filter_results(semantic_all, country_filter, region_filter, end_year_range)
|
140 |
+
filtered_lexical = filter_results(lexical_all, country_filter, region_filter, end_year_range)
|
141 |
|
142 |
filtered_semantic_no_dupe = remove_duplicates(filtered_semantic)
|
143 |
filtered_lexical_no_dupe = remove_duplicates(filtered_lexical)
|