Spaces:
Runtime error
Runtime error
| # import os | |
| # import sys | |
| # from random import randint | |
| # import time | |
| # import uuid | |
| # import argparse | |
| # import streamlit as st | |
| # sys.path.append(os.path.abspath("../supv")) | |
| # from matumizi.util import * | |
| # from mcclf import * | |
| import os | |
| import sys | |
| from random import randint | |
| import time | |
| import uuid | |
| import argparse | |
| import pandas as pd | |
| import streamlit as st | |
| # Add the directory containing the required modules to sys.path | |
| sys.path.append(os.path.abspath("../supv")) | |
| from matumizi.util import * | |
| from mcclf import * | |
| 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) | |
| print(",".join(userSess)) | |
| # def trainModel(mlfpath): | |
| # model = MarkovChainClassifier(mlfpath) | |
| # model.train() | |
| # def predictModel(mlfpath): | |
| # model = MarkovChainClassifier(mlfpath) | |
| # model.predict() | |
| def trainModel(mlfpath): | |
| model = MarkovChainClassifier(mlfpath) | |
| model.train() | |
| return model | |
| def predictModel(mlfpath, userID): | |
| model = MarkovChainClassifier(mlfpath) | |
| res = model.predict(userID) | |
| return res | |
| if op == "Predict": | |
| st.write("Enter the parameters to make a prediction:") | |
| userID = st.text_input("User ID") | |
| st.write("Click the button below to make a prediction") | |
| if st.button("Predict"): | |
| prediction = predictModel(mlfpath, userID) | |
| st.write("Prediction:", prediction) | |
| # if __name__ == "__main__": | |
| # st.title("Conversion Prediction App") | |
| # st.write("Welcome to the Conversion Prediction App. This app uses a Markov chain based classifier to predict whether a customer will convert or not based on their visit history.") | |
| # op = st.sidebar.selectbox("Select Operation", ["Generate Visit History", "Train Model", "Predict"]) | |
| # if op == "Generate Visit History": | |
| # st.write("Enter the parameters to generate the visit history:") | |
| # numUsers = st.number_input("Number of users", min_value=1, max_value=1000, value=100, step=1) | |
| # convRate = st.number_input("Conversion Rate (in percentage)", min_value=0, max_value=100, value=10, step=1) | |
| # label = st.checkbox("Add Labels") | |
| # st.write("Click the button below to generate the visit history") | |
| # if st.button("Generate"): | |
| # genVisitHistory(numUsers, convRate, label) | |
| # elif op == "Train Model": | |
| # st.write("Train the model using the following parameters:") | |
| # mlfpath = st.text_input("MLF Path") | |
| # if st.button("Train"): | |
| # trainModel(mlfpath) | |
| # elif op == "Predict": | |
| # st.write("Predict using the trained model:") | |
| # mlfpath = st.text_input("MLF Path") | |
| # userID = st.text_input("User ID") | |
| # if st.button("Predict"): | |
| # result = predictModel(mlfpath, userID) | |
| # st.write("Prediction Result: ", result) | |
| # def main(): | |
| # st.title("Markov Chain Classifier") | |
| # # Add input fields for command line arguments | |
| # op = st.selectbox("Operation", ["gen", "train", "pred"]) | |
| # numUsers = st.slider("Number of Users", 1, 1000, 100) | |
| # convRate = st.slider("Conversion Rate", 1, 100, 10) | |
| # label = st.checkbox("Add Label") | |
| # mlfpath = st.text_input("ML Config File Path", value="false") | |
| # # Call functions based on selected operation | |
| # if op == "gen": | |
| # st.button("Generate Visit History", on_click=lambda: genVisitHistory(numUsers, convRate, label)) | |
| # elif op == "train": | |
| # st.button("Train Model", on_click=lambda: trainModel(mlfpath)) | |
| # elif op == "pred": | |
| # st.button("Predict Model", on_click=lambda: predictModel(mlfpath)) | |
| # if __name__ == "__main__": | |
| # main() |