Upload 3 files
Browse files- src/__pycache__/constants.cpython-311.pyc +0 -0
- src/app.py +90 -0
- src/constants.py +39 -0
src/__pycache__/constants.cpython-311.pyc
ADDED
Binary file (1.49 kB). View file
|
|
src/app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_chat import message
|
4 |
+
|
5 |
+
from pandasai import PandasAI
|
6 |
+
from constants import file_format, models
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
|
9 |
+
@st.cache_data
|
10 |
+
def load_data(uploaded_file):
|
11 |
+
ext = os.path.splitext(uploaded_file.name)[1][1:].lower()
|
12 |
+
if ext in file_format:
|
13 |
+
return file_format[ext](uploaded_file)
|
14 |
+
|
15 |
+
def generate_response(question_input, dataframes, option, api_key):
|
16 |
+
llm = models[option](api_key)
|
17 |
+
pandas_ai = PandasAI(llm)
|
18 |
+
if len(dataframes) == 1:
|
19 |
+
return pandas_ai(dataframes[0], prompt = question_input, is_conversational_answer = True)
|
20 |
+
else:
|
21 |
+
return pandas_ai(dataframes, prompt = question_input, is_conversational_answer = True)
|
22 |
+
|
23 |
+
st.set_page_config(page_title="PandasAI Chat", page_icon=":panda_face:")
|
24 |
+
st.title("PandasAI Chat :panda_face:")
|
25 |
+
|
26 |
+
if "generated" not in st.session_state:
|
27 |
+
st.session_state["generated"] = []
|
28 |
+
|
29 |
+
if "past" not in st.session_state:
|
30 |
+
st.session_state["past"] = []
|
31 |
+
|
32 |
+
if "plots" not in st.session_state:
|
33 |
+
st.session_state["plots"] = []
|
34 |
+
|
35 |
+
left, right = st.columns([1, 2])
|
36 |
+
with left:
|
37 |
+
model_option = st.selectbox('Model', models.keys())
|
38 |
+
|
39 |
+
with right:
|
40 |
+
api_key = st.text_input('API Key', '', type = 'password')
|
41 |
+
if not api_key:
|
42 |
+
st.info(f"Please input API Token for {model_option}.")
|
43 |
+
|
44 |
+
question_input = None
|
45 |
+
uploaded_files = st.file_uploader("Upload a file", type=list(file_format.keys()), accept_multiple_files=True)
|
46 |
+
dataframes = []
|
47 |
+
|
48 |
+
if not uploaded_files:
|
49 |
+
st.info("Please upload your dataset(s) to begin asking questions!")
|
50 |
+
|
51 |
+
else:
|
52 |
+
for uploaded_file in uploaded_files:
|
53 |
+
dataframe = load_data(uploaded_file)
|
54 |
+
dataframes.append(dataframe)
|
55 |
+
|
56 |
+
with st.expander(uploaded_file.name):
|
57 |
+
st.write(dataframe.head())
|
58 |
+
|
59 |
+
question_input = st.text_input("Enter question")
|
60 |
+
|
61 |
+
st.markdown("---")
|
62 |
+
|
63 |
+
response = None
|
64 |
+
if question_input and uploaded_files and api_key:
|
65 |
+
with st.spinner("Generating..."):
|
66 |
+
try:
|
67 |
+
response = generate_response(question_input, dataframes, model_option, api_key)
|
68 |
+
|
69 |
+
except Exception as e:
|
70 |
+
st.error("An error occurred: either your API token is invalid \
|
71 |
+
or no code was found in the response generated.")
|
72 |
+
|
73 |
+
if len(plt.get_fignums()) > 0:
|
74 |
+
fig = plt.gcf()
|
75 |
+
st.session_state.plots.append(fig)
|
76 |
+
else:
|
77 |
+
st.session_state.plots.append("None")
|
78 |
+
|
79 |
+
if response:
|
80 |
+
st.session_state.past.append(question_input)
|
81 |
+
st.session_state.generated.append(response)
|
82 |
+
|
83 |
+
if "generated" in st.session_state and st.session_state['generated']:
|
84 |
+
for i in range(len(st.session_state['generated'])-1, -1, -1):
|
85 |
+
if st.session_state["plots"][i] != "None":
|
86 |
+
st.pyplot(st.session_state["plots"][i])
|
87 |
+
|
88 |
+
message(st.session_state["generated"][i], key=str(i))
|
89 |
+
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
|
90 |
+
|
src/constants.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from pandasai.llm.openai import OpenAI
|
3 |
+
from pandasai.llm.starcoder import Starcoder
|
4 |
+
from pandasai.llm.google_palm import GooglePalm
|
5 |
+
|
6 |
+
file_format = {
|
7 |
+
"csv": pd.read_csv,
|
8 |
+
"xls": pd.read_excel,
|
9 |
+
"xlsx": pd.read_excel,
|
10 |
+
"xlsm": pd.read_excel,
|
11 |
+
"xlsb": pd.read_excel,
|
12 |
+
"json": pd.read_json,
|
13 |
+
"html": pd.read_html,
|
14 |
+
"sql": pd.read_sql,
|
15 |
+
"feather": pd.read_feather,
|
16 |
+
"parquet": pd.read_parquet,
|
17 |
+
"dta": pd.read_stata,
|
18 |
+
"sas7bdat": pd.read_sas,
|
19 |
+
"h5": pd.read_hdf,
|
20 |
+
"hdf5": pd.read_hdf,
|
21 |
+
"pkl": pd.read_pickle,
|
22 |
+
"pickle": pd.read_pickle,
|
23 |
+
"gbq": pd.read_gbq,
|
24 |
+
"orc": pd.read_orc,
|
25 |
+
"xpt": pd.read_sas,
|
26 |
+
"sav": pd.read_spss,
|
27 |
+
"gz": pd.read_csv,
|
28 |
+
"zip": pd.read_csv,
|
29 |
+
"bz2": pd.read_csv,
|
30 |
+
"xz": pd.read_csv,
|
31 |
+
"txt": pd.read_csv,
|
32 |
+
"xml": pd.read_xml,
|
33 |
+
}
|
34 |
+
|
35 |
+
models = {
|
36 |
+
"OpenAI": OpenAI,
|
37 |
+
"Starcoder": Starcoder,
|
38 |
+
"Google Palm": GooglePalm
|
39 |
+
}
|