Spaces:
Sleeping
Sleeping
cassiebuhler commited on
Commit ·
87ac526
1
Parent(s): e68668d
patch llm
Browse files- app/app.py +16 -6
- app/system_prompt.txt +13 -1
- app/utils.py +8 -0
- app/variables.py +5 -0
app/app.py
CHANGED
|
@@ -54,6 +54,13 @@ with st.sidebar:
|
|
| 54 |
county_choice = 'All'
|
| 55 |
st.divider()
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
legend, position, bg_color, fontsize, shape_type, controls = get_legend(paint, leafmap_choice)
|
| 58 |
# get all the ids that correspond to the filter
|
| 59 |
gdf = filter_data(tpl_table, state_choice, county_choice, year_range)
|
|
@@ -69,7 +76,7 @@ with chatbot_container:
|
|
| 69 |
'''
|
| 70 |
Mapping queries:
|
| 71 |
- Show me the most expensive protected site
|
| 72 |
-
- Show me protected areas with high levels of carbon
|
| 73 |
- Show me sites owned, managed or sponsored by the Trust for Public Land
|
| 74 |
'''
|
| 75 |
|
|
@@ -170,11 +177,11 @@ with st.container():
|
|
| 170 |
|
| 171 |
# extract ids, columns, bounds if present
|
| 172 |
if "fid" in llm_output.columns and not llm_output.empty:
|
| 173 |
-
|
| 174 |
llm_cols = extract_columns(sql_query)
|
| 175 |
-
|
| 176 |
else:
|
| 177 |
-
|
| 178 |
not_mapping = True
|
| 179 |
|
| 180 |
except Exception as e:
|
|
@@ -201,13 +208,16 @@ with st.container():
|
|
| 201 |
|
| 202 |
# define PMTiles style dict (if we didn't already do so using the chatbot)
|
| 203 |
if 'style' not in locals():
|
| 204 |
-
if one_state:
|
| 205 |
# filter to ids in that state
|
| 206 |
style = tpl_style(unique_ids, paint, pmtiles)
|
| 207 |
else:
|
| 208 |
# selected all states, so no need to filter
|
| 209 |
style=tpl_style_default(paint, pmtiles)
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
# add pmtiles to map (using user-specified module)
|
| 213 |
if leafmap_choice == "maplibregl":
|
|
|
|
| 54 |
county_choice = 'All'
|
| 55 |
st.divider()
|
| 56 |
|
| 57 |
+
with st.sidebar:
|
| 58 |
+
with st.popover("ℹ️ Help"):
|
| 59 |
+
st.markdown(help_message)
|
| 60 |
+
if st.button("🧹 Clear Filters", type="secondary", help = 'Reset all the filters to their default state.'):
|
| 61 |
+
st.rerun()
|
| 62 |
+
st.divider()
|
| 63 |
+
|
| 64 |
legend, position, bg_color, fontsize, shape_type, controls = get_legend(paint, leafmap_choice)
|
| 65 |
# get all the ids that correspond to the filter
|
| 66 |
gdf = filter_data(tpl_table, state_choice, county_choice, year_range)
|
|
|
|
| 76 |
'''
|
| 77 |
Mapping queries:
|
| 78 |
- Show me the most expensive protected site
|
| 79 |
+
- Show me protected areas with high levels of carbon in Florida
|
| 80 |
- Show me sites owned, managed or sponsored by the Trust for Public Land
|
| 81 |
'''
|
| 82 |
|
|
|
|
| 177 |
|
| 178 |
# extract ids, columns, bounds if present
|
| 179 |
if "fid" in llm_output.columns and not llm_output.empty:
|
| 180 |
+
unique_ids = list(set(llm_output['fid'].tolist()))
|
| 181 |
llm_cols = extract_columns(sql_query)
|
| 182 |
+
llm_bounds = llm_output.total_bounds.tolist()
|
| 183 |
else:
|
| 184 |
+
unique_ids, llm_cols = [], []
|
| 185 |
not_mapping = True
|
| 186 |
|
| 187 |
except Exception as e:
|
|
|
|
| 208 |
|
| 209 |
# define PMTiles style dict (if we didn't already do so using the chatbot)
|
| 210 |
if 'style' not in locals():
|
| 211 |
+
if one_state or ('llm_output' in locals()):
|
| 212 |
# filter to ids in that state
|
| 213 |
style = tpl_style(unique_ids, paint, pmtiles)
|
| 214 |
else:
|
| 215 |
# selected all states, so no need to filter
|
| 216 |
style=tpl_style_default(paint, pmtiles)
|
| 217 |
+
if 'llm_output' in locals():
|
| 218 |
+
bounds = llm_bounds
|
| 219 |
+
else:
|
| 220 |
+
bounds = get_bounds(state_choice, county_choice, m)
|
| 221 |
|
| 222 |
# add pmtiles to map (using user-specified module)
|
| 223 |
if leafmap_choice == "maplibregl":
|
app/system_prompt.txt
CHANGED
|
@@ -65,7 +65,19 @@ example_assistant: {{"sql_query":
|
|
| 65 |
LEFT JOIN carbon
|
| 66 |
USING (h8)
|
| 67 |
GROUP BY "fid", "site", geom"
|
| 68 |
-
ORDER BY "mean_carbon" DESC LIMIT
|
| 69 |
"explanation":"I joined `conservation_almanac` with `carbon` to retrieve irrecoverable carbon levels for protected areas in the `conservation_almanac`. I returned 10 areas with highest levels of carbon.
|
| 70 |
}}
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
LEFT JOIN carbon
|
| 66 |
USING (h8)
|
| 67 |
GROUP BY "fid", "site", geom"
|
| 68 |
+
ORDER BY "mean_carbon" DESC LIMIT 100;
|
| 69 |
"explanation":"I joined `conservation_almanac` with `carbon` to retrieve irrecoverable carbon levels for protected areas in the `conservation_almanac`. I returned 10 areas with highest levels of carbon.
|
| 70 |
}}
|
| 71 |
|
| 72 |
+
## Example:
|
| 73 |
+
example_user: "Show me protected areas with high species richness"
|
| 74 |
+
example_assistant: {{"sql_query":
|
| 75 |
+
SELECT "fid", "geom", "site", AVG("richness") AS "mean_richness"
|
| 76 |
+
FROM conservation_almanac
|
| 77 |
+
LEFT JOIN richness
|
| 78 |
+
USING (h8)
|
| 79 |
+
GROUP BY "fid", "site", geom"
|
| 80 |
+
ORDER BY "mean_richness" DESC LIMIT 100;
|
| 81 |
+
"explanation":"I joined `conservation_almanac` with `richness` to retrieve species richness for protected areas in the `conservation_almanac`. I returned 100 areas with highest levels of carbon.
|
| 82 |
+
}}
|
| 83 |
+
|
app/utils.py
CHANGED
|
@@ -114,6 +114,14 @@ def tpl_style(ids, paint, pmtiles):
|
|
| 114 |
}
|
| 115 |
return style
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
def get_colorbar(gdf, paint):
|
| 118 |
"""
|
| 119 |
Extracts color hex codes and value range (vmin, vmax) from paint
|
|
|
|
| 114 |
}
|
| 115 |
return style
|
| 116 |
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def extract_columns(sql_query):
|
| 121 |
+
# Find all substrings inside double quotes
|
| 122 |
+
columns = list(dict.fromkeys(re.findall(r'"(.*?)"', sql_query)))
|
| 123 |
+
return columns
|
| 124 |
+
|
| 125 |
def get_colorbar(gdf, paint):
|
| 126 |
"""
|
| 127 |
Extracts color hex codes and value range (vmin, vmax) from paint
|
app/variables.py
CHANGED
|
@@ -159,6 +159,11 @@ basemaps = ['CartoDB.DarkMatter', 'CartoDB.DarkMatterNoLabels',
|
|
| 159 |
# ['Forestry','Historical','Unknown','Other','Farming','Recreation','Environment','Scenic','RAN'],
|
| 160 |
# }
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
#maplibregl tooltip
|
| 164 |
tooltip_cols = ['fid','state','site','sponsor','program','county','year','manager',
|
|
|
|
| 159 |
# ['Forestry','Historical','Unknown','Other','Farming','Recreation','Environment','Scenic','RAN'],
|
| 160 |
# }
|
| 161 |
|
| 162 |
+
help_message = '''
|
| 163 |
+
- ❌ Safari/iOS not fully supported. For Safari/iOS users, change the **Leafmap module** from MapLibre to Folium in **(Map Settings)** below.
|
| 164 |
+
- 📊 Use this sidebar to color-code the map by different attributes **(Group by)**
|
| 165 |
+
- 💬 For a more tailored experience, query our dataset of protected areas and their precomputed metrics for each of the displayed layers, using the experimental chatbot. The language model tries to answer natural language questions by drawing only from curated datasets (listed below).
|
| 166 |
+
'''
|
| 167 |
|
| 168 |
#maplibregl tooltip
|
| 169 |
tooltip_cols = ['fid','state','site','sponsor','program','county','year','manager',
|