#!/usr/bin/env python
# coding: utf-8
# In[1]:
import streamlit as st
from PIL import Image
import pandas as pd
import numpy as np
import catboost
import random
#from streamlit_js_eval import streamlit_js_eval
# Create two columns
col1, col2 = st.columns([1, 3]) # Adjust the ratio as needed
# Load and display the logo image in the first column
with col1:
image_path = "niq.png" # Update this path if your image is in a different directory
st.image(image_path, width=150) # Adjust the width as needed
# Set the title of the app in the second column
with col2:
st.title("Segmentation Tool")
st.sidebar.title("Welcome to the Dollar General Segmentation Tool!")
st.sidebar.info(
"""
**Please follow the instructions below to contribute to our research:**
- On the right side, you will encounter a series of statements.
- **Carefully read each statement** and use the dropdowns and sliders to select the option that best describes your preferences or behaviors.
- Your thoughtful responses are crucial for the accuracy of our segmentation model.
- The information you provide will be used to enhance our understanding of different customer segments.
**Thank you for participating in our research. Your input is invaluable!**
"""
)
st.markdown("
Demographics
", unsafe_allow_html=True)
# In[ ]:
# Add statement for Gender
st.write("**Gender**")
gender_display_options = ["Male", "Female", "Other", "Prefer not to disclose"]
gender_encoding = {"Male": 1, "Female": 2, "Other": 3, "Prefer not to disclose": 4}
selected_gender_display = st.selectbox("Select your gender:", gender_display_options)
selected_gender_encoded = gender_encoding[selected_gender_display]
# Add statement for Age
st.write("**Age**")
age_display_options = ["18-34", "35-44", "45-54", "55-64", "65 and above"]
age_encoding = {"18-34": 3, "35-44": 4, "45-54": 5, "55-64": 6, "65 and above": 7}
selected_age_display = st.selectbox("Select your age range:", age_display_options)
selected_age_encoded = age_encoding[selected_age_display]
# In[ ]:
# Add a heading for Shopping Behaviour section with highlighted color
st.markdown("Shopping Behaviour
", unsafe_allow_html=True)
# In[ ]:
# First statement with dropdown options
statement1 = "Which of the following best describes how well you know the prices of the household items you buy regularly?"
statement1_options = [
"I know the prices of the household items I buy regularly and always notice when the prices change",
"I know the prices of some of the items I buy regularly and usually notice when the prices change",
"I generally know about how much I pay for things, but I don’t pay much attention to how much the products I buy cost or when prices change",
"Convenience is more important to me than lower prices"
]
statement1_encoding = {
"I know the prices of the household items I buy regularly and always notice when the prices change": 1,
"I know the prices of some of the items I buy regularly and usually notice when the prices change": 2,
"I generally know about how much I pay for things, but I don’t pay much attention to how much the products I buy cost or when prices change": 3,
"Convenience is more important to me than lower prices": 4
}
selected_statement1_display = st.selectbox(f"**{statement1}**", statement1_options)
# Save the encoding for the selected statement1 option
selected_statement1_encoded = statement1_encoding[selected_statement1_display]
# In[ ]:
# Second statement with dropdown options
statement2 = "How much did you spend when visiting any Dollar General store in the past month in total?"
statement2_options = ["$10 or less", "$11-$30", "$31-$70", "$71-$200", "Over $200","I have not shopped in the past month"]
statement2_encoding = {
"$10 or less": 1,
"$11-$30": 2,
"$31-$70": 3,
"$71-$200": 4,
"Over $200": 5,
"I have not shopped in the past month":1
}
selected_statement2_display = st.selectbox(f"**{statement2}**", statement2_options)
# Save the encoding for the selected statement2 option
selected_statement2_encoded = statement2_encoding[selected_statement2_display]
# In[ ]:
#Third statement with dropdown options
statement3 = "On a typical shopping trip to Dollar General, how many items do you purchase?"
statement3_options = ["1-2 items", "3-4 items", "5-6 items", "7-8 items", "More than 8 items"]
statement3_encoding = {
"1-2 items": 1,
"3-4 items": 2,
"5-6 items": 3,
"7-8 items": 4,
"More than 8 items": 5
}
selected_statement3_display = st.selectbox(f"**{statement3}**", statement3_options)
# Save the encoding for the selected statement3 option
selected_statement3_encoded = statement3_encoding[selected_statement3_display]
# In[ ]:
#Fourth statement with dropdown options
statement4 = "How often do you go shopping at any Dollar General?"
statement4_options = ["1-2 times a year", "3-5 times a year", "6-11 times a year", "Once a month", "2-3 times a month", "4 or more times a month"]
statement4_encoding = {
"1-2 times a year": 1,
"3-5 times a year": 2,
"6-11 times a year": 3,
"Once a month": 4,
"2-3 times a month": 5,
"4 or more times a month": 6
}
selected_statement4_display = st.selectbox(f"**{statement4}**", statement4_options)
# Save the encoding for the selected statement4 option
selected_statement4_encoded = statement4_encoding[selected_statement4_display]
# Add a heading for Shopping Habit section with highlighted color
st.markdown("Shopping Habit
", unsafe_allow_html=True)
st.write("**If you were to shop for household items, how would you shop? Please select where on the scale you feel best describes you.**")
# Create sliders with descriptive statements
sliders = [
("I always buy well-known brands", "I don’t care much about brands"),
("Promotions / sales rarely change my brand choices", "I buy different brands because of promotions / sales"),
("Often, I am stressed while shopping", "I find shopping enjoyable"),
("I feel shopping is fun" , "I feel shopping is a tedious task"),
("I like to take my time and browse when shopping", "I don’t like spending unnecessary time when shopping"),
("I use apps while shopping", "I do not use apps while shopping"),
("I end up purchasing a lot of things that I didn’t intend to", "I am very disciplined when I shop and only get what I intended to buy"),
("I know prices of household items very well", "I do not pay attention to the price of household items"),
("I know exactly what items to buy before I get to the store", "I tend to make most of my shopping decisions when I’m in the store")
]
#slider_responses = {}
#for idx, (left_text, right_text) in enumerate(sliders):
# cols = st.columns([1, 2, 1]) # Define columns with the desired width ratio
# with cols[0]:
# st.write(left_text) # Right-side statement
# with cols[1]:
# slider_key = f"slider_{idx}"
# slider_responses[(left_text, right_text)] = st.slider(
# "",
# min_value=1,
# max_value=5,
# value=3,
# format="%d",
# key=slider_key
# )
# with cols[2]:
# st.write(right_text) # Left-side statement
#import streamlit as st
# Custom function to display a slider without showing its value
def slider_without_value(label, min_value, max_value, value, key):
# Create a slider and capture its value
selected_value = st.slider(label, min_value, max_value, value, format="", key=key)
# Return the selected value without displaying it
return selected_value
slider_responses = {}
for idx, (left_text, right_text) in enumerate(sliders):
cols = st.columns([1, 2, 1]) # Define columns with the desired width ratio
with cols[0]:
st.write(left_text) # Left-side statement
with cols[1]:
slider_key = f"slider_{idx}"
slider_responses[(left_text, right_text)] = slider_without_value(
"", 1, 5, 3, key=slider_key
)
with cols[2]:
st.write(right_text) # Right-side statement
# Collect responses for each statement
responses = {
"SC2": selected_gender_encoded,
"SC3a": selected_age_encoded,
"PR2a": selected_statement1_encoded,
"SH1": slider_responses[("I always buy well-known brands", "I don’t care much about brands")],
"SH2": slider_responses[("Promotions / sales rarely change my brand choices", "I buy different brands because of promotions / sales")],
"SH3": slider_responses[("Often, I am stressed while shopping", "I find shopping enjoyable")],
"SH4":slider_responses[("I feel shopping is fun" , "I feel shopping is a tedious task")],
"SH5": slider_responses[("I like to take my time and browse when shopping", "I don’t like spending unnecessary time when shopping")],
"SH6": slider_responses[("I use apps while shopping", "I do not use apps while shopping")],
"SH7": slider_responses[("I end up purchasing a lot of things that I didn’t intend to", "I am very disciplined when I shop and only get what I intended to buy")],
"SH8": slider_responses[("I know prices of household items very well", "I do not pay attention to the price of household items")],
"SH9": slider_responses[("I know exactly what items to buy before I get to the store", "I tend to make most of my shopping decisions when I’m in the store")],
"Q21": selected_statement2_encoded,
"Q25": selected_statement3_encoded,
"Q26": selected_statement4_encoded
}
df=pd.DataFrame([responses])
#st.write(df)
# Load the saved model
#import pickle
#model_path = 'Trained_model.pickle'
#with open(model_path, 'rb') as model_file:
# model = pickle.load(model_file)
label_mapping = {
1: "Stacey",
2: "Dana",
3: "Marge",
4: "Carl",
5: "Ivy",
6: "Sue",
7: "Cora",
8: "Strangers"
}
# Make prediction for demo purposes
if st.button('Submit'):
# Choose a random key from label_mapping
random_key = random.choice(list(label_mapping.keys()))
random_label = label_mapping[random_key]
#if st.button('Submit'):
# prediction_numeric = model.predict(df)[0]
# prediction_numeric=prediction_numeric+1
# Convert numpy array to int if it's a single value array
# if isinstance(prediction_numeric, np.ndarray) and prediction_numeric.size == 1:
# prediction_numeric = int(prediction_numeric)
# predicted_label = label_mapping.get(prediction_numeric, "Unknown")
# Assuming 'predicted_label' is defined and holds the prediction result
# Create two columns
col1, col2 = st.columns(2)
# Use the first column to display the statement with a border
with col1:
st.markdown("Assigned Statement:
", unsafe_allow_html=True)
# Use the second column to display the label aligned to the right with a border
with col2:
st.markdown(f"{random_label}
", unsafe_allow_html=True)
# Add prediction to the DataFrame
#df['Assgined_Segment'] = predicted_label