File size: 7,961 Bytes
b48f9b8
 
 
 
 
 
 
 
 
e4e04a8
b48f9b8
0ba77fc
b48f9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e5d933
b48f9b8
 
 
0ba77fc
0e18814
b48f9b8
0ba77fc
 
 
 
 
b48f9b8
 
0ba77fc
 
b48f9b8
c1dad72
 
0ba77fc
b2a5542
c1dad72
 
0ba77fc
f5024c9
b48f9b8
1e3662f
 
 
718c6e2
1e3662f
8f853c4
b48f9b8
 
 
 
 
 
 
 
0ba77fc
 
 
 
 
 
 
b48f9b8
 
 
662ab56
 
ff57fdc
662ab56
 
 
 
 
 
 
 
 
21c7c35
662ab56
21c7c35
 
662ab56
 
 
 
0ba77fc
662ab56
 
 
 
 
0ba77fc
662ab56
 
0ba77fc
662ab56
 
2b0a296
662ab56
 
2b0a296
662ab56
 
 
 
1bd9250
662ab56
 
 
 
 
 
b48f9b8
 
b2a5542
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
import os
import sys
from random import randint
import time
import uuid
import argparse
sys.path.append(os.path.abspath("../supv"))
from matumizi.util import *
from mcclf import *
import streamlit as st


def  genVisitHistory(numUsers, convRate, label):
    for i in range(numUsers):
        userID = genID(12)
        userSess = []
        userSess.append(userID)

        conv = randint(0, 100)
        if (conv < convRate):
            #converted
            if (label):
                if (randint(0,100) < 90):
                    userSess.append("T")
                else:
                    userSess.append("F")


            numSession = randint(2, 20)
            for j in range(numSession):
                sess = randint(0, 100)
                if (sess <= 15):
                    elapsed = "H"
                elif (sess > 15 and sess <= 40):
                    elapsed = "M"
                else:
                    elapsed = "L"

                sess = randint(0, 100)
                if (sess <= 15):
                    duration = "L"
                elif (sess > 15 and sess <= 40):
                    duration = "M"
                else:
                    duration = "H"

                sessSummary = elapsed + duration
                userSess.append(sessSummary)


        else:
            #not converted
            if (label):
                if (randint(0,100) < 90):
                    userSess.append("F")
                else:
                    userSess.append("T")

            numSession = randint(2, 12)
            for j in range(numSession):
                sess = randint(0, 100)
                if (sess <= 20):
                    elapsed = "L"
                elif (sess > 20 and sess <= 45):
                    elapsed = "M"
                else:
                    elapsed = "H"

                sess = randint(0, 100)
                if (sess <= 20):
                    duration = "H"
                elif (sess > 20 and sess <= 45):
                    duration = "M"
                else:
                    duration = "L"

                sessSummary = elapsed + duration
                userSess.append(sessSummary)

        st.write(",".join(userSess))


def main():
    st.set_page_config(page_title="Customer Conversion Prediction", page_icon=":guardsman:", layout="wide")
    st.title("Customer Conversion Prediction")

    # # Add sidebar
    # st.sidebar.title("Navigation")
    # app_mode = st.sidebar.selectbox("Choose the app mode",
    #     ["Instructions", "Generate User Visit History", "Train Model", "Predict Conversion"])

    # Add sidebar
    st.sidebar.title("Navigation")
    app_mode = st.sidebar.selectbox("Choose the App Mode",
        ["Instructions", "Generate User Visit History", "Predict Conversion"])

    if app_mode == "Instructions":
        st.write("Welcome to the Markov Chain Classifier app!")
        # st.write("This app allows you to generate user visit history, train a Markov Chain Classifier model, and predict conversion.")
        st.write("This app allows you to generate user visit history and predict conversion of the visitor into customer.")
        st.write("To get started, use the sidebar to navigate to the desired functionality.")
        st.write("1. **Generate User Visit History**: Select the number of users and conversion rate, and click the 'Generate' button to generate user visit history.")
        # st.write("2. **Train Model**: Upload an ML config file using the file uploader, and click the 'Train' button to train the Markov Chain Classifier model.")
        st.write("2. **Predict Conversion**: Enter the User ID for which you want to predict, and click the 'Predict' button to make predictions whether the visitor will likely to convert into customer or not.")

        # Description of MarkovChainClassifier
        mcclf_description = "The MarkovChainClassifier is a Machine Learning Classifier that utilizes the concept of Markov chains for prediction. Markov chains are mathematical models that represent a system where the future state of the system depends only on its current state, and not on the previous states. The MarkovChainClassifier uses this concept to make predictions by modeling the transition probabilities between different states or categories in the input data. It captures the probabilistic relationships between variables and uses them to classify new data points into one or more predefined categories. The MarkovChainClassifier can be useful in scenarios where the data has a sequential or time-dependent structure, and the relationships between variables can be modeled as Markov chains. It can be applied to various tasks, such as text classification, speech recognition, recommendation systems, and financial forecasting."
        # Display the description in Streamlit app
        st.header("Model description:")
        st.write(mcclf_description)
        
    elif app_mode == "Generate User Visit History":
        st.subheader("Generate User Visit History")
        num_users = st.number_input("Number of users", min_value=1, max_value=10000, value=100, step=1)
        conv_rate = st.slider("Conversion rate", min_value=0, max_value=100, value=10, step=1)
        add_label = st.checkbox("Add label", value=False)
        if st.button("Generate"):
            genVisitHistory(num_users, conv_rate, add_label)

    # elif app_mode == "Train Model":
    #     st.subheader("Train Model")
    #     mlf_path = st.file_uploader("Upload ML config file")
    #     if st.button("Train"):
    #         if mlf_path is not None:
    #             model = MarkovChainClassifier(mlf_path)
    #             model.train()

    elif app_mode == "Predict Conversion":
        st.subheader("Predict Conversion")
    
        # Create an instance of MarkovChainClassifier with the ML config file
        model = MarkovChainClassifier("mcclf_cc.properties")
    
        # Get user input for userID
        user_id = st.text_input("Enter User ID")
        
        # Check if the "Predict" button was clicked
        if st.button("Predict"):
            
        # Call the predict method of the MarkovChainClassifier instance
            pred = model.predict()
            if pred == 'F':
                st.write(f"UserID: {user_id}, Prediction: Visitor is unlikely to convert into a customer.")
            else:
                st.write(f"UserID: {user_id}, Prediction: Visitor is likely to convert into a customer.")
            
        # st.subheader("Predict Conversion")
        # # Upload ML config file using Streamlit's file_uploader function
        # mlf_file = st.file_uploader("Upload ML config file", type=["properties"])

        # # Check if ML config file was uploaded
        # if mlf_file is not None:
        #     # Save the uploaded file to a local file
        #     with open("mcclf_cc.properties", "wb") as f:
        #         f.write(mlf_file.read())

        #     # Create an instance of MarkovChainClassifier with the uploaded ML config file
        #     model = MarkovChainClassifier("mcclf_cc.properties")

        #     # # Load the model from cc.mod
        #     # model = MarkovChainClassifier.load_model("cc.mod")

        #     # Get user input for userID
        #     user_id = st.text_input("Enter User ID")
            
        #     # Check if the "Predict" button was clicked
        #     if st.button("Predict"):
        #         # Load the saved model
        #         # model.load_model("cc.mod")
                
        #         # Call the predict method of the MarkovChainClassifier instance
        #         pred = model.predict()
        #         if pred == 'T':
        #             st.write(f"UserID: {user_id}, Prediction: Visitor is likely to convert into a customer.")
        #         else:
        #             st.write(f"UserID: {user_id}, Prediction: Visitor is unlikely to convert into a customer.")

if __name__ == "__main__":
    main()