-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAPI.py
More file actions
226 lines (187 loc) · 8.4 KB
/
API.py
File metadata and controls
226 lines (187 loc) · 8.4 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
215
216
217
218
219
220
221
222
223
import streamlit as st
import numpy as np
import pandas as pd
import os
from PIL import Image
import io
import plotly.express as px
class API:
@staticmethod
def verify_api():
key = st.text_input("Enter OpenAI API key", type="password")
if key is not None:
if st.button("Verify"):
st.success("API is set")
return key
@staticmethod
def import_data():
input_csv = st.file_uploader("Upload your CSV file", type=['csv'], accept_multiple_files=False)
if input_csv is not None:
try:
df = pd.read_csv(input_csv, encoding= 'latin1')
new_col = list(df.columns)
df.columns = new_col
return df
except:
try:
df = pd.read_csv(input_csv)
new_col = list(df.columns)
df.columns = new_col
return df
except:
st.error("Please upload a valid CSV file")
@staticmethod
def import_another_data():
another_csv = st.file_uploader("Upload Another CSV file", type=['csv'], accept_multiple_files=False)
if another_csv is not None:
try:
df1 = pd.read_csv(another_csv, encoding= 'latin1')
new_col1 = list(df1.columns)
df1.columns = new_col1
return df1
except:
try:
df1 = pd.read_csv(another_csv)
new_col1 = list(df1.columns)
df1.columns = new_col1
return df1
except:
st.error("Please upload a valid CSV file")
@staticmethod
def remove_files():
folder_path = "images"
# Get a list of all files in the folder
file_names = os.listdir(folder_path)
# Remove each file
for file_name in file_names:
file_path = os.path.join(folder_path, file_name)
os.remove(file_path)
@staticmethod
def save_image(img_path):
img = np.array(Image.open(img_path))
img = Image.fromarray(img)
# Convert PIL Image to bytes
img_bytes = io.BytesIO()
img.save(img_bytes, format='PNG')
img_bytes.seek(0)
# Create a download button
st.download_button(label="Save Image", data=img_bytes, file_name='processed_image.png', mime='image/png', key=None)
@staticmethod
def save_to_original_file(df1):
if df1 is not None:
if st.button("Save To Original Dataframe"):
st.session_state["data"] = df1
@staticmethod
def plot_hist():
if st.session_state["data"] is not None:
df = st.session_state["data"]
col = list(df.select_dtypes(include=[np.number]).columns)
chosen_col = st.selectbox("Select Column", col)
# Check if a column is chosen
if chosen_col:
try:
# Create histogram using Plotly Express
bin = st.sidebar.slider("Bins", 5, 200, 10)
fig = px.histogram(df,
x=chosen_col,
y = None,
title=f"{chosen_col} histogram",
nbins=bin,
template="plotly_dark",
color_discrete_sequence=px.colors.sequential.Plasma)
# Display the plot
st.subheader(f"Histogram of {chosen_col}")
st.plotly_chart(fig)
except Exception as e:
st.error(e)
@staticmethod
def plot_bar():
if st.session_state["data"] is not None:
df = st.session_state["data"]
num_col = list(df.select_dtypes(include=[np.number]).columns)
obj_col = list(df.select_dtypes(exclude=[np.number]).columns)
chosen_num_col = st.sidebar.selectbox("Select Column", num_col)
chosen_obj_col = st.sidebar.selectbox("Select Column", obj_col)
# Check if a column is chosen
if chosen_num_col and chosen_obj_col:
try:
fig = px.bar(df,
x=chosen_obj_col,
y = chosen_num_col,
title=f"{chosen_obj_col} vs {chosen_num_col}",
template="plotly_dark",
color_continuous_scale=px.colors.sequential.Plasma)
# Display the plot
st.subheader(f"{chosen_obj_col} vs {chosen_num_col}")
st.plotly_chart(fig)
except Exception as e:
st.error(e)
@staticmethod
def plot_scatter():
if st.session_state["data"] is not None:
df = st.session_state["data"]
num_col1 = list(df.select_dtypes(include=[np.number]).columns)
num_col2 = list(df.select_dtypes(include=[np.number]).columns)
chosen_num_col1 = st.sidebar.selectbox("Select Column1", num_col1)
chosen_num_col2 = st.sidebar.selectbox("Select Column2", num_col2)
# Check if a column is chosen
if chosen_num_col1 and chosen_num_col2:
try:
fig = px.scatter(df,
x=chosen_num_col1,
y = chosen_num_col2,
title=f"{chosen_num_col1} vs {chosen_num_col2}",
template="plotly_dark",
color_continuous_scale=px.colors.sequential.Plasma)
# Display the plot
st.subheader(f"{chosen_num_col1} vs {chosen_num_col2}")
st.plotly_chart(fig)
except Exception as e:
st.error(e)
@staticmethod
def plot_scatter3D():
if st.session_state["data"] is not None:
df = st.session_state["data"]
col1 = list(df.columns)
col2 = list(df.columns)
col3 = list(df.columns)
chosen_col1 = st.sidebar.selectbox("Select Column1", col1)
chosen_col2 = st.sidebar.selectbox("Select Column2", col2)
chosen_col3 = st.sidebar.selectbox("Select Column3", col2)
# Check if a column is chosen
if chosen_col1 and chosen_col2 and chosen_col3:
try:
fig = px.scatter_3d(df,
x=chosen_col1,
y = chosen_col2,
z = chosen_col3,
title=f"{chosen_col1} vs {chosen_col2} vs {chosen_col3}",
template="plotly_dark",
color_continuous_scale=px.colors.sequential.Plasma)
# Display the plot
st.subheader(f"{chosen_col1} vs {chosen_col2} vs {chosen_col3}")
st.plotly_chart(fig)
except Exception as e:
st.error(e)
@staticmethod
def plot_line():
if st.session_state["data"] is not None:
df = st.session_state["data"]
col1 = list(df.columns)
col2 = list(df.columns)
chosen_col1 = st.sidebar.selectbox("Select Column1", col1)
chosen_col2 = st.sidebar.selectbox("Select Column2", col2)
# Check if a column is chosen
if chosen_col1 and chosen_col2:
try:
fig = px.line(df,
x=chosen_col1,
y = chosen_col2,
title=f"{chosen_col1} vs {chosen_col2}",
template="plotly_dark"
)
# Display the plot
st.subheader(f"{chosen_col1} vs {chosen_col2}")
st.plotly_chart(fig)
except Exception as e:
st.error(e)