Repository navigation
Expand file tree
/
Copy pathinterface.py
More file actions
175 lines (149 loc) · 6.72 KB
/
Copy pathinterface.py
File metadata and controls
175 lines (149 loc) · 6.72 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
import streamlit as st
import pandas as pd
import json
import io
from langchain.agents import create_pandas_dataframe_agent
from langchain.chat_models import ChatOpenAI
# Importing the functions from agent.py
from agent import query_agent
# Set up the page without the theme argument
st.set_page_config(
page_title="CSV Analyzer",
layout="wide",
initial_sidebar_state="expanded"
)
# Use CSS to style the app to have a dark theme
st.markdown(
"""
<style>
.reportview-container {
background-color: #0E1117;
color: #FAFAFA;
}
.sidebar .sidebar-content {
background-color: #262730;
color: #FAFAFA;
}
.stButton>button {
background-color: #FF4B4B;
color: white;
}
</style>
""",
unsafe_allow_html=True
)
def decode_response(response: str) -> dict:
"""This function converts the string response from the model to a dictionary object."""
return json.loads(response)
def write_response(response_dict: dict):
"""Write a response from an agent to a Streamlit app."""
if "answer" in response_dict:
st.write(response_dict["answer"])
if "bar" in response_dict:
data = response_dict["bar"]
df = pd.DataFrame(data)
df.set_index("columns", inplace=True)
st.bar_chart(df)
if "line" in response_dict:
data = response_dict["line"]
df.set_index("columns", inplace=True)
st.line_chart(df)
if "table" in response_dict:
data = response_dict["table"]
df = pd.DataFrame(data["data"], columns=data["columns"])
st.table(df)
st.title("👨💻 Chat with your CSV")
st.write("Please upload your CSV file below.")
# Add dropdown menu in the sidebar
option = st.sidebar.selectbox(
"Choose an action:",
("Summarization Report", "Analysis", "Visualization")
)
# Input for CSV file
data = st.file_uploader("Upload a CSV")
# Input for user query (only for analysis or visualization)
query = None
if option == "Analysis" or option == "Visualization":
query = st.text_area("Insert your query")
# Submit button
if st.button("Submit Query", type="primary"):
if data is not None:
# Read the CSV file into a DataFrame
df = pd.read_csv(data)
if option == "Summarization Report":
# Generate summary statistics for the dataset
st.markdown("### **Dataset Summary**")
# Prepare a StringIO buffer to hold the summary text
summary_buffer = io.StringIO()
# Number of rows and columns
num_rows = df.shape[0]
num_cols = df.shape[1]
st.markdown(f"**Number of rows:** {num_rows}")
st.markdown(f"**Number of columns:** {num_cols}")
summary_buffer.write(f"Number of rows: {num_rows}\n")
summary_buffer.write(f"Number of columns: {num_cols}\n")
# Data types of each column
st.markdown("### **Data Types**")
st.markdown(df.dtypes.to_markdown())
summary_buffer.write("Data types:\n")
summary_buffer.write(f"{df.dtypes}\n\n")
# Range of values for numeric columns
numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
if len(numeric_cols) > 0:
st.markdown("### **Range of Values for Numeric Columns**")
summary_buffer.write("Range of values for numeric columns:\n")
for col in numeric_cols:
min_val = df[col].min()
max_val = df[col].max()
st.markdown(f"- **{col}:** min={min_val}, max={max_val}")
summary_buffer.write(f"{col}: min={min_val}, max={max_val}\n")
else:
st.markdown("No numeric columns found.")
summary_buffer.write("No numeric columns found.\n")
# Value counts for categorical columns
categorical_cols = df.select_dtypes(include=['object', 'category']).columns
if len(categorical_cols) > 0:
st.markdown("### **Value Counts for Categorical Columns**")
summary_buffer.write("Value counts for categorical columns:\n")
for col in categorical_cols:
st.markdown(f"**{col}:**")
st.markdown(df[col].value_counts().to_markdown())
summary_buffer.write(f"{col}:\n{df[col].value_counts()}\n\n")
else:
st.markdown("No categorical columns found.")
summary_buffer.write("No categorical columns found.\n")
# Brief description about what the dataset represents
st.markdown("### **Dataset Description**")
st.write("""
This dataset contains various columns representing different features.
Numeric columns show the range of values, and categorical columns list the value counts for each category.
The dataset provides insights based on these features.
""")
summary_buffer.write("""
Dataset Description:\nThis dataset contains various columns representing different features.
Numeric columns show the range of values, and categorical columns list the value counts for each category.
The dataset provides insights based on these features.\n
""")
# Provide download link for the summary as a text file
summary_text = summary_buffer.getvalue()
st.download_button(
label="Download Summary Report",
data=summary_text,
file_name="dataset_summary.txt",
mime="text/plain"
)
else:
# Create a ChatOpenAI instance using GPT-4 model
openai_api_key = ''
openai_instance = ChatOpenAI(api_key=openai_api_key, model="gpt-4", temperature=0)
# Create the pandas DataFrame agent using the GPT-4 model
agent = create_pandas_dataframe_agent(openai_instance, df, verbose=True)
# Query the agent based on the option selected from the dropdown
if option == "Analysis":
response = query_agent(agent=agent, query="Perform an analysis of the dataset")
elif option == "Visualization":
response = query_agent(agent=agent, query=query) # Use the user's input query for visualization
# Decode the response
decoded_response = decode_response(response)
# Write the response to the Streamlit app
write_response(decoded_response)