Upload 3 files
Browse files- app.py +68 -0
- clfmodel.pkl +3 -0
- requirements.txt +46 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pickle
|
| 4 |
+
import random
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Function to load the model
|
| 8 |
+
def load_model():
|
| 9 |
+
try:
|
| 10 |
+
# Get the absolute path to the .pkl file
|
| 11 |
+
model_path = os.path.join(os.getcwd(), "clfmodel.pkl")
|
| 12 |
+
model = pickle.load(open(model_path, 'rb'))
|
| 13 |
+
st.write("Model loaded successfully.")
|
| 14 |
+
return model
|
| 15 |
+
except Exception as e:
|
| 16 |
+
st.write("Something went wrong while loading the model.")
|
| 17 |
+
st.write(e)
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
# Function to make prediction
|
| 21 |
+
@st.cache_data
|
| 22 |
+
def make_prediction(_model, features):
|
| 23 |
+
try:
|
| 24 |
+
predicted_label = _model.predict(features)
|
| 25 |
+
return predicted_label
|
| 26 |
+
except Exception as e:
|
| 27 |
+
st.write("Something went wrong while making prediction.")
|
| 28 |
+
st.write(e)
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
# Main function to run the app
|
| 32 |
+
def main():
|
| 33 |
+
st.title("Custom Grocery App Predictor in Hungary")
|
| 34 |
+
st.write("This app predicts the preferable grocery app based on user characteristics.")
|
| 35 |
+
|
| 36 |
+
# Load the model
|
| 37 |
+
clf_model = load_model()
|
| 38 |
+
|
| 39 |
+
if clf_model:
|
| 40 |
+
# Sidebar inputs
|
| 41 |
+
st.sidebar.header('User Input Features')
|
| 42 |
+
gender = st.sidebar.radio('Gender', ('Male', 'Female'))
|
| 43 |
+
education = st.sidebar.selectbox('Education Level', ('Under Diploma and Diploma', 'Associate', 'Bachelor', 'Master', 'PhD'))
|
| 44 |
+
age = st.sidebar.slider('Age', 18, 100, 30)
|
| 45 |
+
exp_online = st.sidebar.slider('Years of Online Experience', 0, 50, 5)
|
| 46 |
+
exp_app = st.sidebar.slider('Years of App Experience', 0, 20, 1)
|
| 47 |
+
|
| 48 |
+
gender_code = 1 if gender == 'Male' else 2
|
| 49 |
+
education_code = ['Under Diploma and Diploma', 'Associate', 'Bachelor', 'Master', 'PhD'].index(education) + 1
|
| 50 |
+
|
| 51 |
+
# Make prediction
|
| 52 |
+
predicted_label = make_prediction(clf_model, np.array([[gender_code, education_code, age, exp_online, exp_app]]))
|
| 53 |
+
|
| 54 |
+
if predicted_label is not None:
|
| 55 |
+
class_App_names = ['FoodPanda', 'Wolt', 'Spar', 'Tesco online', 'myLidl']
|
| 56 |
+
predicted_App_class = class_App_names[predicted_label[0] - 1]
|
| 57 |
+
|
| 58 |
+
# Generate a random color
|
| 59 |
+
random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
|
| 60 |
+
|
| 61 |
+
# Render the output with only the predicted_App_class colored and bigger font size
|
| 62 |
+
st.write(f"In Hungary, for a {gender}, with education level of {education}, age of {age}, with online experience of {exp_online} years, and shopping experience from online Apps {exp_app} years, It seems that the preferable Grocery App is: ", end="")
|
| 63 |
+
st.markdown(f"<span style='color: {random_color}; font-weight: bold; font-size: 50px'>{predicted_App_class}</span>.", unsafe_allow_html=True)
|
| 64 |
+
else:
|
| 65 |
+
st.write("Failed to load the model.")
|
| 66 |
+
|
| 67 |
+
if __name__ == '__main__':
|
| 68 |
+
main()
|
clfmodel.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ee53eb29cf25b5f58c5c296806f569a003e4f716a7cc6c5a1640551a8a252a10
|
| 3 |
+
size 1520416
|
requirements.txt
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair==5.2.0
|
| 2 |
+
attrs==23.2.0
|
| 3 |
+
blinker==1.7.0
|
| 4 |
+
cachetools==5.3.3
|
| 5 |
+
certifi==2024.2.2
|
| 6 |
+
charset-normalizer==3.3.2
|
| 7 |
+
click==8.1.7
|
| 8 |
+
colorama==0.4.6
|
| 9 |
+
gitdb==4.0.11
|
| 10 |
+
GitPython==3.1.42
|
| 11 |
+
idna==3.6
|
| 12 |
+
Jinja2==3.1.3
|
| 13 |
+
joblib==1.3.2
|
| 14 |
+
jsonschema==4.21.1
|
| 15 |
+
jsonschema-specifications==2023.12.1
|
| 16 |
+
markdown-it-py==3.0.0
|
| 17 |
+
MarkupSafe==2.1.5
|
| 18 |
+
mdurl==0.1.2
|
| 19 |
+
numpy==1.26.4
|
| 20 |
+
packaging==23.2
|
| 21 |
+
pandas==2.2.1
|
| 22 |
+
pillow==10.2.0
|
| 23 |
+
protobuf==4.25.3
|
| 24 |
+
pyarrow==15.0.2
|
| 25 |
+
pydeck==0.8.1b0
|
| 26 |
+
Pygments==2.17.2
|
| 27 |
+
python-dateutil==2.9.0.post0
|
| 28 |
+
pytz==2024.1
|
| 29 |
+
referencing==0.34.0
|
| 30 |
+
requests==2.31.0
|
| 31 |
+
rich==13.7.1
|
| 32 |
+
rpds-py==0.18.0
|
| 33 |
+
scikit-learn==1.4.1.post1
|
| 34 |
+
scipy==1.12.0
|
| 35 |
+
six==1.16.0
|
| 36 |
+
smmap==5.0.1
|
| 37 |
+
streamlit==1.32.2
|
| 38 |
+
tenacity==8.2.3
|
| 39 |
+
threadpoolctl==3.4.0
|
| 40 |
+
toml==0.10.2
|
| 41 |
+
toolz==0.12.1
|
| 42 |
+
tornado==6.4
|
| 43 |
+
typing_extensions==4.10.0
|
| 44 |
+
tzdata==2024.1
|
| 45 |
+
urllib3==2.2.1
|
| 46 |
+
watchdog==4.0.0
|