and here it goes
Browse files
app.py
CHANGED
|
@@ -1,70 +1,51 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
-
|
| 4 |
-
from pandasai.llm import OpenAI
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# +
|
| 10 |
-
import pandas as pd
|
| 11 |
-
import seaborn as sns
|
| 12 |
-
columns = ['year', 'month', 'decimal_date', 'average', 'smooth', 'std_days', 'uncertainty_monthly_mean', 'empty']
|
| 13 |
-
df = pd.read_csv("https://gml.noaa.gov/webdata/ccgg/trends/co2/co2_mm_mlo.txt", sep="\s+", comment="#", names= columns)
|
| 14 |
-
df.to_csv("data.csv")
|
| 15 |
-
|
| 16 |
-
st.session_state.df = df
|
| 17 |
-
st.session_state.prompt_history = []
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# -
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
)
|
| 26 |
-
st.title("CO2 Explorer")
|
| 27 |
|
| 28 |
-
# +
|
| 29 |
-
# st.dataframe(df)
|
| 30 |
-
# -
|
| 31 |
-
|
| 32 |
-
llm = OpenAI(api_token=st.secrets["OPENAI_API_KEY"])
|
| 33 |
-
pandas_ai = SmartDataframe("data.csv",
|
| 34 |
-
config={
|
| 35 |
-
"llm": llm,
|
| 36 |
-
"save_charts": False,
|
| 37 |
-
"verbose": False
|
| 38 |
-
}
|
| 39 |
-
)
|
| 40 |
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if question := st.chat_input("Plot the average CO2 concentration over time (decimal_date) using seaborn", key="chain"):
|
| 46 |
-
st.chat_message("user").write(question)
|
| 47 |
-
with st.chat_message("assistant"):
|
| 48 |
-
x = pandas_ai.chat(question)
|
| 49 |
-
if os.path.isfile(x):
|
| 50 |
-
im = plt.imread(x)
|
| 51 |
-
st.image(im)
|
| 52 |
-
os.remove(x)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
st.session_state.prompt_history.append(question)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
st.subheader("Current dataframe:")
|
| 60 |
-
st.write(st.session_state.df)
|
| 61 |
|
| 62 |
-
st.subheader("Prompt history:")
|
| 63 |
-
st.write(st.session_state.prompt_history)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
st.session_state.prompt_history = []
|
| 68 |
-
st.session_state.df = None
|
| 69 |
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
import pandas as pd
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
from pandasai.llm.openai import OpenAI
|
| 6 |
+
from pandasai import Agent
|
| 7 |
+
from pandasai.responses.streamlit_response import StreamlitResponse
|
| 8 |
|
| 9 |
+
llm = OpenAI(api_token=st.secrets["OPENAI_API_KEY"])
|
| 10 |
+
df1 = pd.read_csv("data.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
agent = Agent(
|
| 13 |
+
[df1],
|
| 14 |
+
config={"verbose": True, "response_parser": StreamlitResponse, "llm": llm},
|
| 15 |
)
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
st.title("Pandas-AI demo using climate data")
|
| 19 |
|
| 20 |
+
'''
|
| 21 |
+
A simple demo based on Mauna Loa CO2 Records. Ask questions of the data below:
|
| 22 |
+
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
## Dataset
|
| 25 |
+
st.dataframe(df1)
|
|
|
|
| 26 |
|
| 27 |
+
## Chatbot
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
with st.container():
|
| 31 |
+
prompt = st.chat_input("Plot CO2 over time")
|
| 32 |
+
if prompt:
|
| 33 |
+
with st.spinner():
|
| 34 |
+
resp = agent.chat(prompt)
|
| 35 |
+
if os.path.isfile('exports/charts/temp_chart.png'):
|
| 36 |
+
im = plt.imread('exports/charts/temp_chart.png')
|
| 37 |
+
st.image(im)
|
| 38 |
+
os.remove('exports/charts/temp_chart.png')
|
| 39 |
+
st.write(resp)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
st.divider()
|
| 43 |
|
| 44 |
+
'''
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
[pandas-ai docs](https://docs.pandas-ai.com/intro)
|
| 47 |
|
| 48 |
+
Copyright 2024 [Carl Boettiger](https://carlboettiger.info), UC Berkeley.
|
| 49 |
+
License: BSD2
|
| 50 |
+
Data from: <https://gml.noaa.gov/webdata/ccgg/trends/co2/co2_mm_mlo.txt>
|
| 51 |
+
'''
|