-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
353 lines (297 loc) · 12.2 KB
/
streamlit_app.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import streamlit as st
import pandas as pd
import time
from preprocessor import preprocess_data
import visualizer as viz
import os
import datetime
import glob2
import insight_generator
import uuid
# Generate a unique session ID for each user
data_folder = 'data'
def delete_old_folders(data_folder):
current_time = time.time()
for subfolder in os.listdir(data_folder):
subfolder_path = os.path.join(data_folder, subfolder)
if os.path.isdir(subfolder_path):
timestamp_file = os.path.join(subfolder_path, 'session_init.txt')
if os.path.exists(timestamp_file):
with open(timestamp_file, 'r') as file:
folder_time = float(file.read().strip())
if current_time - folder_time > 3600:
for root, dirs, files in os.walk(subfolder_path, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
os.rmdir(root)
# # print(f"Deleted folder: {subfolder_path}")
# else:
# print(f"No timestamp file found in {subfolder_path}, skipping.")
delete_old_folders(data_folder=data_folder)
if "session_id" not in st.session_state:
st.session_state.session_id = str(uuid.uuid4())
saved_directory = f"data/{st.session_state.session_id}"
if not os.path.exists(saved_directory):
os.makedirs(saved_directory)
last_activity_file = os.path.join(saved_directory, 'session_init.txt')
with open(last_activity_file, 'w') as f:
f.write(str(datetime.datetime.now().timestamp()))
if "button" not in st.session_state:
st.session_state.button = False
if "generated" not in st.session_state:
st.session_state.generated = ""
if "regenerate" not in st.session_state:
st.session_state.regenerate = False
if st.session_state.button == False:
st.set_page_config(initial_sidebar_state="collapsed", layout='wide', page_title='InsightScope', page_icon=':mag:')
st.markdown(
"""
<style>
[data-testid="collapsedControl"] {
display: none
}
.stApp {
background: rgb(161,98,0);
background: linear-gradient(338deg, rgba(161,98,0,1) 0%, rgba(9,9,121,1) 49%, rgba(2,2,51,1) 100%);
}
</style>
""",
unsafe_allow_html=True,
)
st.title("InsightScope :mag:")
st.subheader("Unlock the Power of Your Data - Discover Insights, Visualize Trends, and Make Informed Decisions.")
st.write("Upload your CSV files to effortlessly analyze and visualize data trends. Generate insightful reports and explore your data with interactive charts, all in one place.")
st.write(" ")
st.write(" ")
with st.form("my_form", clear_on_submit=True):
upload_file = st.file_uploader("Upload a structured dataset (.csv)", accept_multiple_files=False, type=['csv'])
submitted = st.form_submit_button("Submit", use_container_width=True, type='secondary')
if submitted:
if upload_file is not None:
with st.spinner("Loading..."):
time.sleep(5)
date = datetime.datetime.now()
tim = datetime.datetime.now().ctime().split(" ")[3]
file_path = os.path.join(saved_directory,upload_file.name[0:len(upload_file.name)-4]+f"_{int(date.timestamp()) }_"+".csv") #+f"_{date.day}-{date.month}-{date.year}_{tim}"
with open(file_path, "wb") as f:
f.write(upload_file.getbuffer())
st.success(f"File uploaded successfully")
def generate():
st.session_state.button = True
st.write(" ")
st.write(" ")
st.button("Visualize data and generate insights", use_container_width=True, type='primary', on_click=generate)
st.markdown(
"""
<style>
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: black;
color: white;
text-align: center;
padding: 10px;
}
.footer img {
border-radius: 50%;
width: 50px;
height: 50px;
margin-right: 15px;
}
.footer a {
color: white;
margin: 0 10px;
text-decoration: none;
}
.footer a:hover {
color: #0000ff; /* Optional: Add a hover effect */
}
</style>
<div class="footer">
<a href="https://github.com/AdityaGupta0001" target="_blank">GitHub</a>
<a href="https://www.linkedin.com/in/aditya-gupta-475328252/" target="_blank">LinkedIn</a>
</div>
""",
unsafe_allow_html=True
)
else:
st.set_page_config(initial_sidebar_state="collapsed", layout='wide', page_title='InsightScope', page_icon=':mag:')
st.markdown(
"""
<style>
[data-testid="collapsedControl"] {
display: none
}
.stApp {
background: rgb(161,98,0);
background: linear-gradient(338deg, rgba(161,98,0,1) 0%, rgba(9,9,121,1) 49%, rgba(2,2,51,1) 100%);
}
</style>
""",
unsafe_allow_html=True,
)
def stream_data(message):
text = message
for word in text.split(" "):
for i in word:
yield i
time.sleep(0.005)
yield " "
time.sleep(0.02)
st.title("InsightScope :mag:")
st.subheader("Unlock the Power of Your Data - Discover Insights, Visualize Trends, and Make Informed Decisions.")
st.write(" ")
st.write(" ")
data_folder_path = os.path.join("data", st.session_state.session_id)
file_pattern = os.path.join(data_folder_path, "*.csv")
files = glob2.glob(file_pattern)
file_dict = {}
for file in files:
file_name = os.path.basename(file)
try:
name, timestamp, _ = file_name.rsplit('_', 2)
timestamp = float(timestamp)
file_dict[file] = timestamp
except ValueError:
# print(f"Skipping file with unexpected name format: {file_name}")
pass
if file_dict:
most_recent_file = max(file_dict, key=file_dict.get)
for file in files:
if file != most_recent_file:
os.remove(file)
# print(f"Removed file: {file}")
# print(f"Kept file: {most_recent_file}")
else:
# print("No valid files found to process.")
pass
data_contents = os.listdir(data_folder_path)
if len(data_contents) == 0:
st.error("No file uploaded")
else:
for item in data_contents:
if item.endswith('.csv'):
item_path = os.path.join(data_folder_path, item)
df = pd.read_csv(item_path, encoding='ISO-8859-1')
st.subheader('Preprocessed Data')
st.dataframe(df, use_container_width=True)
preprocess_data(item_path, threshold=0.4, scaling_method='minmax', encode_method='onehot')
st.write("")
st.write("")
st.write("")
st.write("")
with st.form(key='insight_form'):
st.subheader("Dataset Insights")
if st.session_state.generated == "":
insights = insight_generator.generate_insights(item_path)
st.session_state.generated = insights
st.write_stream(stream_data(st.session_state.generated))
elif st.session_state.generated != "" and st.session_state.regenerate:
insights = insight_generator.generate_insights(item_path)
st.session_state.generated = insights
st.write_stream(stream_data(st.session_state.generated))
st.session_state.regenerate = False
else:
st.write(st.session_state.generated)
def change_state():
st.session_state.regenerate = True
st.form_submit_button(label="Regenerate Insights", use_container_width=True, on_click=change_state, type='primary')
data = viz.load_and_prepare_data(item_path)
st.write("")
st.write("")
st.write("")
st.write("")
with st.form(key='correlation_heatmap_form'):
st.subheader('Correlation Heatmap')
try:
heatmap_fig = viz.plot_correlation_heatmap(data)
st.pyplot(heatmap_fig, use_container_width=True)
except:
st.info("Correlation Heatmap could not be generated for this dataset")
st.form_submit_button(label='Update Correlation Heatmap', use_container_width=True)
st.write("")
st.write("")
st.write("")
st.write("")
st.subheader('Distributions of Numerical Features')
for column, fig in viz.plot_distributions(data):
with st.form(key=f'distribution_form_{column}'):
st.write(f'Distribution of {column}')
try:
st.plotly_chart(fig, use_container_width=True)
except:
st.info(f"Distribution plot could not be generated for this {column} of this dataset")
st.form_submit_button(label=f'Update Distribution of {column}', use_container_width=True)
st.write("")
st.write("")
st.write("")
st.write("")
with st.form(key='pca_form'):
st.subheader('PCA')
try:
pca_fig = viz.plot_pca(data)
st.plotly_chart(pca_fig, use_container_width=True, theme=None)
except:
st.info("PCA Plot could not be generated for this dataset")
st.form_submit_button(label='Update PCA Plot', use_container_width=True)
st.write("")
st.write("")
st.write("")
st.write("")
with st.form(key='pca_3d_form'):
st.subheader('3D PCA Scatter Plot')
try:
pca_3d_fig = viz.plot_pca_3d(data)
if pca_3d_fig:
st.plotly_chart(pca_3d_fig, use_container_width=True, theme=None)
except:
st.info("PCA 3D Plot could not be generated for this dataset")
st.form_submit_button(label='Update 3D PCA Plot', use_container_width=True)
def generate():
st.session_state.button = False
st.session_state.generated = ""
st.session_state.regenerate = False
st.write("")
st.write("")
st.write("")
st.button("Upload Another File", use_container_width=True, type='primary', on_click=generate)
st.write(" ")
st.write(" ")
st.write(" ")
st.markdown(
"""
<style>
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: black;
color: white;
text-align: center;
padding: 10px;
}
.footer img {
border-radius: 50%;
width: 50px;
height: 50px;
margin-right: 15px;
}
.footer a {
color: white;
margin: 0 10px;
text-decoration: none;
}
.footer a:hover {
color: #0000ff; /* Optional: Add a hover effect */
}
</style>
<div class="footer">
<a href="https://github.com/AdityaGupta0001" target="_blank">GitHub</a>
<a href="https://www.linkedin.com/in/aditya-gupta-475328252/" target="_blank">LinkedIn</a>
</div>
""",
unsafe_allow_html=True
)