File size: 43,115 Bytes
4b35e49
 
 
 
 
 
 
 
 
 
 
 
d919881
 
 
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
 
 
 
 
 
 
 
 
 
 
 
4b35e49
 
 
d919881
4b35e49
d919881
 
 
 
4b35e49
d919881
 
4b35e49
d919881
 
 
4b35e49
d919881
4b35e49
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
d919881
4b35e49
 
 
 
 
d919881
4b35e49
d919881
 
 
4b35e49
 
d919881
 
4b35e49
d919881
 
 
4b35e49
d919881
 
 
 
 
 
 
 
 
 
4b35e49
 
 
 
 
 
 
 
d919881
4b35e49
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
d919881
 
 
 
 
4b35e49
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
 
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
 
4b35e49
 
d919881
 
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
d919881
 
 
 
4b35e49
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
d919881
4b35e49
 
 
 
 
 
d919881
 
4b35e49
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
 
4b35e49
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
d919881
 
 
 
 
 
4b35e49
 
d919881
4b35e49
 
 
d919881
4b35e49
 
d919881
4b35e49
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
d919881
4b35e49
 
 
d919881
4b35e49
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
 
4b35e49
d919881
4b35e49
 
d919881
 
4b35e49
 
 
 
 
d919881
4b35e49
 
 
d919881
4b35e49
 
 
 
d919881
 
 
 
 
 
4b35e49
 
d919881
4b35e49
 
 
d919881
4b35e49
 
d919881
4b35e49
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
d919881
4b35e49
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
d919881
4b35e49
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
 
 
 
4b35e49
 
d919881
 
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
d919881
4b35e49
 
 
d919881
4b35e49
 
 
 
 
 
d919881
4b35e49
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
d919881
4b35e49
 
 
 
 
d919881
4b35e49
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919881
4b35e49
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from PIL import Image
import io
import numpy as np
import tempfile
import os
import torch
import torch.nn as nn
from torchvision import transforms, models
import torch.nn.functional as F

# Import the Google Drive model manager
from simple_model_manager import SimpleModelManager

# Page configuration
st.set_page_config(
    page_title="Sentiment Analysis Testing Ground",
    page_icon="🧠",
    layout="wide",
    initial_sidebar_state="expanded",
)

# Custom CSS for better styling
st.markdown(
    """
<style>
    .main-header {
        font-size: 2.5rem;
        font-weight: bold;
        color: #1f77b4;
        text-align: center;
        margin-bottom: 2rem;
    }
    .model-card {
        background-color: #f0f2f6;
        padding: 1.5rem;
        border-radius: 10px;
        margin: 1rem 0;
        border-left: 4px solid #1f77b4;
    }
    .result-box {
        background-color: #e8f4fd;
        padding: 1rem;
        border-radius: 8px;
        border: 1px solid #1f77b4;
        margin: 1rem 0;
    }
    .upload-section {
        background-color: #f8f9fa;
        padding: 1.5rem;
        border-radius: 10px;
        border: 2px dashed #dee2e6;
        text-align: center;
        margin: 1rem 0;
    }
</style>
""",
    unsafe_allow_html=True,
)


# Initialize the Google Drive model manager
@st.cache_resource
def get_model_manager():
    """Get the Google Drive model manager instance"""
    try:
        manager = SimpleModelManager()
        return manager
    except Exception as e:
        st.error(f"Failed to initialize model manager: {e}")
        return None


# Global variables for models
@st.cache_resource
def load_vision_model():
    """Load the pre-trained ResNet-50 vision sentiment model from Google Drive"""
    try:
        manager = get_model_manager()
        if manager is None:
            st.error("Model manager not available")
            return None, None, None

        # Load the model using the Google Drive manager
        model, device, num_classes = manager.load_vision_model()

        if model is None:
            st.error("Failed to load vision model from Google Drive")
            return None, None, None

        st.success(f"Vision model loaded successfully with {num_classes} classes!")
        return model, device, num_classes
    except Exception as e:
        st.error(f"Error loading vision model: {str(e)}")
        return None, None, None


@st.cache_data
def get_vision_transforms():
    """Get the image transforms used during FER2013 training"""
    return transforms.Compose(
        [
            transforms.Resize(224),  # Match training: transforms.Resize(224)
            transforms.CenterCrop(224),  # Match training: transforms.CenterCrop(224)
            transforms.ToTensor(),
            transforms.Normalize(
                mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
            ),  # ImageNet normalization
        ]
    )


def detect_and_preprocess_face(image, crop_tightness=0.05):
    """
    Detect face in image, crop to face region, convert to grayscale, and resize to 224x224
    to match FER2013 dataset format (grayscale converted to 3-channel RGB)

    Args:
        image: Input image (PIL Image or numpy array)
        crop_tightness: Padding around face (0.0 = no padding, 0.3 = 30% padding)
    """
    try:
        import cv2
        import numpy as np

        # Convert PIL image to OpenCV format
        if isinstance(image, Image.Image):
            # Convert PIL to numpy array
            img_array = np.array(image)
            # Convert RGB to BGR for OpenCV
            if len(img_array.shape) == 3:
                img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
        else:
            img_array = image

        # Load face detection cascade
        face_cascade = cv2.CascadeClassifier(
            cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
        )

        # Convert to grayscale for face detection (detection works better on grayscale)
        gray = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)

        # Detect faces
        faces = face_cascade.detectMultiScale(
            gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)
        )

        if len(faces) == 0:
            st.warning("No face detected in the image. Using center crop instead.")
            # Fallback: center crop and resize
            if isinstance(image, Image.Image):
                # Convert to RGB first
                rgb_pil = image.convert("RGB")
                # Center crop to square
                width, height = rgb_pil.size
                size = min(width, height)
                left = (width - size) // 2
                top = (height - size) // 2
                right = left + size
                bottom = top + size
                cropped = rgb_pil.crop((left, top, right, bottom))
                # Resize to 224x224 (matching FER2013 training: transforms.Resize(224))
                resized = cropped.resize((224, 224), Image.Resampling.LANCZOS)

                # Convert to grayscale and then to 3-channel RGB
                gray_pil = resized.convert("L")
                # Convert back to RGB (this replicates grayscale values to all 3 channels)
                gray_rgb_pil = gray_pil.convert("RGB")
                return gray_rgb_pil
            else:
                return None

        # Get the largest face (assuming it's the main subject)
        x, y, w, h = max(faces, key=lambda rect: rect[2] * rect[3])

        # Add padding around the face based on user preference
        padding_x = int(w * crop_tightness)
        padding_y = int(h * crop_tightness)

        # Ensure we don't go out of bounds
        x1 = max(0, x - padding_x)
        y1 = max(0, y - padding_y)
        x2 = min(img_array.shape[1], x + w + padding_x)
        y2 = min(img_array.shape[0], y + h + padding_y)

        # Crop to face region
        face_crop = img_array[y1:y2, x1:x2]

        # Convert BGR to RGB first
        face_crop_rgb = cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB)

        # Convert to grayscale
        face_gray = cv2.cvtColor(face_crop_rgb, cv2.COLOR_RGB2GRAY)

        # Resize to 224x224 (matching FER2013 training: transforms.Resize(224))
        face_resized = cv2.resize(face_gray, (224, 224), interpolation=cv2.INTER_AREA)

        # Convert grayscale to 3-channel RGB (replicate grayscale values)
        face_rgb_3channel = cv2.cvtColor(face_resized, cv2.COLOR_GRAY2RGB)

        # Convert back to PIL Image
        face_pil = Image.fromarray(face_rgb_3channel)

        return face_pil

    except ImportError:
        st.error(
            "OpenCV not installed. Please install it with: pip install opencv-python"
        )
        st.info("Falling back to basic preprocessing...")
        # Fallback: basic grayscale conversion and resize
        if isinstance(image, Image.Image):
            rgb_pil = image.convert("RGB")
            resized = rgb_pil.resize((48, 48), Image.Resampling.LANCZOS)
            # Convert to grayscale and then to 3-channel RGB
            gray_pil = resized.convert("L")
            gray_rgb_pil = gray_pil.convert("RGB")
            return gray_rgb_pil
        return None
    except Exception as e:
        st.error(f"Error in face detection: {str(e)}")
        st.info("Falling back to basic preprocessing...")
        # Fallback: basic grayscale conversion and resize
        if isinstance(image, Image.Image):
            rgb_pil = image.convert("RGB")
            resized = rgb_pil.resize((48, 48), Image.Resampling.LANCZOS)
            # Convert to grayscale and then to 3-channel RGB
            gray_pil = resized.convert("L")
            gray_rgb_pil = gray_pil.convert("RGB")
            return gray_rgb_pil
        return None


def get_sentiment_mapping(num_classes):
    """Get the sentiment mapping based on number of classes"""
    if num_classes == 3:
        return {0: "Negative", 1: "Neutral", 2: "Positive"}
    elif num_classes == 4:
        # Common 4-class emotion mapping
        return {0: "Angry", 1: "Sad", 2: "Happy", 3: "Neutral"}
    elif num_classes == 7:
        # FER2013 7-class emotion mapping
        return {
            0: "Angry",
            1: "Disgust",
            2: "Fear",
            3: "Happy",
            4: "Sad",
            5: "Surprise",
            6: "Neutral",
        }
    else:
        # Generic mapping for unknown number of classes
        return {i: f"Class_{i}" for i in range(num_classes)}


# Placeholder functions for model predictions
def predict_text_sentiment(text):
    """
    Analyze text sentiment using TextBlob
    """
    if not text or text.strip() == "":
        return "No text provided", 0.0

    try:
        from textblob import TextBlob

        # Create TextBlob object
        blob = TextBlob(text)

        # Get polarity (-1 to 1, where -1 is very negative, 1 is very positive)
        polarity = blob.sentiment.polarity

        # Get subjectivity (0 to 1, where 0 is very objective, 1 is very subjective)
        subjectivity = blob.sentiment.subjectivity

        # Convert polarity to sentiment categories
        if polarity > 0.1:
            sentiment = "Positive"
            confidence = min(0.95, 0.6 + abs(polarity) * 0.3)
        elif polarity < -0.1:
            sentiment = "Negative"
            confidence = min(0.95, 0.6 + abs(polarity) * 0.3)
        else:
            sentiment = "Neutral"
            confidence = 0.7 - abs(polarity) * 0.2

        # Round confidence to 2 decimal places
        confidence = round(confidence, 2)

        return sentiment, confidence

    except ImportError:
        st.error("TextBlob not installed. Please install it with: pip install textblob")
        return "TextBlob not available", 0.0
    except Exception as e:
        st.error(f"Error in text sentiment analysis: {str(e)}")
        return "Error occurred", 0.0


@st.cache_resource
def load_audio_model():
    """Load the pre-trained Wav2Vec2 audio sentiment model from Google Drive"""
    try:
        manager = get_model_manager()
        if manager is None:
            st.error("Model manager not available")
            return None, None, None, None

        # Load the model using the Google Drive manager
        model, device = manager.load_audio_model()

        if model is None:
            st.error("Failed to load audio model from Google Drive")
            return None, None, None, None

        # For Wav2Vec2 models, we need to determine the number of classes
        # This is typically available in the model configuration
        try:
            num_classes = model.config.num_labels
        except:
            # Fallback: try to infer from the model
            try:
                num_classes = model.classifier.out_features
            except:
                num_classes = 3  # Default assumption

        # Load feature extractor
        from transformers import AutoFeatureExtractor

        feature_extractor = AutoFeatureExtractor.from_pretrained(
            "facebook/wav2vec2-base"
        )

        st.success(f"Audio model loaded successfully with {num_classes} classes!")
        return model, device, num_classes, feature_extractor
    except Exception as e:
        st.error(f"Error loading audio model: {str(e)}")
        return None, None, None, None


def predict_audio_sentiment(audio_bytes):
    """
    Analyze audio sentiment using fine-tuned Wav2Vec2 model
    Preprocessing matches CREMA-D + RAVDESS training specifications:
    - Target sampling rate: 16kHz
    - Max duration: 5.0 seconds
    - Feature extraction: AutoFeatureExtractor with max_length, truncation, padding
    """
    if audio_bytes is None:
        return "No audio provided", 0.0

    try:
        # Load model if not already loaded
        model, device, num_classes, feature_extractor = load_audio_model()
        if model is None:
            return "Model not loaded", 0.0

        # Load and preprocess audio
        import librosa
        import io
        import tempfile

        # Save audio bytes to temporary file
        with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
            tmp_file.write(audio_bytes)
            tmp_file_path = tmp_file.name

        try:
            # Load audio with librosa
            audio, sr = librosa.load(tmp_file_path, sr=None)

            # Resample to 16kHz if needed
            if sr != 16000:
                audio = librosa.resample(y=audio, orig_sr=sr, target_sr=16000)

            # Preprocess with feature extractor (matching CREMA-D + RAVDESS training exactly)
            # From training: max_length=int(max_duration_s * TARGET_SAMPLING_RATE) = 5.0 * 16000
            inputs = feature_extractor(
                audio,
                sampling_rate=16000,
                max_length=int(5.0 * 16000),  # 5 seconds max (matching training)
                truncation=True,
                padding="max_length",
                return_tensors="pt",
            )

            # Move to device
            input_values = inputs.input_values.to(device)

            # Run inference
            with torch.no_grad():
                outputs = model(input_values)
                probabilities = torch.softmax(outputs.logits, dim=1)
                confidence, predicted = torch.max(probabilities, 1)

                # Get sentiment mapping based on number of classes
                if num_classes == 3:
                    sentiment_map = {0: "Negative", 1: "Neutral", 2: "Positive"}
                else:
                    # Generic mapping for unknown number of classes
                    sentiment_map = {i: f"Class_{i}" for i in range(num_classes)}

                sentiment = sentiment_map[predicted.item()]
                confidence_score = confidence.item()

            return sentiment, confidence_score

        finally:
            # Clean up temporary file
            os.unlink(tmp_file_path)

    except ImportError as e:
        st.error(f"Required library not installed: {str(e)}")
        st.info("Please install: pip install librosa transformers")
        return "Library not available", 0.0
    except Exception as e:
        st.error(f"Error in audio sentiment prediction: {str(e)}")
        return "Error occurred", 0.0


def predict_vision_sentiment(image, crop_tightness=0.05):
    """
    Load ResNet-50 and run inference for vision sentiment analysis

    Args:
        image: Input image (PIL Image or numpy array)
        crop_tightness: Padding around face (0.0 = no padding, 0.3 = 30% padding)
    """
    if image is None:
        return "No image provided", 0.0

    try:
        # Load model if not already loaded
        model, device, num_classes = load_vision_model()
        if model is None:
            return "Model not loaded", 0.0

        # Preprocess image to match FER2013 format
        st.info(
            "Detecting face and preprocessing image to match training data format..."
        )
        preprocessed_image = detect_and_preprocess_face(image, crop_tightness=0.0)

        if preprocessed_image is None:
            return "Image preprocessing failed", 0.0

        # Show preprocessed image
        st.image(
            preprocessed_image,
            caption="Preprocessed Image (48x48 Grayscale β†’ 3-channel RGB)",
            width=200,
        )

        # Get transforms
        transform = get_vision_transforms()

        # Convert preprocessed image to tensor
        image_tensor = transform(preprocessed_image).unsqueeze(0).to(device)

        # Run inference
        with torch.no_grad():
            outputs = model(image_tensor)

            # Debug: print output shape
            st.info(f"Model output shape: {outputs.shape}")

            probabilities = F.softmax(outputs, dim=1)
            confidence, predicted = torch.max(probabilities, 1)

            # Get sentiment mapping based on number of classes
            sentiment_map = get_sentiment_mapping(num_classes)
            sentiment = sentiment_map[predicted.item()]
            confidence_score = confidence.item()

        return sentiment, confidence_score

    except Exception as e:
        st.error(f"Error in vision sentiment prediction: {str(e)}")
        st.error(
            f"Model output shape mismatch. Expected {num_classes} classes but got different."
        )
        return "Error occurred", 0.0


def predict_fused_sentiment(text=None, audio_bytes=None, image=None):
    """
    TODO: Implement ensemble/fusion logic combining all three models
    This is a placeholder function for fused sentiment analysis
    """
    # Placeholder logic - replace with actual fusion implementation
    results = []

    if text:
        text_sentiment, text_conf = predict_text_sentiment(text)
        results.append((text_sentiment, text_conf))

    if audio_bytes:
        audio_sentiment, audio_conf = predict_audio_sentiment(audio_bytes)
        results.append((audio_sentiment, audio_conf))

    if image:
        vision_sentiment, vision_conf = predict_vision_sentiment(image)
        results.append((vision_sentiment, vision_conf))

    if not results:
        return "No inputs provided", 0.0

    # Simple ensemble logic (replace with your fusion strategy)
    sentiment_counts = {}
    total_confidence = 0

    for sentiment, confidence in results:
        sentiment_counts[sentiment] = sentiment_counts.get(sentiment, 0) + 1
        total_confidence += confidence

    # Majority voting with confidence averaging
    final_sentiment = max(sentiment_counts, key=sentiment_counts.get)
    avg_confidence = total_confidence / len(results)

    return final_sentiment, avg_confidence


# Sidebar navigation
st.sidebar.title("Sentiment Analysis")
st.sidebar.markdown("---")

# Navigation
page = st.sidebar.selectbox(
    "Choose a page:",
    [
        "Home",
        "Text Sentiment",
        "Audio Sentiment",
        "Vision Sentiment",
        "Fused Model",
    ],
)

# Home Page
if page == "Home":
    st.markdown(
        '<h1 class="main-header">Sentiment Analysis Testing Ground</h1>',
        unsafe_allow_html=True,
    )

    st.markdown(
        """
    <div class="model-card">
        <h2>Welcome to your Multi-Modal Sentiment Analysis Testing Platform!</h2>
        <p>This application provides a comprehensive testing environment for your three independent sentiment analysis models:</p>
    </div>
    """,
        unsafe_allow_html=True,
    )

    col1, col2, col3 = st.columns(3)

    with col1:
        st.markdown(
            """
        <div class="model-card">
            <h3>Text Sentiment Model</h3>
            <p>READY TO USE - Analyze sentiment from text input using TextBlob</p>
                         <ul>
                 <li>Process any text input</li>
                 <li>Get sentiment classification (Positive/Negative/Neutral)</li>
                 <li>View confidence scores</li>
                 <li>Real-time NLP analysis</li>
             </ul>
        </div>
        """,
            unsafe_allow_html=True,
        )

    with col2:
        st.markdown(
            """
        <div class="model-card">
            <h3>Audio Sentiment Model</h3>
            <p>READY TO USE - Analyze sentiment from audio files using fine-tuned Wav2Vec2</p>
                         <ul>
                 <li>Upload audio files (.wav, .mp3, .m4a, .flac)</li>
                 <li>Record audio directly with microphone (max 5s)</li>
                 <li>Automatic preprocessing: 16kHz sampling, 5s max duration (CREMA-D + RAVDESS format)</li>
                 <li>Listen to uploaded/recorded audio</li>
                 <li>Get sentiment predictions</li>
                 <li>Real-time audio analysis</li>
             </ul>
        </div>
        """,
            unsafe_allow_html=True,
        )

    with col3:
        st.markdown(
            """
        <div class="model-card">
            <h3>Vision Sentiment Model</h3>
            <p>Analyze sentiment from images using fine-tuned ResNet-50</p>
                         <ul>
                 <li>Upload image files (.png, .jpg, .jpeg, .bmp, .tiff)</li>
                 <li>Automatic face detection & preprocessing</li>
                 <li>Fixed 0% padding for tightest face crop</li>
                 <li>Convert to 224x224 grayscale β†’ 3-channel RGB (FER2013 format)</li>
                 <li>Transforms: Resize(224) β†’ CenterCrop(224) β†’ ImageNet Normalization</li>
                 <li>Preview original & preprocessed images</li>
                 <li>Get sentiment predictions</li>
             </ul>
        </div>
        """,
            unsafe_allow_html=True,
        )

    st.markdown(
        """
    <div class="model-card">
        <h3>Fused Model</h3>
        <p>Combine predictions from all three models for enhanced accuracy</p>
        <ul>
            <li>Multi-modal input processing</li>
            <li>Ensemble prediction strategies</li>
            <li>Comprehensive sentiment analysis</li>
        </ul>
    </div>
    """,
        unsafe_allow_html=True,
    )

    st.markdown("---")
    st.markdown(
        """
    <div style="text-align: center; color: #666;">
        <p><strong>Note:</strong> This application now has <strong>ALL THREE MODELS</strong> fully integrated and ready to use!</p>
        <p><strong>TextBlob</strong> (Text) + <strong>Wav2Vec2</strong> (Audio) + <strong>ResNet-50</strong> (Vision)</p>
        <p><strong>Models are now loaded from Google Drive automatically!</strong></p>
    </div>
    """,
        unsafe_allow_html=True,
    )

# Text Sentiment Page
elif page == "Text Sentiment":
    st.title("Text Sentiment Analysis")
    st.markdown("Analyze the sentiment of your text using our TextBlob-based model.")

    # Text input
    text_input = st.text_area(
        "Enter your text here:",
        height=150,
        placeholder="Type or paste your text here to analyze its sentiment...",
    )

    # Analyze button
    if st.button("Analyze Sentiment", type="primary", use_container_width=True):
        if text_input and text_input.strip():
            with st.spinner("Analyzing text sentiment..."):
                sentiment, confidence = predict_text_sentiment(text_input)

                # Display results
                st.markdown("### Results")

                # Display results in columns
                col1, col2 = st.columns(2)
                with col1:
                    st.metric("Sentiment", sentiment)
                with col2:
                    st.metric("Confidence", f"{confidence:.2f}")

                # Color-coded sentiment display
                sentiment_colors = {
                    "Positive": "🟒",
                    "Negative": "πŸ”΄",
                    "Neutral": "🟑",
                }

                st.markdown(
                    f"""
                <div class="result-box">
                    <h4>{sentiment_colors.get(sentiment, "❓")} Sentiment: {sentiment}</h4>
                    <p><strong>Confidence:</strong> {confidence:.2f}</p>
                    <p><strong>Input Text:</strong> "{text_input[:100]}{'...' if len(text_input) > 100 else ''}"</p>
                    <p><strong>Model:</strong> TextBlob (Natural Language Processing)</p>
                </div>
                """,
                    unsafe_allow_html=True,
                )
        else:
            st.error("Please enter some text to analyze.")

# Audio Sentiment Page
elif page == "Audio Sentiment":
    st.title("Audio Sentiment Analysis")
    st.markdown(
        "Analyze the sentiment of your audio files using our fine-tuned Wav2Vec2 model."
    )

    # Preprocessing information
    st.info(
        "**Audio Preprocessing**: Audio will be automatically processed to match CREMA-D + RAVDESS training format: "
        "16kHz sampling rate, max 5 seconds, with automatic resampling and feature extraction."
    )

    # Model status
    model, device, num_classes, feature_extractor = load_audio_model()
    if model is None:
        st.error(
            "Audio model could not be loaded. Please check the Google Drive setup."
        )
        st.info(
            "Expected: Models should be configured in Google Drive and accessible via the model manager."
        )
    else:
        st.success(
            f"Audio model loaded successfully on {device} with {num_classes} classes!"
        )

    # Input method selection
    st.subheader("Choose Input Method")
    input_method = st.radio(
        "Select how you want to provide audio:",
        ["Upload Audio File", "Record Audio"],
        horizontal=True,
    )

    if input_method == "Upload Audio File":
        # File uploader
        uploaded_audio = st.file_uploader(
            "Choose an audio file",
            type=["wav", "mp3", "m4a", "flac"],
            help="Supported formats: WAV, MP3, M4A, FLAC",
        )

        audio_source = "uploaded_file"
        audio_name = uploaded_audio.name if uploaded_audio else None

    else:  # Audio recording
        st.markdown(
            """
        <div class="model-card">
            <h3>Audio Recording</h3>
            <p>Record audio directly with your microphone (max 5 seconds).</p>
            <p><strong>Note:</strong> Make sure your microphone is accessible and you have permission to use it.</p>
        </div>
        """,
            unsafe_allow_html=True,
        )

        # Audio recorder
        recorded_audio = st.audio_input(
            label="Click to start recording",
            help="Click the microphone button to start/stop recording. Maximum recording time is 5 seconds.",
        )

        if recorded_audio is not None:
            # Display recorded audio
            st.audio(recorded_audio, format="audio/wav")
            st.success("Audio recorded successfully!")

            # Convert recorded audio to bytes for processing
            uploaded_audio = recorded_audio
            audio_source = "recorded"
            audio_name = "Recorded Audio"
        else:
            uploaded_audio = None
            audio_source = None
            audio_name = None

    if uploaded_audio is not None:
        # Display audio player
        if audio_source == "recorded":
            st.audio(uploaded_audio, format="audio/wav")
            st.info(f"{audio_name} | Source: Microphone Recording")
        else:
            st.audio(
                uploaded_audio, format=f'audio/{uploaded_audio.name.split(".")[-1]}'
            )
            # File info for uploaded files
            file_size = len(uploaded_audio.getvalue()) / 1024  # KB
            st.info(f"File: {uploaded_audio.name} | Size: {file_size:.1f} KB")

        # Analyze button
        if st.button(
            "Analyze Audio Sentiment", type="primary", use_container_width=True
        ):
            if model is None:
                st.error("Model not loaded. Cannot analyze audio.")
            else:
                with st.spinner("Analyzing audio sentiment..."):
                    audio_bytes = uploaded_audio.getvalue()
                    sentiment, confidence = predict_audio_sentiment(audio_bytes)

                # Display results
                st.markdown("### Results")

                col1, col2 = st.columns(2)
                with col1:
                    st.metric("Sentiment", sentiment)
                with col2:
                    st.metric("Confidence", f"{confidence:.2f}")

                # Color-coded sentiment display
                sentiment_colors = {"Positive": "🟒", "Negative": "πŸ”΄", "Neutral": "🟑"}

                st.markdown(
                    f"""
                <div class="result-box">
                    <h4>{sentiment_colors.get(sentiment, "❓")} Sentiment: {sentiment}</h4>
                    <p><strong>Confidence:</strong> {confidence:.2f}</p>
                    <p><strong>Audio Source:</strong> {audio_name}</p>
                    <p><strong>Model:</strong> Wav2Vec2 (Fine-tuned on RAVDESS + CREMA-D)</p>
                </div>
                """,
                    unsafe_allow_html=True,
                )
    else:
        if input_method == "Upload Audio File":
            st.info("Please upload an audio file to begin analysis.")
        else:
            st.info("Click the microphone button above to record audio for analysis.")

# Vision Sentiment Page
elif page == "Vision Sentiment":
    st.title("Vision Sentiment Analysis")
    st.markdown(
        "Analyze the sentiment of your images using our fine-tuned ResNet-50 model."
    )

    st.info(
        "**Note**: Images will be automatically preprocessed to match FER2013 format: face detection, grayscale conversion, and 224x224 resize (converted to 3-channel RGB)."
    )

    # Face cropping is set to 0% (no padding) for tightest crop
    st.info("**Face Cropping**: Set to 0% padding for tightest crop on facial features")

    # Model status
    model, device, num_classes = load_vision_model()
    if model is None:
        st.error(
            "Vision model could not be loaded. Please check the Google Drive setup."
        )
        st.info(
            "Expected: Models should be configured in Google Drive and accessible via the model manager."
        )
    else:
        st.success(
            f"Vision model loaded successfully on {device} with {num_classes} classes!"
        )

    # Input method selection
    st.subheader("Choose Input Method")
    input_method = st.radio(
        "Select how you want to provide an image:",
        ["Upload Image File", "Take Photo with Camera"],
        horizontal=True,
    )

    if input_method == "Upload Image File":
        # File uploader
        uploaded_image = st.file_uploader(
            "Choose an image file",
            type=["png", "jpg", "jpeg", "bmp", "tiff"],
            help="Supported formats: PNG, JPG, JPEG, BMP, TIFF",
        )

        if uploaded_image is not None:
            # Display image
            image = Image.open(uploaded_image)
            st.image(
                image,
                caption=f"Uploaded Image: {uploaded_image.name}",
                use_container_width=True,
            )

            # File info
            file_size = len(uploaded_image.getvalue()) / 1024  # KB
            st.info(
                f"File: {uploaded_image.name} | Size: {file_size:.1f} KB | Dimensions: {image.size[0]}x{image.size[1]}"
            )

            # Analyze button
            if st.button(
                "Analyze Image Sentiment", type="primary", use_container_width=True
            ):
                if model is None:
                    st.error("Model not loaded. Cannot analyze image.")
                else:
                    with st.spinner("Analyzing image sentiment..."):
                        sentiment, confidence = predict_vision_sentiment(image)

                        # Display results
                        st.markdown("### Results")

                        col1, col2 = st.columns(2)
                        with col1:
                            st.metric("Sentiment", sentiment)
                        with col2:
                            st.metric("Confidence", f"{confidence:.2f}")

                        # Color-coded sentiment display
                        sentiment_colors = {
                            "Positive": "🟒",
                            "Negative": "πŸ”΄",
                            "Neutral": "🟑",
                        }

                        st.markdown(
                            f"""
                        <div class="result-box">
                            <h4>{sentiment_colors.get(sentiment, "❓")} Sentiment: {sentiment}</h4>
                            <p><strong>Confidence:</strong> {confidence:.2f}</p>
                            <p><strong>Image File:</strong> {uploaded_image.name}</p>
                            <p><strong>Model:</strong> ResNet-50 (Fine-tuned on FER2013)</p>
                        </div>
                        """,
                            unsafe_allow_html=True,
                        )

    else:  # Camera capture
        st.markdown(
            """
        <div class="model-card">
            <h3>Camera Capture</h3>
            <p>Take a photo directly with your camera to analyze its sentiment.</p>
            <p><strong>Note:</strong> Make sure your camera is accessible and you have permission to use it.</p>
        </div>
        """,
            unsafe_allow_html=True,
        )

        # Camera input
        camera_photo = st.camera_input(
            "Take a photo",
            help="Click the camera button to take a photo, or use the upload button to select an existing photo",
        )

        if camera_photo is not None:
            # Display captured image
            image = Image.open(camera_photo)
            st.image(
                image,
                caption="Captured Photo",
                use_container_width=True,
            )

            # Image info
            st.info(
                f"Captured Photo | Dimensions: {image.size[0]}x{image.size[1]} | Format: {image.format}"
            )

            # Analyze button
            if st.button(
                "Analyze Photo Sentiment", type="primary", use_container_width=True
            ):
                if model is None:
                    st.error("Model not loaded. Cannot analyze image.")
                else:
                    with st.spinner("Analyzing photo sentiment..."):
                        sentiment, confidence = predict_vision_sentiment(image)

                        # Display results
                        st.markdown("### Results")

                        col1, col2 = st.columns(2)
                        with col1:
                            st.metric("Sentiment", sentiment)
                        with col2:
                            st.metric("Confidence", f"{confidence:.2f}")

                        # Color-coded sentiment display
                        sentiment_colors = {
                            "Positive": "🟒",
                            "Negative": "πŸ”΄",
                            "Neutral": "🟑",
                        }

                        st.markdown(
                            f"""
                        <div class="result-box">
                            <h4>{sentiment_colors.get(sentiment, "❓")} Sentiment: {sentiment}</h4>
                            <p><strong>Confidence:</strong> {confidence:.2f}</p>
                            <p><strong>Image Source:</strong> Camera Capture</p>
                            <p><strong>Model:</strong> ResNet-50 (Fine-tuned on FER2013)</p>
                        </div>
                        """,
                            unsafe_allow_html=True,
                        )

    # Show info if no image is provided
    if input_method == "Upload Image File" and "uploaded_image" not in locals():
        st.info("Please upload an image file to begin analysis.")
    elif input_method == "Take Photo with Camera" and "camera_photo" not in locals():
        st.info("Click the camera button above to take a photo for analysis.")

# Fused Model Page
elif page == "Fused Model":
    st.title("Fused Model Analysis")
    st.markdown(
        "Combine predictions from all three models for enhanced sentiment analysis."
    )

    st.markdown(
        """
    <div class="model-card">
        <h3>Multi-Modal Sentiment Analysis</h3>
        <p>This page allows you to input text, audio, and/or image data to get a comprehensive sentiment analysis 
        using all three models combined.</p>
    </div>
    """,
        unsafe_allow_html=True,
    )

    # Input sections
    col1, col2 = st.columns(2)

    with col1:
        st.subheader("Text Input")
        text_input = st.text_area(
            "Enter text (optional):",
            height=100,
            placeholder="Type or paste your text here...",
        )

        st.subheader("Audio Input")

        # Audio preprocessing information for fused model
        st.info(
            "**Audio Preprocessing**: Audio will be automatically processed to match CREMA-D + RAVDESS training format: "
            "16kHz sampling rate, max 5 seconds, with automatic resampling and feature extraction."
        )

        # Audio input method for fused model
        audio_input_method = st.radio(
            "Audio input method:",
            ["Upload File", "Record Audio"],
            key="fused_audio_method",
            horizontal=True,
        )

        if audio_input_method == "Upload File":
            uploaded_audio = st.file_uploader(
                "Upload audio file (optional):",
                type=["wav", "mp3", "m4a", "flac"],
                key="fused_audio",
            )
            audio_source = "uploaded_file"
            audio_name = uploaded_audio.name if uploaded_audio else None
        else:
            # Audio recorder for fused model
            recorded_audio = st.audio_input(
                label="Record audio (optional):",
                key="fused_audio_recorder",
                help="Click to record audio for sentiment analysis",
            )

            if recorded_audio is not None:
                st.audio(recorded_audio, format="audio/wav")
                st.success("Audio recorded successfully!")
                uploaded_audio = recorded_audio
                audio_source = "recorded"
                audio_name = "Recorded Audio"
            else:
                uploaded_audio = None
                audio_source = None
                audio_name = None

    with col2:
        st.subheader("Image Input")

        # Face cropping is set to 0% (no padding) for tightest crop
        st.info(
            "**Face Cropping**: Set to 0% padding for tightest crop on facial features"
        )

        # Image input method for fused model
        image_input_method = st.radio(
            "Image input method:",
            ["Upload File", "Take Photo"],
            key="fused_image_method",
            horizontal=True,
        )

        if image_input_method == "Upload File":
            uploaded_image = st.file_uploader(
                "Upload image file (optional):",
                type=["png", "jpg", "jpeg", "bmp", "tiff"],
                key="fused_image",
            )

            if uploaded_image:
                image = Image.open(uploaded_image)
                st.image(image, caption="Uploaded Image", use_container_width=True)
        else:
            # Camera capture for fused model
            camera_photo = st.camera_input(
                "Take a photo (optional):",
                key="fused_camera",
                help="Click to take a photo for sentiment analysis",
            )

            if camera_photo:
                image = Image.open(camera_photo)
                st.image(image, caption="Captured Photo", use_container_width=True)
                # Set uploaded_image to camera_photo for processing
                uploaded_image = camera_photo

        if uploaded_audio:
            st.audio(
                uploaded_audio, format=f'audio/{uploaded_audio.name.split(".")[-1]}'
            )

    # Analyze button
    if st.button("Run Fused Analysis", type="primary", use_container_width=True):
        if text_input or uploaded_audio or uploaded_image:
            with st.spinner("Running fused sentiment analysis..."):
                # Prepare inputs
                audio_bytes = uploaded_audio.getvalue() if uploaded_audio else None
                image = Image.open(uploaded_image) if uploaded_image else None

                # Get fused prediction
                sentiment, confidence = predict_fused_sentiment(
                    text=text_input if text_input else None,
                    audio_bytes=audio_bytes,
                    image=image,
                )

                # Display results
                st.markdown("### Fused Model Results")

                col1, col2 = st.columns(2)
                with col1:
                    st.metric("Final Sentiment", sentiment)
                with col2:
                    st.metric("Overall Confidence", f"{confidence:.2f}")

                # Show individual model results
                st.markdown("### Individual Model Results")

                results_data = []

                if text_input:
                    text_sentiment, text_conf = predict_text_sentiment(text_input)
                    results_data.append(
                        {
                            "Model": "Text (TextBlob)",
                            "Input": f"Text: {text_input[:50]}...",
                            "Sentiment": text_sentiment,
                            "Confidence": f"{text_conf:.2f}",
                        }
                    )

                if uploaded_audio:
                    audio_sentiment, audio_conf = predict_audio_sentiment(audio_bytes)
                    results_data.append(
                        {
                            "Model": "Audio (Wav2Vec2)",
                            "Input": f"Audio: {audio_name}",
                            "Sentiment": audio_sentiment,
                            "Confidence": f"{audio_conf:.2f}",
                        }
                    )

                if uploaded_image:
                    # Face cropping is set to 0% (no padding) for tightest crop
                    vision_sentiment, vision_conf = predict_vision_sentiment(
                        image, crop_tightness=0.0
                    )
                    results_data.append(
                        {
                            "Model": "Vision (ResNet-50)",
                            "Input": f"Image: {uploaded_image.name}",
                            "Sentiment": vision_sentiment,
                            "Confidence": f"{vision_conf:.2f}",
                        }
                    )

                if results_data:
                    df = pd.DataFrame(results_data)
                    st.dataframe(df, use_container_width=True)

                # Final result display
                sentiment_colors = {"Positive": "🟒", "Negative": "πŸ”΄", "Neutral": "🟑"}

                st.markdown(
                    f"""
                <div class="result-box">
                    <h4>{sentiment_colors.get(sentiment, "❓")} Final Fused Sentiment: {sentiment}</h4>
                    <p><strong>Overall Confidence:</strong> {confidence:.2f}</p>
                    <p><strong>Models Used:</strong> {len(results_data)}</p>
                </div>
                """,
                    unsafe_allow_html=True,
                )
        else:
            st.warning(
                "Please provide at least one input (text, audio, or image) for fused analysis."
            )

# Footer
st.markdown("---")
st.markdown(
    """
<div style="text-align: center; color: #666; padding: 1rem;">
    <p>Built with ❀️ | by <a href="https://github.com/iamfaham">iamfaham</a></p>
</div>
""",
    unsafe_allow_html=True,
)