-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchatmydata.py
More file actions
214 lines (162 loc) · 6.82 KB
/
Copy pathchatmydata.py
File metadata and controls
214 lines (162 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import streamlit as st
import pandas as pd
import json
import os
from agent import query_agent
from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import VertexAI
from google.cloud import aiplatform
from google.oauth2 import service_account
import matplotlib
import matplotlib.pyplot as plt
import plotly.express as px
from langchain.memory import ConversationBufferWindowMemory
# reference:
# https://github.com/Ngonie-x/langchain_csv/tree/main
# https://github.com/chatgpt/chart
#assign credential. replace project_id and credential
mycredential = service_account.Credentials.from_service_account_file(os.environ["GOOGLE_APPLICATION_CREDENTIALS"])
myproject=os.environ["PROJECT_ID"]
aiplatform.init(project=myproject, location='us-central1',credentials=mycredential)
@st.cache_data
def convert_df(df):
# Cache the conversion to prevent computation on every rerun
return df.to_csv().encode('utf-8')
def decode_response(response: str) -> dict:
"""converts the string response from the model to a dictionary object.
Args:
response (str): response from the model
Returns:
dict: dictionary with response data
"""
return json.loads(response,strict=False)
def write_response(response_dict: dict):
"""
Write a response from an agent to a Streamlit app.
Args:
response_dict: The response from the agent.
Returns:
None.
"""
# write output to streamlit
# plot chart using plotly if required
# if response is "answer"
if "answer" in response_dict:
st.write(response_dict["answer"])
# if response is bar chart
elif "bar" in response_dict:
code =response_dict['bar']['python_code']
exec(code,globals())
st.plotly_chart(fig,theme=None, use_container_width=True)
# if response is pie chart
elif "pie" in response_dict:
code =response_dict['pie']['python_code']
exec(code,globals())
st.plotly_chart(fig,theme=None, use_container_width=True)
# if response is scatter chart
elif "scatter" in response_dict:
code =response_dict['scatter']['python_code']
exec(code,globals())
st.plotly_chart(fig,theme=None, use_container_width=True)
# if response is line chart
elif "line" in response_dict:
code =response_dict['line']['python_code']
exec(code,globals())
st.plotly_chart(fig,theme=None, use_container_width=True)
# if response is Manipulation or using formula
elif "manipulation" in response_dict:
code =response_dict['manipulation']['python_code']
exec(code,globals())
csv = convert_df(df)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='df.csv',
mime='text/csv',key="manipulation"+str(uniq))
st.write(df)
# if response from agent is table
elif "table" in response_dict:
code="df_temp="+response_dict['table']['python_code']
exec(code,globals())
csv = convert_df(df_temp)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='df.csv',
mime='text/csv',key="table"+str(uniq))
st.write(df_temp)
else:
st.write(response_dict)
st.title("👨💻 Chat with your data")
st.write("Please upload your CSV file below.")
with st.sidebar:
option = st.selectbox("Model Temperature", [0,0.1, 0.2, 0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
# upload csv
data = st.file_uploader("Upload a CSV")
if data is not None:
df = pd.read_csv(data)
reset_df=df
st.write(df)
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
len_msg=len(st.session_state.messages)
uniq=len_msg
# Display chat messages from history on app rerun
for message in st.session_state.messages:
try:
uniq=uniq+1
with st.chat_message(message["role"]):
if message["role"] == "assistant":
if "{" in message["content"]:
decoded_history = decode_response(message["content"])
else:
decoded_history = message["content"]
if type(decoded_history) == dict:
decoded_history = decoded_history
else:
decoded_history = str(decoded_history)
write_response(decoded_history)
else:
st.markdown(message["content"])
except Exception as e:
st.error(f"An error occurred: please try another query/question")
# capture query from user input
if prompt := st.chat_input("Enter your prompt:"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
uniq=uniq+1
try:
with st.spinner('Please wait...'):
# chat memory up to 3 previous requests
memory = ConversationBufferWindowMemory(memory_key="chat_history",k=3)
#declare llm
llm = VertexAI(model_name="text-bison@001",
temperature=option,
max_retry=3)
agent=create_pandas_dataframe_agent(llm, df, memory=memory, verbose=True)
response = query_agent(agent=agent, query=prompt)
if "{" in response:
decoded_response = decode_response(response)
else:
decoded_response = response
if type(decoded_response) == dict:
decoded_response = decoded_response
else:
decoded_response = str(decoded_response)
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.write("Here the answer : ")
write_response(decoded_response)
except Exception as e:
st.error(f"An error occurred: please try another query/question")
uniq=uniq+1
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
with st.sidebar:
reset_button_key = "reset_button"
reset_button = st.button("Reset Chat",key=reset_button_key)
if reset_button:
st.session_state.messages = []