File size: 8,583 Bytes
248e491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import os
from intro import Intro  # importing function from python file from same directory


st.set_page_config(
    page_title="Automated ML",
    page_icon="🤖",
    layout="wide",
    initial_sidebar_state="expanded",
)


page_bg_img_link = f"""
<style>
[data-testid="stAppViewContainer"]> .main{{

background-color: #FFDEE9;
background-image: linear-gradient(0deg,  #B5FFFC 0%, #FFDEE9 100%);




}}


[data-testid="stHeader"]{{
background-color: rgba(0,0,0,0)

}}

[data-testid="stToolbar"]{{
right : 2 rem;
}}

[data-testid="stSidebar"] > div:first-child{{


background: linear-gradient(to right bottom,
                 rgba(285,205,205,0.7),
                 rgba(285,205,205,0.3));
}}


</style>
"""
st.markdown(page_bg_img_link, unsafe_allow_html=True)


# Import profiling capability
from ydata_profiling import profile_report
from streamlit_pandas_profiling import st_profile_report

# ML Pycaret Modules
from pycaret.classification import (
    setup as classification_setup,
    compare_models as classification_compare_model,
    pull as classification_pull,
    predict_model as classification_predict_model,
    save_model as classification_save_model,
)

from pycaret.regression import (
    setup as regression_setup,
    compare_models as regression_compare_models,
    pull as regression_pull,
    predict_model as regression_predict_model,
    save_model as regression_save_model,
)

from pycaret.clustering import (
    setup as clustering_steup,
    pull as clustering_pull,
    create_model as clustering_create_model,
    predict_model as clustering_predict_model,
    plot_model as clustering_plot_model,
    save_model as clustering_save_model,
)

from pycaret.anomaly import (
    setup as anomaly_setup,
    pull as anomaly_pull,
    create_model as anomaly_create_model,
    models as anomaly_models,
    plot_model as anomaly_plot_model,
    assign_model as anomaly_assign_model,
    save_model as anomaly_save_model,
)


def main():
    st.sidebar.image("https://www.onepointltd.com/wp-content/uploads/2020/03/inno2.png")
    st.sidebar.title("AutoStreamML")
    choice = st.sidebar.radio(
        "Navigation", ["Instruction", "Upload", "Profiling", "ML", "Download"]
    )
    st.sidebar.info(
        "This Application allows us to build an automated ML Pipeline using Streamlit, Pandas and PyCaret. And it's magic!"
    )

    if os.path.exists("Sourcedata.csv"):
        df = pd.read_csv("Sourcedata.csv", index_col=None)
    else:
        df = None

    ## Section1 - Instruction ##
    if choice == "Instruction":
        Intro()

    ## Section2 - Upload ##
    elif choice == "Upload":
        upload_section(df)

    ## Section3 - Profiling dataset ##
    elif choice == "Profiling":
        profiling_section(df)

    ## Section4 - Machine Learning ##
    elif choice == "ML":
        ml_section(df)

    ## Section5 - Download pipeline ##
    elif choice == "Download":
        download_section()


## Section2 - Upload ##
def upload_section(df):
    st.title("Upload Your Data for Modelling!")
    file = st.file_uploader("Upload Your Dataset Here")
    if file:
        df = pd.read_csv(file, index_col=None)
        df.to_csv("Sourcedata.csv", index=None)
        st.dataframe(df)


## Section3 - Profiling dataset ##
def profiling_section(df):
    st.title("Automated Exploratory Data Analysis")
    if df is not None:
        profile_report = df.profile_report()
        st_profile_report(profile_report)
    else:
        st.info("Upload a dataset for profiling.")


## Section4 - Machine Learning ##
def ml_section(df):
    st.title("Machine Learning Go 🤘🤘🤘")
    model = st.radio(
        "Select Your Model",
        [
            "Classification",
            "Regression",
            "Clustering",
            "Anomaly Detection",
        ],
    )

    if model == "Classification":
        classification_subsection(df)
    elif model == "Regression":
        regression_subsection(df)
    elif model == "Clustering":
        clustering_subsection(df)
    elif model == "Anomaly Detection":
        AnomalyDetection_subsection(df)

    # Add other model subsections here


## for CLASSIFICATION
def classification_subsection(df):
    st.subheader("Classification")
    chosen_target = st.selectbox("Select Your Target for Classification", df.columns)
    if st.button("Train Model"):
        st.info("This is dataset")
        st.dataframe(df.head())

        classification_setup(df, target=chosen_target)
        setup_df = classification_pull()
        st.info("This is ML Experiment Settings ")
        st.dataframe(setup_df)

        best_model = classification_compare_model()
        compare_df = classification_pull()
        st.info("This is the ML Model")
        st.dataframe(compare_df)

        predict = classification_predict_model(best_model)
        compare_df = classification_pull()
        st.info("This is predicted value of best Model")
        st.dataframe(predict)

        classification_save_model(best_model, "best_model")


## for Regression
def regression_subsection(df):
    st.subheader("Regression")
    chosen_target = st.selectbox("Select Your Target for Regresssion", df.columns)
    if st.button("Train Model"):
        st.info("This is dataset")
        st.dataframe(df.head())

        regression_setup(df, target=chosen_target)
        setup_df = regression_pull()
        st.info("This is ML Experiment Settings ")
        st.dataframe(setup_df)

        best_model = regression_compare_models()
        compare_df = regression_pull()
        st.info("This is the ML Model")
        st.dataframe(compare_df)

        predict = regression_predict_model(best_model)
        st.info("This is predicted value of best Model")
        st.dataframe(predict)

        regression_save_model(best_model, "best_model")


## for Clustering
def clustering_subsection(df):
    st.subheader("Clustering")
    st.info("We are using KMean Clustering Method : ")
    if st.button("Train Model"):
        st.info("This is dataset")
        st.dataframe(df.head())
        clustering_steup(df)
        setup_df = clustering_pull()
        st.info("This is ML Experiment Settings ")
        st.dataframe(setup_df)

        kmeans = clustering_create_model("kmeans")
        created_model_df = clustering_pull()
        st.info("This is the ML Kmean Clustering Model")
        st.dataframe(created_model_df)

        predict = clustering_predict_model(kmeans, data=df)
        # predict_df = clustering_pull()
        st.info(
            "generates cluster labels using a trained model to Kmean Clustering Model"
        )
        st.dataframe(predict)
        clustering_save_model(kmeans, "best_model")

        plot_mod = clustering_plot_model(kmeans)
        plot_mod


## for  Anomaly Detection
def AnomalyDetection_subsection(df):
    st.subheader("Anomaly Detection")
    method = st.selectbox(
        "Select Your method",
        [
            "iforest",
            "knn",
            "svm",
            "abod",
            "cluster",
        ],
    )
    if st.button("Train Model"):
        st.info("This is dataset")
        st.dataframe(df.head())
        anomaly_setup(df, session_id=123)
        setup_df = anomaly_pull()
        st.info("This is ML Experiment Settings ")
        st.dataframe(setup_df)

        iforest = anomaly_create_model(method)  # we use iforest model
        models = anomaly_models()
        st.info("We use these models for anomaly detection ! ")
        st.dataframe(models)

        st.info(
            "predict anomaly labels to the dataset for a given model. (1 = outlier, 0 = inlier)"
        )
        predictions = anomaly_assign_model(iforest)
        st.dataframe(predictions)

        anomaly_save_model(iforest, "best_model")

        plot_mod = anomaly_plot_model(iforest)
        plot_mod


## Section5 - Download pipeline ##


def download_section():
    st.title("Download trained pipeline!")
    with open("best_model.pkl", "rb") as file:
        st.download_button("Download", file, "trained_model.pkl")

    st.markdown("""""")
    st.markdown("""""")
    st.markdown("""""")
    st.markdown("""""")
    st.markdown("""""")
    st.markdown("""""")
    st.markdown("""""")
    st.markdown("""""")
    st.markdown("""""")

    with st.container():
        col = st.columns([1])
        # HARSH #
        with col[0]:
            st.write(
                "This app was created by [Harsh Narayan](https://portfolio-7bwl.onrender.com/)"
            )


if __name__ == "__main__":
    main()