RKP64 commited on
Commit
56349b2
1 Parent(s): 8f63864

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
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
+