File size: 47,918 Bytes
2247005
567d64c
 
 
 
 
095ee1e
 
 
9611f6e
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567d64c
 
095ee1e
567d64c
 
 
 
 
 
 
 
 
 
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567d64c
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567d64c
 
095ee1e
567d64c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567d64c
2247005
567d64c
 
 
 
 
 
 
 
 
095ee1e
2247005
 
095ee1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2247005
 
095ee1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567d64c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
095ee1e
567d64c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
095ee1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567d64c
 
2247005
 
 
 
567d64c
2247005
 
 
 
 
 
567d64c
 
2247005
567d64c
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567d64c
 
 
 
 
2247005
567d64c
 
 
 
 
 
2247005
095ee1e
2247005
 
 
095ee1e
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b83924
2247005
567d64c
 
2247005
 
 
 
 
 
567d64c
2247005
 
 
 
 
 
567d64c
2247005
 
 
 
 
567d64c
2247005
 
 
 
 
 
567d64c
 
2247005
567d64c
 
 
2247005
567d64c
 
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567d64c
 
 
 
 
 
 
 
 
 
 
 
2247005
 
 
 
 
567d64c
 
2247005
 
 
 
 
 
 
 
 
 
 
 
567d64c
 
 
 
 
095ee1e
2247005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
095ee1e
 
2247005
 
 
 
 
 
 
 
567d64c
 
 
2247005
095ee1e
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
# Standard library imports
import os
import tempfile
import uuid
import base64
import io
import json
import re
from datetime import datetime, timedelta

# Third-party imports
import gradio as gr
import groq
import numpy as np
import pandas as pd
import openpyxl
import requests
import fitz  # PyMuPDF
from PIL import Image
from dotenv import load_dotenv
from transformers import AutoProcessor, AutoModelForVision2Seq
import torch
import sass
from pathlib import Path
import pyttsx3
import speech_recognition as sr

# LangChain imports
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Load environment variables
load_dotenv()
client = groq.Client(api_key=os.getenv("GROQ_TECH_API_KEY"))
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

# Directory to store FAISS indexes
FAISS_INDEX_DIR = "faiss_indexes_tech"
if not os.path.exists(FAISS_INDEX_DIR):
    os.makedirs(FAISS_INDEX_DIR)

# Dictionary to store user-specific vectorstores
user_vectorstores = {}

# Advanced SCSS with cyberpunk styling
CYBERPUNK_SCSS = """
// Advanced Cyberpunk Theme with Neural Network Aesthetics
@use "sass:math";
@use "sass:color";

// Neural Color System
$neural-colors: (
    'synapse-blue': #00F3FF,
    'neural-red': #FF0033,
    'data-yellow': #FFE600,
    'matrix-green': #00FF9F,
    'void-black': #0D0D0D,
    'deep-void': #080808,
    'neural-white': #E6E6E6,
    'grid-alpha': 0.1
);

// Dynamic Color Functions
@function neural-glow($color, $intensity: 1) {
    $glow-color: map-get($neural-colors, $color);
    @return (
        0 0 #{10px * $intensity} $glow-color,
        0 0 #{20px * $intensity} $glow-color
    );
}

@function generate-glitch-animation($name, $color1, $color2) {
    @keyframes #{$name} {
        0%, 100% { 
            text-shadow: -2px 0 map-get($neural-colors, $color1), 
                        2px 2px map-get($neural-colors, $color2);
        }
        25% {
            text-shadow: 2px -2px map-get($neural-colors, $color1),
                        -2px -2px map-get($neural-colors, $color2);
        }
        50% {
            text-shadow: 1px 3px map-get($neural-colors, $color1),
                        -3px -1px map-get($neural-colors, $color2);
        }
        75% {
            text-shadow: -3px 1px map-get($neural-colors, $color1),
                        1px -1px map-get($neural-colors, $color2);
        }
    }
}

// Generate Multiple Glitch Animations
#{generate-glitch-animation('neural-glitch', 'synapse-blue', 'neural-red')}
#{generate-glitch-animation('data-glitch', 'data-yellow', 'matrix-green')}

// Advanced Mixins
@mixin neural-container($depth: 1) {
    background: linear-gradient(
        170deg,
        rgba(map-get($neural-colors, 'deep-void'), 0.9),
        rgba(map-get($neural-colors, 'void-black'), 0.95)
    );
    border: #{$depth}px solid map-get($neural-colors, 'synapse-blue');
    box-shadow: neural-glow('synapse-blue', $depth);
    backdrop-filter: blur(5px);
    position: relative;
    overflow: hidden;

    &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
        right: 0;
        height: 1px;
        background: linear-gradient(
            90deg,
            transparent,
            map-get($neural-colors, 'synapse-blue'),
            transparent
        );
        animation: neural-scan 2s linear infinite;
    }
}

@mixin cyber-text($size, $color: 'synapse-blue') {
    font-family: 'Orbitron', 'Rajdhani', sans-serif;
    font-size: $size;
    color: map-get($neural-colors, $color);
    text-transform: uppercase;
    letter-spacing: 2px;
    position: relative;
    text-shadow: 0 0 5px map-get($neural-colors, $color);
}

// Advanced Animations
@keyframes neural-scan {
    0% { transform: translateX(-100%); opacity: 0; }
    50% { opacity: 1; }
    100% { transform: translateX(100%); opacity: 0; }
}

@keyframes data-pulse {
    0%, 100% { opacity: 0.8; transform: scale(1); }
    50% { opacity: 1; transform: scale(1.02); }
}

// Base Styles
body {
    background-color: map-get($neural-colors, 'void-black');
    background-image: 
        linear-gradient(
            rgba(map-get($neural-colors, 'synapse-blue'), 
            map-get($neural-colors, 'grid-alpha')) 1px,
            transparent 1px
        ),
        linear-gradient(
            90deg,
            rgba(map-get($neural-colors, 'synapse-blue'), 
            map-get($neural-colors, 'grid-alpha')) 1px,
            transparent 1px
        );
    background-size: 20px 20px;
    color: map-get($neural-colors, 'neural-white');
}

// Advanced Components
.neural-interface {
    @include neural-container(2);
    padding: 20px;
    margin: 20px;
    clip-path: polygon(
        0 20px,
        20px 0,
        calc(100% - 20px) 0,
        100% 20px,
        100% calc(100% - 20px),
        calc(100% - 20px) 100%,
        20px 100%,
        0 calc(100% - 20px)
    );

    &__header {
        @include cyber-text(2rem);
    text-align: center;
        margin-bottom: 20px;
        animation: neural-glitch 5s infinite;
    }

    &__content {
    position: relative;
        z-index: 1;
    }
}

.data-display {
    @include neural-container(1);
    padding: 15px;
    margin: 10px 0;
    animation: data-pulse 4s infinite;

    &__label {
        @include cyber-text(0.9rem, 'data-yellow');
        margin-bottom: 5px;
    }

    &__value {
        @include cyber-text(1.2rem, 'matrix-green');
    }
}

// Interactive Elements
.neural-button {
    @include neural-container(1);
    padding: 10px 20px;
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover {
        transform: translateY(-2px) scale(1.02);
        box-shadow: neural-glow('synapse-blue', 2);
    }
    
    &:active {
        transform: translateY(1px);
    }
}

// Code Display
.code-matrix {
    @include neural-container(1);
    font-family: 'Source Code Pro', monospace;
    padding: 20px;
    margin: 15px 0;
    
    &__line {
    position: relative;
        padding-left: 20px;
        
        &::before {
            content: '>';
            position: absolute;
            left: 0;
            color: map-get($neural-colors, 'matrix-green');
        }
    }
}

// Status Indicators
.neural-status {
    display: flex;
    align-items: center;
    gap: 10px;
    
    &__indicator {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        background: map-get($neural-colors, 'matrix-green');
        animation: data-pulse 2s infinite;
    }
    
    &__text {
        @include cyber-text(0.9rem, 'matrix-green');
    }
}

// Advanced Grid Layout
.neural-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    padding: 20px;
    
    &__item {
        @include neural-container(1);
        padding: 15px;
        transition: transform 0.3s ease;
        
        &:hover {
            transform: translateZ(20px);
            z-index: 2;
        }
    }
}
"""

# Compile SCSS to CSS
def compile_scss():
    try:
        return sass.compile(string=CYBERPUNK_SCSS)
    except sass.CompileError as e:
        print(f"SCSS Compilation Error: {e}")
        return ""

# Advanced JavaScript for dynamic effects
NEURAL_JS = """
<script>
class NeuralInterface {
    constructor() {
        this.initializeEffects();
        this.setupEventListeners();
    }

    initializeEffects() {
        this.setupGlitchEffects();
        this.setupDataStreams();
        this.setupHolographicEffects();
    }

    setupGlitchEffects() {
        document.querySelectorAll('.neural-interface__header').forEach(element => {
            setInterval(() => {
                if (Math.random() < 0.1) {
                    element.style.transform = `translate(${Math.random() * 4 - 2}px, ${Math.random() * 4 - 2}px)`;
                    setTimeout(() => element.style.transform = 'none', 100);
                }
            }, 2000);
        });
    }

    setupDataStreams() {
        const canvas = document.createElement('canvas');
        document.body.appendChild(canvas);
        canvas.style.position = 'fixed';
        canvas.style.top = '0';
        canvas.style.left = '0';
        canvas.style.width = '100%';
        canvas.style.height = '100%';
        canvas.style.pointerEvents = 'none';
        canvas.style.zIndex = '1';
        canvas.style.opacity = '0.1';

        const ctx = canvas.getContext('2d');
        const matrix = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789@#$%^&*()*&^%";
        const drops = [];

        function initMatrix() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            const columns = canvas.width / 20;
            for(let i = 0; i < columns; i++) drops[i] = 1;
        }

        function drawMatrix() {
            ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            ctx.fillStyle = '#0F0';
            ctx.font = '15px monospace';
            for(let i = 0; i < drops.length; i++) {
                const text = matrix[Math.floor(Math.random() * matrix.length)];
                ctx.fillText(text, i * 20, drops[i] * 20);
                if(drops[i] * 20 > canvas.height && Math.random() > 0.975)
                    drops[i] = 0;
                drops[i]++;
            }
        }

        window.addEventListener('resize', initMatrix);
        initMatrix();
        setInterval(drawMatrix, 50);
    }

    setupHolographicEffects() {
        document.querySelectorAll('.neural-button').forEach(button => {
            button.addEventListener('mousemove', e => {
                const rect = button.getBoundingClientRect();
                const x = e.clientX - rect.left;
                const y = e.clientY - rect.top;
                
                button.style.setProperty('--x', `${x}px`);
                button.style.setProperty('--y', `${y}px`);
            });
        });
    }

    setupEventListeners() {
        document.addEventListener('click', e => {
            if (e.target.closest('.neural-button')) {
                this.createRippleEffect(e);
            }
        });
    }

    createRippleEffect(e) {
        const button = e.target.closest('.neural-button');
        const ripple = document.createElement('span');
        ripple.classList.add('ripple');
        button.appendChild(ripple);
        
        const rect = button.getBoundingClientRect();
        const size = Math.max(rect.width, rect.height);
        ripple.style.width = ripple.style.height = `${size}px`;
        
        const x = e.clientX - rect.left - size/2;
        const y = e.clientY - rect.top - size/2;
        ripple.style.left = `${x}px`;
        ripple.style.top = `${y}px`;
        
        setTimeout(() => ripple.remove(), 600);
    }
}

// Initialize Neural Interface
document.addEventListener('DOMContentLoaded', () => {
    new NeuralInterface();
});
</script>
"""

# Function to process PDF files
def process_pdf(pdf_file):
    if pdf_file is None:
        return None, "No file uploaded", {"page_images": [], "total_pages": 0, "total_words": 0}
    try:
        session_id = str(uuid.uuid4())
        with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_file:
            temp_file.write(pdf_file)
            pdf_path = temp_file.name
        
        doc = fitz.open(pdf_path)
        texts = [page.get_text() for page in doc]
        page_images = []
        for page in doc:
            pix = page.get_pixmap()
            img_bytes = pix.tobytes("png")
            img_base64 = base64.b64encode(img_bytes).decode("utf-8")
            page_images.append(img_base64)
        total_pages = len(doc)
        total_words = sum(len(text.split()) for text in texts)
        doc.close()

        text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
        chunks = text_splitter.create_documents(texts)
        vectorstore = FAISS.from_documents(chunks, embeddings)
        index_path = os.path.join(FAISS_INDEX_DIR, session_id)
        vectorstore.save_local(index_path)
        user_vectorstores[session_id] = vectorstore

        os.unlink(pdf_path)
        pdf_state = {"page_images": page_images, "total_pages": total_pages, "total_words": total_words}
        return session_id, f"βœ… Successfully processed {len(chunks)} text chunks from your PDF", pdf_state
    except Exception as e:
        if "pdf_path" in locals() and os.path.exists(pdf_path):
            os.unlink(pdf_path)
        return None, f"Error processing PDF: {str(e)}", {"page_images": [], "total_pages": 0, "total_words": 0}

# New function to process Excel files
def process_excel(excel_file):
    if excel_file is None:
        return None, "No file uploaded", {"data_preview": "", "total_sheets": 0, "total_rows": 0}
    
    try:
        session_id = str(uuid.uuid4())
        with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as temp_file:
            temp_file.write(excel_file)
            excel_path = temp_file.name
        
        # Read Excel file with pandas
        excel_data = pd.ExcelFile(excel_path)
        sheet_names = excel_data.sheet_names
        all_texts = []
        total_rows = 0
        
        # Process each sheet
        for sheet in sheet_names:
            df = pd.read_excel(excel_path, sheet_name=sheet)
            total_rows += len(df)
            
            # Convert dataframe to text for vectorization
            sheet_text = f"Sheet: {sheet}\n"
            sheet_text += df.to_string(index=False)
            all_texts.append(sheet_text)
        
        # Generate HTML preview of first sheet
        first_df = pd.read_excel(excel_path, sheet_name=0)
        preview_rows = min(10, len(first_df))
        data_preview = first_df.head(preview_rows).to_html(classes="excel-preview-table", index=False)
        
        # Process for vectorstore
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
        chunks = text_splitter.create_documents(all_texts)
        vectorstore = FAISS.from_documents(chunks, embeddings)
        index_path = os.path.join(FAISS_INDEX_DIR, session_id)
        vectorstore.save_local(index_path)
        user_vectorstores[session_id] = vectorstore

        os.unlink(excel_path)
        excel_state = {"data_preview": data_preview, "total_sheets": len(sheet_names), "total_rows": total_rows}
        return session_id, f"βœ… Successfully processed {len(chunks)} text chunks from Excel file", excel_state
    except Exception as e:
        if "excel_path" in locals() and os.path.exists(excel_path):
            os.unlink(excel_path)
        return None, f"Error processing Excel file: {str(e)}", {"data_preview": "", "total_sheets": 0, "total_rows": 0}

# Function to analyze image using SmolDocling
def analyze_image(image_file):
    """
    Basic image analysis function that doesn't rely on external models
    """
    if image_file is None:
        return "No image uploaded. Please upload an image to analyze."
    
    try:
        image = Image.open(image_file)
        width, height = image.size
        format = image.format
        mode = image.mode
        
        analysis = f"""## Technical Document Analysis

**Image Properties:**
- Dimensions: {width}x{height} pixels
- Format: {format}
- Color Mode: {mode}

**Technical Analysis:**
1. Document Quality:
   - Resolution: {'High' if width > 2000 or height > 2000 else 'Medium' if width > 1000 or height > 1000 else 'Low'}
   - Color Depth: {mode}

2. Recommendations:
   - For text extraction, consider using PDF format
   - For technical diagrams, ensure high resolution
   - Consider OCR for text content
   
**Note:** For detailed technical analysis, please convert to PDF format
"""
        return analysis
    except Exception as e:
        return f"Error analyzing image: {str(e)}\n\nPlease try using PDF format instead."

# Function to handle different file types
def process_file(file_data, file_type):
    if file_data is None:
        return None, "No file uploaded", None
    
    if file_type == "pdf":
        return process_pdf(file_data)
    elif file_type == "excel":
        return process_excel(file_data)
    elif file_type == "image":
        # For image files, we'll just use them directly for analysis
        # But we'll return a session ID to maintain consistency
        session_id = str(uuid.uuid4())
        return session_id, "βœ… Image file ready for analysis", None
    else:
        return None, "Unsupported file type", None

# Function for speech-to-text conversion
def speech_to_text():
    try:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
            text = r.recognize_google(audio)
            return text
    except sr.UnknownValueError:
        return "Could not understand audio. Please try again."
    except sr.RequestError as e:
        return f"Error with speech recognition service: {e}"
    except Exception as e:
        return f"Error converting speech to text: {str(e)}"

# Function for text-to-speech conversion
def text_to_speech(text, history):
    if not text or not history:
        return None
    
    try:
        # Get the last bot response
        last_response = history[-1][1]
        
        # Convert text to speech
        tts = pyttsx3.init()
        tts.setProperty('rate', 150)
        tts.setProperty('volume', 0.9)
        tts.save_to_file(last_response, "temp_output.mp3")
        tts.runAndWait()
        
        return "temp_output.mp3"
    except Exception as e:
        print(f"Error in text-to-speech: {e}")
        return None

# Function to generate chatbot responses with Tech theme
def generate_response(message, session_id, model_name, history, web_search_enabled=True):
    if not message:
        return history
    try:
        context = ""
        if session_id and session_id in user_vectorstores:
            vectorstore = user_vectorstores[session_id]
            docs = vectorstore.similarity_search(message, k=3)
            if docs:
                context = "\n\nRelevant information from uploaded PDF:\n" + "\n".join(f"- {doc.page_content}" for doc in docs)
        
        # Check if it's a GitHub repo search and web search is enabled
        if web_search_enabled and re.match(r'^/github\s+.+', message, re.IGNORECASE):
            query = re.sub(r'^/github\s+', '', message, flags=re.IGNORECASE)
            repo_results = search_github_repos(query)
            if repo_results:
                response = "**GitHub Repository Search Results:**\n\n"
                for repo in repo_results[:3]:  # Limit to top 3 results
                    response += f"**[{repo['name']}]({repo['html_url']})**\n"
                    if repo['description']:
                        response += f"{repo['description']}\n"
                    response += f"⭐ {repo['stargazers_count']} | 🍴 {repo['forks_count']} | Language: {repo['language'] or 'Not specified'}\n"
                    response += f"Updated: {repo['updated_at'][:10]}\n\n"
                history.append((message, response))
                return history
            else:
                history.append((message, "No GitHub repositories found for your query."))
                return history
        
        # Check if it's a Stack Overflow search and web search is enabled
        if web_search_enabled and re.match(r'^/stack\s+.+', message, re.IGNORECASE):
            query = re.sub(r'^/stack\s+', '', message, flags=re.IGNORECASE)
            qa_results = search_stackoverflow(query)
            if qa_results:
                response = "**Stack Overflow Search Results:**\n\n"
                for qa in qa_results[:3]:  # Limit to top 3 results
                    response += f"**[{qa['title']}]({qa['link']})**\n"
                    response += f"Score: {qa['score']} | Answers: {qa['answer_count']}\n"
                    if 'tags' in qa and qa['tags']:
                        response += f"Tags: {', '.join(qa['tags'][:5])}\n"
                    response += f"Asked: {qa['creation_date']}\n\n"
                history.append((message, response))
                return history
            else:
                history.append((message, "No Stack Overflow questions found for your query."))
                return history
        
        # Check if it's a code explanation request
        code_match = re.search(r'/explain\s+```(?:.+?)?\n(.+?)```', message, re.DOTALL)
        if code_match:
            code = code_match.group(1).strip()
            explanation = explain_code(code)
            history.append((message, explanation))
            return history
                
        system_prompt = "You are a technical assistant specializing in software development, programming, and IT topics."
        system_prompt += " Format code snippets with proper markdown code blocks with language specified."
        system_prompt += " For technical explanations, be precise and include examples where helpful."
        if context:
            system_prompt += " Use the following context to answer the question if relevant: " + context
        completion = client.chat.completions.create(
            model=model_name,
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": message}
            ],
            temperature=0.7,
            max_tokens=1024
        )
        response = completion.choices[0].message.content
        history.append((message, response))
        return history
    except Exception as e:
        history.append((message, f"Error generating response: {str(e)}"))
        return history

# Functions to update PDF viewer
def update_pdf_viewer(pdf_state):
    if not pdf_state["total_pages"]:
        return 0, None, "No PDF uploaded yet"
    try:
        img_data = base64.b64decode(pdf_state["page_images"][0])
        img = Image.open(io.BytesIO(img_data))
        return pdf_state["total_pages"], img, f"**Total Pages:** {pdf_state['total_pages']}\n**Total Words:** {pdf_state['total_words']}"
    except Exception as e:
        print(f"Error decoding image: {e}")
        return 0, None, "Error displaying PDF"

def update_image(page_num, pdf_state):
    if not pdf_state["total_pages"] or page_num < 1 or page_num > pdf_state["total_pages"]:
        return None
    try:
        img_data = base64.b64decode(pdf_state["page_images"][page_num - 1])
        img = Image.open(io.BytesIO(img_data))
        return img
    except Exception as e:
        print(f"Error decoding image: {e}")
        return None

# GitHub API integration
def search_github_repos(query, sort="stars", order="desc", per_page=10):
    """Search for GitHub repositories"""
    try:
        github_token = os.getenv("GITHUB_TOKEN", "")
        headers = {}
        if github_token:
            headers["Authorization"] = f"token {github_token}"
            
        params = {
            "q": query,
            "sort": sort,
            "order": order,
            "per_page": per_page
        }
        
        response = requests.get(
            "https://api.github.com/search/repositories",
            headers=headers,
            params=params
        )
        
        if response.status_code != 200:
            print(f"GitHub API Error: {response.status_code} - {response.text}")
            return []
            
        data = response.json()
        return data.get("items", [])
    except Exception as e:
        print(f"Error in GitHub search: {e}")
        return []

# Stack Overflow API integration
def search_stackoverflow(query, sort="votes", site="stackoverflow", pagesize=10):
    """Search for questions on Stack Overflow"""
    try:
        params = {
            "order": "desc",
            "sort": sort,
            "site": site,
            "pagesize": pagesize,
            "intitle": query
        }
        
        response = requests.get(
            "https://api.stackexchange.com/2.3/search/advanced",
            params=params
        )
        
        if response.status_code != 200:
            print(f"Stack Exchange API Error: {response.status_code} - {response.text}")
            return []
            
        data = response.json()
        
        # Process results to convert Unix timestamps to readable dates
        for item in data.get("items", []):
            if "creation_date" in item:
                item["creation_date"] = datetime.fromtimestamp(item["creation_date"]).strftime("%Y-%m-%d")
                
        return data.get("items", [])
    except Exception as e:
        print(f"Error in Stack Overflow search: {e}")
        return []

def get_stackoverflow_answers(question_id, site="stackoverflow"):
    """Get answers for a specific question on Stack Overflow"""
    try:
        params = {
            "order": "desc",
            "sort": "votes",
            "site": site,
            "filter": "withbody"  # Include the answer body in the response
        }
        
        response = requests.get(
            f"https://api.stackexchange.com/2.3/questions/{question_id}/answers",
            params=params
        )
        
        if response.status_code != 200:
            print(f"Stack Exchange API Error: {response.status_code} - {response.text}")
            return []
            
        data = response.json()
        
        # Process results
        for item in data.get("items", []):
            if "creation_date" in item:
                item["creation_date"] = datetime.fromtimestamp(item["creation_date"]).strftime("%Y-%m-%d")
                
        return data.get("items", [])
    except Exception as e:
        print(f"Error getting Stack Overflow answers: {e}")
        return []

def explain_code(code):
    """Explain code using LLM"""
    try:
        system_prompt = "You are an expert programmer and code reviewer. Your task is to explain the provided code in a clear, concise manner. Include:"
        system_prompt += "\n1. What the code does (high-level overview)"
        system_prompt += "\n2. Key functions/components and their purposes"
        system_prompt += "\n3. Potential issues or optimization opportunities"
        system_prompt += "\n4. Any best practices that are followed or violated"
        
        completion = client.chat.completions.create(
            model="llama3-70b-8192",  # Using more capable model for code explanation
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": f"Explain this code:\n```\n{code}\n```"}
            ],
            temperature=0.3,
            max_tokens=1024
        )
        
        explanation = completion.choices[0].message.content
        return f"**Code Explanation:**\n\n{explanation}"
    except Exception as e:
        return f"Error explaining code: {str(e)}"

def perform_repo_search(query, language, sort_by, min_stars):
    """Perform GitHub repository search with UI parameters"""
    try:
        if not query:
            return "Please enter a search query"
            
        # Build the search query with filters
        search_query = query
        if language and language != "any":
            search_query += f" language:{language}"
        if min_stars and min_stars != "0":
            search_query += f" stars:>={min_stars}"
            
        # Map sort_by to GitHub API parameters
        sort_param = "stars"
        if sort_by == "updated":
            sort_param = "updated"
        elif sort_by == "forks":
            sort_param = "forks"
            
        results = search_github_repos(search_query, sort=sort_param)
        
        if not results:
            return "No repositories found. Try different search terms."
            
        # Format results as markdown
        markdown = "## GitHub Repository Search Results\n\n"
        
        for i, repo in enumerate(results, 1):
            markdown += f"### {i}. [{repo['full_name']}]({repo['html_url']})\n\n"
            
            if repo['description']:
                markdown += f"{repo['description']}\n\n"
                
            markdown += f"**Language:** {repo['language'] or 'Not specified'}\n"
            markdown += f"**Stars:** {repo['stargazers_count']} | **Forks:** {repo['forks_count']} | **Watchers:** {repo['watchers_count']}\n"
            markdown += f"**Created:** {repo['created_at'][:10]} | **Updated:** {repo['updated_at'][:10]}\n\n"
            
            if repo.get('topics'):
                markdown += f"**Topics:** {', '.join(repo['topics'])}\n\n"
                
            if repo.get('license') and repo['license'].get('name'):
                markdown += f"**License:** {repo['license']['name']}\n\n"
                
            markdown += f"[View Repository]({repo['html_url']}) | [Clone URL]({repo['clone_url']})\n\n"
            markdown += "---\n\n"
            
        return markdown
    except Exception as e:
        return f"Error searching for repositories: {str(e)}"

def perform_stack_search(query, tag, sort_by):
    """Perform Stack Overflow search with UI parameters"""
    try:
        if not query:
            return "Please enter a search query"
            
        # Add tag to query if specified
        if tag and tag != "any":
            query_with_tag = f"{query} [tag:{tag}]"
        else:
            query_with_tag = query
            
        # Map sort_by to Stack Exchange API parameters
        sort_param = "votes"
        if sort_by == "newest":
            sort_param = "creation"
        elif sort_by == "activity":
            sort_param = "activity"
            
        results = search_stackoverflow(query_with_tag, sort=sort_param)
        
        if not results:
            return "No questions found. Try different search terms."
            
        # Format results as markdown
        markdown = "## Stack Overflow Search Results\n\n"
        
        for i, question in enumerate(results, 1):
            markdown += f"### {i}. [{question['title']}]({question['link']})\n\n"
            
            # Score and answer stats
            markdown += f"**Score:** {question['score']} | **Answers:** {question['answer_count']}"
            if question.get('is_answered'):
                markdown += " βœ“ (Accepted answer available)"
            markdown += "\n\n"
            
            # Tags
            if question.get('tags'):
                markdown += "**Tags:** "
                for tag in question['tags']:
                    markdown += f"`{tag}` "
                markdown += "\n\n"
                
            # Asked info
            markdown += f"**Asked:** {question['creation_date']} | **Views:** {question.get('view_count', 'N/A')}\n\n"
            
            markdown += f"[View Question]({question['link']})\n\n"
            markdown += "---\n\n"
            
        return markdown
    except Exception as e:
        return f"Error searching Stack Overflow: {str(e)}"

def detect_language(file_extension):
    """Map file extensions to programming languages"""
    language_map = {
        ".py": "Python",
        ".js": "JavaScript",
        ".java": "Java",
        ".cpp": "C++",
        ".c": "C",
        ".cs": "C#",
        ".php": "PHP",
        ".rb": "Ruby",
        ".go": "Go",
        ".rs": "Rust",
        ".swift": "Swift",
        ".kt": "Kotlin",
        ".ts": "TypeScript",
        ".html": "HTML",
        ".css": "CSS",
        ".sql": "SQL",
        ".r": "R",
        ".m": "Objective-C/MATLAB",
        ".h": "C/C++ Header",
        ".hpp": "C++ Header",
        ".jsx": "React JSX",
        ".tsx": "React TSX",
        ".vue": "Vue.js",
        ".scala": "Scala",
        ".pl": "Perl",
        ".sh": "Shell Script",
        ".bash": "Bash Script",
        ".ps1": "PowerShell",
        ".yaml": "YAML",
        ".yml": "YAML",
        ".json": "JSON",
        ".xml": "XML",
        ".toml": "TOML",
        ".ini": "INI"
    }
    return language_map.get(file_extension.lower(), "Unknown")

def analyze_code(code_file):
    """Analyze code files and provide insights"""
    if code_file is None:
        return "No file uploaded. Please upload a code file to analyze."
    
    try:
        # Get file extension
        file_extension = os.path.splitext(code_file.name)[1]
        language = detect_language(file_extension)
        
        # Read the file content
        content = code_file.read().decode('utf-8', errors='ignore')
        
        # Basic code metrics
        total_lines = len(content.splitlines())
        blank_lines = len([line for line in content.splitlines() if not line.strip()])
        code_lines = total_lines - blank_lines
        
        # Calculate complexity metrics
        complexity_metrics = calculate_complexity(content, language)
        
        # Generate analysis using LLM
        analysis_prompt = f"""Analyze this {language} code and provide insights about:
1. Code structure and organization
2. Potential improvements or best practices
3. Security considerations
4. Performance implications
5. Maintainability factors

Code metrics:
- Total lines: {total_lines}
- Code lines: {code_lines}
- Blank lines: {blank_lines}
{complexity_metrics}

First 1000 characters of code:
{content[:1000]}...
"""
        
        completion = client.chat.completions.create(
            model="llama3-70b-8192",
            messages=[
                {"role": "system", "content": "You are an expert code reviewer and technical architect."},
                {"role": "user", "content": analysis_prompt}
            ],
            temperature=0.3,
            max_tokens=1500
        )
        
        # Format the analysis
        analysis = f"""## Code Analysis Report

**File Type:** {language}

### Code Metrics
- Total Lines: {total_lines}
- Code Lines: {code_lines}
- Blank Lines: {blank_lines}

### Complexity Analysis
{complexity_metrics}

### Expert Analysis
{completion.choices[0].message.content}

### Recommendations
1. Consider using a linter specific to {language}
2. Review the security considerations mentioned above
3. Consider automated testing to validate the code
4. Document any complex algorithms or business logic
"""
        return analysis
        
    except Exception as e:
        return f"Error analyzing code: {str(e)}\n\nPlease ensure the file is properly formatted and encoded."

def calculate_complexity(content, language):
    """Calculate various complexity metrics based on the language"""
    try:
        # Count function/method definitions
        function_patterns = {
            "Python": r"def\s+\w+\s*\(",
            "JavaScript": r"function\s+\w+\s*\(|const\s+\w+\s*=\s*\([^)]*\)\s*=>",
            "Java": r"(public|private|protected)?\s*\w+\s+\w+\s*\([^)]*\)\s*\{",
            "C++": r"\w+\s+\w+\s*\([^)]*\)\s*\{",
        }
        
        pattern = function_patterns.get(language, r"\w+\s+\w+\s*\([^)]*\)")
        function_count = len(re.findall(pattern, content))
        
        # Calculate cyclomatic complexity (rough estimate)
        decision_patterns = [
            r"\bif\b",
            r"\bwhile\b",
            r"\bfor\b",
            r"\bcase\b",
            r"\bcatch\b",
            r"\b&&\b",
            r"\b\|\|\b"
        ]
        
        decision_points = sum(len(re.findall(p, content)) for p in decision_patterns)
        
        # Estimate maintainability
        avg_line_length = sum(len(line) for line in content.splitlines()) / len(content.splitlines()) if content.splitlines() else 0
        
        return f"""**Complexity Metrics:**
- Estimated Function Count: {function_count}
- Decision Points: {decision_points}
- Average Line Length: {avg_line_length:.2f} characters
- Cyclomatic Complexity Estimate: {decision_points + 1}
"""
    except Exception as e:
        return f"Error calculating complexity: {str(e)}"

def update_status_with_animation(status):
    return f"""
    <div class="status-message">
        <div class="loading-container">
            <div class="loading-bar"></div>
        </div>
        > {status}
    </div>
    """

# Update the analysis results display
def format_analysis_results(analysis):
    return f"""
    <div class="analysis-container">
        <div class="analysis-header">> ANALYSIS COMPLETE</div>
        {analysis}
        <div class="loading-container">
            <div class="loading-bar"></div>
        </div>
    </div>
    """

def format_code_metrics(metrics):
    return f"""
    <div class="metric-card">
        <div style="color: var(--neon-yellow);">SYSTEM METRICS</div>
        <div style="margin-top: 10px;">
            {metrics}
        </div>
    </div>
    """

# Add cyberpunk UI sound effects
def play_interface_sound(sound_type):
    sounds = {
        "hover": "hover.mp3",
        "click": "click.mp3",
        "success": "success.mp3",
        "error": "error.mp3"
    }
    return gr.Audio(value=sounds.get(sound_type), autoplay=True, visible=False)

# Create the Gradio interface with advanced cyberpunk styling
def create_cyberpunk_interface():
    css = compile_scss()
    
    with gr.Blocks(css=css, head=NEURAL_JS) as demo:
    current_session_id = gr.State(None)
    pdf_state = gr.State({"page_images": [], "total_pages": 0, "total_words": 0})
    excel_state = gr.State({"data_preview": "", "total_sheets": 0, "total_rows": 0})
    file_type = gr.State("none")
    audio_status = gr.State("Ready")
    
    gr.HTML("""
        <div class="neural-interface">
            <div class="neural-interface__header">TECH-VISION_v3.0</div>
            <div class="neural-status">
                <div class="neural-status__indicator"></div>
                <div class="neural-status__text">SYSTEM ONLINE</div>
        </div>
    </div>
    """)
        with gr.Row(elem_classes="neural-grid"):
        with gr.Column(scale=1, min_width=300):
            with gr.Tabs():
                with gr.TabItem("[SYS:SCAN] Code Analysis"):
                    gr.HTML("""
                    <div class="upload-container">
                        <div style="color: var(--neon-blue); margin-bottom: 10px;">
                            > INITIATE CODE SCAN
                        </div>
                    """)
                    code_file = gr.File(
                        label="UPLOAD SOURCE CODE",
                        file_types=[".py", ".js", ".java", ".cpp", ".c", ".cs", ".php", ".rb", 
                                  ".go", ".rs", ".swift", ".kt", ".ts", ".html", ".css", 
                                  ".sql", ".r", ".m", ".h", ".hpp", ".jsx", ".tsx", 
                                  ".vue", ".scala", ".pl", ".sh", ".bash", ".ps1",
                                  ".yaml", ".yml", ".json", ".xml", ".toml", ".ini"],
                        type="binary"
                    )
                    gr.HTML("</div>")
                    code_analyze_btn = gr.Button("INITIATE ANALYSIS", elem_classes="primary-btn")
                
                with gr.TabItem("PDF"):
                    pdf_file = gr.File(label="Upload PDF Document", file_types=[".pdf"], type="binary")
                    pdf_upload_button = gr.Button("Process PDF", variant="primary")
                
                with gr.TabItem("Excel"):
                    excel_file = gr.File(label="Upload Excel File", file_types=[".xlsx", ".xls"], type="binary")
                    excel_upload_button = gr.Button("Process Excel", variant="primary")
                
                with gr.TabItem("Image"):
                    image_input = gr.File(
                        label="Upload Image", 
                        file_types=["image"],
                        type="filepath"
                    )
                    analyze_btn = gr.Button("Analyze Image")
            
            file_status = gr.Markdown("No file uploaded yet")
            
            # Model selector
            model_dropdown = gr.Dropdown(
                choices=["llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma-7b-it"],
                value="llama3-70b-8192",
                label="Select Groq Model"
            )
        
        with gr.Column(scale=2, min_width=600):
            with gr.Tabs():
                with gr.TabItem("PDF Viewer"):
                    with gr.Column(elem_classes="pdf-viewer-container"):
                        page_slider = gr.Slider(minimum=1, maximum=1, step=1, label="Page Number", value=1)
                        pdf_image = gr.Image(label="PDF Page", type="pil", elem_classes="pdf-viewer-image")
                        pdf_stats = gr.Markdown("No PDF uploaded yet", elem_classes="stats-box")
                
                with gr.TabItem("Excel Viewer"):
                    excel_preview = gr.HTML(label="Excel Preview", elem_classes="file-preview")
                    excel_stats = gr.Markdown("No Excel file uploaded yet", elem_classes="stats-box")
                
                with gr.TabItem("Image Analysis"):
                    image_preview = gr.Image(label="Image Preview", type="pil")
                    image_analysis_results = gr.Markdown("Upload an image and click 'Analyze Image' to see analysis results")
                
                with gr.TabItem("Code Analysis Results"):
                    analysis_results = gr.Markdown("Upload a code file and click 'Analyze Code' to see analysis results")
                    with gr.Row():
                        copy_btn = gr.Button("πŸ“‹ Copy Analysis")
                        export_btn = gr.Button("πŸ“₯ Export Report")
    
    # Audio visualization elements
    with gr.Row(elem_classes="container"):
        with gr.Column():
            audio_vis = gr.HTML("""
            <div class="audio-visualization">
                <div class="audio-bar" style="height: 5px;"></div>
                <div class="audio-bar" style="height: 12px;"></div>
                <div class="audio-bar" style="height: 18px;"></div>
                <div class="audio-bar" style="height: 15px;"></div>
                <div class="audio-bar" style="height: 10px;"></div>
                <div class="audio-bar" style="height: 20px;"></div>
                <div class="audio-bar" style="height: 14px;"></div>
                <div class="audio-bar" style="height: 8px;"></div>
            </div>
            """, visible=False)
            audio_status_display = gr.Markdown("", elem_classes="audio-status")
    
    # Chat interface
    with gr.Row(elem_classes="container"):
        with gr.Column(scale=2, min_width=600):
            chatbot = gr.Chatbot(
                height=400, 
                show_copy_button=True, 
                elem_classes="chat-container",
                type="messages"  # Use the new messages format
            )
            with gr.Row():
                msg = gr.Textbox(
                    show_label=False, 
                    placeholder="Ask about your document or click the microphone to speak...", 
                    scale=5
                )
                voice_btn = gr.Button("🎀", elem_classes="voice-btn")
                send_btn = gr.Button("Send", scale=1)
                
            with gr.Row(elem_classes="audio-controls"):
                clear_btn = gr.Button("Clear Conversation")
                speak_btn = gr.Button("πŸ”Š Speak Response", elem_classes="speak-btn")
                audio_player = gr.Audio(label="Response Audio", type="filepath", visible=False)
    
    # Event Handlers for PDF processing
    pdf_upload_button.click(
        lambda x: ("pdf", x),
        inputs=[pdf_file],
        outputs=[file_type, file_status]
    ).then(
        process_pdf,
        inputs=[pdf_file],
        outputs=[current_session_id, file_status, pdf_state]
    ).then(
        update_pdf_viewer,
        inputs=[pdf_state],
        outputs=[page_slider, pdf_image, pdf_stats]
    )
    
    # Event Handlers for Excel processing
    def update_excel_preview(state):
        if not state:
            return "", "No Excel file uploaded yet"
        preview = state.get("data_preview", "")
        sheets = state.get("total_sheets", 0)
        rows = state.get("total_rows", 0)
        stats = f"**Excel Statistics:**\nSheets: {sheets}\nTotal Rows: {rows}"
        return preview, stats
    
    excel_upload_button.click(
        lambda x: ("excel", x),
        inputs=[excel_file],
        outputs=[file_type, file_status]
    ).then(
        process_excel,
        inputs=[excel_file],
        outputs=[current_session_id, file_status, excel_state]
    ).then(
        update_excel_preview,
        inputs=[excel_state],
        outputs=[excel_preview, excel_stats]
    )
    
    # Event Handlers for Image Analysis
    analyze_btn.click(
        lambda x: ("image", x),
        inputs=[image_input],
        outputs=[file_type, file_status]
    ).then(
        analyze_image,
        inputs=[image_input],
        outputs=[image_analysis_results]
    ).then(
        lambda x: Image.open(x) if x else None,
        inputs=[image_input],
        outputs=[image_preview]
    )
    
    # Event Handlers for Code Analysis
    code_analyze_btn.click(
        update_status_with_animation,
        inputs=[],
        outputs=[file_status]
    ).then(
        analyze_code,
        inputs=[code_file],
        outputs=[analysis_results]
    ).then(
        format_analysis_results,
        inputs=[analysis_results],
        outputs=[analysis_results]
    )
    
    # Chat message handling
    msg.submit(
        generate_response,
        inputs=[msg, current_session_id, model_dropdown, chatbot],
        outputs=[chatbot]
    ).then(lambda: "", None, [msg])
    
    send_btn.click(
        generate_response,
        inputs=[msg, current_session_id, model_dropdown, chatbot],
        outputs=[chatbot]
    ).then(lambda: "", None, [msg])
    
    # Improved speech-to-text with visual feedback
    voice_btn.click(
        speech_to_text,
        inputs=[audio_status],
        outputs=[audio_status_display, audio_vis, msg]
    )
    
    # Improved text-to-speech with visual feedback
    speak_btn.click(
        text_to_speech,
        inputs=[audio_status, chatbot],
        outputs=[audio_status_display, audio_vis, audio_player]
    ).then(
        lambda x: gr.update(visible=True) if x else gr.update(visible=False),
        inputs=[audio_player],
        outputs=[audio_player]
    )
    
    # Page navigation for PDF
    page_slider.change(
        update_image,
        inputs=[page_slider, pdf_state],
        outputs=[pdf_image]
    )
    
    # Clear conversation and reset UI
    clear_btn.click(
        lambda: (
            [], None, "No file uploaded yet", 
            {"page_images": [], "total_pages": 0, "total_words": 0},
            {"data_preview": "", "total_sheets": 0, "total_rows": 0},
            "none", 0, None, "No PDF uploaded yet", "", 
            "No Excel file uploaded yet", None, 
            "Upload an image and click 'Analyze Image' to see results", None,
            gr.update(visible=False), "Ready"
        ),
        None,
        [chatbot, current_session_id, file_status, pdf_state, excel_state, 
         file_type, page_slider, pdf_image, pdf_stats, excel_preview, 
         excel_stats, image_preview, image_analysis_results, audio_player, 
         audio_vis, audio_status_display]
    )

    # Add footer with creator attribution
    gr.HTML("""
    <div style="text-align: center; margin-top: 20px; padding: 10px; color: #666; font-size: 0.8rem; border-top: 1px solid #eee;">
        Created by Calvin Allen Crawford
    </div>
    """)

    return demo

# Launch the app
if __name__ == "__main__":
    demo = create_cyberpunk_interface()
    demo.launch()