Spaces:
Runtime error
Runtime error
Commit
·
b48f9b8
1
Parent(s):
0a3ff2c
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from random import randint
|
| 4 |
+
import time
|
| 5 |
+
import uuid
|
| 6 |
+
import argparse
|
| 7 |
+
sys.path.append(os.path.abspath("../supv"))
|
| 8 |
+
from matumizi.util import *
|
| 9 |
+
from mcclf import *
|
| 10 |
+
|
| 11 |
+
def genVisitHistory(numUsers, convRate, label):
|
| 12 |
+
for i in range(numUsers):
|
| 13 |
+
userID = genID(12)
|
| 14 |
+
userSess = []
|
| 15 |
+
userSess.append(userID)
|
| 16 |
+
|
| 17 |
+
conv = randint(0, 100)
|
| 18 |
+
if (conv < convRate):
|
| 19 |
+
#converted
|
| 20 |
+
if (label):
|
| 21 |
+
if (randint(0,100) < 90):
|
| 22 |
+
userSess.append("T")
|
| 23 |
+
else:
|
| 24 |
+
userSess.append("F")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
numSession = randint(2, 20)
|
| 28 |
+
for j in range(numSession):
|
| 29 |
+
sess = randint(0, 100)
|
| 30 |
+
if (sess <= 15):
|
| 31 |
+
elapsed = "H"
|
| 32 |
+
elif (sess > 15 and sess <= 40):
|
| 33 |
+
elapsed = "M"
|
| 34 |
+
else:
|
| 35 |
+
elapsed = "L"
|
| 36 |
+
|
| 37 |
+
sess = randint(0, 100)
|
| 38 |
+
if (sess <= 15):
|
| 39 |
+
duration = "L"
|
| 40 |
+
elif (sess > 15 and sess <= 40):
|
| 41 |
+
duration = "M"
|
| 42 |
+
else:
|
| 43 |
+
duration = "H"
|
| 44 |
+
|
| 45 |
+
sessSummary = elapsed + duration
|
| 46 |
+
userSess.append(sessSummary)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
else:
|
| 50 |
+
#not converted
|
| 51 |
+
if (label):
|
| 52 |
+
if (randint(0,100) < 90):
|
| 53 |
+
userSess.append("F")
|
| 54 |
+
else:
|
| 55 |
+
userSess.append("T")
|
| 56 |
+
|
| 57 |
+
numSession = randint(2, 12)
|
| 58 |
+
for j in range(numSession):
|
| 59 |
+
sess = randint(0, 100)
|
| 60 |
+
if (sess <= 20):
|
| 61 |
+
elapsed = "L"
|
| 62 |
+
elif (sess > 20 and sess <= 45):
|
| 63 |
+
elapsed = "M"
|
| 64 |
+
else:
|
| 65 |
+
elapsed = "H"
|
| 66 |
+
|
| 67 |
+
sess = randint(0, 100)
|
| 68 |
+
if (sess <= 20):
|
| 69 |
+
duration = "H"
|
| 70 |
+
elif (sess > 20 and sess <= 45):
|
| 71 |
+
duration = "M"
|
| 72 |
+
else:
|
| 73 |
+
duration = "L"
|
| 74 |
+
|
| 75 |
+
sessSummary = elapsed + duration
|
| 76 |
+
userSess.append(sessSummary)
|
| 77 |
+
|
| 78 |
+
print(",".join(userSess))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def main():
|
| 82 |
+
st.set_page_config(page_title="Markov Chain Classifier", page_icon=":guardsman:", layout="wide")
|
| 83 |
+
st.title("Markov Chain Classifier")
|
| 84 |
+
|
| 85 |
+
# Add sidebar
|
| 86 |
+
st.sidebar.title("Navigation")
|
| 87 |
+
app_mode = st.sidebar.selectbox("Choose the app mode",
|
| 88 |
+
["Instructions", "Generate User Visit History", "Train Model", "Predict Conversion"])
|
| 89 |
+
|
| 90 |
+
if app_mode == "Instructions":
|
| 91 |
+
st.write("Welcome to the Markov Chain Classifier app!")
|
| 92 |
+
st.write("This app allows you to generate user visit history, train a Markov Chain Classifier model, and predict conversion.")
|
| 93 |
+
st.write("To get started, use the sidebar to navigate to the desired functionality.")
|
| 94 |
+
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.")
|
| 95 |
+
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.")
|
| 96 |
+
st.write("3. **Predict Conversion**: Upload an ML config file using the file uploader, and click the 'Predict' button to make predictions with the trained model.")
|
| 97 |
+
|
| 98 |
+
elif app_mode == "Generate User Visit History":
|
| 99 |
+
st.subheader("Generate User Visit History")
|
| 100 |
+
num_users = st.number_input("Number of users", min_value=1, max_value=10000, value=100, step=1)
|
| 101 |
+
conv_rate = st.slider("Conversion rate", min_value=0, max_value=100, value=10, step=1)
|
| 102 |
+
add_label = st.checkbox("Add label", value=False)
|
| 103 |
+
if st.button("Generate"):
|
| 104 |
+
genVisitHistory(num_users, conv_rate, add_label)
|
| 105 |
+
|
| 106 |
+
elif app_mode == "Train Model":
|
| 107 |
+
st.subheader("Train Model")
|
| 108 |
+
mlf_path = st.file_uploader("Upload ML config file")
|
| 109 |
+
if st.button("Train"):
|
| 110 |
+
if mlf_path is not None:
|
| 111 |
+
model = MarkovChainClassifier(mlf_path)
|
| 112 |
+
model.train()
|
| 113 |
+
|
| 114 |
+
elif app_mode == "Predict Conversion":
|
| 115 |
+
st.subheader("Predict Conversion")
|
| 116 |
+
mlf_path = st.file_uploader("Upload ML config file")
|
| 117 |
+
if st.button("Predict"):
|
| 118 |
+
if mlf_path is not None:
|
| 119 |
+
model = MarkovChainClassifier(mlf_path)
|
| 120 |
+
model.predict()
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
main()
|