forked from waleed488/SmartRetail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1523 lines (1363 loc) · 70.8 KB
/
Copy pathapp.py
File metadata and controls
1523 lines (1363 loc) · 70.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
SmartRetail AI - Customer Intelligence Dashboard
------------------------------------------------
A fully offline Streamlit application that loads pre-trained artifacts
(.joblib / .pt / .json) and serves supervised predictions, dimensionality
projections, and reinforcement-learning marketing recommendations.
Run locally with a single command:
streamlit run app.py
UI design notes (HCI principles applied):
- Match between system & real world: plain-language labels, currency/units on every metric.
- Visibility of system status: live artifact-readiness indicators are always on screen.
- Recognition over recall: the active customer is pinned in the sidebar across every view.
- Consistency & standards: one light design-token system drives every surface.
- Aesthetic & minimalist design: generous whitespace, a restrained palette, real icons.
- Error prevention & recovery: instructive empty states instead of raw tracebacks.
- Flexibility: the required PCA/LDA toggle and customer selector are first-class controls.
This module ONLY changes presentation. All model/data logic loads from saved files
and is never retrained on launch.
"""
import os
import json
import joblib
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import streamlit as st
import torch
from typing import Dict, Any, Optional
from src.rl_agents import StateDiscretizer, QLearningAgent, DQNAgent
# -----------------------------------------------------------------------------
# PAGE CONFIG
# -----------------------------------------------------------------------------
st.set_page_config(
page_title="SmartRetail AI · Customer Intelligence",
page_icon="◆",
layout="wide",
initial_sidebar_state="expanded",
)
# -----------------------------------------------------------------------------
# PATHS
# -----------------------------------------------------------------------------
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
MODELS_DIR = os.path.join(PROJECT_ROOT, "models")
PROCESSED_DIR = os.path.join(PROJECT_ROOT, "data", "processed")
FIGURES_DIR = os.path.join(PROJECT_ROOT, "outputs", "figures")
# -----------------------------------------------------------------------------
# LOADERS (offline; artifacts are read from disk, never retrained)
# -----------------------------------------------------------------------------
@st.cache_resource
def load_estimator(filename: str) -> Optional[Any]:
path = os.path.join(MODELS_DIR, filename)
if os.path.exists(path):
return joblib.load(path)
return None
@st.cache_data
def load_dataset(filename: str) -> Optional[pd.DataFrame]:
path = os.path.join(PROCESSED_DIR, filename)
if os.path.exists(path):
df = pd.read_csv(path)
if "CustomerID" in df.columns:
df = df.set_index("CustomerID")
return df
return None
@st.cache_data
def load_json_file(path: str) -> Optional[Dict[str, Any]]:
if os.path.exists(path):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
return None
# Datasets
train_df = load_dataset("train.csv")
validation_df = load_dataset("validation.csv")
test_df = load_dataset("test.csv")
train_pca_df = load_dataset("train_pca.csv")
test_pca_df = load_dataset("test_pca.csv")
train_lda_df = load_dataset("train_lda.csv")
test_lda_df = load_dataset("test_lda.csv")
test_raw_scaled_df = load_dataset("test_raw_scaled.csv")
# Models & serialized transforms
best_classifier = load_estimator("best_classifier.joblib")
best_regressor = load_estimator("best_regressor.joblib")
scaler_raw = load_estimator("scaler.joblib")
scaler_pca = load_estimator("pca_scaler.joblib")
scaler_ns = load_estimator("non_spend_scaler.joblib")
pca_model = load_estimator("pca.joblib")
lda_model = load_estimator("lda.joblib")
# Result metadata
class_results = load_json_file(os.path.join(PROCESSED_DIR, "classification_results.json"))
reg_results = load_json_file(os.path.join(PROCESSED_DIR, "regression_results.json"))
pca_metadata = load_json_file(os.path.join(PROCESSED_DIR, "pca_metadata.json"))
metadata = load_json_file(os.path.join(PROCESSED_DIR, "metadata.json"))
rl_eval_results = load_json_file(os.path.join(PROCESSED_DIR, "rl_evaluation_results.json"))
@st.cache_resource
def load_rl_agents():
try:
dqn = DQNAgent(state_size=5, action_size=3)
dqn.load()
q_agent = QLearningAgent(state_size=8, action_size=3)
q_agent.load()
discretizer = StateDiscretizer(n_clusters=8)
discretizer.load()
return dqn, q_agent, discretizer
except Exception as e:
st.warning(f"RL agent binaries loading failed: {str(e)}")
return None, None, None
dqn_agent, q_agent, discretizer = load_rl_agents()
# =============================================================================
# DESIGN SYSTEM (single light theme)
# =============================================================================
INK = "#0f172a" # primary text
MUTED = "#64748b" # secondary text
FAINT = "#94a3b8" # tertiary text
BORDER = "#e6eaf0" # hairlines
SURFACE = "#ffffff" # cards
CANVAS = "#f5f7fb" # app background
PRIMARY = "#2563eb" # brand / actions
PRIMARY_DK = "#1d4ed8"
POSITIVE = "#059669" # high-value / good
NEGATIVE = "#dc2626" # standard / risk
AMBER = "#d97706"
GRID = "#eef1f6"
# Cohesive categorical palette for charts (no prominent purple).
CHART_COLORS = ["#2563eb", "#059669", "#d97706", "#dc2626", "#0891b2", "#64748b"]
# Marketing action reference (shared across views).
ACTIONS = ["No Action", "10% Discount Coupon", "Free Premium Trial"]
ACTION_COSTS = ["$0", "$1", "$5"]
# Icon library (Lucide-style inline SVG, stroke = currentColor). No emoji as icons.
_ICON_PATHS = {
"gauge": '<path d="M12 14 4 6"/><path d="M20.4 14.5A9 9 0 1 0 3.6 14.5"/>',
"user": '<circle cx="12" cy="8" r="4"/><path d="M6 21v-2a4 4 0 0 1 4-4h4a4 4 0 0 1 4 4v2"/>',
"chart": '<path d="M3 3v18h18"/><rect x="7" y="10" width="3" height="7"/><rect x="12" y="6" width="3" height="11"/><rect x="17" y="13" width="3" height="4"/>',
"brain": '<path d="M12 5a3 3 0 1 0-5.997.142M12 5a3 3 0 1 1 5.997.142M12 5v14M6.003 5.142A3 3 0 0 0 4 8c0 .5.1.9.3 1.3A3 3 0 0 0 5 15M18 8a3 3 0 0 0-.003-2.858M19 15a3 3 0 0 0 .7-5.7c.2-.4.3-.8.3-1.3"/>',
"database": '<ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5v14a9 3 0 0 0 18 0V5"/><path d="M3 12a9 3 0 0 0 18 0"/>',
"info": '<circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/>',
"users": '<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/>',
"layers": '<path d="m12 2 9 5-9 5-9-5 9-5Z"/><path d="m3 12 9 5 9-5"/><path d="m3 17 9 5 9-5"/>',
"cpu": '<rect x="4" y="4" width="16" height="16" rx="2"/><rect x="9" y="9" width="6" height="6"/><path d="M9 2v2M15 2v2M9 20v2M15 20v2M2 9h2M2 15h2M20 9h2M20 15h2"/>',
"trending": '<polyline points="22 7 13.5 15.5 8.5 10.5 2 17"/><polyline points="16 7 22 7 22 13"/>',
"target": '<circle cx="12" cy="12" r="10"/><circle cx="12" cy="12" r="6"/><circle cx="12" cy="12" r="2"/>',
"activity": '<path d="M22 12h-4l-3 9L9 3l-3 9H2"/>',
"clock": '<circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/>',
"repeat": '<path d="m17 2 4 4-4 4"/><path d="M3 11v-1a4 4 0 0 1 4-4h14"/><path d="m7 22-4-4 4-4"/><path d="M21 13v1a4 4 0 0 1-4 4H3"/>',
"dollar": '<line x1="12" y1="2" x2="12" y2="22"/><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/>',
"grid": '<rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/>',
"shield": '<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10Z"/>',
"sparkles": '<path d="m12 3 1.9 5.8L20 10.7l-6.1 1.9L12 18l-1.9-5.4L4 10.7l6.1-1.9L12 3Z"/>',
"check": '<path d="M20 6 9 17l-5-5"/>',
"alert": '<circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/>',
"flag": '<path d="M4 15s1-1 4-1 5 2 8 2 4-1 4-1V3s-1 1-4 1-5-2-8-2-4 1-4 1Z"/><line x1="4" y1="22" x2="4" y2="15"/>',
"route": '<circle cx="6" cy="19" r="3"/><path d="M9 19h8.5a3.5 3.5 0 0 0 0-7h-11a3.5 3.5 0 0 1 0-7H15"/><circle cx="18" cy="5" r="3"/>',
"compass": '<circle cx="12" cy="12" r="10"/><polygon points="16.24 7.76 14.12 14.12 7.76 16.24 9.88 9.88 16.24 7.76"/>',
"tag": '<path d="M12.586 2.586A2 2 0 0 0 11.172 2H4a2 2 0 0 0-2 2v7.172a2 2 0 0 0 .586 1.414l8 8a2 2 0 0 0 2.828 0l7.172-7.172a2 2 0 0 0 0-2.828z"/><circle cx="7.5" cy="7.5" r="1"/>',
"book": '<path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/>',
}
def icon(name: str, size: int = 18) -> str:
body = _ICON_PATHS.get(name, _ICON_PATHS["info"])
return (
f'<svg width="{size}" height="{size}" viewBox="0 0 24 24" fill="none" '
f'stroke="currentColor" stroke-width="2" stroke-linecap="round" '
f'stroke-linejoin="round" style="display:block">{body}</svg>'
)
# -----------------------------------------------------------------------------
# GLOBAL STYLES
# -----------------------------------------------------------------------------
st.markdown(
f"""
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Plus+Jakarta+Sans:wght@600;700;800&display=swap" rel="stylesheet">
<style>
:root {{
--ink:{INK}; --muted:{MUTED}; --faint:{FAINT};
--border:{BORDER}; --surface:{SURFACE}; --canvas:{CANVAS};
--primary:{PRIMARY}; --primary-dk:{PRIMARY_DK};
--positive:{POSITIVE}; --negative:{NEGATIVE}; --amber:{AMBER};
--radius:16px;
--shadow:0 1px 2px rgba(15,23,42,.04), 0 8px 24px rgba(15,23,42,.05);
--font-body:'Inter',system-ui,sans-serif;
--font-head:'Plus Jakarta Sans','Inter',sans-serif;
}}
html, body, [class*="css"], .stMarkdown, input, textarea, button {{
font-family: var(--font-body) !important;
}}
.stApp {{ background: var(--canvas) !important; }}
.block-container {{ padding-top: 0.2rem !important; padding-bottom: 3rem; max-width: 1360px; }}
.main .block-container {{ padding-top: 0.2rem !important; }}
#MainMenu, footer {{ visibility: hidden; }}
.stAppDeployButton {{ display: none; }}
/* Hide header ONLY on desktop to allow the hamburger menu on mobile */
@media (min-width: 768px) {{
header[data-testid="stHeader"] {{ display:none !important; }}
}}
/* On mobile, collapse the native header bar itself (so it takes no visual
space at the top and never overlaps the sticky tab bar), but pull the
sidebar-toggle button out of it and float it as a round button pinned
to the bottom-left of the screen instead. */
@media (max-width: 767px) {{
/* Neutralize any transformed ancestor Streamlit may use for page-load
/ transition animations — a transform on an ancestor would make our
fixed-position button anchor to THAT box instead of the real screen,
which is why it can appear stuck at the top instead of floating. */
[data-testid="stAppViewContainer"],
[data-testid="stApp"],
[data-testid="stMain"],
[data-testid="stToolbar"],
.stApp,
section.main {{
transform: none !important;
}}
header[data-testid="stHeader"] {{
position: static !important;
background: transparent !important;
box-shadow: none !important;
height: 0 !important;
min-height: 0 !important;
overflow: visible !important;
transform: none !important;
}}
[data-testid="stExpandSidebarButton"] {{
position: fixed !important;
top: auto !important;
bottom: 20px !important;
left: 20px !important;
right: auto !important;
inset: auto auto 20px 20px !important;
transform: none !important;
z-index: 999999 !important;
width: 46px !important;
height: 46px !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
background: var(--primary) !important;
border-radius: 999px !important;
box-shadow: 0 8px 20px rgba(37,99,235,.35) !important;
}}
[data-testid="stExpandSidebarButton"] span,
[data-testid="stExpandSidebarButton"] svg {{
color: #fff !important;
fill: #fff !important;
}}
}}
section[data-testid="stSidebar"] > div:first-child {{ padding-top: 0.2rem !important; overflow: hidden !important; }}
section[data-testid="stSidebar"] .block-container {{ padding-top: 0.2rem !important; padding-bottom: 0.2rem !important; }}
.stApp h1,.stApp h2,.stApp h3,.stApp h4 {{ font-family: var(--font-head) !important; color:var(--ink); }}
.stApp p,.stApp label,.stApp span,.stApp li,.stApp small {{ color: var(--ink); }}
/* ------------------------------------------------------ SIDEBAR */
section[data-testid="stSidebar"] {{
background: var(--surface) !important;
border-right: 1px solid var(--border) !important;
}}
section[data-testid="stSidebar"] > div {{ padding: 1.2rem 1.1rem; }}
/* Lock sidebar open and hide collapse controls ONLY on desktop */
@media (min-width: 768px) {{
section[data-testid="stSidebar"] {{
min-width: 300px !important; max-width: 300px !important; width: 300px !important;
transform: none !important; visibility: visible !important;
}}
[data-testid="stSidebarCollapseButton"],
[data-testid="stSidebarCollapsedControl"],
[data-testid="collapsedControl"],
[data-testid="stSidebarResizeHandle"] {{ display:none !important; }}
}}
.brand {{ display:flex; align-items:center; gap:12px; padding:16px 0 16px 0;
margin-bottom:6px; border-bottom:1px solid var(--border); }}
.brand-mark {{ width:42px; height:42px; border-radius:12px; flex:0 0 auto;
display:flex; align-items:center; justify-content:center; font-size:1.15rem;
background: linear-gradient(140deg,#2563eb,#1d4ed8); color:#fff;
box-shadow: 0 6px 18px rgba(37,99,235,.30); }}
.brand-name {{ font-family:var(--font-head); font-size:1.1rem; font-weight:800;
color:var(--ink); letter-spacing:-0.02em; line-height:1.1; }}
.brand-sub {{ font-size:0.66rem; color:var(--faint); text-transform:uppercase;
letter-spacing:.14em; font-weight:700; margin-top:3px; }}
.rail-label {{ font-size:0.66rem; font-weight:800; text-transform:uppercase;
letter-spacing:.13em; color:var(--faint); margin:22px 2px 9px 2px;
display:flex; align-items:center; gap:7px; }}
.rail-label svg {{ color:var(--primary); }}
/* status list */
.status-box {{ background:var(--canvas); border:1px solid var(--border);
border-radius:12px; padding:6px 12px; }}
.status-row {{ display:flex; align-items:center; justify-content:space-between; padding:8px 0;
border-bottom:1px solid var(--border); }}
.status-row:last-child {{ border-bottom:none; }}
.status-name {{ font-size:0.83rem; color:var(--muted); font-weight:500; }}
.status-pill {{ display:inline-flex; align-items:center; gap:5px; font-size:0.7rem;
font-weight:700; padding:3px 9px; border-radius:999px; }}
.status-pill.ok {{ background:rgba(5,150,105,.10); color:var(--positive); }}
.status-pill.ok svg {{ color:var(--positive); }}
.status-pill.off {{ background:rgba(220,38,38,.10); color:var(--negative); }}
.status-pill.off svg {{ color:var(--negative); }}
/* active-customer card in sidebar */
.cust-card {{ background:linear-gradient(140deg,#1e3a8a,#2563eb); border-radius:14px;
padding:16px; color:#fff; box-shadow:0 8px 22px rgba(37,99,235,.28); }}
.cust-card .cc-k {{ font-size:0.66rem; text-transform:uppercase; letter-spacing:.12em;
font-weight:700; color:#bfdbfe; }}
.cust-card .cc-v {{ font-family:var(--font-head); font-size:1.6rem; font-weight:800; margin-top:2px; }}
.cust-card .cc-s {{ font-size:0.74rem; color:#dbeafe; margin-top:4px; }}
.sb-help {{ font-size:0.78rem; color:var(--muted); line-height:1.55; }}
/* ------------------------------------------------------ TAB NAVIGATION */
div[data-testid="stTabs"] {{ position:relative; }}
div[data-testid="stTabs"] div[data-baseweb="tab-list"] {{
gap:4px; background:var(--surface); border:1px solid var(--border);
border-radius:14px; padding:6px;
position:sticky; top:8px; z-index:1000;
box-shadow:0 0 0 8px var(--canvas), var(--shadow);
}}
div[data-testid="stTabs"] button[data-baseweb="tab"] {{
height:auto; padding:9px 16px; border-radius:10px; background:transparent;
color:var(--muted) !important; font-weight:600; font-size:0.9rem;
}}
div[data-testid="stTabs"] button[data-baseweb="tab"]:hover {{ background:var(--canvas); }}
div[data-testid="stTabs"] button[data-baseweb="tab"][aria-selected="true"] {{
background:var(--primary); color:#fff !important; box-shadow:0 4px 12px rgba(37,99,235,.28);
}}
div[data-testid="stTabs"] div[data-baseweb="tab-highlight"],
div[data-testid="stTabs"] div[data-baseweb="tab-border"] {{ display:none; }}
div[data-testid="stTabs"] div[data-baseweb="tab-panel"] {{ padding-top:22px; }}
/* ------------------------------------------------------ PAGE HEADER */
.page-head {{ margin-bottom:18px; }}
.page-title {{ font-family:var(--font-head); font-size:1.5rem; font-weight:800;
letter-spacing:-0.02em; line-height:1.15; color:var(--ink); }}
.page-sub {{ font-size:0.92rem; color:var(--muted); margin-top:4px; max-width:80ch; }}
/* ------------------------------------------------------ HERO */
.hero {{ position:relative; overflow:hidden; border-radius:20px; padding:34px 36px;
margin-bottom:22px; color:#fff;
background:radial-gradient(120% 140% at 0% 0%, #1e3a8a 0%, #172554 55%, #0f172a 100%);
border:1px solid rgba(255,255,255,.06); }}
.hero .eyebrow {{ display:inline-flex; align-items:center; gap:8px; font-size:0.72rem;
font-weight:700; text-transform:uppercase; letter-spacing:.16em; color:#93c5fd;
background:rgba(147,197,253,.12); padding:5px 12px; border-radius:999px; margin-bottom:14px; }}
.hero .hero-title {{ color:#f8fafc; font-family:var(--font-head);
font-size:2.05rem; font-weight:800;
margin:0 0 10px 0; letter-spacing:-0.025em; line-height:1.12; }}
.hero p {{ color:#cbd5e1; font-size:0.96rem; margin:0; line-height:1.65; max-width:72ch; }}
/* ------------------------------------------------------ STAT CARDS */
.stat {{ background:var(--surface); border:1px solid var(--border); border-radius:var(--radius);
padding:18px 20px; box-shadow:var(--shadow); height:100%;
transition:transform .16s ease, box-shadow .16s ease; }}
.stat:hover {{ transform:translateY(-3px); box-shadow:0 12px 28px rgba(15,23,42,.10); }}
.stat-top {{ display:flex; align-items:center; justify-content:space-between; margin-bottom:14px; }}
.stat-label {{ font-size:0.72rem; text-transform:uppercase; letter-spacing:.08em;
color:var(--muted); font-weight:700; }}
.stat-ic {{ width:36px; height:36px; border-radius:10px; flex:0 0 auto;
display:flex; align-items:center; justify-content:center; }}
.stat-ic.blue{{background:rgba(37,99,235,.10);color:#2563eb;}}
.stat-ic.teal{{background:rgba(8,145,178,.10);color:#0891b2;}}
.stat-ic.green{{background:rgba(5,150,105,.10);color:#059669;}}
.stat-ic.amber{{background:rgba(217,119,6,.12);color:#d97706;}}
.stat-ic.rose{{background:rgba(220,38,38,.10);color:#dc2626;}}
.stat-ic.slate{{background:rgba(100,116,139,.12);color:#475569;}}
.stat-value {{ font-family:var(--font-head); font-size:1.6rem; font-weight:800;
line-height:1.1; color:var(--ink); letter-spacing:-0.02em; }}
.stat-sub {{ font-size:0.78rem; color:var(--muted); margin-top:7px; line-height:1.45; }}
/* ------------------------------------------------------ SECTION TITLE */
.sec {{ display:flex; align-items:center; gap:11px; margin:6px 0 14px 0; }}
.sec-ic {{ width:34px; height:34px; border-radius:10px; flex:0 0 auto; display:flex;
align-items:center; justify-content:center; background:rgba(37,99,235,.10); color:var(--primary); }}
.sec-title {{ font-family:var(--font-head); font-size:1.12rem; font-weight:700;
letter-spacing:-0.01em; color:var(--ink); }}
.sec-desc {{ font-size:0.84rem; color:var(--muted); margin-top:2px; }}
/* container(border=True) -> panel language */
div[data-testid="stVerticalBlockBorderWrapper"] > div:first-child {{
background:var(--surface) !important; border:1px solid var(--border) !important;
border-radius:var(--radius) !important; padding:22px !important; box-shadow:var(--shadow) !important;
}}
.panel-title {{ font-family:var(--font-head); font-weight:700; font-size:1rem;
color:var(--ink); margin-bottom:14px; display:flex; align-items:center; gap:9px; }}
.panel-title svg {{ color:var(--primary); }}
.plot-card {{ border:1px solid var(--border); border-radius:var(--radius); padding:12px;
background:var(--surface); box-shadow:var(--shadow); margin-bottom:14px; }}
/* ------------------------------------------------------ RESULT PANEL */
.result {{ border:1px solid var(--border); border-radius:14px; padding:18px; margin-bottom:14px;
background:var(--canvas); }}
.result .r-label {{ font-size:0.72rem; text-transform:uppercase; letter-spacing:.07em;
color:var(--muted); font-weight:700; display:flex; align-items:center; gap:7px; }}
.result .r-value {{ font-family:var(--font-head); font-size:1.55rem; font-weight:800;
line-height:1.15; margin-top:8px; }}
.result .r-meta {{ font-size:0.78rem; color:var(--muted); margin-top:6px; }}
.conf-track {{ height:9px; border-radius:999px; background:#e5e9f0; overflow:hidden; margin-top:12px; }}
.conf-fill {{ height:100%; border-radius:999px; transition:width .4s ease; }}
.seg-tag {{ display:inline-flex; align-items:center; gap:6px; font-size:0.72rem; font-weight:700;
padding:3px 10px; border-radius:999px; }}
/* ------------------------------------------------------ ACTION ROWS (RL) */
.act {{ display:flex; align-items:center; gap:14px; border:1px solid var(--border);
border-radius:13px; padding:14px 16px; margin-bottom:10px; background:var(--surface);
transition:border-color .15s ease; }}
.act.rec {{ border:1.5px solid var(--primary); background:rgba(37,99,235,.05);
box-shadow:0 6px 18px rgba(37,99,235,.12); }}
.act-num {{ width:30px; height:30px; border-radius:9px; flex:0 0 auto; display:flex;
align-items:center; justify-content:center; font-weight:800; font-size:0.85rem;
background:var(--canvas); color:var(--muted); font-family:var(--font-head); }}
.act.rec .act-num {{ background:var(--primary); color:#fff; }}
.act-body {{ flex:1; min-width:0; }}
.act-name {{ font-weight:700; font-size:0.95rem; color:var(--ink); display:flex;
align-items:center; gap:9px; flex-wrap:wrap; }}
.act-cost {{ font-size:0.76rem; color:var(--muted); margin-top:2px; }}
.act-q {{ text-align:right; flex:0 0 auto; }}
.act-q .q-val {{ font-family:var(--font-head); font-weight:800; font-size:1.1rem; color:var(--ink); }}
.act-q .q-cap {{ font-size:0.66rem; text-transform:uppercase; letter-spacing:.06em; color:var(--faint); font-weight:700; }}
.rec-badge {{ display:inline-flex; align-items:center; gap:5px; font-size:0.66rem; font-weight:800;
text-transform:uppercase; letter-spacing:.06em; color:#fff; background:var(--primary);
padding:3px 9px; border-radius:999px; }}
.qbar-track {{ height:6px; border-radius:999px; background:#e5e9f0; overflow:hidden; margin-top:9px; }}
.qbar-fill {{ height:100%; border-radius:999px; background:var(--faint); }}
.act.rec .qbar-fill {{ background:var(--primary); }}
/* ------------------------------------------------------ CALLOUT / BADGE / EMPTY */
.callout {{ border:1px solid var(--border); border-left:4px solid var(--primary);
background:var(--canvas); border-radius:12px; padding:16px 18px; }}
.callout .co-label {{ font-size:0.68rem; font-weight:800; text-transform:uppercase;
letter-spacing:.09em; color:var(--primary); }}
.callout .co-value {{ font-family:var(--font-head); font-size:1.1rem; font-weight:800; margin-top:5px; }}
.callout .co-body {{ font-size:0.9rem; color:var(--ink); margin-top:9px; line-height:1.55; }}
.badge {{ background:var(--surface); color:var(--ink); border:1px solid var(--border);
padding:7px 14px; border-radius:999px; font-size:.8rem; font-weight:600;
display:inline-flex; align-items:center; gap:6px; margin:4px 8px 4px 0; box-shadow:var(--shadow); }}
.empty {{ text-align:center; padding:44px 24px; background:var(--surface);
border:1px dashed var(--border); border-radius:var(--radius); }}
.empty-ic {{ width:52px; height:52px; border-radius:14px; margin:0 auto 14px auto; display:flex;
align-items:center; justify-content:center; background:rgba(217,119,6,.12); color:var(--amber); }}
.empty h4 {{ font-family:var(--font-head); font-weight:700; font-size:1.05rem; margin:0; color:var(--ink); }}
.empty p {{ color:var(--muted); font-size:0.9rem; margin-top:8px; line-height:1.55; }}
/* timeline */
.timeline {{ border-left:2px solid var(--border); padding-left:22px; margin-left:8px; }}
.timeline-item {{ margin-bottom:22px; position:relative; }}
.timeline-item::before {{ content:''; position:absolute; left:-29px; top:3px;
background:var(--primary); border:3px solid var(--surface); border-radius:50%;
width:13px; height:13px; box-shadow:0 0 0 1px var(--border); }}
.timeline-title {{ font-family:var(--font-head); font-weight:700; color:var(--ink); margin-bottom:3px; }}
.timeline-body {{ font-size:0.88rem; color:var(--muted); line-height:1.55; }}
/* ------------------------------------------------------ TABLES - FIXED */
div[data-testid="stTable"] {{
background: var(--surface) !important;
border-radius: var(--radius) !important;
overflow: hidden !important;
border: 1px solid var(--border) !important;
}}
div[data-testid="stTable"] table {{
width: 100% !important;
border-collapse: collapse !important;
background: var(--surface) !important;
}}
div[data-testid="stTable"] thead tr th {{
background: var(--canvas) !important;
color: var(--muted) !important;
border-bottom: 2px solid var(--border) !important;
padding: 12px 14px !important;
font-size: 0.72rem !important;
font-weight: 700 !important;
text-transform: uppercase !important;
letter-spacing: .05em !important;
text-align: left !important;
}}
div[data-testid="stTable"] tbody tr td {{
border-bottom: 1px solid var(--border) !important;
padding: 11px 14px !important;
color: var(--ink) !important;
font-size: 0.875rem !important;
background: var(--surface) !important;
}}
div[data-testid="stTable"] tbody tr:last-child td {{
border-bottom: none !important;
}}
div[data-testid="stTable"] tbody tr:hover td {{
background: var(--canvas) !important;
}}
/* ------------------------------------------------------ EXPANDER */
div[data-testid="stExpander"] {{ border:1px solid var(--border) !important;
background:var(--surface) !important; border-radius:14px !important;
box-shadow:var(--shadow); overflow:hidden; }}
div[data-testid="stExpander"] details {{ background:var(--surface) !important; }}
div[data-testid="stExpander"] summary {{ background:var(--canvas) !important;
color:var(--ink) !important; font-weight:600; padding:12px 16px !important; }}
div[data-testid="stExpander"] summary:hover {{ color:var(--primary) !important; }}
div[data-testid="stExpander"] summary svg {{ fill:var(--muted) !important; }}
div[data-testid="stExpander"] summary p {{ color:var(--ink) !important; font-weight:600 !important; }}
div[data-testid="stExpander"] div[data-testid="stExpanderDetails"] {{
background:var(--surface) !important; padding:16px !important; }}
/* ------------------------------------------------------ DATAFRAME - FIXED */
div[data-testid="stDataFrame"] {{
border: 1px solid var(--border) !important;
border-radius: var(--radius) !important;
overflow: hidden !important;
background: var(--surface) !important;
}}
div[data-testid="stDataFrame"] * {{
color: var(--ink) !important;
}}
div[data-testid="stDataFrame"] table {{
background: var(--surface) !important;
}}
div[data-testid="stDataFrame"] thead th {{
background: var(--canvas) !important;
color: var(--muted) !important;
border-bottom: 2px solid var(--border) !important;
}}
div[data-testid="stDataFrame"] tbody td {{
background: var(--surface) !important;
border-bottom: 1px solid var(--border) !important;
color: var(--ink) !important;
}}
div[data-testid="stDataFrame"] tbody tr:hover td {{
background: var(--canvas) !important;
}}
/* ------------------------------------------------------ SELECT BOX */
div[data-baseweb="select"] > div {{
background: var(--surface) !important;
border-color: var(--border) !important;
border-radius: 10px !important;
color: var(--ink) !important;
}}
div[data-baseweb="select"] input {{
color: var(--ink) !important;
}}
div[data-baseweb="select"] * {{
color: var(--ink) !important;
}}
section[data-testid="stSidebar"] div[data-baseweb="select"] > div {{
background: var(--canvas) !important;
}}
/* ------------------------------------------------------ POPOVER */
div[data-baseweb="popover"] div[role="listbox"],
div[data-baseweb="popover"] ul[role="listbox"],
ul[data-baseweb="menu"] {{
background: var(--surface) !important;
border: 1px solid var(--border) !important;
border-radius: 12px !important;
box-shadow: 0 10px 30px rgba(15,23,42,.14) !important;
}}
div[data-baseweb="popover"] li,
ul[data-baseweb="menu"] li {{
color: var(--ink) !important;
background: var(--surface) !important;
}}
div[data-baseweb="popover"] li:hover,
ul[data-baseweb="menu"] li:hover,
div[data-baseweb="popover"] li[aria-selected="true"],
ul[data-baseweb="menu"] li[aria-selected="true"] {{
background: var(--canvas) !important;
}}
/* Remove the logo spacer that's creating the gap */
[data-testid="stSidebarHeader"] {{
display: none !important;
height: 0 !important;
padding: 0 !important;
margin: 0 !important;
}}
/* Fix for long URLs on mobile - wrap text */
.stMarkdown a {{
word-break: break-all !important;
overflow-wrap: break-word !important;
max-width: 100% !important;
display: inline-block !important;
}}
@media (max-width: 768px) {{
.stMarkdown a {{
word-break: break-all !important;
overflow-wrap: break-word !important;
max-width: 100% !important;
display: inline-block !important;
}}
div[data-testid="stVerticalBlockBorderWrapper"] > div:first-child {{
padding: 12px !important;
overflow: hidden !important;
}}
}}
</style>
""",
unsafe_allow_html=True,
)
# -----------------------------------------------------------------------------
# UI HELPERS
# -----------------------------------------------------------------------------
def page_header(title: str, subtitle: str) -> None:
st.markdown(
f"<div class='page-head'><div class='page-title'>{title}</div>"
f"<div class='page-sub'>{subtitle}</div></div>",
unsafe_allow_html=True,
)
def section_title(text: str, icon_name: str = "activity", desc: str = "") -> None:
d = f"<div class='sec-desc'>{desc}</div>" if desc else ""
st.markdown(
f"<div class='sec'><div class='sec-ic'>{icon(icon_name, 18)}</div>"
f"<div><div class='sec-title'>{text}</div>{d}</div></div>",
unsafe_allow_html=True,
)
def stat_card(label: str, value: str, sub: str = "", accent: str = "blue", icon_name: str = "activity") -> None:
s = f"<div class='stat-sub'>{sub}</div>" if sub else ""
st.markdown(
f"""
<div class="stat">
<div class="stat-top">
<div class="stat-label">{label}</div>
<div class="stat-ic {accent}">{icon(icon_name, 18)}</div>
</div>
<div class="stat-value">{value}</div>
{s}
</div>
""",
unsafe_allow_html=True,
)
def panel_title(text: str, icon_name: str = "activity") -> None:
st.markdown(f"<div class='panel-title'>{icon(icon_name, 17)}{text}</div>", unsafe_allow_html=True)
def empty_state(title: str, body: str, icon_name: str = "alert") -> None:
st.markdown(
f"""
<div class="empty">
<div class="empty-ic">{icon(icon_name, 26)}</div>
<h4>{title}</h4>
<p>{body}</p>
</div>
""",
unsafe_allow_html=True,
)
def style_plot(fig, height: int = 340, legend_title: Optional[str] = None):
fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
height=height,
margin=dict(l=70, r=90, t=34, b=58),
font=dict(color=INK, family="Inter", size=12),
legend=dict(font=dict(color=INK)),
xaxis=dict(gridcolor=GRID, zerolinecolor=GRID, linecolor=BORDER, automargin=True),
yaxis=dict(gridcolor=GRID, zerolinecolor=GRID, linecolor=BORDER, automargin=True),
colorway=CHART_COLORS,
title=None,
)
if legend_title is not None:
fig.update_layout(legend_title_text=legend_title)
return fig
def vspace(h: int = 8) -> None:
st.markdown(f"<div style='height:{h}px'></div>", unsafe_allow_html=True)
# =============================================================================
# SIDEBAR — BRAND · CUSTOMER SELECTOR · SYSTEM STATUS
# =============================================================================
st.sidebar.markdown(
"""
<div class="brand">
<div class="brand-mark">◆</div>
<div>
<div class="brand-name">SmartRetail AI</div>
<div class="brand-sub">Customer Intelligence</div>
</div>
</div>
""",
unsafe_allow_html=True,
)
# --- Customer selector (required GUI component) ---
st.sidebar.markdown(
f"<div class='rail-label'>{icon('user', 13)}Customer Selector</div>", unsafe_allow_html=True
)
if test_df is not None:
customer_ids = sorted([int(cid) for cid in test_df.index.tolist()])
selected_customer_id = st.sidebar.selectbox(
"Customer ID (test split)",
customer_ids,
label_visibility="collapsed",
help="Choose a Customer ID from the held-out test split to analyze.",
)
st.sidebar.markdown(
f"""
<div class="cust-card">
<div class="cc-k">Active customer</div>
<div class="cc-v">#{selected_customer_id}</div>
<div class="cc-s">Source · held-out test split</div>
</div>
""",
unsafe_allow_html=True,
)
else:
st.sidebar.warning("Datasets not loaded. Run preprocessing first.")
selected_customer_id = None
# --- System status (visibility of system status) ---
st.sidebar.markdown(
f"<div class='rail-label'>{icon('activity', 13)}System Status</div>", unsafe_allow_html=True
)
def _status_row(label: str, ok: bool) -> str:
cls = "ok" if ok else "off"
dot = "check" if ok else "alert"
state = "Ready" if ok else "Offline"
return (
f"<div class='status-row'><span class='status-name'>{label}</span>"
f"<span class='status-pill {cls}'>{icon(dot, 12)}{state}</span></div>"
)
data_ok = test_df is not None
models_ok = (best_classifier is not None) and (best_regressor is not None)
rl_ok = (dqn_agent is not None) and (q_agent is not None) and (discretizer is not None)
st.sidebar.markdown(
"<div class='status-box'>"
+ _status_row("Datasets", data_ok)
+ _status_row("Supervised models", models_ok)
+ _status_row("RL agents", rl_ok)
+ "</div>",
unsafe_allow_html=True,
)
st.sidebar.markdown(
f"<div class='rail-label'>{icon('book', 13)}How to use</div>", unsafe_allow_html=True
)
st.sidebar.markdown(
"<div class='sb-help'>Pick a Customer ID above, then open the "
"<strong>Customer Intelligence</strong> tab to view its loyalty prediction, "
"spend forecast, and the recommended marketing action.</div>",
unsafe_allow_html=True,
)
# =============================================================================
# MAIN — TAB NAVIGATION
# =============================================================================
tab_overview, tab_customer, tab_proj, tab_models, tab_data, tab_about = st.tabs(
[
"Overview",
"Customer Intelligence",
"PCA / LDA Projections",
"Model Performance",
"Dataset Explorer",
"About",
]
)
# -----------------------------------------------------------------------------
# TAB: OVERVIEW
# -----------------------------------------------------------------------------
with tab_overview:
st.markdown(
"""
<div class="hero">
<div class="eyebrow">Customer Loyalty Engine</div>
<div class="hero-title">SmartRetail AI Intelligence Platform</div>
<p>An offline analytics platform that scores customer loyalty, forecasts future spend,
and applies reinforcement-learning policies to recommend the most profitable marketing
action for every customer — all served from pre-trained model artifacts.</p>
</div>
""",
unsafe_allow_html=True,
)
total_customers = 0
if metadata:
total_customers = (
metadata.get("train_customers_count", 0)
+ metadata.get("val_customers_count", 0)
+ metadata.get("test_customers_count", 0)
)
best_classifier_name = (
class_results.get("best_model_selected", "N/A").replace("_", " ") if class_results else "N/A"
)
best_regressor_name = (
reg_results.get("best_model_selected", "N/A").replace("_", " ") if reg_results else "N/A"
)
c1, c2, c3, c4 = st.columns(4)
with c1:
stat_card("Total Profiles", f"{total_customers:,}", "Customers across all splits", "blue", "users")
with c2:
stat_card("Feature Space", "13 Dimensions", "Behavioral + category spend", "teal", "layers")
with c3:
stat_card("Best Classifier", best_classifier_name, "Top validation F1-score", "amber", "cpu")
with c4:
stat_card("Best Regressor", best_regressor_name, "Top validation R²-score", "green", "trending")
vspace(20)
# ---- REQUIRED: cumulative-profit summary chart (RL policy vs baselines) ----
section_title(
"Cumulative Policy Net Profit",
"route",
"Net profit each policy would have earned on the held-out test set — RL policies vs. baselines.",
)
col_l, col_r = st.columns([3, 2])
with col_l:
with st.container(border=True):
panel_title("Policy Comparison · Test Set", "chart")
if rl_eval_results:
profits_data = pd.DataFrame(
{
"Policy": ["Always No-Action", "Random Action", "Tabular Q-Learning", "DQN Policy"],
"Net Profit ($)": [
rl_eval_results.get("Always_No_Action", 0),
rl_eval_results.get("Random_Action", 0),
rl_eval_results.get("Tabular_Q_Policy", 0),
rl_eval_results.get("DQN_Policy", 0),
],
"Kind": ["Baseline", "Baseline", "RL Policy", "RL Policy"],
}
)
profits_data = profits_data.sort_values("Net Profit ($)")
fig = px.bar(
profits_data,
x="Net Profit ($)",
y="Policy",
orientation="h",
color="Policy",
color_discrete_map={
"Always No-Action": "#dc2626",
"Random Action": "#d97706",
"Tabular Q-Learning": "#0891b2",
"DQN Policy": "#2563eb",
},
text="Net Profit ($)",
)
style_plot(fig, height=360)
fig.update_layout(showlegend=False, yaxis_title="", xaxis_title="Net Profit ($)")
fig.update_traces(
texttemplate="$%{text:,.0f}", textposition="outside",
textfont=dict(color=INK),
cliponaxis=False,
)
st.plotly_chart(fig, width="stretch", theme=None)
else:
empty_state(
"RL evaluation results not found",
"Run train_rl.py to generate rl_evaluation_results.json, then reload.",
)
with col_r:
with st.container(border=True):
panel_title("Segment Policy Playbook", "compass")
st.markdown(
"""
**High-Value Loyalty Segment**
The DQN agent prefers **Free Premium Trial** (cost $5).
This segment responds with a 2× spend multiplier, yielding strong positive net profit.
**Medium-Value Repeat Segment**
The agent prefers a **10% Discount Coupon** (cost $1),
stimulating purchase frequency without exhausting the marketing budget.
**At-Risk / Low-Value Segment**
The agent selects **No Action** (cost $0),
preserving capital for higher-value customers.
"""
)
# -----------------------------------------------------------------------------
# TAB: CUSTOMER INTELLIGENCE (predictions + RL recommendation)
# -----------------------------------------------------------------------------
with tab_customer:
page_header(
"Customer Intelligence",
"Supervised predictions and the reinforcement-learning marketing recommendation for the selected customer.",
)
if selected_customer_id is None:
empty_state(
"No customer selected",
"Choose a Customer ID from the sidebar to inspect a profile and generate recommendations.",
)
else:
cust_row = test_df.loc[selected_customer_id]
# --- profile snapshot ---
p1, p2, p3, p4 = st.columns(4)
with p1:
stat_card("Recency", f"{int(cust_row['Recency'])} days", "Since last transaction", "blue", "clock")
with p2:
stat_card("Frequency", f"{int(cust_row['Frequency'])} orders", "Unique transactions", "teal", "repeat")
with p3:
stat_card("Monetary", f"${cust_row['Monetary']:,.2f}", "Spend · Months 1–9", "green", "dollar")
with p4:
stat_card("Diversity", f"{int(cust_row['ProductDiversity'])} items", "Distinct products bought", "amber", "grid")
vspace(18)
# ---- REQUIRED: classification + regression result panel ----
section_title(
"Supervised Predictions",
"cpu",
"Loyalty classification (label + confidence) and future-spend regression (prediction + RMSE).",
)
pred_l, pred_r = st.columns(2)
# classification
with pred_l:
with st.container(border=True):
panel_title("Loyalty Classification", "target")
best_rep = class_results.get("best_model_representation", "raw_scaled") if class_results else "raw_scaled"
if best_rep == "raw_scaled" and test_raw_scaled_df is not None:
features = [c for c in test_raw_scaled_df.columns if c not in ["High_Value_Customer", "Future_Spend"]]
X = test_raw_scaled_df.loc[selected_customer_id][features].values.reshape(1, -1)
elif best_rep == "pca" and test_pca_df is not None:
features = [c for c in test_pca_df.columns if c not in ["High_Value_Customer", "Future_Spend"]]
X = test_pca_df.loc[selected_customer_id][features].values.reshape(1, -1)
elif best_rep == "lda" and test_lda_df is not None:
features = [c for c in test_lda_df.columns if c not in ["High_Value_Customer", "Future_Spend"]]
X = test_lda_df.loc[selected_customer_id][features].values.reshape(1, -1)
else:
X = None
if X is not None and best_classifier is not None: