and so it begins ...
Browse files- app.py +70 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# +
|
2 |
+
import streamlit as st
|
3 |
+
from pandasai import SmartDataframe # requires 1.x numpy
|
4 |
+
from pandasai.llm import OpenAI
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
import os
|
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 |
+
st.set_page_config(
|
23 |
+
page_title="Chat tool",
|
24 |
+
page_icon="🦜",
|
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 |
+
chatbox = st.container()
|
44 |
+
with chatbox:
|
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 |
+
if x is not None:
|
55 |
+
st.write(x)
|
56 |
+
st.session_state.prompt_history.append(question)
|
57 |
+
|
58 |
+
if st.session_state.df is not None:
|
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 |
+
if st.button("Clear"):
|
67 |
+
st.session_state.prompt_history = []
|
68 |
+
st.session_state.df = None
|
69 |
+
|
70 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas-ai
|
3 |
+
seaborn
|