AlignAI commited on
Commit
c0a4a7d
Β·
verified Β·
1 Parent(s): 8d3494c

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +27 -18
src/streamlit_app.py CHANGED
@@ -3,7 +3,7 @@ import joblib
3
  import numpy as np
4
  import plotly.graph_objects as go
5
 
6
- # 1. Page Configuration (Must be the first command)
7
  st.set_page_config(
8
  page_title="Purchase Intention AI",
9
  page_icon="πŸ›οΈ",
@@ -12,8 +12,14 @@ st.set_page_config(
12
  )
13
 
14
  # Custom CSS for styling
 
15
  st.markdown("""
16
  <style>
 
 
 
 
 
17
  .stButton>button {
18
  width: 100%;
19
  background-color: #FF4B4B;
@@ -22,17 +28,22 @@ st.markdown("""
22
  padding: 0.5rem;
23
  border-radius: 10px;
24
  }
 
 
25
  .main-header {
26
  font-size: 2.5rem;
27
  font-weight: 700;
28
- color: #333;
29
  text-align: center;
30
- margin-bottom: 2rem;
31
  }
 
 
32
  .sub-text {
33
  text-align: center;
34
- color: #666;
35
  font-size: 1.1rem;
 
36
  }
37
  </style>
38
  """, unsafe_allow_html=True)
@@ -40,16 +51,15 @@ st.markdown("""
40
  # 2. Load Model
41
  @st.cache_resource
42
  def load_model():
43
- # Handles both the pipeline (smart) version and separate files version
44
  try:
45
  # Try loading the smart pipeline first
46
- model = joblib.load('src/svm_model.pkl')
47
- return model, None # No separate scaler needed
48
  except:
49
  try:
50
  # Fallback to separate files
51
- model = joblib.load('src/svm_model.pkl')
52
- scaler = joblib.load('src/scaler.pkl')
53
  return model, scaler
54
  except FileNotFoundError:
55
  return None, None
@@ -60,7 +70,7 @@ if model is None:
60
  st.error("🚨 Model files not found! Please run `train_model.py` first.")
61
  st.stop()
62
 
63
- # 3. Header Section
64
  st.markdown('<div class="main-header">πŸ›οΈ Purchase Intention Predictor</div>', unsafe_allow_html=True)
65
  st.markdown('<p class="sub-text">Adjust the psychometric drivers in the sidebar to predict user behavior.</p>', unsafe_allow_html=True)
66
  st.markdown("---")
@@ -95,17 +105,16 @@ input_values = np.array([[att, sns, pbc, eo, ec]])
95
  if scaler:
96
  final_input = scaler.transform(input_values)
97
  else:
98
- final_input = input_values # Pipeline handles scaling internally
99
 
100
- # Real-time Prediction (or on button click)
101
  prediction = model.predict(final_input)[0]
102
- # Clip prediction to 1-7 range for visuals
103
  prediction = max(1.0, min(7.0, prediction))
104
 
105
  with col1:
106
  st.subheader("πŸ“Š Prediction Result")
107
 
108
- # Gauge Chart for PI
109
  fig_gauge = go.Figure(go.Indicator(
110
  mode = "gauge+number",
111
  value = prediction,
@@ -131,16 +140,16 @@ with col1:
131
 
132
  # Text Interpretation
133
  if prediction >= 5.5:
134
- st.success("**High Probability**: User is very likely to purchase.")
135
  elif prediction >= 3.5:
136
- st.info("**Moderate Probability**: User is undecided.")
137
  else:
138
- st.warning("**Low Probability**: User is unlikely to purchase.")
139
 
140
  with col2:
141
  st.subheader("πŸ•ΈοΈ User Profile Analysis")
142
 
143
- # Radar Chart for Inputs
144
  categories = ['Attitude', 'Social Norms', 'Control', 'Outcome', 'Concern']
145
  r_values = [att, sns, pbc, eo, ec]
146
 
 
3
  import numpy as np
4
  import plotly.graph_objects as go
5
 
6
+ # 1. Page Configuration
7
  st.set_page_config(
8
  page_title="Purchase Intention AI",
9
  page_icon="πŸ›οΈ",
 
12
  )
13
 
14
  # Custom CSS for styling
15
+ # UPDATED: Added 'color: white;' to .main-header and .sub-text
16
  st.markdown("""
17
  <style>
18
+ /* Main background dark mode adjustment (optional, for contrast) */
19
+ .stApp {
20
+ background-color: #0E1117;
21
+ }
22
+
23
  .stButton>button {
24
  width: 100%;
25
  background-color: #FF4B4B;
 
28
  padding: 0.5rem;
29
  border-radius: 10px;
30
  }
31
+
32
+ /* TARGET 1: Main Title */
33
  .main-header {
34
  font-size: 2.5rem;
35
  font-weight: 700;
36
+ color: white !important; /* Forced white color */
37
  text-align: center;
38
+ margin-bottom: 1rem;
39
  }
40
+
41
+ /* TARGET 2: Sub-header text */
42
  .sub-text {
43
  text-align: center;
44
+ color: white !important; /* Forced white color */
45
  font-size: 1.1rem;
46
+ margin-bottom: 2rem;
47
  }
48
  </style>
49
  """, unsafe_allow_html=True)
 
51
  # 2. Load Model
52
  @st.cache_resource
53
  def load_model():
 
54
  try:
55
  # Try loading the smart pipeline first
56
+ model = joblib.load('svm_pipeline_model.pkl')
57
+ return model, None
58
  except:
59
  try:
60
  # Fallback to separate files
61
+ model = joblib.load('svm_model.pkl')
62
+ scaler = joblib.load('scaler.pkl')
63
  return model, scaler
64
  except FileNotFoundError:
65
  return None, None
 
70
  st.error("🚨 Model files not found! Please run `train_model.py` first.")
71
  st.stop()
72
 
73
+ # 3. Header Section (Applies the CSS classes defined above)
74
  st.markdown('<div class="main-header">πŸ›οΈ Purchase Intention Predictor</div>', unsafe_allow_html=True)
75
  st.markdown('<p class="sub-text">Adjust the psychometric drivers in the sidebar to predict user behavior.</p>', unsafe_allow_html=True)
76
  st.markdown("---")
 
105
  if scaler:
106
  final_input = scaler.transform(input_values)
107
  else:
108
+ final_input = input_values
109
 
110
+ # Real-time Prediction
111
  prediction = model.predict(final_input)[0]
 
112
  prediction = max(1.0, min(7.0, prediction))
113
 
114
  with col1:
115
  st.subheader("πŸ“Š Prediction Result")
116
 
117
+ # Gauge Chart
118
  fig_gauge = go.Figure(go.Indicator(
119
  mode = "gauge+number",
120
  value = prediction,
 
140
 
141
  # Text Interpretation
142
  if prediction >= 5.5:
143
+ st.success("High Probability: User is likely to purchase.")
144
  elif prediction >= 3.5:
145
+ st.info("Moderate Probability: User is undecided.")
146
  else:
147
+ st.warning("Low Probability: User is unlikely to purchase.")
148
 
149
  with col2:
150
  st.subheader("πŸ•ΈοΈ User Profile Analysis")
151
 
152
+ # Radar Chart
153
  categories = ['Attitude', 'Social Norms', 'Control', 'Outcome', 'Concern']
154
  r_values = [att, sns, pbc, eo, ec]
155