landvote / app.py
cboettig's picture
experimental chat
cc8b36c
raw
history blame
2.75 kB
import ibis
from ibis import _
import streamlit as st
st.set_page_config(layout="wide",
page_title="TPL LandVote",
page_icon=":globe:")
'''
# LandVote Prototype
'''
## Chatbot
import os
import pandas as pd
import matplotlib.pyplot as plt
from pandasai.llm.openai import OpenAI
from pandasai import Agent
from pandasai.responses.streamlit_response import StreamlitResponse
llm = OpenAI(api_token=st.secrets["OPENAI_API_KEY"])
df1 = pd.read_csv("data.csv")
agent = Agent(
[df1],
config={"verbose": True, "response_parser": StreamlitResponse, "llm": llm},
)
with st.sidebar:
'''
## Data Assistant (experimental)
Ask questions about the landvote data, like:
- What are the top states for approved conservation funds?
- Plot the total funds spent in conservation each year.
- What city has approved the most funds in a single measure? What was the description of that vote?
- Which state has had largest number measures fail? What is that as a fraction of it's total measures?
'''
prompt = st.chat_input("Ask about the data")
if prompt:
with st.spinner():
resp = agent.chat(prompt)
if os.path.isfile('exports/charts/temp_chart.png'):
im = plt.imread('exports/charts/temp_chart.png')
st.image(im)
os.remove('exports/charts/temp_chart.png')
st.write(resp)
# year = st.slider("Select a year", min_value=1988, max_value=2024, value=2022, step=2)
year = st.slider("Select a year", min_value=1988, max_value=2024, value=2022, step=1)
#gdf = df.filter(_.year==year).execute()
import leafmap.maplibregl as leafmap
m = leafmap.Map(style="positron", center=(-100, 30), zoom=5)
url = "https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/vote.pmtiles"
#gdf = df.filter(_.year==1988).execute()
#gdf.to_file("vote.geojson")
outcome = [
'match',
['get', 'Status'],
"Pass", '#2E865F',
"Fail", '#FF3300',
'#ccc'
]
paint = {"fill-extrusion-color": outcome,
"fill-extrusion-opacity": 0.7,
"fill-extrusion-height": ["*", ["get", "log_amount"], 5000],
}
style = {
"layers": [
{
"id": "votes",
"source": "vote",
"source-layer": "vote",
"type": "fill-extrusion",
"filter": [
"==",
["get", "year"],
year,
], # only show buildings with height info
"paint": paint
},
],
}
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=1.0,
tooltip=True,
fit_bounds=True,
)
#m.add_layer_control()
m.to_streamlit()