File size: 50,966 Bytes
d6eab4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b481357
 
 
 
 
 
 
 
 
 
 
 
 
d6eab4f
 
 
 
5c8cf60
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5df6c06
 
 
 
d6eab4f
 
 
 
 
 
 
 
 
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6eab4f
b481357
5df6c06
 
d6eab4f
 
 
b481357
 
 
 
 
 
 
 
 
d6eab4f
b481357
 
 
 
 
 
 
 
 
 
 
 
5df6c06
 
 
 
b481357
 
 
 
 
 
 
d6eab4f
 
 
b481357
5df6c06
 
 
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6eab4f
b481357
 
 
 
 
d6eab4f
b481357
d6eab4f
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3311ec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b481357
3311ec5
 
 
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3311ec5
 
 
 
 
5df6c06
b481357
 
 
 
3311ec5
 
5df6c06
b481357
 
 
 
 
 
 
 
 
 
 
 
3311ec5
d6eab4f
3311ec5
b481357
d6eab4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3311ec5
b481357
 
 
 
 
 
 
 
 
 
 
 
74e2e27
 
 
 
 
4c2dac4
74e2e27
 
 
4c2dac4
74e2e27
4c2dac4
74e2e27
4c2dac4
74e2e27
 
 
4c2dac4
 
 
 
 
 
 
 
 
74e2e27
 
 
 
 
 
 
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41fb331
 
 
 
 
 
 
 
 
 
 
 
 
 
b481357
41fb331
b481357
 
 
 
3311ec5
 
 
 
 
 
 
 
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41fb331
 
 
b481357
 
 
 
 
 
5c8cf60
 
 
 
 
 
 
 
 
b481357
 
 
d6eab4f
5c8cf60
116d200
5c8cf60
d6eab4f
 
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3311ec5
b481357
 
 
 
2509cce
 
 
 
b481357
 
 
2509cce
b481357
2509cce
b481357
 
 
 
 
 
 
2509cce
 
 
b481357
 
 
2509cce
b481357
 
 
 
 
 
 
 
 
5df6c06
b481357
 
 
5df6c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b481357
5df6c06
d6eab4f
5df6c06
 
d6eab4f
 
 
5df6c06
 
 
d6eab4f
 
 
5df6c06
 
d6eab4f
5df6c06
 
d6eab4f
5df6c06
 
 
 
 
 
 
 
d6eab4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c8cf60
 
 
 
 
 
 
 
 
5df6c06
 
 
d6eab4f
5c8cf60
aee51e6
5c8cf60
d6eab4f
 
5df6c06
 
 
 
 
 
 
 
 
 
d6eab4f
5df6c06
 
d6eab4f
5df6c06
 
 
 
d6eab4f
b481357
5df6c06
 
 
 
 
 
 
 
 
d6eab4f
5df6c06
d6eab4f
 
5df6c06
 
3187d23
d6eab4f
 
 
 
5df6c06
5c8cf60
 
 
 
 
 
 
 
 
 
 
5df6c06
 
 
 
d6eab4f
5c8cf60
 
 
d6eab4f
 
 
5df6c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6eab4f
5df6c06
d6eab4f
5df6c06
 
 
d6eab4f
 
 
5df6c06
 
d6eab4f
5df6c06
d6eab4f
5df6c06
 
 
 
 
 
 
 
d6eab4f
 
 
 
5df6c06
 
 
 
 
d6eab4f
 
b481357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# import gradio as gr
# import numpy as np
# import json
# import pandas as pd
# from openai import OpenAI
# import yaml
# from typing import Optional, List, Dict, Tuple, Any
# from topk_sae import FastAutoencoder
# import torch
# import plotly.express as px
# from collections import Counter
# from huggingface_hub import hf_hub_download
# import os
# import networkx as nx
# import plotly.graph_objs as go
# from ast import literal_eval as make_tuple
# import random

# import os
# print(os.getenv('MODEL_REPO_ID'))

# # Constants
# EMBEDDING_MODEL = "text-embedding-3-small"
# d_model = 1536
# n_dirs = d_model * 6
# k = 64
# auxk = 128
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# torch.set_grad_enabled(False)

# # Function to download all necessary files
# def download_all_files():
#     files_to_download = [
#         "astroPH_paper_metadata.csv",
#         "csLG_feature_analysis_results_64.json",
#         "astroPH_topk_indices_64_9216_int32.npy",
#         "astroPH_64_9216.pth",
#         "astroPH_topk_values_64_9216_float16.npy",
#         "csLG_abstract_texts.json",
#         "csLG_topk_values_64_9216_float16.npy",
#         "csLG_abstract_embeddings_float16.npy",
#         "csLG_paper_metadata.csv",
#         "csLG_64_9216.pth",
#         "astroPH_abstract_texts.json",
#         "astroPH_feature_analysis_results_64.json",
#         "csLG_topk_indices_64_9216_int32.npy",
#         "astroPH_abstract_embeddings_float16.npy",
#         # "csLG_clean_families_64_9216.json",
#         # "astroPH_clean_families_64_9216.json",
#         # "astroPH_family_analysis_64_9216.json",
#         "csLG_family_analysis_64_9216.json"
#     ]

#     for file in files_to_download:
#         local_path = os.path.join("data", file)
#         os.makedirs(os.path.dirname(local_path), exist_ok=True)
#         hf_hub_download(repo_id="charlieoneill/saerch-ai-data", filename=file, local_dir="data")
#         print(f"Downloaded {file}")

# # Load configuration and initialize OpenAI client
# download_all_files()

# # Load the API key from the environment variable
# api_key = os.getenv('openai_key')

# # Ensure the API key is set
# if not api_key:
#     raise ValueError("The environment variable 'openai_key' is not set.")

# # Initialize the OpenAI client with the API key
# client = OpenAI(api_key=api_key)

# # Function to load data for a specific subject
# def load_subject_data(subject):

#     embeddings_path = f"data/{subject}_abstract_embeddings_float16.npy"
#     texts_path = f"data/{subject}_abstract_texts.json"
#     feature_analysis_path = f"data/{subject}_feature_analysis_results_{k}.json"
#     metadata_path = f'data/{subject}_paper_metadata.csv'
#     topk_indices_path = f"data/{subject}_topk_indices_{k}_{n_dirs}_int32.npy"
#     norms_path = f"data/{subject}_norms_{k}_{n_dirs}.npy"
#     topk_values_path = f"data/{subject}_topk_values_{k}_{n_dirs}_float16.npy"
#     families_path = f"data/{subject}_clean_families_{k}_{n_dirs}.json"
#     family_analysis_path = f"data/{subject}_family_analysis_{k}_{n_dirs}.json"
#     nns_32to64 = json.load(open(f"data/{subject}_nns_32to64.json"))
#     nns_16to32 = json.load(open(f"data/{subject}_nns_16to32.json"))
#     nns_16to64 = json.load(open(f"data/{subject}_nns_16to64.json"))

#     abstract_embeddings = np.load(embeddings_path).astype(np.float32)  # Load float16 and convert to float32
#     with open(texts_path, 'r') as f:
#         abstract_texts = json.load(f)
#     with open(feature_analysis_path, 'r') as f:
#         feature_analysis = json.load(f)
#     df_metadata = pd.read_csv(metadata_path)
#     topk_indices = np.load(topk_indices_path)  # Already in int32, no conversion needed
#     topk_values = np.load(topk_values_path).astype(np.float32)
#     norms = np.load(norms_path).astype(np.float32)

#     model_filename = f"{subject}_64_9216.pth"
#     model_path = os.path.join("data", model_filename)

#     ae = FastAutoencoder(n_dirs, d_model, k, auxk, multik=0).to(device)
#     ae.load_state_dict(torch.load(model_path))
#     ae.eval()

#     weights = torch.load(model_path)
#     decoder = weights['decoder.weight'].cpu().numpy()
#     del weights

#     with open(family_analysis_path, 'r') as f:
#         family_analysis = json.load(f)


#     return {
#         'abstract_embeddings': abstract_embeddings,
#         'abstract_texts': abstract_texts,
#         'feature_analysis': feature_analysis,
#         'df_metadata': df_metadata,
#         'topk_indices': topk_indices,
#         'topk_values': topk_values,
#         'norms': norms,
#         'nns_32to64': nns_32to64,
#         'nns_16to64': nns_16to64,
#         'ae': ae,
#         'decoder': decoder,
#         # 'feature_families': feature_families,
#         'family_analysis': family_analysis
#     }

import gradio as gr
import numpy as np
import json
import pandas as pd
from openai import OpenAI
import yaml
from typing import Optional, List, Dict, Tuple, Any
from topk_sae import FastAutoencoder
import torch
import plotly.express as px
from collections import Counter
from huggingface_hub import hf_hub_download
import os
import networkx as nx
import plotly.graph_objs as go
from ast import literal_eval as make_tuple
import random
import math

import os
print(os.getenv('MODEL_REPO_ID'))

# Constants
EMBEDDING_MODEL = "text-embedding-3-small"
d_model = 1536
n_dirs = d_model * 6
k = 64
auxk = 128
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
torch.set_grad_enabled(False)

# Function to download all necessary files
def download_all_files():
    files_to_download = [
        "astroPH_paper_metadata.csv",
        "csLG_feature_analysis_results_64.json",
        "astroPH_topk_indices_64_9216_int32.npy",
        "astroPH_64_9216.pth",
        "astroPH_topk_values_64_9216_float16.npy",
        "csLG_abstract_texts.json",
        "csLG_topk_values_64_9216_float16.npy",
        "csLG_abstract_embeddings_float16.npy",
        "csLG_paper_metadata.csv",
        "csLG_64_9216.pth",
        "astroPH_abstract_texts.json",
        "astroPH_feature_analysis_results_64.json",
        "csLG_topk_indices_64_9216_int32.npy",
        "astroPH_abstract_embeddings_float16.npy",
        # "csLG_clean_families_64_9216.json",
        # "astroPH_clean_families_64_9216.json",
        "astroPH_family_analysis_64_9216.json",
        "csLG_family_analysis_64_9216.json",
        "csLG_nns_32to64.json",
        "csLG_nns_16to32.json",
        "csLG_nns_16to64.json",
        "astroPH_nns_32to64.json",
        "astroPH_nns_16to32.json",
        "astroPH_nns_16to64.json",
        "csLG_norms_64_9216_float16.npy",
        "astroPH_norms_64_9216_float16.npy"
    ]

    for file in files_to_download:
        local_path = os.path.join("data", file)
        os.makedirs(os.path.dirname(local_path), exist_ok=True)
        hf_hub_download(repo_id="charlieoneill/saerch-ai-data", filename=file, local_dir="data")
        print(f"Downloaded {file}")

# Load configuration and initialize OpenAI client
download_all_files()

# Load the API key from the environment variable
api_key = os.getenv('openai_key')

# Ensure the API key is set
if not api_key:
    raise ValueError("The environment variable 'openai_key' is not set.")

# Initialize the OpenAI client with the API key
client = OpenAI(api_key=api_key)

# Function to load data for a specific subject
def load_subject_data(subject):

    embeddings_path = f"data/{subject}_abstract_embeddings_float16.npy"
    texts_path = f"data/{subject}_abstract_texts.json"
    feature_analysis_path = f"data/{subject}_feature_analysis_results_{k}.json"
    metadata_path = f'data/{subject}_paper_metadata.csv'
    topk_indices_path = f"data/{subject}_topk_indices_{k}_{n_dirs}_int32.npy"
    norms_path = f"data/{subject}_norms_{k}_{n_dirs}_float16.npy"
    topk_values_path = f"data/{subject}_topk_values_{k}_{n_dirs}_float16.npy"
    families_path = f"data/{subject}_clean_families_{k}_{n_dirs}.json"
    family_analysis_path = f"data/{subject}_family_analysis_{k}_{n_dirs}.json"
    nns_32to64 = json.load(open(f"data/{subject}_nns_32to64.json"))
    nns_16to32 = json.load(open(f"data/{subject}_nns_16to32.json"))
    nns_16to64 = json.load(open(f"data/{subject}_nns_16to64.json"))

    abstract_embeddings = np.load(embeddings_path).astype(np.float32)  # Load float16 and convert to float32
    with open(texts_path, 'r') as f:
        abstract_texts = json.load(f)
    with open(feature_analysis_path, 'r') as f:
        feature_analysis = json.load(f)
    df_metadata = pd.read_csv(metadata_path)
    topk_indices = np.load(topk_indices_path)  # Already in int32, no conversion needed
    topk_values = np.load(topk_values_path).astype(np.float32)
    norms = np.load(norms_path).astype(np.float32)

    model_filename = f"{subject}_64_9216.pth"
    model_path = os.path.join("data", model_filename)

    ae = FastAutoencoder(n_dirs, d_model, k, auxk, multik=0).to(device)
    ae.load_state_dict(torch.load(model_path))
    ae.eval()

    weights = torch.load(model_path)
    decoder = weights['decoder.weight'].cpu().numpy()
    del weights

    with open(family_analysis_path, 'r') as f:
        family_analysis = json.load(f)


    return {
        'abstract_embeddings': abstract_embeddings,
        'abstract_texts': abstract_texts,
        'feature_analysis': feature_analysis,
        'df_metadata': df_metadata,
        'topk_indices': topk_indices,
        'topk_values': topk_values,
        'norms': norms,
        'nns_32to64': nns_32to64,
        'nns_16to64': nns_16to64,
        'ae': ae,
        'decoder': decoder,
        # 'feature_families': feature_families,
        'family_analysis': family_analysis
    }

# Load data for both subjects
subject_data = {
    'astroPH': load_subject_data('astroPH'),
    'csLG': load_subject_data('csLG')
}

# Update existing functions to use the selected subject's data
def get_embedding(text: Optional[str], model: str = EMBEDDING_MODEL) -> Optional[np.ndarray]:
    try:
        embedding = client.embeddings.create(input=[text], model=model).data[0].embedding
        return np.array(embedding, dtype=np.float32)
    except Exception as e:
        print(f"Error getting embedding: {e}")
        return None

def intervened_hidden_to_intervened_embedding(topk_indices, topk_values, ae):
    with torch.no_grad():
        return ae.decode_sparse(topk_indices, topk_values)

# Function definitions for feature activation, co-occurrence, styling, etc.
def get_feature_activations(subject, feature_index, m=5, min_length=100):
    abstract_texts = subject_data[subject]['abstract_texts']
    abstract_embeddings = subject_data[subject]['abstract_embeddings']
    topk_indices = subject_data[subject]['topk_indices']
    topk_values = subject_data[subject]['topk_values']

    doc_ids = abstract_texts['doc_ids']
    abstracts = abstract_texts['abstracts']
    
    feature_mask = topk_indices == feature_index
    activated_indices = np.where(feature_mask.any(axis=1))[0]
    activation_values = np.where(feature_mask, topk_values, 0).max(axis=1)
    
    sorted_activated_indices = activated_indices[np.argsort(-activation_values[activated_indices])]
    
    top_m_abstracts = []
    top_m_indices = []
    for i in sorted_activated_indices:
        if len(abstracts[i]) > min_length:
            top_m_abstracts.append((doc_ids[i], abstracts[i], activation_values[i]))
            top_m_indices.append(i)
        if len(top_m_abstracts) == m:
            break
    
    return top_m_abstracts

def calculate_co_occurrences(subject, target_index, n_features=9216):
    topk_indices = subject_data[subject]['topk_indices']
    norms = subject_data[subject]['norms']

    mask = np.any(topk_indices == target_index, axis=1)
    co_occurring_indices = topk_indices[mask].flatten()
    co_occurrences = Counter(co_occurring_indices)
    del co_occurrences[target_index]
    result = np.zeros(n_features, dtype=np.float32)
    result[list(co_occurrences.keys())] = list(co_occurrences.values())
    result[list(co_occurrences.keys())] /= np.minimum(norms[list(co_occurrences.keys())], norms[target_index])  
    return result

def style_dataframe(df: pd.DataFrame, is_top: bool) -> pd.DataFrame:
    cosine_values = df['Cosine similarity'].astype(float)
    min_val = cosine_values.min()
    max_val = cosine_values.max()
    
    def color_similarity(val):
        val = float(val)
        # Normalize the value between 0 and 1
        if is_top:
            normalized_val = (val - min_val) / (max_val - min_val)
        else:
            # For bottom correlated, reverse the normalization
            normalized_val = (max_val - val) / (max_val - min_val)
        
        # Adjust the color intensity to avoid zero intensity
        color_intensity = 0.2 + (normalized_val * 0.8)  # This ensures the range is from 0.2 to 1.0
        
        if is_top:
            color = f'background-color: rgba(0, 255, 0, {color_intensity:.2f})'
        else:
            color = f'background-color: rgba(255, 0, 0, {color_intensity:.2f})'
        return color

    return df.style.applymap(color_similarity, subset=['Cosine similarity'])

def get_feature_from_index(subject, index):
    feature = next((f for f in subject_data[subject]['feature_analysis'] if f['index'] == index), None)
    return feature

def visualize_feature(subject, index):
    feature = next((f for f in subject_data[subject]['feature_analysis'] if f['index'] == index), None)
    if feature is None:
        return "Invalid feature index", None, None, None, None, None, None

    output = f"# {feature['label']}\n\n"
    output += f"* Pearson correlation: {feature['pearson_correlation']:.4f}\n\n"
    output += f"* Density: {feature['density']:.4f}\n\n"

    # Top m abstracts
    top_m_abstracts = get_feature_activations(subject, index)
    
    # Create dataframe for top abstracts with clickable links
    df_data = []
    for doc_id, abstract, activation_value in top_m_abstracts:
        title = abstract.split('\n\n')[0]
        title = title.replace('[', '').replace(']', '')
        title = title.replace("'", "")
        title = title.replace('"', '')
        url_id = doc_id.replace('_arXiv.txt', '')
        if 'astro-ph' in url_id:
            url_id = url_id.split('astro-ph')[1]
            url = f"https://arxiv.org/abs/astro-ph/{url_id}"
        else:
            if '.' in doc_id:
                url = f"https://arxiv.org/abs/{url_id}"
            else:
                url = f"https://arxiv.org/abs/hep-ph/{url_id}"
        
        linked_title = f"[{title}]({url})"
        df_data.append({"Title": linked_title, "Activation value": activation_value})
    
    df_top_abstracts = pd.DataFrame(df_data)
    styled_top_abstracts = df_top_abstracts.style.format({
        "Activation value": "{:.4f}"
    })

    # Activation value distribution
    topk_indices = subject_data[subject]['topk_indices']
    topk_values = subject_data[subject]['topk_values']

    activation_values = np.where(topk_indices == index, topk_values, 0).max(axis=1)
    fig2 = px.histogram(x=activation_values, nbins=50)
    fig2.update_layout(
        #title=f'{feature["label"]}',
        xaxis_title='Activation value',
        yaxis_title=None,
        yaxis_type='log',
        height=220,
    )

    # Correlated features
    decoder = subject_data[subject]['decoder']
    feature_vector = decoder[:, index]
    decoder_without_feature = np.delete(decoder, index, axis=1)
    cosine_similarities = np.dot(feature_vector, decoder_without_feature) / (np.linalg.norm(decoder_without_feature, axis=0) * np.linalg.norm(feature_vector))

    topk = 5
    topk_indices_cosine = np.argsort(-cosine_similarities)[:topk]
    topk_values_cosine = cosine_similarities[topk_indices_cosine]

    bottomk = 5
    bottomk_indices_cosine = np.argsort(cosine_similarities)[:bottomk]
    bottomk_values_cosine = cosine_similarities[bottomk_indices_cosine]
    
    df_top_correlated = pd.DataFrame({
        "Feature": [get_feature_from_index(subject, i)['label'] for i in topk_indices_cosine],
        "Cosine similarity": topk_values_cosine
    })
    df_top_correlated_styled = style_dataframe(df_top_correlated, is_top=True)

    # Create dataframe for bottom 5 correlated features
    df_bottom_correlated = pd.DataFrame({
        "Feature": [get_feature_from_index(subject, i)['label'] for i in bottomk_indices_cosine],
        "Cosine similarity": bottomk_values_cosine
    })
    df_bottom_correlated_styled = style_dataframe(df_bottom_correlated, is_top=False)

    # Co-occurrences
    co_occurrences = calculate_co_occurrences(subject, index)
    topk = 5
    topk_indices_co_occurrence = np.argsort(-co_occurrences)[:topk]
    topk_values_co_occurrence = co_occurrences[topk_indices_co_occurrence]

    # Create dataframe for top 5 co-occurring features
    df_co_occurrences = pd.DataFrame({
        "Feature": [get_feature_from_index(subject, i)['label'] for i in topk_indices_co_occurrence],
        "Co-occurrences": topk_values_co_occurrence
    })
    df_co_occurrences_styled = df_co_occurrences.style.format({
        "Co-occurrences": "{:.2f}"  # 2 decimal points
    })

    # Add new code for feature splitting
    nns_16to64 = subject_data[subject]['nns_16to64']
    nns_32to64 = subject_data[subject]['nns_32to64']

    # Get nearest neighbors for 16 and 32
    #nn_16 = nns_16to64[str(index)]

    # this is really involved it's a lot easier the other direction
    nn_16 = []
    for key in nns_16to64.keys():
        for match in nns_16to64[key]:
            if index == match['feature'][0]:
                nn_16.append([key, float(match['similarity'])])
    
    #nn_32 = nns_32to64[str(index)]
    nn_32 = []
    for key in nns_32to64.keys():
        for match in nns_32to64[key]:
            if index == match['feature'][0]:
                nn_32.append([key, float(match['similarity'])])

    # Create dataframes for 16 and 32 nearest neighbors
    try:
        df_16 = pd.DataFrame(nn_16, columns=["Feature", "Cosine Similarity"])
        df_16 = df_16.style.format({"Cosine Similarity": "{:.4f}"})
    except:
        df_16 = pd.DataFrame(["No Match"], columns=["Feature"])

    try:
        df_32 = pd.DataFrame(nn_32, columns=["Feature", "Cosine Similarity"])
        df_32 = df_32.style.format({"Cosine Similarity": "{:.4f}"})
    except:
        df_32 = pd.DataFrame(["No Match"], columns=["Feature"])

    return output, styled_top_abstracts, df_top_correlated_styled, df_bottom_correlated_styled, df_co_occurrences_styled, fig2, df_16, df_32

def create_interactive_directed_graph(family):
    matrix = np.array(family['matrix'])
    matrix[matrix < 0.07] = 0
    densities = family['densities']
    for i in range(len(densities)):
        for j in range(len(densities)):
            if densities[i] < densities[j]:
                matrix[i][j] = 0
    
    G = nx.from_numpy_array(matrix, create_using=nx.DiGraph())

    num_nodes = len(family['feature_f1'])
    all_f1s = family['feature_pearson'] + [family['family_pearson']]
    node_info = {i: {"name": f"{family['feature_names'][i]}", "density": family['densities'][i], "pearson": all_f1s[i]} for i in range(num_nodes)}
    nx.set_node_attributes(G, node_info)

    # Create node trace
    node_x = []
    node_y = []
    node_text = []
    node_size = []
    node_color = []
    pos = nx.spring_layout(G, k = np.sqrt(1/num_nodes) * 3)
    for node in G.nodes():
        x, y = pos[node]
        node_x.append(x)
        node_y.append(y)
        node_text.append(G.nodes[node]['name'] + "<br>log density: " + str(round(np.log10(G.nodes[node]['density'] + 1e-5), 3)))
        node_size.append((np.log10(G.nodes[node]['density'] + 1e-5) + 6) * 10)
        node_color.append(G.nodes[node]['pearson'])

    node_trace = go.Scatter(
        x=node_x, y=node_y,
        mode='markers',
        hoverinfo='text',
        marker=dict(
            showscale=True,
            colorscale='purples',
            size=node_size,  # Set node marker size to node['f1']
            color=node_color,
            cmin = 0,
            cmax = 1,
            colorbar=dict(
                thickness=15,
                title='Pearson Correlation',
                xanchor='left',
                titleside='right',
            ),
            line_width=2,
            opacity = 1,),
            opacity = 1)

    node_trace.text = node_text

    # Create edge trace
    edge_traces = []
    annotations = []
    for edge in G.edges():
        x0, y0 = pos[edge[0]]
        x1, y1 = pos[edge[1]]
        weight = matrix[edge[0], edge[1]]

        # Calculate offset (adjust this value to move arrows further from or closer to nodes)
        offset = 0.00
        start_x = x0
        start_y = y0
        end_x = x1
        end_y = y1

        # # Calculate new start and end points
        # if start_x > end_x:
        #     start_x = x0 - offset
        #     end_x = x0 + offset
        # else:
        #     start_x = x0 + offset
        #     end_x = x1 - offset
        # if start_y > end_y:
        #     start_y = y0 - offset
        #     end_y = y1 + offset
        # else:
        #     start_y = y0 + offset
        #     end_y = y1 - offset

        edge_trace = go.Scatter(
            x=[start_x, end_x, None],
            y=[start_y, end_y, None],
            line=dict(width=weight * 20, color='#888'),  # Multiply weight by 20 for better visibility
            hovertext="weight: " + str(round(weight, 3)),  # Set the hover text to the edge weight
            mode='lines',
            line_shape='spline',
            opacity = 0.5,
        )
        edge_traces.append(edge_trace)
        
        annotation = dict(
            ax=start_x,
            ay=start_y,
            x=end_x,
            y=end_y,
            xref='x',
            yref='y',
            axref='x',
            ayref='y',
            showarrow=True,
            arrowhead=4,
            arrowsize=4, #max(min(weight * 3, 0.3), 2),  # Reduced from 30 to 10
            arrowwidth=1,  # Reduced from 30 to 2
            arrowcolor='#999',
            opacity = 1,
        )
        annotations.append(annotation)

    annotation_trace = go.Scatter(x=[], y=[], mode='markers', hoverinfo='none', marker=dict(opacity=0))

    # Create the figure
    fig = go.Figure(data=[annotation_trace, *edge_traces, node_trace],
                    layout=go.Layout(
                        showlegend=False,
                        hovermode='closest',
                        margin=dict(b=20,l=5,r=5,t=40),
                        xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
                        yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)),
                    )
    fig.update_xaxes(showline=False, linewidth=0, gridcolor='white')
    fig.update_yaxes(showline=False, linewidth=0, gridcolor='white')
    fig.update_layout(
        plot_bgcolor='white',
        annotations=annotations,
    )
    return fig

# Modify the main interface function
def create_interface():
    custom_css = """
    #custom-slider-* {
        background-color: #ffe6e6;
    }
    """

    with gr.Blocks(css=custom_css) as demo:
        subject = gr.Dropdown(choices=['astroPH', 'csLG'], label="Select Subject", value='astroPH')
        
        with gr.Tabs():

            with gr.Tab("Home"):
                gr.Markdown("""
                # SAErch: Sparse Autoencoder-enhanced Semantic Search

                Welcome to SAErch, an innovative approach to semantic search using Sparse Autoencoders (SAEs) trained on dense text embeddings. This tool builds upon recent advancements in the application of SAEs to language models and embeddings.

                ## Key Concepts:

                1. **Sparse Autoencoders (SAEs)**: Neural networks that learn to reconstruct input data using a sparse set of features, helping to disentangle complex representations. SAEs have shown promising results in uncovering interpretable features in language models.

                2. **Feature Families**: Groups of related SAE features that represent concepts at varying levels of abstraction, allowing for multi-scale semantic analysis and manipulation.

                3. **Embedding Interventions**: Technique to modify search queries by manipulating specific semantic features identified by the SAE, enabling fine-grained control over query semantics.

                ## How It Works:

                1. SAEs are trained on embeddings from scientific paper abstracts, learning interpretable features that capture various semantic concepts.
                2. Users can interact with these features to fine-tune search queries.
                3. The system performs semantic search using the modified embeddings, allowing for more precise and controllable results.

                ## Key References:

                - [Towards Monosemanticity: Decomposing Language Models With Dictionary Learning](https://transformer-circuits.pub/2023/monosemantic-features) - Anthropic's pioneering work on applying SAEs to language models.
                - [Prism: Mapping Interpretable Concepts and Features in a Latent Space of Language](https://thesephist.com/posts/prism/#caveats-and-limitations) - An early application of SAEs to embeddings, demonstrating their potential for interpretable concept mapping.
                - [Scaling and Evaluating Sparse Autoencoders](https://arxiv.org/html/2406.04093v1) - OpenAI's research on scaling SAEs, showcasing the effectiveness of top-k SAEs.

                Explore the "SAErch" tab to try out the semantic search capabilities, or dive into the "Feature Visualisation" tab to examine the learned features in more detail.

                This tool demonstrates how SAEs can bridge the gap between the semantic richness of dense embeddings and the interpretability of sparse representations, offering new possibilities for precise and explainable semantic search.
                """)


            with gr.Tab("SAErch"):
                input_text = gr.Textbox(label="input")
                search_results_state = gr.State([])
                feature_values_state = gr.State([])
                feature_indices_state = gr.State([])
                manually_added_features_state = gr.State([])

                def update_search_results(feature_values, feature_indices, manually_added_features, current_subject):
                    ae = subject_data[current_subject]['ae']
                    abstract_embeddings = subject_data[current_subject]['abstract_embeddings']
                    abstract_texts = subject_data[current_subject]['abstract_texts']
                    df_metadata = subject_data[current_subject]['df_metadata']

                    # Combine manually added features with query-generated features
                    all_indices = []
                    all_values = []
                    
                    # Add manually added features first
                    for index in manually_added_features:
                        if index not in all_indices:
                            all_indices.append(index)
                            all_values.append(feature_values[feature_indices.index(index)] if index in feature_indices else 0.0)
                    
                    # Add remaining query-generated features
                    for index, value in zip(feature_indices, feature_values):
                        if index not in all_indices:
                            all_indices.append(index)
                            all_values.append(value)

                    # Reconstruct query embedding
                    topk_indices = torch.tensor(all_indices).to(device)
                    topk_values = torch.tensor(all_values).to(device)
                    
                    intervened_embedding = intervened_hidden_to_intervened_embedding(topk_indices, topk_values, ae)
                    intervened_embedding = intervened_embedding.cpu().numpy().flatten()

                    # Perform similarity search
                    sims = np.dot(abstract_embeddings, intervened_embedding)
                    topk_indices_search = np.argsort(sims)[::-1][:10]
                    doc_ids = abstract_texts['doc_ids']
                    topk_doc_ids = [doc_ids[i] for i in topk_indices_search]

                    # Prepare search results
                    search_results = []
                    for doc_id in topk_doc_ids:
                        metadata = df_metadata[df_metadata['arxiv_id'] == doc_id].iloc[0]
                        title = metadata['title'].replace('[', '').replace(']', '')
                        title = title.replace("'", "")

                        url_id = doc_id.replace('_arXiv.txt', '')
                        if 'astro-ph' in url_id:
                            url_id = url_id.split('astro-ph')[1]
                            url = f"https://arxiv.org/abs/astro-ph/{url_id}"
                        else:
                            if '.' in doc_id:
                                url = f"https://arxiv.org/abs/{doc_id.replace('_arXiv.txt', '')}"
                            else:
                                url = f"https://arxiv.org/abs/hep-ph/{doc_id.replace('_arXiv.txt', '')}"
                        
                        linked_title = f"[{title}]({url})"
                        
                        search_results.append([
                            linked_title,
                            int(metadata['citation_count']),
                            int(metadata['year'])
                        ])

                    # Convert search_results to a DataFrame and apply styling
                    df_search_results = pd.DataFrame(search_results, columns=["Title", "Citation Count", "Year"])
                    styled_search_results = df_search_results.style.format({
                        "Citation Count": "{:.0f}",  # Keep as integer
                        "Year": "{:.0f}"  # Keep as integer
                    })

                    return styled_search_results, all_values, all_indices

                @gr.render(inputs=[input_text, search_results_state, feature_values_state, feature_indices_state, manually_added_features_state, subject])
                def show_components(text, search_results, feature_values, feature_indices, manually_added_features, current_subject):
                    if len(text) == 0:
                        return gr.Markdown("## No Input Provided")

                    if not search_results or text != getattr(show_components, 'last_query', None):
                        show_components.last_query = text
                        query_embedding = get_embedding(text)

                        ae = subject_data[current_subject]['ae']
                        with torch.no_grad():
                            recons, z_dict = ae(torch.tensor(query_embedding).unsqueeze(0).to(device))
                            topk_indices = z_dict['topk_indices'][0].cpu().numpy()
                            topk_values = z_dict['topk_values'][0].cpu().numpy()

                        feature_values = topk_values.tolist()
                        feature_indices = topk_indices.tolist()
                        search_results, feature_values, feature_indices = update_search_results(feature_values, feature_indices, manually_added_features, current_subject)

                    with gr.Row():
                        with gr.Column(scale=2):
                            df = gr.Dataframe(
                                headers=["Title", "Citation Count", "Year"],
                                value=search_results,
                                label="Top 10 Search Results",
                                datatype=["markdown", "number", "number"],  # Add this line
                                wrap=True  # Add this line to ensure long titles don't get cut off
                            )

                            feature_search = gr.Textbox(label="Search Feature Labels")
                            feature_matches = gr.CheckboxGroup(label="Matching Features", choices=[])
                            add_button = gr.Button("Add Selected Features")

                            # def search_feature_labels(search_text):
                            #     if not search_text:
                            #         return gr.CheckboxGroup(choices=[])
                            #     matches = [f for f in subject_data[current_subject]['feature_analysis'] if search_text.lower() in f['label'].lower()]
                            #     matches = sorted(matches, key=lambda x: x['pearson_correlation'], reverse=True)
                            #     matches = [f"{f['label']} ({f['index']})" for f in matches]
                                
                            #     return gr.CheckboxGroup(choices=matches[:10])

                            def search_feature_labels(search_text):
                                if not search_text:
                                    return gr.CheckboxGroup(choices=[])
                                matches = [f for f in subject_data[current_subject]['feature_analysis'] if search_text.lower() in f['label'].lower()]
                                for match in matches:
                                    if math.isnan(match['pearson_correlation']):
                                        match['pearson_correlation'] = 0
                                matches = sorted(matches, key=lambda x: x['pearson_correlation'], reverse=True)
                                matches = [f"{f['label']} ({f['index']})" for f in matches]
                                return gr.CheckboxGroup(choices=matches[:10])

                            feature_search.change(search_feature_labels, inputs=[feature_search], outputs=[feature_matches])

                            def on_add_features(selected_features, current_values, current_indices, manually_added_features):
                                if selected_features:
                                    new_indices = [int(f.split('(')[-1].strip(')')) for f in selected_features]
                                    
                                    # Add new indices to manually_added_features if they're not already there
                                    manually_added_features = list(dict.fromkeys(manually_added_features + new_indices))
                                    
                                    return gr.CheckboxGroup(value=[]), current_values, current_indices, manually_added_features
                                return gr.CheckboxGroup(value=[]), current_values, current_indices, manually_added_features

                            add_button.click(
                                on_add_features,
                                inputs=[feature_matches, feature_values_state, feature_indices_state, manually_added_features_state],
                                outputs=[feature_matches, feature_values_state, feature_indices_state, manually_added_features_state]
                            )

                        with gr.Column(scale=1):
                            update_button = gr.Button("Update Results")
                            sliders = []

                            for i, (value, index) in enumerate(zip(feature_values, feature_indices)):
                                feature = next((f for f in subject_data[current_subject]['feature_analysis'] if f['index'] == index), None)
                                label = f"{feature['label']} ({index})" if feature else f"Feature {index}"
                                
                                # Transform the value to a 0-1 range
                                transformed_value = max(0.01, min(1, value)) # Ensure value is between 0.01 and 1
                                linear_value = (np.log10(transformed_value) + 2) / 2 # Map 0.01-1 to 0-1
                                
                                # Add prefix and change color for manually added features
                                if index in manually_added_features:
                                    label = f"[Custom] {label}"
                                    slider = gr.Slider(minimum=0, maximum=1, step=0.01, value=linear_value, label=label, key=f"slider-{index}", elem_id=f"custom-slider-{index}")
                                else:
                                    slider = gr.Slider(minimum=0, maximum=1, step=0.01, value=linear_value, label=label, key=f"slider-{index}")
                                
                                sliders.append(slider)

                    def on_slider_change(*values):
                        manually_added_features = values[-1]
                        slider_values = list(values[:-1])
                        
                        # Transform slider values back to original scale
                        transformed_values = [10 ** ((2 * float(v)) - 2) for v in slider_values]
                        
                        # Reconstruct feature_indices based on the order of sliders
                        reconstructed_indices = [int(slider.label.split('(')[-1].split(')')[0]) for slider in sliders]
                        
                        new_results, new_values, new_indices = update_search_results(transformed_values, reconstructed_indices, manually_added_features, current_subject)
                        return new_results, new_values, new_indices, manually_added_features

                    update_button.click(
                        on_slider_change,
                        inputs=sliders + [manually_added_features_state],
                        outputs=[search_results_state, feature_values_state, feature_indices_state, manually_added_features_state]
                    )

                    return [df, feature_search, feature_matches, add_button, update_button] + sliders
                
            with gr.Tab("Feature Visualisation"):
                gr.Markdown("# Feature Visualiser")
                
                with gr.Tabs():
                    with gr.Tab("Individual Features"):
                        with gr.Row():
                            feature_search = gr.Textbox(label="Search Feature Labels")
                            feature_matches = gr.CheckboxGroup(label="Matching Features", choices=[])
                            visualize_button = gr.Button("Visualize Feature")
                        
                        feature_info = gr.Markdown()

                        abstracts_heading = gr.Markdown("## Top 5 Abstracts")
                        top_abstracts = gr.Dataframe(
                            headers=["Title", "Activation value"],
                            datatype=["markdown", "number"],
                            interactive=False,
                            wrap=True
                        )
                        
                        gr.Markdown("## Feature Splitting")
                        with gr.Row():
                            with gr.Column(scale=1):
                                gr.Markdown("### Best Match in SAE16")
                                nn_16_table = gr.Dataframe(
                                    headers=["Feature", "Cosine Similarity"],
                                    interactive=False
                                )
                            with gr.Column(scale=1):
                                gr.Markdown("### Best Match in SAE32")
                                nn_32_table = gr.Dataframe(
                                    headers=["Feature", "Cosine Similarity"],
                                    interactive=False
                                )

                        with gr.Row():
                            with gr.Column(scale=1):
                                gr.Markdown("## Top Co-occurring Features")
                                co_occurring_features = gr.Dataframe(
                                    headers=["Feature", "Co-occurrences"],
                                    interactive=False
                                )
                            with gr.Column(scale=1):
                                gr.Markdown(f"## Activation Value Distribution")
                                activation_dist = gr.Plot()

                        gr.Markdown("## Correlated Features")
                        with gr.Row():
                            with gr.Column(scale=1):
                                gr.Markdown("### Top Correlated Features")
                                top_correlated = gr.Dataframe(
                                    headers=["Feature", "Cosine similarity"],
                                    interactive=False
                                )
                            with gr.Column(scale=1):
                                gr.Markdown("### Bottom Correlated Features")
                                bottom_correlated = gr.Dataframe(
                                    headers=["Feature", "Cosine similarity"],
                                    interactive=False
                                )
                        
                       
                        

                        # def search_feature_labels(search_text, current_subject):
                        #     if not search_text:
                        #         return gr.CheckboxGroup(choices=[])
                        #     matches = [f for f in subject_data[current_subject]['feature_analysis'] if search_text.lower() in f['label'].lower()]
                        #     matches = sorted(matches, key=lambda x: x['pearson_correlation'], reverse=True)
                        #     matches = [f"{f['label']} ({f['index']})" for f in matches]
                            
                        #     return gr.CheckboxGroup(choices=matches[:10])

                        def search_feature_labels(search_text, current_subject):
                            if not search_text:
                                return gr.CheckboxGroup(choices=[])
                            matches = [f for f in subject_data[current_subject]['feature_analysis'] if search_text.lower() in f['label'].lower()]
                            for match in matches:
                                if math.isnan(match['pearson_correlation']):
                                    match['pearson_correlation'] = 0
                            matches = sorted(matches, key=lambda x: x['pearson_correlation'], reverse=True)
                            matches = [f"{f['label']} ({f['index']})" for f in matches]
                            return gr.CheckboxGroup(choices=matches[:10])

                        feature_search.change(search_feature_labels, inputs=[feature_search, subject], outputs=[feature_matches])

                        def on_visualize(selected_features, current_subject):
                            if not selected_features:
                                return "Please select a feature to visualize.", None, None, None, None, None, "", []
                            
                            # Extract the feature index from the selected feature string
                            feature_index = int(selected_features[0].split('(')[-1].strip(')'))
                            feature_info, top_abstracts, top_correlated, bottom_correlated, co_occurring_features, activation_dist, nn_16, nn_32 = visualize_feature(current_subject, feature_index)
                            
                            # Return the visualization results along with empty values for search box and checkbox
                            return feature_info, top_abstracts, top_correlated, bottom_correlated, co_occurring_features, activation_dist, "", [], nn_16, nn_32

                        visualize_button.click(
                            on_visualize,
                            inputs=[feature_matches, subject],
                            outputs=[feature_info, top_abstracts, top_correlated, bottom_correlated, co_occurring_features, activation_dist, feature_search, feature_matches, nn_16_table, nn_32_table]
                        )

                    with gr.Tab("Feature Families"):
                        gr.Markdown("# Feature Families")
                        
                        with gr.Row():
                            family_search = gr.Textbox(label="Search Feature Families")
                            family_matches = gr.CheckboxGroup(label="Matching Feature Families", choices=[])
                            visualize_family_button = gr.Button("Visualize Feature Family")
                        
                        
                        family_dataframe = gr.Dataframe(
                            headers=["Feature", "Parent Co-Occurrence", "F1 Score", "Pearson"],
                            datatype=["markdown", "number", "number", "number"],
                            label="Family and Child Features"
                        )

                        gr.Markdown("# Family Graph")
                        graph_plot = gr.Plot(label="Directed Graph")

                        # family_info = gr.Markdown()

                        # def search_feature_families(search_text, current_subject):
                        #     family_analysis = subject_data[current_subject]['family_analysis']
                        #     if not search_text:
                        #         return gr.CheckboxGroup(choices=[])
                        #     matches = [family for family in family_analysis if search_text.lower() in family['superfeature'].lower()]
                            
                        #     matches = sorted(matches, key=lambda x: x['family_pearson'], reverse=True)
                        #     matches = [family['superfeature'] for family in matches]
                        #     matches = list(dict.fromkeys(matches))
                        #     return gr.CheckboxGroup(choices=matches[:10])  # Limit to top 10 matches

                        def search_feature_families(search_text, current_subject):
                            family_analysis = subject_data[current_subject]['family_analysis']
                            if not search_text:
                                return gr.CheckboxGroup(choices=[])
                            matches = [family for family in family_analysis if search_text.lower() in family['superfeature'].lower()]
                            for family in matches:
                                if math.isnan(family['family_pearson']):
                                    family['family_pearson'] = 0
                            matches = sorted(matches, key=lambda x: x['family_pearson'], reverse=True)
                            matches = [family['superfeature'] for family in matches]
                            matches = list(dict.fromkeys(matches))
                            return gr.CheckboxGroup(choices=matches[:10])  # Limit to top 10 matches

                        def visualize_feature_family(selected_families, current_subject):
                            if not selected_families:
                                return "Please select a feature family to visualize.", None, "", []

                            selected_family = selected_families[0]  # Take the first selected family
                            family_analysis = subject_data[current_subject]['family_analysis']
                            
                            family_data = next((family for family in family_analysis if family['superfeature'] == selected_family), None)
                            if not family_data:
                                return "Invalid feature family selected.", None, "", []
                            
                            output = f"# {family_data['superfeature']}\n\n"
                            
                            # Create DataFrame
                            df_data = [
                                {
                                    "Feature": f"## {family_data['superfeature']}",
                                    "Parent Co-Occurrence": 1,
                                    "F1 Score": round(family_data['family_f1'], 2),
                                    "Pearson": round(family_data['family_pearson'], 4)
                                },
                            ]
                            
                            coocs = np.array(family_data['matrix'])[:, -1]
                            # print(coocs)
                            for name, cooc, f1, pearson in zip(family_data['feature_names'], coocs, family_data['feature_f1'], family_data['feature_pearson']):
                                df_data.append({
                                    "Feature": name,
                                    "Parent Co-Occurrence": round(cooc, 2),
                                    "F1 Score": round(f1, 2),
                                    "Pearson": round(pearson, 4)
                                })
                            
                            df = pd.DataFrame(df_data)
                            
                            # Add super reasoning below the dataframe
                            output += "## Super Reasoning\n"
                            output += f"{family_data['super_reasoning']}\n\n"
                            
                            graph = create_interactive_directed_graph(family_data)

                            #return output, df, "", [], graph  # Return empty string for search box and empty list for checkbox
                            return df, "", [], graph

                        family_search.change(search_feature_families, inputs=[family_search, subject], outputs=[family_matches])
                        visualize_family_button.click(
                            visualize_feature_family,
                            inputs=[family_matches, subject],
                            #outputs=[family_info, family_dataframe, family_search, family_matches, graph_plot]
                            outputs=[family_dataframe, family_search, family_matches, graph_plot]
                        )


        # Add logic to update components when subject changes
        def on_subject_change(new_subject):
            # Clear all states and return empty values for all components
            return [], [], [], [], "", [], "", [], None, None, None, None, None, None

        subject.change(
            on_subject_change,
            inputs=[subject],
            outputs=[search_results_state, feature_values_state, feature_indices_state, manually_added_features_state, 
                     input_text, feature_matches, feature_search, feature_matches, 
                     feature_info, top_abstracts, top_correlated, bottom_correlated, co_occurring_features, activation_dist]
        )

    return demo

# Launch the interface
if __name__ == "__main__":
    demo = create_interface()
    demo.launch()