-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
195 lines (135 loc) · 5.63 KB
/
app.py
File metadata and controls
195 lines (135 loc) · 5.63 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
import streamlit as st
import pandas as pd
import json
from langchain import OpenAI
from langchain_experimental.agents.agent_toolkits.pandas.base import create_pandas_dataframe_agent
import pandas as pd
API_KEY = st.secrets["API_KEY"]
page_element="""
<style>
[data-testid="stAppViewContainer"]{
background-image: url("https://www.surveylegend.com/wordpress/wp-content/themes/sage/resources/images/img/modal/upgrade-file-upload.png");
background-size: cover;
}
[data-testid="stHeader"]{
background-color: rgba(0,0,0,0);
}
[data-testid="stToolbar"]{
right: 2rem;
background-image: url("");
background-size: cover;
}
[data-testid="stSidebar"]> div:first-child{
background-image: url("https://img.freepik.com/premium-vector/skyblue-gradient-background-advertisers-gradient-hq-wallpaper_189959-513.jpg");
background-size: cover;
}
</style>
"""
st.markdown(page_element, unsafe_allow_html=True)
st.markdown("<h1 style='text-align: center; color: white';>Dynamic Analytics Crime Modelling 🚔👮</h1>", unsafe_allow_html=True)
st.markdown("---")
# st.write("Please upload your Chicago Crime CSV file")
# data = st.file_uploader("Upload a CSV")
file_path = "Crimes-2023.csv"
df = pd.read_csv(file_path)
df=df.dropna()
with st.sidebar:
st.title("Dataset Description")
st.write("This dataset contains information about crimes in the year 2023.")
st.write("It includes various attributes such as ID, Case Number, Date, Block, Primary Type, Location Description, Arrest, and more.")
st.subheader("Dataset Features")
crime_counts = df.columns
result = ", ".join(crime_counts)
st.write(result)
st.write(f"The dataset consists of {len(df)} records.")
st.header("Exploring Crime Dataset")
st.subheader("Dataset Overview")
st.write("Here's an overview of the dataset:")
st.dataframe(df.head())
st.subheader("Dataset Statistics")
st.write("Here are some statistics about the dataset:")
st.write(df.describe())
st.subheader("Crime Types")
crime_counts = df["Primary Type"].value_counts()
st.bar_chart(crime_counts)
query = st.text_input("Please do ask here to get insights from the CSV - ")
def agent(df):
llm = OpenAI(openai_api_key=API_KEY)
return create_pandas_dataframe_agent(llm, df, verbose=False)
def query_llm(agent, query):
prompt = (
"""
For the following query, if it requires drawing a table, reply as follows:
{"table": {"columns": ["column1", "column2", ...], "data": [[value1, value2, ...], [value1, value2, ...], ...]}}
If the query requires creating a bar chart, reply as follows:
{"bar": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}
If the query requires creating a line chart, reply as follows:
{"line": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}
The various types of chart, "bar", "line" and "pie" plot
If it is just asking a question that requires neither, reply as follows:
{"answer": "answer"}
Example:
{"answer": "The title with the highest rating is 'Gilead'"}
If you do not know the answer, reply as follows:
{"answer": "I do not know."}
Return all output as a string.
All strings in "columns" list and data list, should be in double quotes,
For example: {"columns": ["title", "ratings_count"], "data": [["Gilead", 361], ["Spider's Web", 5164]]}
Lets think step by step.
Below is the query.
Query:
"""
+ query
)
response = agent.run(prompt)
return response.__str__()
def query_analysis(agent):
prompt = (
"""
If you do not know the answer, reply as follows:
{"answer": "I do not know."}
Return all output as a string.
All strings in "columns" list and data list, should be in double quotes,
For example: {"columns": ["title", "ratings_count"], "data": [["Gilead", 361], ["Spider's Web", 5164]]}
Lets think step by step.
Below is the query.
Query:Prepare the analytical questions by looking the complex realtion, trend and behavior of the various columns in the csv file
The Response should be multiple questions all in one string
Response:
"""
)
response = str(agent.run(prompt))
return response.__str__()
def decode_response(response: str) -> dict:
return json.loads(response)
def write_response(response_dict: dict):
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 = pd.DataFrame(data)
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)
col_1, col_2= st.columns(2)
with col_1:
# if st.button("Get Question", type="primary"):
# agent = agent(df)
# response = query_analysis(agent=agent)
# decoded_response = decode_response(response)
# write_response(decoded_response)
pass
with col_2:
if st.button("Submit Query", type="primary"):
agent = agent(df)
response = query_llm(agent=agent, query=query)
decoded_response = decode_response(response)
write_response(decoded_response)