File size: 59,753 Bytes
e806184
 
 
 
baa5f11
e806184
baa5f11
 
 
 
 
 
 
 
e806184
baa5f11
 
 
 
 
 
 
 
 
 
 
e806184
 
 
baa5f11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e806184
 
 
 
 
baa5f11
 
 
 
 
 
 
 
e806184
5d67075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
baa5f11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e806184
 
 
 
 
 
72a027e
 
 
434d1a1
d1ec49f
baa5f11
d1ec49f
 
baa5f11
d1ec49f
 
72a027e
d1ec49f
 
c9481e0
72a027e
d1ec49f
72a027e
baa5f11
72a027e
 
 
 
d1ec49f
72a027e
d1ec49f
72a027e
d1ec49f
 
72a027e
d1ec49f
72a027e
d1ec49f
72a027e
 
 
 
d1ec49f
 
 
 
 
 
 
 
72a027e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1ec49f
 
 
 
baa5f11
d1ec49f
72a027e
d1ec49f
 
 
 
 
 
72a027e
d1ec49f
 
72a027e
 
 
 
 
 
e806184
72a027e
 
 
 
 
e806184
baa5f11
 
74d7aaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
baa5f11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74d7aaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e806184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
import os
import streamlit as st

# =============================
# GLOBAL STATE + PERSISTENCE
# =============================
import streamlit as st
import os
import json

# File to persist site data
SITE_DB_FILE = "sites_db.json"

# Initialize session state
if "sites" not in st.session_state:
    # Load from disk if available
    if os.path.exists(SITE_DB_FILE):
        try:
            with open(SITE_DB_FILE, "r") as f:
                st.session_state["sites"] = json.load(f)
        except Exception as e:
            st.error(f"⚠️ Could not load sites database: {e}")
            st.session_state["sites"] = []
    else:
        st.session_state["sites"] = []

if "active_site_idx" not in st.session_state:
    st.session_state["active_site_idx"] = None

# =============================
# SECRETS MANAGEMENT
# =============================
def load_secret(key: str, required: bool = True, default: str = None):
    """
    Load secret keys (Groq API, Earth Engine, etc.).
    1. st.secrets
    2. os.environ
    3. default (if given)
    """
    value = None
    try:
        if key in st.secrets:
            value = st.secrets[key]
        elif key in os.environ:
            value = os.environ[key]
        elif default is not None:
            value = default
        elif required:
            st.error(f"❌ Missing required secret: {key}")
    except Exception as e:
        st.error(f"⚠️ Error loading secret `{key}`: {e}")
    return value

# Preload secrets
GROQ_API_KEY = load_secret("GROQ_API_KEY")
SERVICE_ACCOUNT = load_secret("SERVICE_ACCOUNT")
EARTH_ENGINE_KEY = load_secret("EARTH_ENGINE_KEY", required=False)

# =============================
# HELPERS
# =============================

def persist_sites():
    """Save sites to local JSON database"""
    try:
        with open(SITE_DB_FILE, "w") as f:
            json.dump(st.session_state["sites"], f, indent=2)
    except Exception as e:
        st.error(f"⚠️ Error saving sites: {e}")

def get_active_site():
    # Fetch current index safely
    idx = st.session_state.get("active_site_idx", 0)
    
    # Ensure sites list exists
    sites = st.session_state.get("sites", [])
    
    # If no sites exist, create a default site
    if not sites:
        st.session_state["sites"] = [{"Site Name": "Site 1"}]
        st.session_state["active_site_idx"] = 0
        return st.session_state["sites"][0]
    
    # Ensure idx is within bounds
    if idx < 0 or idx >= len(sites):
        st.session_state["active_site_idx"] = 0
        idx = 0
    
    return sites[idx]


def save_active_site(site_data):
    sites = st.session_state.get("sites", [])
    
    if not sites:
        st.session_state["sites"] = [site_data]
        st.session_state["active_site_idx"] = 0
    else:
        idx = st.session_state.get("active_site_idx", 0)
        if idx < 0 or idx >= len(sites):
            idx = 0
            st.session_state["active_site_idx"] = 0
        st.session_state["sites"][idx] = site_data


def create_new_site(name: str):
    """Create new site with maximum soil details and set active"""
    new_site = {
        "name": name,
        "Soil Profile": None,
        "USCS Classification": None,
        "AASHTO Classification": None,
        "Soil Recognizer Confidence": None,
        "Region": None,
        "Moisture Content (%)": None,
        "Dry Density (kN/mΒ³)": None,
        "Saturation (%)": None,
        "Void Ratio": None,
        "Porosity (%)": None,
        "Plastic Limit (%)": None,
        "Liquid Limit (%)": None,
        "Plasticity Index (%)": None,
        "Cohesion (kPa)": None,
        "Angle of Internal Friction (Ο†, degrees)": None,
        "Permeability (m/s)": None,
        "Compression Index (Cc)": None,
        "Recompression Index (Cr)": None,
        "Bearing Capacity (kN/mΒ²)": None,
        "Settlement (mm)": None,
        "Slope Stability Factor of Safety": None,
        "Compaction Optimum Moisture Content (%)": None,
        "Compaction Maximum Dry Density (kN/mΒ³)": None,
        "Seepage Analysis Notes": None,
        "Consolidation Notes": None,
        "Engineering Recommendations": [],
        "LLM Insights": [],
        "Notes": "",
    }
    st.session_state["sites"].append(new_site)
    st.session_state["active_site_idx"] = len(st.session_state["sites"]) - 1
    persist_sites()
    return new_site

def list_sites():
    """Return list of all site names"""
    return [site["name"] for site in st.session_state["sites"]]

# =============================
# SIDEBAR NAVIGATION + SITE MANAGER
# =============================

PAGES = {
    "🏠 Home": "home",
    "πŸ–ΌοΈ Soil Recognizer": "soil_recognizer",
    "πŸ“Š Soil Classifier": "soil_classifier",
    "πŸ€– RAG Chatbot": "rag_chatbot",
    "πŸ—ΊοΈ Maps": "maps",
    "πŸ“„ PDF Export": "pdf_export",
    "πŸ’¬ Feedback": "feedback"
}

def sidebar_navigation():
    st.sidebar.title("🌍 GeoMate Navigation")

    # --- SITE MANAGER ---
    st.sidebar.subheader("πŸ—οΈ Site Manager")
    sites = list_sites()

    if sites:
        selected = st.sidebar.selectbox(
            "Select Active Site",
            options=range(len(sites)),
            format_func=lambda i: sites[i],
            index=st.session_state.get("active_site_idx") or 0
        )
        if selected is not None:
            st.session_state["active_site_idx"] = selected
            site = get_active_site()
            st.sidebar.success(f"Active Site: {site['name']}")

        if st.sidebar.button("πŸ—‘οΈ Delete Active Site"):
            idx = st.session_state.get("active_site_idx")
            if idx is not None and idx < len(st.session_state["sites"]):
                deleted_name = st.session_state["sites"][idx]["name"]
                st.session_state["sites"].pop(idx)
                st.session_state["active_site_idx"] = None
                persist_sites()
                st.sidebar.warning(f"Deleted site: {deleted_name}")

    else:
        st.sidebar.info("No sites available. Create one below.")

    with st.sidebar.expander("βž• Create New Site"):
        new_name = st.text_input("Enter new site name")
        if st.button("Create Site"):
            if new_name.strip():
                new_site = create_new_site(new_name.strip())
                st.sidebar.success(f"βœ… Created new site: {new_site['name']}")
            else:
                st.sidebar.error("Please enter a valid site name.")

    st.sidebar.markdown("---")

    # --- PAGE NAVIGATION ---
    st.sidebar.subheader("πŸ“‘ Pages")
    page_choice = st.sidebar.radio(
        "Go to",
        list(PAGES.keys())
    )
    return PAGES[page_choice]


# =============================
# SITE DETAILS PANEL (Main UI)
# =============================
def site_details_panel():
    st.subheader("πŸ“‹ Active Site Details")
    site = get_active_site()
    if not site:
        st.info("No active site selected. Please create or select one from the sidebar.")
        return

    # Editable details
    site["location"] = st.text_input("πŸ“ Location", value=site.get("location", ""))
    site["Soil Profile"] = st.text_input("🧱 Soil Profile", value=site.get("Soil Profile", ""))
    site["Depth (m)"] = st.number_input("πŸ“ Depth (m)", value=float(site.get("Depth (m)", 0.0)))
    site["Moisture Content (%)"] = st.number_input("πŸ’§ Moisture Content (%)", value=float(site.get("Moisture Content (%)", 0.0)))
    site["Dry Density (kN/mΒ³)"] = st.number_input("πŸ‹οΈ Dry Density (kN/mΒ³)", value=float(site.get("Dry Density (kN/mΒ³)", 0.0)))
    site["Liquid Limit (%)"] = st.number_input("🌊 Liquid Limit (%)", value=float(site.get("Liquid Limit (%)", 0.0)))
    site["Plastic Limit (%)"] = st.number_input("πŸŒ€ Plastic Limit (%)", value=float(site.get("Plastic Limit (%)", 0.0)))
    site["Grain Size (%)"] = st.number_input("πŸ”¬ Grain Size (%)", value=float(site.get("Grain Size (%)", 0.0)))

    if st.button("πŸ’Ύ Save Site Details"):
        save_active_site(site)
        st.success("Site details updated successfully!")



# =============================
# FEATURE MODULES
# =============================
# ----------------------------
# Soil Recognizer Page (Integrated 6-Class ResNet18)
# ----------------------------
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as T
from PIL import Image
import streamlit as st

# ----------------------------
# Load Soil Model (6 Classes)
# ----------------------------
@st.cache_resource
def load_soil_model(path="soil_best_model.pth"):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    try:
        model = models.resnet18(pretrained=False)
        num_ftrs = model.fc.in_features
        model.fc = nn.Linear(num_ftrs, 6)  # 6 soil classes

        # Load checkpoint
        state_dict = torch.load(path, map_location=device)
        model.load_state_dict(state_dict)
        model = model.to(device)
        model.eval()
        return model, device
    except Exception as e:
        st.error(f"⚠️ Could not load soil model: {e}")
        return None, device

soil_model, device = load_soil_model()

# ----------------------------
# Soil Classes & Transform
# ----------------------------
SOIL_CLASSES = ["Clay", "Gravel", "Loam", "Peat", "Sand", "Silt"]

transform = T.Compose([
    T.Resize((224, 224)),
    T.ToTensor(),
    T.Normalize([0.485, 0.456, 0.406],
                [0.229, 0.224, 0.225])
])

# ----------------------------
# Prediction Function
# ----------------------------
def predict_soil(img: Image.Image):
    if soil_model is None:
        return "Model not loaded", {}

    img = img.convert("RGB")
    inp = transform(img).unsqueeze(0).to(device)

    with torch.no_grad():
        logits = soil_model(inp)
        probs = torch.softmax(logits[0], dim=0)

    top_idx = torch.argmax(probs).item()
    predicted_class = SOIL_CLASSES[top_idx]

    result = {SOIL_CLASSES[i]: float(probs[i]) for i in range(len(SOIL_CLASSES))}
    return predicted_class, result

# ----------------------------
# Soil Recognizer Page
# ----------------------------
def soil_recognizer_page():
    st.header("πŸ–ΌοΈ Soil Recognizer (ResNet18)")

    site = get_active_site()  # your existing site getter
    if site is None:
        st.warning("⚠️ No active site selected. Please add or select a site from the sidebar.")
        return

    uploaded = st.file_uploader("Upload soil image", type=["jpg", "jpeg", "png"])
    if uploaded is not None:
        img = Image.open(uploaded)
        st.image(img, caption="Uploaded soil image", use_column_width=True)

        predicted_class, confidence_scores = predict_soil(img)
        st.success(f"βœ… Predicted: **{predicted_class}**")

        st.subheader("Confidence Scores")
        for cls, score in confidence_scores.items():
            st.write(f"{cls}: {score:.2%}")

        if st.button("Save to site"):
            site["Soil Profile"] = predicted_class
            site["Soil Recognizer Confidence"] = confidence_scores[predicted_class]
            save_active_site(site)
            st.success("Saved prediction to active site memory.")


# ----------------------------
# Verbose USCS + AASHTO classifier + LLM report + PDF export
# Drop this into your app.py and call soil_classifier_page() from your navigation
# ----------------------------
import re
import io
import json
from math import floor
from typing import Dict, Any, Tuple
from PIL import Image
import pytesseract
import requests
import streamlit as st

# reportlab for PDF
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image as RLImage, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors

# ----------------------------
# Helpers to access site memory - adapt if your app uses different helpers
# ----------------------------
def get_active_site():
    idx = st.session_state.get("active_site_idx", 0)
    sites = st.session_state.get("sites", [])
    if 0 <= idx < len(sites):
        return sites[idx]
    # if none, create default
    if sites == []:
        st.session_state["sites"] = [{"Site Name": "Site 1"}]
        st.session_state["active_site_idx"] = 0
        return st.session_state["sites"][0]
    return None

def save_active_site(site: dict):
    idx = st.session_state.get("active_site_idx", 0)
    st.session_state["sites"][idx] = site
    st.session_state.modified = True

# ----------------------------
# Utility for numeric input retrieval (flexible key names)
# ----------------------------
def _readf(inputs: Dict[str,Any], *keys, default: float = 0.0) -> float:
    for k in keys:
        if k in inputs and inputs[k] is not None and inputs[k] != "":
            try:
                return float(inputs[k])
            except Exception:
                try:
                    return float(str(inputs[k]).replace("%","").strip())
                except Exception:
                    pass
    return default

# ----------------------------
# AASHTO: verbatim logic from your supplied script
# ----------------------------
def classify_aashto_verbatim(inputs: Dict[str,Any]) -> Tuple[str, str, int, str]:
    """
    Returns (ResultCode_str, description_str, GI_int, decision_path_str)
    Inputs keys expected:
      - P200 or P2 : percent passing sieve no.200
      - P4 : percent passing sieve no.40  (your script uses 'P4' labelled that way)
      - P10 or P1 : percent passing sieve no.10 (optional)
      - LL, PL
    """
    P2 = _readf(inputs, "P200", "P2")
    P4 = _readf(inputs, "P40", "P4")  # accept P40 or P4
    LL = _readf(inputs, "LL")
    PL = _readf(inputs, "PL")
    PI = LL - PL
    decision = []
    def note(s): decision.append(s)

    note(f"Input AASHTO: P2={P2}, P4={P4}, LL={LL}, PL={PL}, PI={PI}")

    Result = None
    desc = ""

    # Granular Materials
    if P2 <= 35:
        note("P2 <= 35% β†’ Granular branch")
        if (P2 <= 15) and (P4 <= 30) and (PI <= 6):
            note("Condition matched: P2<=15 and P4<=30 and PI<=6 β†’ need P10 to decide A-1-a")
            P1 = _readf(inputs, "P10", "P1")
            if P1 == 0:
                # Can't complete without P1; return note
                note("P10 not provided; cannot fully decide A-1-a. Returning tentative 'A-1-a(?)'")
                return "A-1-a(?)", "Candidate A-1-a (P10 missing).", 0, " -> ".join(decision)
            else:
                if P1 <= 50:
                    Result = "A-1-a"
                    desc = "Granular material with very good quality (A-1-a)."
                    note("P10 <= 50 -> A-1-a")
                else:
                    note("P10 > 50 -> inconsistent for A-1-a -> input check required")
                    return "ERROR", "Inconsistent inputs for A-1-a (P10 > 50).", 0, " -> ".join(decision)
        elif (P2 <= 25) and (P4 <= 50) and (PI <= 6):
            Result = "A-1-b"
            desc = "Granular material (A-1-b)."
            note("P2 <= 25 and P4 <= 50 and PI <= 6 -> A-1-b")
        elif (P2 <= 35) and (P4 > 0):
            note("P2 <= 35 and P4 > 0 -> A-2 family branch")
            if LL <= 40 and PI <= 10:
                Result = "A-2-4"
                desc = "A-2-4: granular material with silt-like fines."
                note("LL <= 40 and PI <= 10 -> A-2-4")
            elif LL >= 41 and PI <= 10:
                Result = "A-2-5"
                desc = "A-2-5: granular with higher LL fines."
                note("LL >= 41 and PI <= 10 -> A-2-5")
            elif LL <= 40 and PI >= 11:
                Result = "A-2-6"
                desc = "A-2-6: granular with clay-like fines."
                note("LL <= 40 and PI >= 11 -> A-2-6")
            elif LL >= 41 and PI >= 11:
                Result = "A-2-7"
                desc = "A-2-7: granular with high plasticity fines."
                note("LL >= 41 and PI >= 11 -> A-2-7")
            else:
                Result = "A-2-?"
                desc = "A-2 family ambiguous - needs more data."
                note("A-2 branch ambigous.")
        else:
            Result = "A-3"
            desc = "A-3: clean sand."
            note("Else -> A-3 (clean sands)")
    else:
        # Silt-Clay Materials
        note("P2 > 35% -> Fine (silt/clay) branch")
        if LL <= 40 and PI <= 10:
            Result = "A-4"
            desc = "A-4: silt of low LL/PI."
            note("LL <= 40 and PI <= 10 -> A-4")
        elif LL >= 41 and PI <= 10:
            Result = "A-5"
            desc = "A-5: elastic silt (higher LL but low PI)."
            note("LL >= 41 and PI <= 10 -> A-5")
        elif LL <= 40 and PI >= 11:
            Result = "A-6"
            desc = "A-6: clay of low LL and higher PI."
            note("LL <= 40 and PI >= 11 -> A-6")
        else:
            # final A-7 determination
            if PI <= (LL - 30):
                Result = "A-7-5"
                desc = "A-7-5: clay of intermediate plasticity."
                note("PI <= (LL - 30) -> A-7-5")
            elif PI > (LL - 30):
                Result = "A-7-6"
                desc = "A-7-6: clay of relatively higher plasticity."
                note("PI > (LL - 30) -> A-7-6")
            else:
                Result = "ERROR"
                desc = "Ambiguous A-7 branch."
                note("AASHTO A-7 branch ambiguous")

    # --- Group Index (GI) calculation verbatim from your snippet ---
    a = P2 - 35
    if a <= 40 and a >= 0:
        a_val = a
    elif a < 0:
        a_val = 0
    else:
        a_val = 40

    b = P2 - 15
    if b <= 40 and b >= 0:
        b_val = b
    elif b < 0:
        b_val = 0
    else:
        b_val = 40

    c = LL - 40
    if c <= 20 and c >= 0:
        c_val = c
    elif c < 0:
        c_val = 0
    else:
        c_val = 20

    d = PI - 10
    if d <= 20 and d >= 0:
        d_val = d
    elif d < 0:
        d_val = 0
    else:
        d_val = 20

    GI = floor(0.2 * a_val + 0.005 * a_val * c_val + 0.01 * b_val * d_val)
    note(f"GI compute -> a={a_val}, b={b_val}, c={c_val}, d={d_val}, GI={GI}")

    decision_path = " -> ".join(decision)
    full_code = f"{Result} ({GI})" if Result not in [None, "ERROR", "A-1-a(?)"] else (Result if Result != "A-1-a(?)" else "A-1-a (?)")
    return full_code, desc, GI, decision_path

# ----------------------------
# USCS: verbatim logic from your supplied script
# ----------------------------
def classify_uscs_verbatim(inputs: Dict[str,Any]) -> Tuple[str, str, str]:
    """
    Returns (USCS_code_str, description_str, decision_path_str)
    Accepts inputs:
      - organic (bool or 'y'/'n')
      - P200 / P2 percent passing #200
      - P4 : percent passing sieve no.4 (4.75 mm)
      - D60, D30, D10 (mm)
      - LL, PL
      - nDS, nDIL, nTG options for fines behaviour (integers)
    Implementation follows your original code's branches exactly.
    """
    decision = []
    def note(s): decision.append(s)

    organic = inputs.get("organic", False)
    if isinstance(organic, str):
        organic = organic.lower() in ("y","yes","true","1")

    if organic:
        note("Organic content indicated -> Pt")
        return "Pt", "Peat / Organic soil β€” compressible, poor engineering properties.", "Organic branch: Pt"

    P2 = _readf(inputs, "P200", "P2")
    note(f"P200 = {P2}%")

    if P2 <= 50:
        # Coarse-grained soils
        P4 = _readf(inputs, "P4", "P4_sieve", "P40")
        note(f"% passing #4 (P4) = {P4}%")
        op = inputs.get("d_values_provided", None)
        D60 = _readf(inputs, "D60")
        D30 = _readf(inputs, "D30")
        D10 = _readf(inputs, "D10")
        if D60 != 0 and D30 != 0 and D10 != 0:
            Cu = (D60 / D10) if D10 != 0 else 0
            Cc = ((D30 ** 2) / (D10 * D60)) if (D10 * D60) != 0 else 0
            note(f"D-values present -> D60={D60}, D30={D30}, D10={D10}, Cu={Cu}, Cc={Cc}")
        else:
            Cu = 0
            Cc = 0
            note("D-values missing or incomplete -> using Atterberg/fines-based branches")

        LL = _readf(inputs, "LL")
        PL = _readf(inputs, "PL")
        PI = LL - PL
        note(f"LL={LL}, PL={PL}, PI={PI}")

        # Gravels
        if P4 <= 50:
            note("P4 <= 50 -> Gravel family")
            if (Cu != 0) and (Cc != 0):
                if (Cu >= 4) and (1 <= Cc <= 3):
                    note("Cu >=4 and 1<=Cc<=3 -> GW")
                    return "GW", "Well-graded gravel with excellent load-bearing capacity.", "GW via Cu/Cc"
                elif not ((Cu < 4) and (1 <= Cc <= 3)):
                    note("Cu <4 or Cc out of 1..3 -> GP")
                    return "GP", "Poorly-graded gravel.", "GP via Cu/Cc"
            else:
                # no D-values: use fines/PI checks
                if (PI < 4) or (PI < 0.73 * (LL - 20)):
                    note("PI < 4 or PI < 0.73*(LL-20) -> GM")
                    return "GM", "Silty gravel with moderate properties.", "GM via fines"
                elif (PI > 7) and (PI > 0.73 * (LL - 20)):
                    note("PI > 7 and PI > 0.73*(LL-20) -> GC")
                    return "GC", "Clayey gravel β€” reduced drainage.", "GC via fines"
                else:
                    note("Intermediate fines -> GM-GC")
                    return "GM-GC", "Mixed silt/clay in gravel β€” variable.", "GM-GC via fines"
        else:
            # Sands path
            note("P4 > 50 -> Sand family")
            if (Cu != 0) and (Cc != 0):
                if (Cu >= 6) and (1 <= Cc <= 3):
                    note("Cu >= 6 and 1 <= Cc <= 3 -> SW")
                    return "SW", "Well-graded sand with good engineering behavior.", "SW via Cu/Cc"
                elif not ((Cu < 6) and (1 <= Cc <= 3)):
                    note("Cu <6 or Cc out of 1..3 -> SP")
                    return "SP", "Poorly-graded sand.", "SP via Cu/Cc"
            else:
                if (PI < 4) or (PI <= 0.73 * (LL - 20)):
                    note("PI < 4 or PI <= 0.73*(LL-20) -> SM")
                    return "SM", "Silty sand β€” moderate engineering quality.", "SM via fines"
                elif (PI > 7) and (PI > 0.73 * (LL - 20)):
                    note("PI > 7 and PI > 0.73*(LL-20) -> SC")
                    return "SC", "Clayey sand β€” reduced permeability and strength.", "SC via fines"
                else:
                    note("Intermediate -> SM-SC")
                    return "SM-SC", "Sand mixed with fines (silt/clay).", "SM-SC via fines"
    else:
        # Fine-grained soils
        note("P200 > 50 -> Fine-grained path")
        LL = _readf(inputs, "LL")
        PL = _readf(inputs, "PL")
        PI = LL - PL
        note(f"LL={LL}, PL={PL}, PI={PI}")

        # Read behaviour options
        nDS = int(_readf(inputs, "nDS", default=0))
        nDIL = int(_readf(inputs, "nDIL", default=0))
        nTG = int(_readf(inputs, "nTG", default=0))
        note(f"Behavior options (nDS,nDIL,nTG) = ({nDS},{nDIL},{nTG})")

        # Low plasticity fines
        if LL < 50:
            note("LL < 50 -> low plasticity branch")
            if (20 <= LL < 50) and (PI <= 0.73 * (LL - 20)):
                note("20 <= LL < 50 and PI <= 0.73*(LL-20)")
                if (nDS == 1) or (nDIL == 3) or (nTG == 3):
                    note("-> ML")
                    return "ML", "Silt of low plasticity.", "ML via LL/PI/observations"
                elif (nDS == 3) or (nDIL == 3) or (nTG == 3):
                    note("-> OL (organic silt)")
                    return "OL", "Organic silt β€” compressible.", "OL via observations"
                else:
                    note("-> ML-OL (ambiguous)")
                    return "ML-OL", "Mixed silt/organic.", "ML-OL via ambiguity"
            elif (10 <= LL <= 30) and (4 <= PI <= 7) and (PI > 0.72 * (LL - 20)):
                note("10 <= LL <=30 and 4<=PI<=7 and PI > 0.72*(LL-20)")
                if (nDS == 1) or (nDIL == 1) or (nTG == 1):
                    note("-> ML")
                    return "ML", "Low plasticity silt", "ML via specific conditions"
                elif (nDS == 2) or (nDIL == 2) or (nTG == 2):
                    note("-> CL")
                    return "CL", "Low plasticity clay", "CL via specific conditions"
                else:
                    note("-> ML-CL (ambiguous)")
                    return "ML-CL", "Mixed ML/CL", "ML-CL via ambiguity"
            else:
                note("Default low-plasticity branch -> CL")
                return "CL", "Low plasticity clay", "CL default"
        else:
            # High plasticity fines
            note("LL >= 50 -> high plasticity branch")
            if PI < 0.73 * (LL - 20):
                note("PI < 0.73*(LL-20)")
                if (nDS == 3) or (nDIL == 4) or (nTG == 4):
                    note("-> MH")
                    return "MH", "Elastic silt (high LL)", "MH via observations"
                elif (nDS == 2) or (nDIL == 2) or (nTG == 4):
                    note("-> OH")
                    return "OH", "Organic high plasticity silt/clay", "OH via observations"
                else:
                    note("-> MH-OH (ambiguous)")
                    return "MH-OH", "Mixed MH/OH", "MH-OH via ambiguity"
            else:
                note("PI >= 0.73*(LL-20) -> CH")
                return "CH", "High plasticity clay β€” compressible, problematic for foundations.", "CH default high-PL"

    note("Fell through branches -> UNCLASSIFIED")
    return "UNCLASSIFIED", "Insufficient data for USCS classification.", "No valid decision path"

# ----------------------------
# Engineering descriptors & LaTeX-table mapping
# ----------------------------
ENGINEERING_TABLE = {
    "Gravel": {
        "Settlement": "None",
        "Quicksand": "Impossible",
        "Frost": "None",
        "Groundwater lowering": "Possible",
        "Cement grouting": "Possible",
        "Silicate/bitumen": "Unsuitable",
        "Compressed air": "Possible (loss of air, slow progress)"
    },
    "Coarse sand": {
        "Settlement": "None",
        "Quicksand": "Impossible",
        "Frost": "None",
        "Groundwater lowering": "Suitable",
        "Cement grouting": "Possible only if very coarse",
        "Silicate/bitumen": "Suitable",
        "Compressed air": "Suitable"
    },
    "Medium sand": {
        "Settlement": "None",
        "Quicksand": "Unlikely",
        "Frost": "None",
        "Groundwater lowering": "Suitable",
        "Cement grouting": "Impossible",
        "Silicate/bitumen": "Suitable",
        "Compressed air": "Suitable"
    },
    "Fine sand": {
        "Settlement": "None",
        "Quicksand": "Liable",
        "Frost": "None",
        "Groundwater lowering": "Suitable",
        "Cement grouting": "Impossible",
        "Silicate/bitumen": "Not possible in very fine sands",
        "Compressed air": "Suitable"
    },
    "Silt": {
        "Settlement": "Occurs",
        "Quicksand": "Liable (coarse silts / silty sands)",
        "Frost": "Occurs",
        "Groundwater lowering": "Impossible (except electro-osmosis)",
        "Cement grouting": "Impossible",
        "Silicate/bitumen": "Impossible",
        "Compressed air": "Suitable"
    },
    "Clay": {
        "Settlement": "Occurs",
        "Quicksand": "Impossible",
        "Frost": "None",
        "Groundwater lowering": "Impossible",
        "Cement grouting": "Only in stiff, fissured clay",
        "Silicate/bitumen": "Impossible",
        "Compressed air": "Used for support only (Glossop & Skempton)"
    }
}

def engineering_characteristics_from_uscs(uscs_code: str) -> Dict[str,str]:
    # map family codes to table entries
    if uscs_code.startswith("G"):
        return ENGINEERING_TABLE["Gravel"]
    if uscs_code.startswith("S"):
        # differentiate coarse/medium/fine sand? We'll return Medium sand baseline
        return ENGINEERING_TABLE["Medium sand"]
    if uscs_code in ("ML","MH","OL","OH"):
        return ENGINEERING_TABLE["Silt"]
    if uscs_code.startswith("C") or uscs_code == "CL" or uscs_code == "CH":
        return ENGINEERING_TABLE["Clay"]
    # default
    return {"Settlement":"Varies", "Quicksand":"Varies", "Frost":"Varies"}

# ----------------------------
# Combined classifier that produces a rich result
# ----------------------------
def classify_all(inputs: Dict[str,Any]) -> Dict[str,Any]:
    """
    Run both AASHTO & USCS verbatim logic and return a dictionary with:
      - AASHTO_code, AASHTO_desc, GI, AASHTO_decision_path
      - USCS_code, USCS_desc, USCS_decision_path
      - engineering_characteristics (dict)
      - engineering_summary (short deterministic summary)
    """
    aashto_code, aashto_desc, GI, aashto_path = classify_aashto_verbatim(inputs)
    uscs_code, uscs_desc, uscs_path = classify_uscs_verbatim(inputs)

    eng_chars = engineering_characteristics_from_uscs(uscs_code)

    # Deterministic engineering summary
    summary_lines = []
    summary_lines.append(f"USCS: {uscs_code} β€” {uscs_desc}")
    summary_lines.append(f"AASHTO: {aashto_code} β€” {aashto_desc}")
    summary_lines.append(f"Group Index: {GI}")
    # family derived remarks
    if uscs_code.startswith("C") or uscs_code in ("CH","CL"):
        summary_lines.append("Clayey behavior: expect significant compressibility, low permeability, potential long-term settlement β€” advisable to assess consolidation & use deep foundations for heavy loads.")
    elif uscs_code.startswith("G") or uscs_code.startswith("S"):
        summary_lines.append("Granular behavior: good drainage and bearing; suitable for shallow foundations/pavements when properly compacted.")
    elif uscs_code in ("ML","MH","OL","OH"):
        summary_lines.append("Silty/organic behavior: moderate-to-high compressibility; frost-susceptible; avoid as direct support for heavy structures without treatment.")
    else:
        summary_lines.append("Mixed or unclear behavior; recommend targeted lab testing and conservative design assumptions.")

    out = {
        "AASHTO_code": aashto_code,
        "AASHTO_description": aashto_desc,
        "GI": GI,
        "AASHTO_decision_path": aashto_path,
        "USCS_code": uscs_code,
        "USCS_description": uscs_desc,
        "USCS_decision_path": uscs_path,
        "engineering_characteristics": eng_chars,
        "engineering_summary": "\n".join(summary_lines)
    }
    return out

# ----------------------------
# LLM integration (Groq) to produce a rich humanized report
# ----------------------------
def call_groq_for_explanation(prompt: str, model_name: str = "meta-llama/llama-4-maverick-17b-128e-instruct", max_tokens: int = 800) -> str:
    """
    Use Groq client via REST if GROQ_API_KEY in st.secrets
    (Note: adapt to your Groq client wrapper if you have it)
    """
    key = None
    # check st.secrets first
    if "GROQ_API_KEY" in st.secrets:
        key = st.secrets["GROQ_API_KEY"]
    else:
        key = st.session_state.get("GROQ_API_KEY") or None

    if not key:
        return "Groq API key not found. LLM humanized explanation not available."

    url = "https://api.groq.com/v1/chat/completions"
    headers = {"Authorization": f"Bearer {key}", "Content-Type":"application/json"}
    payload = {
        "model": model_name,
        "messages": [
            {"role":"system","content":"You are GeoMate, a professional geotechnical engineering assistant."},
            {"role":"user","content": prompt}
        ],
        "temperature": 0.2,
        "max_tokens": max_tokens
    }
    try:
        resp = requests.post(url, headers=headers, json=payload, timeout=60)
        resp.raise_for_status()
        data = resp.json()
        # try to extract content defensively
        if "choices" in data and len(data["choices"])>0:
            content = data["choices"][0].get("message", {}).get("content") or data["choices"][0].get("text") or str(data["choices"][0])
            return content
        return json.dumps(data)
    except Exception as e:
        return f"LLM call failed: {e}"

# ----------------------------
# Build PDF bytes for classification report
# ----------------------------
def build_classification_pdf_bytes(site: Dict[str,Any], classification: Dict[str,Any], explanation_text: str) -> bytes:
    buf = io.BytesIO()
    doc = SimpleDocTemplate(buf, pagesize=A4, leftMargin=18*mm, rightMargin=18*mm, topMargin=18*mm, bottomMargin=18*mm)
    styles = getSampleStyleSheet()
    title_style = ParagraphStyle("title", parent=styles["Title"], fontSize=18, textColor=colors.HexColor("#FF6600"), alignment=1)
    h1 = ParagraphStyle("h1", parent=styles["Heading1"], fontSize=12, textColor=colors.HexColor("#FF6600"))
    body = ParagraphStyle("body", parent=styles["BodyText"], fontSize=10)

    elems = []
    elems.append(Paragraph("GeoMate V2 β€” Classification Report", title_style))
    elems.append(Spacer(1,6))
    elems.append(Paragraph(f"Site: {site.get('Site Name','Unnamed')}", h1))
    elems.append(Paragraph(f"Date: {st.datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M UTC')}", body))
    elems.append(Spacer(1,8))

    # Inputs summary
    elems.append(Paragraph("Laboratory Inputs", h1))
    inputs = site.get("classifier_inputs", {})
    if inputs:
        data = [["Parameter","Value"]]
        for k,v in inputs.items():
            data.append([str(k), str(v)])
        table = Table(data, colWidths=[80*mm, 80*mm])
        table.setStyle(TableStyle([("GRID",(0,0),(-1,-1),0.5,colors.grey), ("BACKGROUND",(0,0),(-1,0),colors.HexColor("#FF6600")), ("TEXTCOLOR",(0,0),(-1,0),colors.white)]))
        elems.append(table)
    else:
        elems.append(Paragraph("No lab inputs recorded.", body))
    elems.append(Spacer(1,8))

    # Deterministic results
    elems.append(Paragraph("Deterministic Classification Results", h1))
    elems.append(Paragraph(f"USCS: {classification.get('USCS_code','N/A')} β€” {classification.get('USCS_description','')}", body))
    elems.append(Paragraph(f"AASHTO: {classification.get('AASHTO_code','N/A')} β€” {classification.get('AASHTO_description','')}", body))
    elems.append(Paragraph(f"Group Index: {classification.get('GI','N/A')}", body))
    elems.append(Spacer(1,6))
    elems.append(Paragraph("USCS decision path (verbatim):", h1))
    elems.append(Paragraph(classification.get("USCS_decision_path","Not recorded"), body))
    elems.append(Spacer(1,6))
    elems.append(Paragraph("AASHTO decision path (verbatim):", h1))
    elems.append(Paragraph(classification.get("AASHTO_decision_path","Not recorded"), body))
    elems.append(Spacer(1,8))

    # Engineering characteristics table
    elems.append(Paragraph("Engineering Characteristics (from reference table)", h1))
    eng = classification.get("engineering_characteristics", {})
    if eng:
        eng_data = [["Property","Value"]]
        for k,v in eng.items():
            eng_data.append([k, v])
        t2 = Table(eng_data, colWidths=[60*mm, 100*mm])
        t2.setStyle(TableStyle([("GRID",(0,0),(-1,-1),0.5,colors.grey), ("BACKGROUND",(0,0),(-1,0),colors.HexColor("#FF6600")), ("TEXTCOLOR",(0,0),(-1,0),colors.white)]))
        elems.append(t2)
    elems.append(Spacer(1,8))

    # LLM Explanation (humanized)
    elems.append(Paragraph("Humanized Engineering Explanation (LLM)", h1))
    if explanation_text:
        # avoid overly long text blocks; split into paragraphs
        for para in explanation_text.strip().split("\n\n"):
            elems.append(Paragraph(para.strip().replace("\n"," "), body))
            elems.append(Spacer(1,4))
    else:
        elems.append(Paragraph("No LLM explanation available.", body))

    # Map snapshot (optional)
    if "map_snapshot" in site and site["map_snapshot"]:
        snap = site["map_snapshot"]
        # If snapshot is HTML, skip embedding; if it's an image path, include it.
        if isinstance(snap, str) and snap.lower().endswith((".png",".jpg",".jpeg")) and os.path.exists(snap):
            elems.append(PageBreak())
            elems.append(Paragraph("Map Snapshot", h1))
            elems.append(RLImage(snap, width=160*mm, height=90*mm))

    doc.build(elems)
    pdf_bytes = buf.getvalue()
    buf.close()
    return pdf_bytes

# ----------------------------
# Streamlit Chat-style Soil Classifier Page
# ----------------------------
def soil_classifier_page():
    st.header("🧭 Soil Classifier β€” USCS & AASHTO (Verbatim)")

    site = get_active_site()
    if site is None:
        st.warning("No active site. Add a site first in the sidebar.")
        return

    # Ensure classifier_inputs exists
    site.setdefault("classifier_inputs", {})

    col1, col2 = st.columns([2,1])
    with col1:
        st.markdown("**Upload lab sheet (image) for OCR** β€” the extracted values will auto-fill classifier inputs.")
        uploaded = st.file_uploader("Upload image (png/jpg)", type=["png","jpg","jpeg"], key="clf_ocr_upload")
        if uploaded:
            img = Image.open(uploaded)
            st.image(img, caption="Uploaded lab sheet (OCR)", use_column_width=True)
            try:
                raw_text = pytesseract.image_to_string(img)
                st.text_area("OCR raw text (preview)", raw_text, height=180)
                # Basic numeric extraction heuristics (LL, PL, P200, P4, D60/D30/D10)
                # Try many patterns for robustness
                def find_first(pattern):
                    m = re.search(pattern, raw_text, re.IGNORECASE)
                    return float(m.group(1)) if m else None

                possible = {}
                for pat_key, pats in {
                    "LL": [r"LL\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"Liquid\s*Limit\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "PL": [r"PL\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"Plastic\s*Limit\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "P200":[r"%\s*Passing\s*#?200\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"P200\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"Passing\s*0\.075\s*mm\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "P4":[r"%\s*Passing\s*#?4\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"P4\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "D60":[r"D60\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"D_{60}\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "D30":[r"D30\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "D10":[r"D10\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"]
                }.items():
                    for p in pats:
                        v = find_first(p)
                        if v is not None:
                            possible[pat_key] = v
                            break
                # copy found to site inputs
                for k,v in possible.items():
                    site["classifier_inputs"][k] = v
                save_active_site(site)
                st.success(f"OCR auto-filled: {', '.join([f'{k}={v}' for k,v in possible.items()])}")
            except Exception as e:
                st.error(f"OCR parsing failed: {e}")

        st.markdown("**Or type soil parameters / paste lab line** (e.g. `LL=45 PL=22 P200=58 P4=12 D60=1.2 D30=0.45 D10=0.08`) β€” chat-style input below.")
        user_text = st.text_area("Enter parameters or notes", value="", key="clf_text_input", height=120)

        if st.button("Run Classification"):
            # parse user_text for numbers too (merge with site inputs)
            txt = user_text or ""
            # find key=value pairs
            kvs = dict(re.findall(r"([A-Za-z0-9_%]+)\s*[=:\-]\s*([0-9]+(?:\.[0-9]+)?)", txt))
            # normalize keys
            norm = {}
            for k,v in kvs.items():
                klow = k.strip().lower()
                if klow in ("ll","liquidlimit","liquid_limit","liquid"):
                    norm["LL"] = float(v)
                elif klow in ("pl","plasticlimit","plastic_limit","plastic"):
                    norm["PL"] = float(v)
                elif klow in ("pi","plasticityindex"):
                    norm["PI"] = float(v)
                elif klow in ("p200","%200","p_200","passing200"):
                    norm["P200"] = float(v)
                elif klow in ("p4","p_4","passing4"):
                    norm["P4"] = float(v)
                elif klow in ("d60","d_60"):
                    norm["D60"] = float(v)
                elif klow in ("d30","d_30"):
                    norm["D30"] = float(v)
                elif klow in ("d10","d_10"):
                    norm["D10"] = float(v)
            # merge into site inputs
            site["classifier_inputs"].update(norm)
            save_active_site(site)

            # run verbatim classifiers
            inputs_for_class = site["classifier_inputs"]
            # ensure keys exist (coerce to numeric defaults)
            result = classify_all(inputs_for_class)
            # store result into site memory
            site["classification_report"] = result
            save_active_site(site)

            st.success("Deterministic classification complete.")
            st.markdown("**USCS result:** " + str(result.get("USCS_code")))
            st.markdown("**AASHTO result:** " + str(result.get("AASHTO_code")) + f" (GI={result.get('GI')})")
            st.markdown("**Engineering summary (deterministic):**")
            st.info(result.get("engineering_summary"))

            # call LLM to produce a humanized expanded report (if GROQ key exists)
            prompt = f"""
            You are GeoMate, a professional geotechnical engineer assistant.
            Given the following laboratory inputs and deterministic classification, produce a clear, technical
            and human-friendly classification report, explaining what the soil is, how it behaves, engineering
            implications (bearing, settlement, stiffness), suitability for shallow foundations and road subgrades,
            and practical recommendations for site engineering.

            Site: {site.get('Site Name','Unnamed')}
            Inputs (as parsed): {json.dumps(site.get('classifier_inputs',{}), indent=2)}
            Deterministic classification results:
            USCS: {result.get('USCS_code')}
            USCS decision path: {result.get('USCS_decision_path')}
            AASHTO: {result.get('AASHTO_code')}
            AASHTO decision path: {result.get('AASHTO_decision_path')}
            Group Index: {result.get('GI')}
            Engineering characteristics reference table: {json.dumps(result.get('engineering_characteristics',{}), indent=2)}

            Provide:
            - Executive summary (3-5 sentences)
            - Engineering interpretation (detailed)
            - Specific recommendations (foundations, drainage, compaction, stabilization)
            - Short checklist of items for further testing.
            """
            st.info("Generating humanized report via LLM (Groq) β€” this may take a few seconds.")
            explanation = call_groq_for_explanation(prompt)
            # fallback if failed
            if explanation.startswith("LLM call failed") or explanation.startswith("Groq API key not found"):
                # build local humanized explanation deterministically
                explanation = ("Humanized explanation not available via LLM. "
                               "Deterministic summary: \n\n" + result.get("engineering_summary", "No summary."))

            # save explanation to site memory
            site.setdefault("reports", {})
            site["reports"]["last_classification_explanation"] = explanation
            save_active_site(site)

            st.markdown("**Humanized Explanation (LLM or fallback):**")
            st.write(explanation)

            # Build PDF bytes and offer download
            pdf_bytes = build_classification_pdf_bytes(site, result, explanation)
            st.download_button("Download Classification PDF", data=pdf_bytes, file_name=f"classification_{site.get('Site Name','site')}.pdf", mime="application/pdf")

    # side column shows current parsed inputs / last results
    with col2:
        st.markdown("**Current parsed inputs**")
        st.json(site.get("classifier_inputs", {}))
        st.markdown("**Last deterministic classification (if any)**")
        st.json(site.get("classification_report", {}))

# End of snippet

# ----------------------------
# LLM integration (Groq) to produce a rich humanized report
# ----------------------------
def call_groq_for_explanation(prompt: str, model_name: str = "meta-llama/llama-4-maverick-17b-128e-instruct", max_tokens: int = 800) -> str:
    """
    Use Groq client via REST if GROQ_API_KEY in st.secrets
    (Note: adapt to your Groq client wrapper if you have it)
    """
    key = None
    # check st.secrets first
    if "GROQ_API_KEY" in st.secrets:
        key = st.secrets["GROQ_API_KEY"]
    else:
        key = st.session_state.get("GROQ_API_KEY") or None

    if not key:
        return "Groq API key not found. LLM humanized explanation not available."

    url = "https://api.groq.com/v1/chat/completions"
    headers = {"Authorization": f"Bearer {key}", "Content-Type":"application/json"}
    payload = {
        "model": model_name,
        "messages": [
            {"role":"system","content":"You are GeoMate, a professional geotechnical engineering assistant."},
            {"role":"user","content": prompt}
        ],
        "temperature": 0.2,
        "max_tokens": max_tokens
    }
    try:
        resp = requests.post(url, headers=headers, json=payload, timeout=60)
        resp.raise_for_status()
        data = resp.json()
        # try to extract content defensively
        if "choices" in data and len(data["choices"])>0:
            content = data["choices"][0].get("message", {}).get("content") or data["choices"][0].get("text") or str(data["choices"][0])
            return content
        return json.dumps(data)
    except Exception as e:
        return f"LLM call failed: {e}"

# ----------------------------
# Build PDF bytes for classification report
# ----------------------------
def build_classification_pdf_bytes(site: Dict[str,Any], classification: Dict[str,Any], explanation_text: str) -> bytes:
    buf = io.BytesIO()
    doc = SimpleDocTemplate(buf, pagesize=A4, leftMargin=18*mm, rightMargin=18*mm, topMargin=18*mm, bottomMargin=18*mm)
    styles = getSampleStyleSheet()
    title_style = ParagraphStyle("title", parent=styles["Title"], fontSize=18, textColor=colors.HexColor("#FF6600"), alignment=1)
    h1 = ParagraphStyle("h1", parent=styles["Heading1"], fontSize=12, textColor=colors.HexColor("#FF6600"))
    body = ParagraphStyle("body", parent=styles["BodyText"], fontSize=10)

    elems = []
    elems.append(Paragraph("GeoMate V2 β€” Classification Report", title_style))
    elems.append(Spacer(1,6))
    elems.append(Paragraph(f"Site: {site.get('Site Name','Unnamed')}", h1))
    elems.append(Paragraph(f"Date: {st.datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M UTC')}", body))
    elems.append(Spacer(1,8))

    # Inputs summary
    elems.append(Paragraph("Laboratory Inputs", h1))
    inputs = site.get("classifier_inputs", {})
    if inputs:
        data = [["Parameter","Value"]]
        for k,v in inputs.items():
            data.append([str(k), str(v)])
        table = Table(data, colWidths=[80*mm, 80*mm])
        table.setStyle(TableStyle([("GRID",(0,0),(-1,-1),0.5,colors.grey), ("BACKGROUND",(0,0),(-1,0),colors.HexColor("#FF6600")), ("TEXTCOLOR",(0,0),(-1,0),colors.white)]))
        elems.append(table)
    else:
        elems.append(Paragraph("No lab inputs recorded.", body))
    elems.append(Spacer(1,8))

    # Deterministic results
    elems.append(Paragraph("Deterministic Classification Results", h1))
    elems.append(Paragraph(f"USCS: {classification.get('USCS_code','N/A')} β€” {classification.get('USCS_description','')}", body))
    elems.append(Paragraph(f"AASHTO: {classification.get('AASHTO_code','N/A')} β€” {classification.get('AASHTO_description','')}", body))
    elems.append(Paragraph(f"Group Index: {classification.get('GI','N/A')}", body))
    elems.append(Spacer(1,6))
    elems.append(Paragraph("USCS decision path (verbatim):", h1))
    elems.append(Paragraph(classification.get("USCS_decision_path","Not recorded"), body))
    elems.append(Spacer(1,6))
    elems.append(Paragraph("AASHTO decision path (verbatim):", h1))
    elems.append(Paragraph(classification.get("AASHTO_decision_path","Not recorded"), body))
    elems.append(Spacer(1,8))

    # Engineering characteristics table
    elems.append(Paragraph("Engineering Characteristics (from reference table)", h1))
    eng = classification.get("engineering_characteristics", {})
    if eng:
        eng_data = [["Property","Value"]]
        for k,v in eng.items():
            eng_data.append([k, v])
        t2 = Table(eng_data, colWidths=[60*mm, 100*mm])
        t2.setStyle(TableStyle([("GRID",(0,0),(-1,-1),0.5,colors.grey), ("BACKGROUND",(0,0),(-1,0),colors.HexColor("#FF6600")), ("TEXTCOLOR",(0,0),(-1,0),colors.white)]))
        elems.append(t2)
    elems.append(Spacer(1,8))

    # LLM Explanation (humanized)
    elems.append(Paragraph("Humanized Engineering Explanation (LLM)", h1))
    if explanation_text:
        # avoid overly long text blocks; split into paragraphs
        for para in explanation_text.strip().split("\n\n"):
            elems.append(Paragraph(para.strip().replace("\n"," "), body))
            elems.append(Spacer(1,4))
    else:
        elems.append(Paragraph("No LLM explanation available.", body))

    # Map snapshot (optional)
    if "map_snapshot" in site and site["map_snapshot"]:
        snap = site["map_snapshot"]
        # If snapshot is HTML, skip embedding; if it's an image path, include it.
        if isinstance(snap, str) and snap.lower().endswith((".png",".jpg",".jpeg")) and os.path.exists(snap):
            elems.append(PageBreak())
            elems.append(Paragraph("Map Snapshot", h1))
            elems.append(RLImage(snap, width=160*mm, height=90*mm))

    doc.build(elems)
    pdf_bytes = buf.getvalue()
    buf.close()
    return pdf_bytes

# ----------------------------
# Streamlit Chat-style Soil Classifier Page
# ----------------------------
def soil_classifier_page():
    st.header("🧭 Soil Classifier β€” USCS & AASHTO (Verbatim)")

    site = get_active_site()
    if site is None:
        st.warning("No active site. Add a site first in the sidebar.")
        return

    # Ensure classifier_inputs exists
    site.setdefault("classifier_inputs", {})

    col1, col2 = st.columns([2,1])
    with col1:
        st.markdown("**Upload lab sheet (image) for OCR** β€” the extracted values will auto-fill classifier inputs.")
        uploaded = st.file_uploader("Upload image (png/jpg)", type=["png","jpg","jpeg"], key="clf_ocr_upload")
        if uploaded:
            img = Image.open(uploaded)
            st.image(img, caption="Uploaded lab sheet (OCR)", use_column_width=True)
            try:
                raw_text = pytesseract.image_to_string(img)
                st.text_area("OCR raw text (preview)", raw_text, height=180)
                # Basic numeric extraction heuristics (LL, PL, P200, P4, D60/D30/D10)
                # Try many patterns for robustness
                def find_first(pattern):
                    m = re.search(pattern, raw_text, re.IGNORECASE)
                    return float(m.group(1)) if m else None

                possible = {}
                for pat_key, pats in {
                    "LL": [r"LL\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"Liquid\s*Limit\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "PL": [r"PL\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"Plastic\s*Limit\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "P200":[r"%\s*Passing\s*#?200\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"P200\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"Passing\s*0\.075\s*mm\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "P4":[r"%\s*Passing\s*#?4\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"P4\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "D60":[r"D60\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"D_{60}\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "D30":[r"D30\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"],
                    "D10":[r"D10\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)"]
                }.items():
                    for p in pats:
                        v = find_first(p)
                        if v is not None:
                            possible[pat_key] = v
                            break
                # copy found to site inputs
                for k,v in possible.items():
                    site["classifier_inputs"][k] = v
                save_active_site(site)
                st.success(f"OCR auto-filled: {', '.join([f'{k}={v}' for k,v in possible.items()])}")
            except Exception as e:
                st.error(f"OCR parsing failed: {e}")

        st.markdown("**Or type soil parameters / paste lab line** (e.g. `LL=45 PL=22 P200=58 P4=12 D60=1.2 D30=0.45 D10=0.08`) β€” chat-style input below.")
        user_text = st.text_area("Enter parameters or notes", value="", key="clf_text_input", height=120)

        if st.button("Run Classification"):
            # parse user_text for numbers too (merge with site inputs)
            txt = user_text or ""
            # find key=value pairs
            kvs = dict(re.findall(r"([A-Za-z0-9_%]+)\s*[=:\-]\s*([0-9]+(?:\.[0-9]+)?)", txt))
            # normalize keys
            norm = {}
            for k,v in kvs.items():
                klow = k.strip().lower()
                if klow in ("ll","liquidlimit","liquid_limit","liquid"):
                    norm["LL"] = float(v)
                elif klow in ("pl","plasticlimit","plastic_limit","plastic"):
                    norm["PL"] = float(v)
                elif klow in ("pi","plasticityindex"):
                    norm["PI"] = float(v)
                elif klow in ("p200","%200","p_200","passing200"):
                    norm["P200"] = float(v)
                elif klow in ("p4","p_4","passing4"):
                    norm["P4"] = float(v)
                elif klow in ("d60","d_60"):
                    norm["D60"] = float(v)
                elif klow in ("d30","d_30"):
                    norm["D30"] = float(v)
                elif klow in ("d10","d_10"):
                    norm["D10"] = float(v)
            # merge into site inputs
            site["classifier_inputs"].update(norm)
            save_active_site(site)

            # run verbatim classifiers
            inputs_for_class = site["classifier_inputs"]
            # ensure keys exist (coerce to numeric defaults)
            result = classify_all(inputs_for_class)
            # store result into site memory
            site["classification_report"] = result
            save_active_site(site)

            st.success("Deterministic classification complete.")
            st.markdown("**USCS result:** " + str(result.get("USCS_code")))
            st.markdown("**AASHTO result:** " + str(result.get("AASHTO_code")) + f" (GI={result.get('GI')})")
            st.markdown("**Engineering summary (deterministic):**")
            st.info(result.get("engineering_summary"))

            # call LLM to produce a humanized expanded report (if GROQ key exists)
            prompt = f"""
            You are GeoMate, a professional geotechnical engineer assistant.
            Given the following laboratory inputs and deterministic classification, produce a clear, technical
            and human-friendly classification report, explaining what the soil is, how it behaves, engineering
            implications (bearing, settlement, stiffness), suitability for shallow foundations and road subgrades,
            and practical recommendations for site engineering.

            Site: {site.get('Site Name','Unnamed')}
            Inputs (as parsed): {json.dumps(site.get('classifier_inputs',{}), indent=2)}
            Deterministic classification results:
            USCS: {result.get('USCS_code')}
            USCS decision path: {result.get('USCS_decision_path')}
            AASHTO: {result.get('AASHTO_code')}
            AASHTO decision path: {result.get('AASHTO_decision_path')}
            Group Index: {result.get('GI')}
            Engineering characteristics reference table: {json.dumps(result.get('engineering_characteristics',{}), indent=2)}

            Provide:
            - Executive summary (3-5 sentences)
            - Engineering interpretation (detailed)
            - Specific recommendations (foundations, drainage, compaction, stabilization)
            - Short checklist of items for further testing.
            """
            st.info("Generating humanized report via LLM (Groq) β€” this may take a few seconds.")
            explanation = call_groq_for_explanation(prompt)
            # fallback if failed
            if explanation.startswith("LLM call failed") or explanation.startswith("Groq API key not found"):
                # build local humanized explanation deterministically
                explanation = ("Humanized explanation not available via LLM. "
                               "Deterministic summary: \n\n" + result.get("engineering_summary", "No summary."))

            # save explanation to site memory
            site.setdefault("reports", {})
            site["reports"]["last_classification_explanation"] = explanation
            save_active_site(site)

            st.markdown("**Humanized Explanation (LLM or fallback):**")
            st.write(explanation)

            # Build PDF bytes and offer download
            pdf_bytes = build_classification_pdf_bytes(site, result, explanation)
            st.download_button("Download Classification PDF", data=pdf_bytes, file_name=f"classification_{site.get('Site Name','site')}.pdf", mime="application/pdf")

    # side column shows current parsed inputs / last results
    with col2:
        st.markdown("**Current parsed inputs**")
        st.json(site.get("classifier_inputs", {}))
        st.markdown("**Last deterministic classification (if any)**")
        st.json(site.get("classification_report", {}))

# End of snippet

    pass


# 3. Locator (Earth Engine + Maps)
def locator_page():
    st.header("🌍 Locator (Earth Engine Powered)")
    # TODO: implement EE init + fetch flood, seismic, topo, soil
    pass


# 4. RAG Chatbot (FAISS + Groq)
def rag_chatbot_page():
    st.header("πŸ’¬ Knowledge Assistant (RAG + Groq)")
    # TODO: implement FAISS search + Groq LLM API
    pass


# 5. PDF Report Generator
def report_page():
    st.header("πŸ“‘ Generate Report")
    # TODO: compile site data β†’ PDF download
    pass


# 6. Feedback Form
def feedback_page():
    st.header("πŸ“ Feedback & Suggestions")
    # TODO: implement form β†’ send email (trendn.help@gmail.com)
    pass


# =============================
# NAVIGATION
# =============================

PAGES = {
    "Soil Recognizer": soil_recognizer_page,
    "Soil Classifier": soil_classifier_page,
    "Locator": locator_page,
    "Knowledge Assistant": rag_chatbot_page,
    "Report": report_page,
    "Feedback": feedback_page,
}

def main():
    st.sidebar.title("🌍 GeoMate V2")
    choice = st.sidebar.radio("Navigate", list(PAGES.keys()))

    # Site memory: add/manage multiple sites
    if st.sidebar.button("βž• Add Site"):
        st.session_state["sites"].append({})
        st.session_state["active_site_idx"] = len(st.session_state["sites"]) - 1
    if st.session_state["sites"]:
        st.sidebar.write("Sites:")
        for i, s in enumerate(st.session_state["sites"]):
            label = f"Site {i+1}"
            if st.sidebar.button(label, key=f"site_{i}"):
                st.session_state["active_site_idx"] = i

    # Run selected page
    PAGES[choice]()


if __name__ == "__main__":
    main()