Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +121 -0
- requirements.txt +0 -0
- temp_chart.png +0 -0
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from pandasai import SmartDataframe, SmartDatalake
|
| 5 |
+
from pandasai.llm import GooglePalm
|
| 6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
+
from langchain_experimental.agents import create_pandas_dataframe_agent
|
| 8 |
+
# from pandasai.llm.openai import OpenAI
|
| 9 |
+
from langchain_groq import ChatGroq
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def read_csv_file(uploaded_file):
|
| 13 |
+
try:
|
| 14 |
+
df = pd.read_csv(uploaded_file)
|
| 15 |
+
except UnicodeDecodeError:
|
| 16 |
+
st.warning("UTF-8 encoding failed. Trying latin1 encoding.")
|
| 17 |
+
try:
|
| 18 |
+
df = pd.read_csv(uploaded_file, encoding='latin1')
|
| 19 |
+
print(uploaded_file)
|
| 20 |
+
except UnicodeDecodeError:
|
| 21 |
+
st.error("Unable to read the file with both UTF-8 and ISO-8859-1 encodings.")
|
| 22 |
+
return None
|
| 23 |
+
return df
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
st.set_page_config(page_title="Smart Data Query App", layout="wide")
|
| 28 |
+
|
| 29 |
+
st.title("Smart Data Query App")
|
| 30 |
+
|
| 31 |
+
# Choice to upload one or two CSV files
|
| 32 |
+
file_count = st.radio("How many CSV files would you like to upload?", (1, 2))
|
| 33 |
+
|
| 34 |
+
col1, col2 = st.columns(2)
|
| 35 |
+
with col1:
|
| 36 |
+
uploaded_file1 = st.file_uploader("Choose the first CSV file", type="csv", key="file1")
|
| 37 |
+
if file_count == 2:
|
| 38 |
+
with col2:
|
| 39 |
+
uploaded_file2 = st.file_uploader("Choose the second CSV file", type="csv", key="file2")
|
| 40 |
+
else:
|
| 41 |
+
uploaded_file2 = None
|
| 42 |
+
|
| 43 |
+
if uploaded_file1 is not None or uploaded_file2 is not None:
|
| 44 |
+
df1 = read_csv_file(uploaded_file1) if uploaded_file1 is not None else None
|
| 45 |
+
df2 = read_csv_file(uploaded_file2) if uploaded_file2 is not None else None
|
| 46 |
+
|
| 47 |
+
if df1 is not None or df2 is not None:
|
| 48 |
+
if df1 is not None:
|
| 49 |
+
with st.expander("Preview First CSV File"):
|
| 50 |
+
st.dataframe(df1.head())
|
| 51 |
+
if df2 is not None:
|
| 52 |
+
with st.expander("Preview Second CSV File"):
|
| 53 |
+
st.dataframe(df2.head())
|
| 54 |
+
|
| 55 |
+
pandas_api = os.environ['PANDASAI_API_KEY']
|
| 56 |
+
google_api = os.environ['GOOGLE_API_KEY']
|
| 57 |
+
groq_api = os.environ['GROQ_API_KEY']
|
| 58 |
+
|
| 59 |
+
# Set up the ChatGroq model
|
| 60 |
+
llm = ChatGroq(
|
| 61 |
+
groq_api_key=groq_api,
|
| 62 |
+
model_name='mixtral-8x7b-32768'
|
| 63 |
+
)
|
| 64 |
+
# llm = GooglePalm(api_key=google_api)
|
| 65 |
+
|
| 66 |
+
if df1 is not None and df2 is not None:
|
| 67 |
+
lake = SmartDatalake([df1, df2])
|
| 68 |
+
else:
|
| 69 |
+
lake = SmartDataframe(df1, config = {"LLM": llm, "conversational": True, "verbose": True}) if df1 is not None else None
|
| 70 |
+
|
| 71 |
+
datalake_1 = lake
|
| 72 |
+
query = st.text_input("Enter your query:")
|
| 73 |
+
|
| 74 |
+
submitted = st.button("Submit")
|
| 75 |
+
|
| 76 |
+
if submitted:
|
| 77 |
+
if query:
|
| 78 |
+
response = datalake_1.chat(query)
|
| 79 |
+
|
| 80 |
+
if "Unfortunately, I was not able to answer your question, because of the following error:" in response:
|
| 81 |
+
llm = ChatGoogleGenerativeAI(
|
| 82 |
+
model="gemini-pro", verbose=True, google_api_key=google_api
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
agent = create_pandas_dataframe_agent(
|
| 86 |
+
llm,
|
| 87 |
+
df1,
|
| 88 |
+
verbose=True,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
response2 = agent.invoke(query)
|
| 92 |
+
st.write("Response:")
|
| 93 |
+
st.write(response2["output"])
|
| 94 |
+
|
| 95 |
+
else:
|
| 96 |
+
st.write("Response:")
|
| 97 |
+
st.write(response)
|
| 98 |
+
|
| 99 |
+
# Append the query and response to the session state for query history
|
| 100 |
+
if 'query_history' not in st.session_state:
|
| 101 |
+
st.session_state.query_history = []
|
| 102 |
+
st.session_state.query_history.append((query, response))
|
| 103 |
+
else:
|
| 104 |
+
st.write("Please enter a query.")
|
| 105 |
+
|
| 106 |
+
# Display query history
|
| 107 |
+
if 'query_history' in st.session_state and st.session_state.query_history:
|
| 108 |
+
st.subheader("Query History")
|
| 109 |
+
for q, r in st.session_state.query_history:
|
| 110 |
+
st.write(f"**Query:** {q}")
|
| 111 |
+
st.write(f"**Response:** {r}")
|
| 112 |
+
st.write("---")
|
| 113 |
+
|
| 114 |
+
else:
|
| 115 |
+
st.error("Failed to read one or both CSV files. Please check the files and try again.")
|
| 116 |
+
else:
|
| 117 |
+
st.write("Please upload at least one CSV file.")
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
main()
|
requirements.txt
ADDED
|
Binary file (4.01 kB). View file
|
|
|
temp_chart.png
ADDED
|