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a57
 
 
 
 
 
 
 
 
 
 
 
 
 
d12bf77
f0f1a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d12bf77
 
f0f1a57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
import streamlit as st
import anthropic
import openai 
import base64
import cv2
import glob
import json
import math
import os
import pytz
import random
import re
import requests
import textract
import time
import zipfile
import plotly.graph_objects as go
import streamlit.components.v1 as components
from datetime import datetime
from audio_recorder_streamlit import audio_recorder
from bs4 import BeautifulSoup
from collections import defaultdict, deque, Counter
from dotenv import load_dotenv
from gradio_client import Client
from huggingface_hub import InferenceClient
from io import BytesIO
from PIL import Image
from PyPDF2 import PdfReader
from urllib.parse import quote
from xml.etree import ElementTree as ET
from openai import OpenAI
import extra_streamlit_components as stx
from streamlit.runtime.scriptrunner import get_script_run_ctx
import asyncio
import edge_tts
from streamlit_marquee import streamlit_marquee
from concurrent.futures import ThreadPoolExecutor
from functools import partial
from typing import Dict, List, Optional, Tuple, Union

# ─────────────────────────────────────────────────────────
# 1. CORE CONFIGURATION & SETUP
# ─────────────────────────────────────────────────────────
st.set_page_config(
    page_title="🚲TalkingAIResearcherπŸ†",
    page_icon="πŸš²πŸ†",
    layout="wide",
    initial_sidebar_state="auto",
    menu_items={
        'Get Help': 'https://huggingface.co/awacke1',
        'Report a bug': 'https://huggingface.co/spaces/awacke1',
        'About': "🚲TalkingAIResearcherπŸ†"
    }
)

load_dotenv()

# Available English voices for Edge TTS
EDGE_TTS_VOICES = [
    "en-US-AriaNeural",
    "en-US-GuyNeural",
    "en-US-JennyNeural",
    "en-GB-SoniaNeural",
    "en-GB-RyanNeural",
    "en-AU-NatashaNeural",
    "en-AU-WilliamNeural",
    "en-CA-ClaraNeural",
    "en-CA-LiamNeural"
]

# Session state initialization with default values
DEFAULT_SESSION_STATE = {
    'marquee_settings': {
        "background": "#1E1E1E",
        "color": "#FFFFFF",
        "font-size": "14px",
        "animationDuration": "20s",
        "width": "100%",
        "lineHeight": "35px"
    },
    'tts_voice': EDGE_TTS_VOICES[0],
    'audio_format': 'mp3',
    'transcript_history': [],
    'chat_history': [],
    'openai_model': "gpt-4o-2024-05-13",
    'messages': [],
    'last_voice_input': "",
    'editing_file': None,
    'edit_new_name': "",
    'edit_new_content': "",
    'viewing_prefix': None,
    'should_rerun': False,
    'old_val': None,
    'last_query': "",
    'marquee_content': "πŸš€ Welcome to TalkingAIResearcher | πŸ€– Your Research Assistant",
    'enable_audio': False,
    'enable_download': False,
    'enable_claude': True,
    'audio_cache': {},
    'paper_cache': {},
    'download_link_cache': {},
    'performance_metrics': defaultdict(list),
    'operation_timings': defaultdict(float)
}

# Initialize session state
for key, value in DEFAULT_SESSION_STATE.items():
    if key not in st.session_state:
        st.session_state[key] = value

# API Keys and Configuration
openai_api_key = os.getenv('OPENAI_API_KEY', "")
anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "")
xai_key = os.getenv('xai', "")

if 'OPENAI_API_KEY' in st.secrets:
    openai_api_key = st.secrets['OPENAI_API_KEY']
if 'ANTHROPIC_API_KEY' in st.secrets:
    anthropic_key = st.secrets["ANTHROPIC_API_KEY"]

openai.api_key = openai_api_key
openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_ORG_ID'))
HF_KEY = os.getenv('HF_KEY')
API_URL = os.getenv('API_URL')

# File type emojis for display
FILE_EMOJIS = {
    "md": "πŸ“",
    "mp3": "🎡",
    "wav": "πŸ”Š",
    "pdf": "πŸ“„",
    "txt": "πŸ“‹",
    "json": "πŸ“Š",
    "csv": "πŸ“ˆ"
}

# ─────────────────────────────────────────────────────────
# 2. PERFORMANCE MONITORING & TIMING
# ─────────────────────────────────────────────────────────

class PerformanceTimer:
    """Context manager for timing operations with automatic logging."""
    def __init__(self, operation_name: str):
        self.operation_name = operation_name
        self.start_time = None
        
    def __enter__(self):
        self.start_time = time.time()
        return self
        
    def __exit__(self, exc_type, exc_val, exc_tb):
        if not exc_type:  # Only log if no exception occurred
            duration = time.time() - self.start_time
            st.session_state['operation_timings'][self.operation_name] = duration
            st.session_state['performance_metrics'][self.operation_name].append(duration)

def log_performance_metrics():
    """Display performance metrics in the sidebar."""
    st.sidebar.markdown("### ⏱️ Performance Metrics")
    
    metrics = st.session_state['operation_timings']
    if metrics:
        total_time = sum(metrics.values())
        st.sidebar.write(f"**Total Processing Time:** {total_time:.2f}s")
        
        # Create timing breakdown
        for operation, duration in metrics.items():
            percentage = (duration / total_time) * 100
            st.sidebar.write(f"**{operation}:** {duration:.2f}s ({percentage:.1f}%)")
            
        # Show timing history chart
        if st.session_state['performance_metrics']:
            history_data = []
            for op, times in st.session_state['performance_metrics'].items():
                if times:  # Only show if we have timing data
                    avg_time = sum(times) / len(times)
                    history_data.append({"Operation": op, "Avg Time (s)": avg_time})
            
            if history_data:  # Create chart if we have data
                st.sidebar.markdown("### πŸ“ˆ Timing History")
                chart_data = pd.DataFrame(history_data)
                st.sidebar.bar_chart(chart_data.set_index("Operation"))

# ─────────────────────────────────────────────────────────
# 3. OPTIMIZED AUDIO GENERATION
# ─────────────────────────────────────────────────────────

def clean_for_speech(text: str) -> str:
    """Clean up text for TTS output with enhanced cleaning."""
    with PerformanceTimer("text_cleaning"):
        # Remove markdown formatting
        text = re.sub(r'#+ ', '', text)  # Remove headers
        text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text)  # Clean links
        text = re.sub(r'[*_~`]', '', text)  # Remove emphasis markers
        
        # Remove code blocks
        text = re.sub(r'```[\s\S]*?```', '', text)
        text = re.sub(r'`[^`]*`', '', text)
        
        # Clean up whitespace
        text = re.sub(r'\s+', ' ', text)
        text = text.replace("\n", " ")
        text = text.replace("</s>", " ")
        
        # Remove URLs
        text = re.sub(r'https?://\S+', '', text)
        text = re.sub(r'\(https?://[^\)]+\)', '', text)
        
        # Final cleanup
        text = text.strip()
        return text

async def async_edge_tts_generate(
    text: str,
    voice: str,
    rate: int = 0,
    pitch: int = 0,
    file_format: str = "mp3"
) -> Tuple[Optional[str], float]:
    """Asynchronous TTS generation with performance tracking and caching."""
    with PerformanceTimer("tts_generation") as timer:
        # Clean and validate text
        text = clean_for_speech(text)
        if not text.strip():
            return None, 0
        
        # Check cache
        cache_key = f"{text[:100]}_{voice}_{rate}_{pitch}_{file_format}"
        if cache_key in st.session_state['audio_cache']:
            return st.session_state['audio_cache'][cache_key], 0
        
        try:
            # Generate audio
            rate_str = f"{rate:+d}%"
            pitch_str = f"{pitch:+d}Hz"
            communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
            
            # Generate unique filename
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"audio_{timestamp}_{random.randint(1000, 9999)}.{file_format}"
            
            # Save audio file
            await communicate.save(filename)
            
            # Cache result
            st.session_state['audio_cache'][cache_key] = filename
            return filename, time.time() - timer.start_time
            
        except Exception as e:
            st.error(f"Error generating audio: {str(e)}")
            return None, 0

async def async_save_qa_with_audio(
    question: str,
    answer: str,
    voice: Optional[str] = None
) -> Tuple[str, Optional[str], float, float]:
    """Asynchronously save Q&A to markdown and generate audio with timing."""
    voice = voice or st.session_state['tts_voice']
    
    with PerformanceTimer("qa_save") as timer:
        # Save markdown
        md_start = time.time()
        combined_text = f"# Question\n{question}\n\n# Answer\n{answer}"
        md_file = create_file(question, answer, "md")
        md_time = time.time() - md_start
        
        # Generate audio if enabled
        audio_file = None
        audio_time = 0
        if st.session_state['enable_audio']:
            audio_text = f"{question}\n\nAnswer: {answer}"
            audio_file, audio_time = await async_edge_tts_generate(
                audio_text,
                voice=voice,
                file_format=st.session_state['audio_format']
            )
        
        return md_file, audio_file, md_time, audio_time

def create_download_link_with_cache(
    file_path: str,
    file_type: str = "mp3"
) -> str:
    """Create download link with caching and error handling."""
    with PerformanceTimer("download_link_generation"):
        # Check cache first
        cache_key = f"dl_{file_path}"
        if cache_key in st.session_state['download_link_cache']:
            return st.session_state['download_link_cache'][cache_key]
        
        try:
            with open(file_path, "rb") as f:
                b64 = base64.b64encode(f.read()).decode()
            
            # Generate appropriate link based on file type
            filename = os.path.basename(file_path)
            if file_type == "mp3":
                link = f'<a href="data:audio/mpeg;base64,{b64}" download="{filename}">🎡 Download {filename}</a>'
            elif file_type == "wav":
                link = f'<a href="data:audio/wav;base64,{b64}" download="{filename}">πŸ”Š Download {filename}</a>'
            elif file_type == "md":
                link = f'<a href="data:text/markdown;base64,{b64}" download="{filename}">πŸ“ Download {filename}</a>'
            else:
                link = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">⬇️ Download {filename}</a>'
            
            # Cache and return
            st.session_state['download_link_cache'][cache_key] = link
            return link
            
        except Exception as e:
            st.error(f"Error creating download link: {str(e)}")
            return ""



# ---
def display_voice_tab():
    """Display voice input tab with TTS settings."""
    st.subheader("🎀 Voice Input")
    
    # Voice Settings Section
    st.markdown("### 🎀 Voice Settings")
    captionFemale='Top: 🌸 **Aria** – 🎢 **Jenny** – 🌺 **Sonia** – 🌌 **Natasha** – 🌷 **Clara**'
    captionMale='Bottom: 🌟 **Guy** – πŸ› οΈ **Ryan** – 🎻 **William** – 🌟 **Liam**'
    st.sidebar.image('Group Picture - Voices.png', caption=captionFemale + ' - ' + captionMale)
    st.sidebar.markdown("""
    
    # πŸŽ™οΈ Voice Character Agent Selector 🎭
        1. Female:
        - 🌸 **Aria** – Female: 🌟 The voice of elegance and creativity, perfect for soothing storytelling or inspiring ideas.  
        - 🎢 **Jenny** – Female: πŸ’– Sweet and friendly, she’s the go-to for warm, conversational tones.  
        - 🌺 **Sonia** – Female: πŸ’ƒ Bold and confident, ideal for commanding attention and delivering with flair.  
        - 🌌 **Natasha** – Female: ✨ Enigmatic and sophisticated, Natasha is great for a touch of mystery and charm.  
        - 🌷 **Clara** – Female: πŸŽ€ Cheerful and gentle, perfect for nurturing, empathetic conversations.  
        ---
        2. Male:
        - 🌟 **Guy** – Male: 🎩 Sophisticated and versatile, a natural fit for clear and authoritative delivery.  
        - πŸ› οΈ **Ryan** – Male: 🀝 Down-to-earth and approachable, ideal for friendly and casual exchanges.  
        - 🎻 **William** – Male: πŸ“š Classic and refined, perfect for a scholarly or thoughtful tone.  
        - 🌟 **Liam** – Male: ⚑ Energetic and upbeat, great for dynamic, engaging interactions.
        
    """)
    selected_voice = st.selectbox(
        "Select TTS Voice:",
        options=EDGE_TTS_VOICES,
        index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
    )
    
    # Audio Format Selection
    st.markdown("### πŸ”Š Audio Format")
    selected_format = st.radio(
        "Choose Audio Format:",
        options=["MP3", "WAV"],
        index=0
    )

    # Update session state if settings change
    if selected_voice != st.session_state['tts_voice']:
        st.session_state['tts_voice'] = selected_voice
        st.rerun()
    if selected_format.lower() != st.session_state['audio_format']:
        st.session_state['audio_format'] = selected_format.lower()
        st.rerun()

    # Text Input Area
    user_text = st.text_area("πŸ’¬ Message:", height=100)
    user_text = user_text.strip().replace('\n', ' ')

    # Send Button
    if st.button("πŸ“¨ Send"):
        process_voice_input(user_text)

    # Chat History
    st.subheader("πŸ“œ Chat History")
    for c in st.session_state.chat_history:
        st.write("**You:**", c["user"])
        st.write("**Response:**", c["claude"])

def display_arxiv_tab():
    """Display ArXiv search tab with options."""
    st.subheader("πŸ” Query ArXiv")
    q = st.text_input("πŸ” Query:", key="arxiv_query")
    
    # Options Section
    st.markdown("### πŸŽ› Options")
    col1, col2 = st.columns(2)
    
    with col1:
        vocal_summary = st.checkbox("πŸŽ™ Short Audio", value=True, 
                                  key="option_vocal_summary")
        extended_refs = st.checkbox("πŸ“œ Long Refs", value=False, 
                                  key="option_extended_refs")
        
    with col2:
        titles_summary = st.checkbox("πŸ”– Titles Only", value=True, 
                                   key="option_titles_summary")
        full_audio = st.checkbox("πŸ“š Full Audio", value=False, 
                               key="option_full_audio")
    
    full_transcript = st.checkbox("🧾 Full Transcript", value=False, 
                                key="option_full_transcript")
    
    if q and st.button("πŸ” Run Search"):
        st.session_state.last_query = q
        result, timings = perform_ai_lookup(
            q,
            vocal_summary=vocal_summary,
            extended_refs=extended_refs,
            titles_summary=titles_summary,
            full_audio=full_audio
        )
        
        if full_transcript:
            create_file(q, result, "md")

def display_media_tab():
    """Display media gallery tab with audio, images, and video."""
    st.header("πŸ“Έ Media Gallery")

    # Create tabs for different media types
    tabs = st.tabs(["🎡 Audio", "πŸ–Ό Images", "πŸŽ₯ Video"])
    
    # Audio Files Tab
    with tabs[0]:
        st.subheader("🎡 Audio Files")
        audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
        
        if audio_files:
            for audio_file in audio_files:
                with st.expander(os.path.basename(audio_file)):
                    st.audio(audio_file)
                    ext = os.path.splitext(audio_file)[1].replace('.', '')
                    dl_link = get_download_link(audio_file, file_type=ext)
                    st.markdown(dl_link, unsafe_allow_html=True)
        else:
            st.write("No audio files found.")
    
    # Images Tab
    with tabs[1]:
        st.subheader("πŸ–Ό Image Files")
        image_files = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
        
        if image_files:
            cols = st.slider("Columns:", 1, 5, 3, key="cols_images")
            image_cols = st.columns(cols)
            
            for i, img_file in enumerate(image_files):
                with image_cols[i % cols]:
                    try:
                        img = Image.open(img_file)
                        st.image(img, use_column_width=True)
                    except Exception as e:
                        st.error(f"Error loading image {img_file}: {str(e)}")
        else:
            st.write("No images found.")
    
    # Video Tab
    with tabs[2]:
        st.subheader("πŸŽ₯ Video Files")
        video_files = glob.glob("*.mp4") + glob.glob("*.mov") + glob.glob("*.avi")
        
        if video_files:
            for video_file in video_files:
                with st.expander(os.path.basename(video_file)):
                    st.video(video_file)
        else:
            st.write("No videos found.")

def display_editor_tab():
    """Display text editor tab with file management."""
    st.subheader("πŸ“ Text Editor")

    # File Management Section
    st.markdown("### πŸ“‚ File Management")
    
    # File Selection
    md_files = glob.glob("*.md")
    selected_file = st.selectbox(
        "Select file to edit:",
        ["New File"] + md_files,
        key="file_selector"
    )

    # Edit Area
    if selected_file == "New File":
        new_filename = st.text_input("New filename (without extension):")
        file_content = st.text_area("Content:", height=300)
        
        if st.button("πŸ’Ύ Save File"):
            if new_filename:
                try:
                    with open(f"{new_filename}.md", 'w', encoding='utf-8') as f:
                        f.write(file_content)
                    st.success(f"File {new_filename}.md saved successfully!")
                    st.session_state.should_rerun = True
                except Exception as e:
                    st.error(f"Error saving file: {str(e)}")
            else:
                st.warning("Please enter a filename.")
    else:
        try:
            # Load existing file content
            with open(selected_file, 'r', encoding='utf-8') as f:
                file_content = f.read()
            
            # Edit existing file
            edited_content = st.text_area(
                "Edit content:",
                value=file_content,
                height=300
            )
            
            col1, col2 = st.columns(2)
            with col1:
                if st.button("πŸ’Ύ Save Changes"):
                    try:
                        with open(selected_file, 'w', encoding='utf-8') as f:
                            f.write(edited_content)
                        st.success("Changes saved successfully!")
                    except Exception as e:
                        st.error(f"Error saving changes: {str(e)}")
            
            with col2:
                if st.button("πŸ—‘ Delete File"):
                    try:
                        os.remove(selected_file)
                        st.success(f"File {selected_file} deleted successfully!")
                        st.session_state.should_rerun = True
                    except Exception as e:
                        st.error(f"Error deleting file: {str(e)}")
                        
        except Exception as e:
            st.error(f"Error loading file {selected_file}: {str(e)}")

def display_settings_tab():
    """Display application settings tab."""
    st.subheader("βš™οΈ Settings")
    
    # General Settings
    st.markdown("### πŸ”§ General Settings")
    
    # Theme Selection
    theme = st.selectbox(
        "Color Theme:",
        ["Dark", "Light", "Custom"],
        index=0
    )
    
    if theme == "Custom":
        st.color_picker("Primary Color:", "#1E1E1E")
        st.color_picker("Secondary Color:", "#2D2D2D")
    
    # Performance Settings
    st.markdown("### ⚑ Performance Settings")
    
    # Cache Settings
    cache_size = st.slider(
        "Maximum Cache Size (MB):",
        0, 1000, 100
    )
    
    if st.button("Clear Cache"):
        st.session_state['audio_cache'] = {}
        st.session_state['paper_cache'] = {}
        st.session_state['download_link_cache'] = {}
        st.success("Cache cleared successfully!")
    
    # API Settings
    st.markdown("### πŸ”‘ API Settings")
    
    # Show/hide API keys
    show_keys = st.checkbox("Show API Keys")
    if show_keys:
        st.text_input("OpenAI API Key:", value=openai_api_key)
        st.text_input("Anthropic API Key:", value=anthropic_key)
    
    # Save Settings
    if st.button("πŸ’Ύ Save Settings"):
        st.success("Settings saved successfully!")
        st.session_state.should_rerun = True



def get_download_link(file: str, file_type: str = "zip") -> str:
    """
    Convert a file to base64 and return an HTML link for download.
    Supports multiple file types with appropriate MIME types.
    """
    try:
        with open(file, "rb") as f:
            b64 = base64.b64encode(f.read()).decode()
        
        # Get filename for display
        filename = os.path.basename(file)
        
        # Define MIME types and emoji icons for different file types
        mime_types = {
            "zip": ("application/zip", "πŸ“‚"),
            "mp3": ("audio/mpeg", "🎡"),
            "wav": ("audio/wav", "πŸ”Š"),
            "md": ("text/markdown", "πŸ“"),
            "pdf": ("application/pdf", "πŸ“„"),
            "txt": ("text/plain", "πŸ“‹"),
            "json": ("application/json", "πŸ“Š"),
            "csv": ("text/csv", "πŸ“ˆ"),
            "png": ("image/png", "πŸ–Ό"),
            "jpg": ("image/jpeg", "πŸ–Ό"),
            "jpeg": ("image/jpeg", "πŸ–Ό")
        }
        
        # Get MIME type and emoji for file
        mime_type, emoji = mime_types.get(
            file_type.lower(), 
            ("application/octet-stream", "⬇️")
        )
        
        # Create download link with appropriate MIME type
        link = f'<a href="data:{mime_type};base64,{b64}" download="{filename}">{emoji} Download {filename}</a>'
        
        return link
        
    except FileNotFoundError:
        return f"<p style='color: red'>❌ File not found: {file}</p>"
    except Exception as e:
        return f"<p style='color: red'>❌ Error creating download link: {str(e)}</p>"

def play_and_download_audio(file_path: str, file_type: str = "mp3"):
    """
    Display audio player and download link for audio file.
    Includes error handling and file validation.
    """
    if not file_path:
        st.warning("No audio file provided.")
        return
        
    if not os.path.exists(file_path):
        st.error(f"Audio file not found: {file_path}")
        return
        
    try:
        # Display audio player
        st.audio(file_path)
        
        # Create and display download link
        dl_link = get_download_link(file_path, file_type=file_type)
        st.markdown(dl_link, unsafe_allow_html=True)
        
    except Exception as e:
        st.error(f"Error playing audio: {str(e)}")

def get_file_info(file_path: str) -> dict:
    """
    Get detailed information about a file.
    Returns dictionary with size, modification time, and other metadata.
    """
    try:
        stats = os.stat(file_path)
        
        # Get basic file information
        info = {
            'name': os.path.basename(file_path),
            'path': file_path,
            'size': stats.st_size,
            'modified': datetime.fromtimestamp(stats.st_mtime),
            'created': datetime.fromtimestamp(stats.st_ctime),
            'type': os.path.splitext(file_path)[1].lower().strip('.'),
        }
        
        # Add formatted size
        if info['size'] < 1024:
            info['size_fmt'] = f"{info['size']} B"
        elif info['size'] < 1024 * 1024:
            info['size_fmt'] = f"{info['size']/1024:.1f} KB"
        else:
            info['size_fmt'] = f"{info['size']/(1024*1024):.1f} MB"
        
        # Add formatted dates
        info['modified_fmt'] = info['modified'].strftime("%Y-%m-%d %H:%M:%S")
        info['created_fmt'] = info['created'].strftime("%Y-%m-%d %H:%M:%S")
        
        return info
        
    except Exception as e:
        st.error(f"Error getting file info: {str(e)}")
        return None

def sanitize_filename(filename: str) -> str:
    """
    Clean and sanitize a filename to ensure it's safe for filesystem.
    Removes/replaces unsafe characters and enforces length limits.
    """
    # Remove or replace unsafe characters
    filename = re.sub(r'[<>:"/\\|?*]', '_', filename)
    
    # Remove leading/trailing spaces and dots
    filename = filename.strip('. ')
    
    # Limit length (reserving space for extension)
    max_length = 255
    name, ext = os.path.splitext(filename)
    if len(filename) > max_length:
        return name[:(max_length-len(ext))] + ext
        
    return filename

def create_file_with_metadata(filename: str, content: str, metadata: dict = None):
    """
    Create a file with optional metadata header.
    Useful for storing additional information with files.
    """
    try:
        # Sanitize filename
        safe_filename = sanitize_filename(filename)
        
        # Ensure directory exists
        os.makedirs(os.path.dirname(safe_filename) or '.', exist_ok=True)
        
        # Prepare content with metadata
        if metadata:
            metadata_str = json.dumps(metadata, indent=2)
            full_content = f"""---
{metadata_str}
---
{content}"""
        else:
            full_content = content
            
        # Write file
        with open(safe_filename, 'w', encoding='utf-8') as f:
            f.write(full_content)
            
        return safe_filename
        
    except Exception as e:
        st.error(f"Error creating file: {str(e)}")
        return None

def read_file_with_metadata(filename: str) -> tuple:
    """
    Read a file and extract any metadata header.
    Returns tuple of (content, metadata).
    """
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            content = f.read()
            
        # Check for metadata section
        if content.startswith('---\n'):
            # Find end of metadata section
            end_meta = content.find('\n---\n', 4)
            if end_meta != -1:
                try:
                    metadata = json.loads(content[4:end_meta])
                    content = content[end_meta+5:]
                    return content, metadata
                except json.JSONDecodeError:
                    pass
                    
        return content, None
        
    except Exception as e:
        st.error(f"Error reading file: {str(e)}")
        return None, None

def archive_files(file_paths: list, archive_name: str = None) -> str:
    """
    Create a zip archive containing the specified files.
    Returns path to created archive.
    """
    try:
        # Generate archive name if not provided
        if not archive_name:
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            archive_name = f"archive_{timestamp}.zip"
            
        # Create zip file
        with zipfile.ZipFile(archive_name, 'w', zipfile.ZIP_DEFLATED) as zf:
            for file_path in file_paths:
                if os.path.exists(file_path):
                    zf.write(file_path, os.path.basename(file_path))
                    
        return archive_name
        
    except Exception as e:
        st.error(f"Error creating archive: {str(e)}")
        return None

def list_files_by_type(directory: str = ".", 
                      extensions: list = None, 
                      recursive: bool = False) -> dict:
    """
    List files in directory filtered by extension.
    Returns dict grouping files by type.
    """
    try:
        if extensions is None:
            extensions = ['md', 'mp3', 'wav', 'pdf', 'txt', 'json', 'csv']
            
        files = {}
        pattern = "**/*" if recursive else "*"
        
        for ext in extensions:
            glob_pattern = f"{pattern}.{ext}"
            matches = glob.glob(os.path.join(directory, glob_pattern), 
                              recursive=recursive)
            if matches:
                files[ext] = matches
                
        return files
        
    except Exception as e:
        st.error(f"Error listing files: {str(e)}")
        return {}






def get_central_time() -> datetime:
    """Get current time in US Central timezone."""
    central = pytz.timezone('US/Central')
    return datetime.now(central)

def format_timestamp_prefix() -> str:
    """Generate timestamp prefix in format MM_dd_yy_hh_mm_AM/PM."""
    ct = get_central_time()
    return ct.strftime("%m_%d_%y_%I_%M_%p")

def get_formatted_time(dt: datetime = None, 
                      timezone: str = 'US/Central',
                      include_timezone: bool = True,
                      include_seconds: bool = False) -> str:
    """
    Format a datetime object with specified options.
    If no datetime is provided, uses current time.
    """
    if dt is None:
        tz = pytz.timezone(timezone)
        dt = datetime.now(tz)
    elif dt.tzinfo is None:
        tz = pytz.timezone(timezone)
        dt = tz.localize(dt)
    
    format_string = "%Y-%m-%d %I:%M"
    if include_seconds:
        format_string += ":%S"
    format_string += " %p"
    if include_timezone:
        format_string += " %Z"
    
    return dt.strftime(format_string)

def parse_timestamp(timestamp_str: str, 
                   timezone: str = 'US/Central') -> Optional[datetime]:
    """
    Parse a timestamp string in various formats.
    Returns timezone-aware datetime object.
    """
    try:
        # Try different format patterns
        patterns = [
            "%m_%d_%y_%I_%M_%p",  # Standard app format
            "%Y-%m-%d %I:%M %p",   # Common 12-hour format
            "%Y-%m-%d %H:%M",      # 24-hour format
            "%m/%d/%y %I:%M %p",   # US date format
            "%d/%m/%y %I:%M %p"    # European date format
        ]
        
        dt = None
        for pattern in patterns:
            try:
                dt = datetime.strptime(timestamp_str, pattern)
                break
            except ValueError:
                continue
        
        if dt is None:
            raise ValueError(f"Could not parse timestamp: {timestamp_str}")
        
        # Add timezone if not present
        if dt.tzinfo is None:
            tz = pytz.timezone(timezone)
            dt = tz.localize(dt)
            
        return dt
        
    except Exception as e:
        st.error(f"Error parsing timestamp: {str(e)}")
        return None

def get_time_ago(dt: datetime) -> str:
    """
    Convert datetime to human-readable "time ago" format.
    E.g., "2 hours ago", "3 days ago", etc.
    """
    try:
        now = datetime.now(dt.tzinfo)
        diff = now - dt
        
        seconds = diff.total_seconds()
        
        if seconds < 60:
            return "just now"
        elif seconds < 3600:
            minutes = int(seconds / 60)
            return f"{minutes} minute{'s' if minutes != 1 else ''} ago"
        elif seconds < 86400:
            hours = int(seconds / 3600)
            return f"{hours} hour{'s' if hours != 1 else ''} ago"
        elif seconds < 604800:
            days = int(seconds / 86400)
            return f"{days} day{'s' if days != 1 else ''} ago"
        elif seconds < 2592000:
            weeks = int(seconds / 604800)
            return f"{weeks} week{'s' if weeks != 1 else ''} ago"
        elif seconds < 31536000:
            months = int(seconds / 2592000)
            return f"{months} month{'s' if months != 1 else ''} ago"
        else:
            years = int(seconds / 31536000)
            return f"{years} year{'s' if years != 1 else ''} ago"
            
    except Exception as e:
        st.error(f"Error calculating time ago: {str(e)}")
        return "unknown time ago"

def format_duration(seconds: float) -> str:
    """
    Format a duration in seconds to human-readable string.
    E.g., "2m 30s", "1h 15m", etc.
    """
    try:
        if seconds < 0:
            return "invalid duration"
            
        # Handle special cases
        if seconds < 1:
            return f"{seconds * 1000:.0f}ms"
        if seconds < 60:
            return f"{seconds:.1f}s"
            
        # Calculate hours, minutes, seconds
        hours = int(seconds // 3600)
        minutes = int((seconds % 3600) // 60)
        secs = seconds % 60
        
        # Build duration string
        parts = []
        if hours > 0:
            parts.append(f"{hours}h")
        if minutes > 0:
            parts.append(f"{minutes}m")
        if secs > 0 and hours == 0:  # Only show seconds if less than an hour
            parts.append(f"{secs:.1f}s")
            
        return " ".join(parts)
        
    except Exception as e:
        st.error(f"Error formatting duration: {str(e)}")
        return "unknown duration"






async def create_paper_audio_files(papers: List[Dict], input_question: str):
    """Generate audio files for papers asynchronously with improved naming."""
    with PerformanceTimer("paper_audio_generation"):
        tasks = []
        for paper in papers:
            try:
                # Prepare text for audio generation
                audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
                audio_text = clean_for_speech(audio_text)
                
                # Create sanitized title for filename
                safe_title = paper['title'].lower()
                safe_title = re.sub(r'[^\w\s-]', '', safe_title)  # Remove special chars
                safe_title = re.sub(r'\s+', '_', safe_title)      # Replace spaces with underscores
                safe_title = safe_title[:100]                     # Limit length
                
                # Generate timestamp
                timestamp = format_timestamp_prefix()
                
                # Create filename with timestamp and title
                filename = f"{timestamp}_{safe_title}.{st.session_state['audio_format']}"
                
                # Create task for audio generation
                async def generate_audio(text, filename):
                    rate_str = "0%"
                    pitch_str = "0Hz"
                    communicate = edge_tts.Communicate(text, st.session_state['tts_voice'])
                    await communicate.save(filename)
                    return filename
                
                task = generate_audio(audio_text, filename)
                tasks.append((paper, task, filename))
            
            except Exception as e:
                st.warning(f"Error preparing audio for paper {paper['title']}: {str(e)}")
                continue
        
        # Process all audio generation tasks concurrently
        for paper, task, filename in tasks:
            try:
                audio_file = await task
                if audio_file:
                    paper['full_audio'] = audio_file
                    if st.session_state['enable_download']:
                        paper['download_base64'] = create_download_link_with_cache(
                            audio_file,
                            st.session_state['audio_format']
                        )
            except Exception as e:
                st.warning(f"Error generating audio for paper {paper['title']}: {str(e)}")
                paper['full_audio'] = None
                paper['download_base64'] = ''




                

        


# ─────────────────────────────────────────────────────────
# 4. PAPER PROCESSING & DISPLAY
# ─────────────────────────────────────────────────────────

def parse_arxiv_refs(ref_text: str) -> List[Dict[str, str]]:
    """Parse arxiv references with improved error handling."""
    if not ref_text:
        return []

    with PerformanceTimer("parse_refs"):
        results = []
        current_paper = {}
        lines = ref_text.split('\n')
        
        for i, line in enumerate(lines):
            try:
                if line.count('|') == 2:
                    # Found a new paper line
                    if current_paper:
                        results.append(current_paper)
                        if len(results) >= 20:  # Limit to 20 papers
                            break
                    
                    # Parse header parts
                    header_parts = line.strip('* ').split('|')
                    date = header_parts[0].strip()
                    title = header_parts[1].strip()
                    url_match = re.search(r'(https://arxiv.org/\S+)', line)
                    url = url_match.group(1) if url_match else f"paper_{len(results)}"
                    
                    current_paper = {
                        'date': date,
                        'title': title,
                        'url': url,
                        'authors': '',
                        'summary': '',
                        'full_audio': None,
                        'download_base64': '',
                    }
                
                elif current_paper:
                    # Add content to current paper
                    line = line.strip('* ')
                    if not current_paper['authors']:
                        current_paper['authors'] = line
                    else:
                        if current_paper['summary']:
                            current_paper['summary'] += ' ' + line
                        else:
                            current_paper['summary'] = line
            
            except Exception as e:
                st.warning(f"Error parsing line {i}: {str(e)}")
                continue
        
        # Add final paper if exists
        if current_paper:
            results.append(current_paper)
        
        return results[:20]  # Ensure we don't exceed 20 papers

async def create_paper_audio_files(papers: List[Dict], input_question: str):
    """Generate audio files for papers asynchronously with progress tracking."""
    with PerformanceTimer("paper_audio_generation"):
        tasks = []
        for paper in papers:
            try:
                # Prepare text for audio generation
                audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
                audio_text = clean_for_speech(audio_text)
                
                # Create task for audio generation
                task = async_edge_tts_generate(
                    audio_text,
                    voice=st.session_state['tts_voice'],
                    file_format=st.session_state['audio_format']
                )
                tasks.append((paper, task))
            
            except Exception as e:
                st.warning(f"Error preparing audio for paper {paper['title']}: {str(e)}")
                continue
        
        # Process all audio generation tasks concurrently
        for paper, task in tasks:
            try:
                audio_file, gen_time = await task
                if audio_file:
                    paper['full_audio'] = audio_file
                    if st.session_state['enable_download']:
                        paper['download_base64'] = create_download_link_with_cache(
                            audio_file,
                            st.session_state['audio_format']
                        )
            except Exception as e:
                st.warning(f"Error generating audio for paper {paper['title']}: {str(e)}")
                paper['full_audio'] = None
                paper['download_base64'] = ''


def initialize_marquee_settings():
    """Initialize default marquee settings if not present in session state."""
    if 'marquee_settings' not in st.session_state:
        st.session_state['marquee_settings'] = {
            "background": "#1E1E1E",
            "color": "#FFFFFF",
            "font-size": "14px",
            "animationDuration": "20s",
            "width": "100%",
            "lineHeight": "35px"
        }

def get_marquee_settings():
    """Get current marquee settings, initializing if needed."""
    initialize_marquee_settings()
    return st.session_state['marquee_settings']

def update_marquee_settings_ui():
    """Add color pickers & sliders for marquee configuration in sidebar."""
    st.sidebar.markdown("### 🎯 Marquee Settings")
    
    # Create two columns for settings
    cols = st.sidebar.columns(2)
    
    # Column 1: Color settings
    with cols[0]:
        # Background color picker
        bg_color = st.color_picker(
            "🎨 Background", 
            st.session_state['marquee_settings']["background"], 
            key="bg_color_picker"
        )
        
        # Text color picker
        text_color = st.color_picker(
            "✍️ Text Color", 
            st.session_state['marquee_settings']["color"], 
            key="text_color_picker"
        )
    
    # Column 2: Size and speed settings
    with cols[1]:
        # Font size slider
        font_size = st.slider(
            "πŸ“ Font Size", 
            10, 24, 14, 
            key="font_size_slider"
        )
        
        # Animation duration slider
        duration = st.slider(
            "⏱️ Animation Speed", 
            1, 20, 20, 
            key="duration_slider"
        )

    # Update session state with new settings
    st.session_state['marquee_settings'].update({
        "background": bg_color,
        "color": text_color,
        "font-size": f"{font_size}px",
        "animationDuration": f"{duration}s"
    })

def display_marquee(text: str, settings: dict, key_suffix: str = ""):
    """Show marquee text with specified style settings."""
    # Truncate long text to prevent performance issues
    truncated_text = text[:280] + "..." if len(text) > 280 else text
    
    # Display the marquee
    streamlit_marquee(
        content=truncated_text,
        **settings,
        key=f"marquee_{key_suffix}"
    )
    
    # Add spacing after marquee
    st.write("")

def create_paper_links_md(papers: list) -> str:
    """Creates a minimal markdown file linking to each paper's arxiv URL."""
    lines = ["# Paper Links\n"]
    for i, p in enumerate(papers, start=1):
        lines.append(f"{i}. **{p['title']}** β€” [Arxiv]({p['url']})")
    return "\n".join(lines)

def apply_custom_styling():
    """Apply custom CSS styling to the app."""
    st.markdown("""
        <style>
            .main { 
                background: linear-gradient(to right, #1a1a1a, #2d2d2d); 
                color: #fff; 
            }
            .stMarkdown { 
                font-family: 'Helvetica Neue', sans-serif; 
            }
            .stButton>button { 
                margin-right: 0.5rem; 
            }
            .streamlit-marquee {
                margin: 1rem 0;
                border-radius: 4px;
            }
            .st-emotion-cache-1y4p8pa {
                padding: 1rem;
            }
        </style>
    """, unsafe_allow_html=True)

def display_performance_metrics(timings: dict):
    """Display performance metrics with visualizations."""
    st.sidebar.markdown("### ⏱️ Performance Metrics")
    
    # Calculate total time
    total_time = sum(timings.values())
    st.sidebar.write(f"**Total Processing Time:** {total_time:.2f}s")
    
    # Show breakdown of operations
    st.sidebar.markdown("#### Operation Breakdown")
    for operation, duration in timings.items():
        percentage = (duration / total_time) * 100 if total_time > 0 else 0
        st.sidebar.write(f"**{operation}:** {duration:.2f}s ({percentage:.1f}%)")
        
        # Create a progress bar for visual representation
        st.sidebar.progress(percentage / 100)
        



def display_papers(papers: List[Dict], marquee_settings: Dict):
    """Display paper information with enhanced visualization."""
    with PerformanceTimer("paper_display"):
        st.write("## πŸ“š Research Papers")
        
        # Create tabs for different views
        tab1, tab2 = st.tabs(["πŸ“‹ List View", "πŸ“Š Grid View"])
        
        with tab1:
            for i, paper in enumerate(papers, start=1):
                # Create marquee for paper title
                marquee_text = f"πŸ“„ {paper['title']} | πŸ‘€ {paper['authors'][:120]}"
                display_marquee(marquee_text, marquee_settings, key_suffix=f"paper_{i}")
                
                # Paper details expander
                with st.expander(f"{i}. πŸ“„ {paper['title']}", expanded=True):
                    # Create PDF link
                    pdf_url = paper['url'].replace('/abs/', '/pdf/')
                    
                    # Display paper information
                    st.markdown(f"""
                        **Date:** {paper['date']}  
                        **Title:** {paper['title']}  
                        **Links:** πŸ“„ [Abstract]({paper['url']}) | πŸ“‘ [PDF]({pdf_url})
                    """)
                    st.markdown(f"**Authors:** {paper['authors']}")
                    st.markdown(f"**Summary:** {paper['summary']}")
                    
                    # Audio player and download if available
                    if paper.get('full_audio'):
                        st.write("🎧 Paper Audio Summary")
                        st.audio(paper['full_audio'])
                        if paper['download_base64']:
                            st.markdown(paper['download_base64'], unsafe_allow_html=True)
        
        with tab2:
            # Create a grid layout of papers
            cols = st.columns(3)
            for i, paper in enumerate(papers):
                with cols[i % 3]:
                    st.markdown(f"""
                        ### πŸ“„ {paper['title'][:50]}...
                        **Date:** {paper['date']}  
                        [Abstract]({paper['url']}) | [PDF]({paper['url'].replace('/abs/', '/pdf/')})
                    """)
                    if paper.get('full_audio'):
                        st.audio(paper['full_audio'])

def display_papers_in_sidebar(papers: List[Dict]):
    """Display paper listing in sidebar with lazy loading."""
    with PerformanceTimer("sidebar_display"):
        st.sidebar.title("πŸ“š Papers Overview")
        
        # Add filter options
        filter_date = st.sidebar.date_input("Filter by date:", None)
        search_term = st.sidebar.text_input("Search papers:", "")
        
        # Filter papers based on criteria
        filtered_papers = papers
        if filter_date:
            filtered_papers = [p for p in filtered_papers 
                             if filter_date.strftime("%Y-%m-%d") in p['date']]
        if search_term:
            search_lower = search_term.lower()
            filtered_papers = [p for p in filtered_papers 
                             if search_lower in p['title'].lower() 
                             or search_lower in p['authors'].lower()]
        
        # Display filtered papers
        for i, paper in enumerate(filtered_papers, start=1):
            paper_key = f"paper_{paper['url']}"
            if paper_key not in st.session_state:
                st.session_state[paper_key] = False
            
            with st.sidebar.expander(f"{i}. {paper['title'][:50]}...", expanded=False):
                # Paper metadata
                st.markdown(f"**Date:** {paper['date']}")
                
                # Links
                pdf_url = paper['url'].replace('/abs/', '/pdf/')
                st.markdown(f"πŸ“„ [Abstract]({paper['url']}) | πŸ“‘ [PDF]({pdf_url})")
                
                # Preview of authors and summary
                st.markdown(f"**Authors:** {paper['authors'][:100]}...")
                if paper['summary']:
                    st.markdown(f"**Summary:** {paper['summary'][:200]}...")
                
                # Audio controls
                if paper['full_audio']:
                    if st.button("🎡 Load Audio", key=f"btn_{paper_key}"):
                        st.session_state[paper_key] = True
                    
                    if st.session_state[paper_key]:
                        st.audio(paper['full_audio'])
                        if paper['download_base64']:
                            st.markdown(paper['download_base64'], unsafe_allow_html=True)

# ─────────────────────────────────────────────────────────
# 5. FILE MANAGEMENT & HISTORY
# ─────────────────────────────────────────────────────────

def create_file(prompt: str, response: str, file_type: str = "md") -> str:
    """Create a file with proper naming and error handling."""
    with PerformanceTimer("file_creation"):
        try:
            # Generate filename
            filename = generate_filename(prompt.strip(), response.strip(), file_type)
            
            # Ensure directory exists
            os.makedirs("generated_files", exist_ok=True)
            filepath = os.path.join("generated_files", filename)
            
            # Write content
            with open(filepath, 'w', encoding='utf-8') as f:
                if file_type == "md":
                    f.write(f"# Query\n{prompt}\n\n# Response\n{response}")
                else:
                    f.write(f"{prompt}\n\n{response}")
            
            return filepath
        
        except Exception as e:
            st.error(f"Error creating file: {str(e)}")
            return ""

def get_high_info_terms(text: str, top_n: int = 10) -> List[str]:
    """Extract most informative terms from text."""
    # Common English stop words to filter out
    stop_words = set([
        'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 
        'for', 'of', 'with', 'by', 'from', 'up', 'about', 'into', 'over',
        'after', 'the', 'this', 'that', 'these', 'those', 'what', 'which'
    ])
    
    # Extract words and bi-grams
    words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
    bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
    
    # Combine and filter terms
    combined = words + bi_grams
    filtered = [term for term in combined 
               if term not in stop_words 
               and len(term.split()) <= 2 
               and len(term) > 3]
    
    # Count and return top terms
    counter = Counter(filtered)
    return [term for term, freq in counter.most_common(top_n)]

def clean_text_for_filename(text: str) -> str:
    """Clean text for use in filenames."""
    # Remove special characters
    text = text.lower()
    text = re.sub(r'[^\w\s-]', '', text)
    
    # Remove common unhelpful words
    stop_words = set([
        'the', 'and', 'for', 'with', 'this', 'that', 'what', 'which',
        'where', 'when', 'why', 'how', 'who', 'whom', 'whose', 'ai',
        'library', 'function', 'method', 'class', 'object', 'variable'
    ])
    
    words = text.split()
    filtered = [w for w in words if len(w) > 3 and w not in stop_words]
    
    return '_'.join(filtered)[:200]

def generate_filename(prompt: str, response: str, file_type: str = "md", 
                     max_length: int = 200) -> str:
    """Generate descriptive filename from content."""
    # Get timestamp prefix
    prefix = format_timestamp_prefix() + "_"
    
    # Extract informative terms
    combined_text = (prompt + " " + response)[:500]
    info_terms = get_high_info_terms(combined_text, top_n=5)
    
    # Get content snippet
    snippet = (prompt[:40] + " " + response[:40]).strip()
    snippet_cleaned = clean_text_for_filename(snippet)
    
    # Combine and deduplicate parts
    name_parts = info_terms + [snippet_cleaned]
    seen = set()
    unique_parts = []
    for part in name_parts:
        if part not in seen:
            seen.add(part)
            unique_parts.append(part)
    
    # Create final filename
    full_name = '_'.join(unique_parts).strip('_')
    leftover_chars = max_length - len(prefix) - len(file_type) - 1
    if len(full_name) > leftover_chars:
        full_name = full_name[:leftover_chars]
    
    return f"{prefix}{full_name}.{file_type}"

def create_zip_of_files(md_files: List[str], mp3_files: List[str], 
                       wav_files: List[str], input_question: str) -> Optional[str]:
    """Create zip archive of files with optimization."""
    with PerformanceTimer("zip_creation"):
        # Filter out readme and empty files
        md_files = [f for f in md_files 
                   if os.path.basename(f).lower() != 'readme.md'
                   and os.path.getsize(f) > 0]
        
        all_files = md_files + mp3_files + wav_files
        if not all_files:
            return None
        
        try:
            # Generate zip name
            all_content = []
            for f in all_files:
                if f.endswith('.md'):
                    with open(f, 'r', encoding='utf-8') as file:
                        all_content.append(file.read())
                elif f.endswith(('.mp3', '.wav')):
                    basename = os.path.splitext(os.path.basename(f))[0]
                    all_content.append(basename.replace('_', ' '))
            
            all_content.append(input_question)
            combined_content = " ".join(all_content)
            info_terms = get_high_info_terms(combined_content, top_n=10)
            
            timestamp = format_timestamp_prefix()
            name_text = '-'.join(term for term in info_terms[:5])
            zip_name = f"archive_{timestamp}_{name_text[:50]}.zip"
            
            # Create zip file
            with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as z:
                for f in all_files:
                    z.write(f, os.path.basename(f))
            
            return zip_name
            
        except Exception as e:
            st.error(f"Error creating zip archive: {str(e)}")
            return None

# ─────────────────────────────────────────────────────────
# 6. OPTIMIZED AI LOOKUP & PROCESSING
# ─────────────────────────────────────────────────────────

def perform_ai_lookup(q: str, vocal_summary: bool = True, 
                     extended_refs: bool = False,
                     titles_summary: bool = True, 
                     full_audio: bool = False) -> Tuple[str, Dict[str, float]]:
    """Main AI lookup routine with performance optimization."""
    with PerformanceTimer("total_lookup") as total_timer:
        timings = {}

        # Add operation controls if not present
        if 'operation_controls' not in st.session_state:
            st.sidebar.markdown("### πŸ”§ Operation Controls")
            st.session_state['enable_claude'] = st.sidebar.checkbox(
                "Enable Claude Search", 
                value=st.session_state['enable_claude']
            )
            st.session_state['enable_audio'] = st.sidebar.checkbox(
                "Generate Audio", 
                value=st.session_state['enable_audio']
            )
            st.session_state['enable_download'] = st.sidebar.checkbox(
                "Create Download Links", 
                value=st.session_state['enable_download']
            )
            st.session_state['operation_controls'] = True

        result = ""
        
        # 1. Claude API (if enabled)
        if st.session_state['enable_claude']:
            with PerformanceTimer("claude_api") as claude_timer:
                try:
                    client = anthropic.Anthropic(api_key=anthropic_key)
                    response = client.messages.create(
                        model="claude-3-sonnet-20240229",
                        max_tokens=1000,
                        messages=[{"role": "user", "content": q}]
                    )
                    st.write("Claude's reply 🧠:")
                    st.markdown(response.content[0].text)
                    result = response.content[0].text
                    timings['claude_api'] = time.time() - claude_timer.start_time
                except Exception as e:
                    st.error(f"Error with Claude API: {str(e)}")
                    result = "Error occurred during Claude API call"
                    timings['claude_api'] = 0

        # 2. Async save and audio generation
        async def process_results():
            with PerformanceTimer("results_processing") as proc_timer:
                md_file, audio_file, md_time, audio_time = await async_save_qa_with_audio(
                    q, result
                )
                timings['markdown_save'] = md_time
                timings['audio_generation'] = audio_time
                
                if audio_file and st.session_state['enable_audio']:
                    st.subheader("πŸ“ Main Response Audio")
                    st.audio(audio_file)
                    
                    if st.session_state['enable_download']:
                        st.markdown(
                            create_download_link_with_cache(
                                audio_file, 
                                st.session_state['audio_format']
                            ),
                            unsafe_allow_html=True
                        )

        # Run async operations
        asyncio.run(process_results())

        # 3. Arxiv RAG with performance tracking
        if st.session_state['enable_claude']:
            with PerformanceTimer("arxiv_rag") as rag_timer:
                try:
                    st.write('Running Arxiv RAG with Claude inputs.')
                    client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
                    refs = client.predict(
                        q, 
                        10, 
                        "Semantic Search", 
                        "mistralai/Mixtral-8x7B-Instruct-v0.1",
                        api_name="/update_with_rag_md"
                    )[0]
                    timings['arxiv_rag'] = time.time() - rag_timer.start_time

                    # Process papers asynchronously
                    papers = parse_arxiv_refs(refs)
                    if papers:
                        with PerformanceTimer("paper_processing") as paper_timer:
                            async def process_papers():
                                # Create minimal links page
                                paper_links = create_paper_links_md(papers)
                                links_file = create_file(q, paper_links, "md")
                                st.markdown(paper_links)
                                
                                # Generate audio and display papers
                                await create_paper_audio_files(papers, q)
                                display_papers(papers, get_marquee_settings())
                                display_papers_in_sidebar(papers)
                            
                            asyncio.run(process_papers())
                            timings['paper_processing'] = time.time() - paper_timer.start_time
                    else:
                        st.warning("No papers found in the response.")
                except Exception as e:
                    st.error(f"Error during Arxiv RAG: {str(e)}")
                    timings['arxiv_rag'] = 0

        return result, timings

def process_voice_input(text: str):
    """Process voice input with enhanced error handling and feedback."""
    if not text:
        st.warning("Please provide some input text.")
        return

    with PerformanceTimer("voice_processing"):
        try:
            st.subheader("πŸ” Search Results")
            result, timings = perform_ai_lookup(
                text, 
                vocal_summary=True,
                extended_refs=False,
                titles_summary=True,
                full_audio=True
            )

            # Save results
            md_file, audio_file = save_qa_with_audio(text, result)
            
            # Display results
            st.subheader("πŸ“ Generated Files")
            col1, col2 = st.columns(2)
            with col1:
                st.write(f"πŸ“„ Markdown: {os.path.basename(md_file)}")
                st.markdown(get_download_link(md_file, "md"), unsafe_allow_html=True)
            
            with col2:
                if audio_file:
                    st.write(f"🎡 Audio: {os.path.basename(audio_file)}")
                    play_and_download_audio(
                        audio_file, 
                        st.session_state['audio_format']
                    )

        except Exception as e:
            st.error(f"Error processing voice input: {str(e)}")

# ─────────────────────────────────────────────────────────
# 7. SIDEBAR AND FILE HISTORY
# ─────────────────────────────────────────────────────────

def display_file_history_in_sidebar():
    """Display file history with enhanced organization and filtering."""
    with PerformanceTimer("file_history"):
        st.sidebar.markdown("---")
        st.sidebar.markdown("### πŸ“‚ File History")

        # Gather all files
        md_files = glob.glob("*.md")
        mp3_files = glob.glob("*.mp3")
        wav_files = glob.glob("*.wav")
        all_files = md_files + mp3_files + wav_files

        if not all_files:
            st.sidebar.write("No files found.")
            return

        # Add file management controls
        col1, col2 = st.sidebar.columns(2)
        with col1:
            if st.button("πŸ—‘ Delete All"):
                try:
                    for f in all_files:
                        os.remove(f)
                    st.session_state.should_rerun = True
                    st.success("All files deleted successfully.")
                except Exception as e:
                    st.error(f"Error deleting files: {str(e)}")

        with col2:
            if st.button("⬇️ Zip All"):
                zip_name = create_zip_of_files(
                    md_files, 
                    mp3_files, 
                    wav_files, 
                    st.session_state.get('last_query', '')
                )
                if zip_name:
                    st.sidebar.markdown(
                        get_download_link(zip_name, "zip"),
                        unsafe_allow_html=True
                    )

        # Add file filtering options
        st.sidebar.markdown("### πŸ” Filter Files")
        file_search = st.sidebar.text_input("Search files:", "")
        file_type_filter = st.sidebar.multiselect(
            "File types:",
            ["Markdown", "Audio"],
            default=["Markdown", "Audio"]
        )

        # Sort files by modification time
        all_files.sort(key=os.path.getmtime, reverse=True)

        # Filter files based on search and type
        filtered_files = []
        for f in all_files:
            if file_search.lower() in f.lower():
                ext = os.path.splitext(f)[1].lower()
                if (("Markdown" in file_type_filter and ext == ".md") or
                    ("Audio" in file_type_filter and ext in [".mp3", ".wav"])):
                    filtered_files.append(f)

        # Display filtered files
        for f in filtered_files:
            fname = os.path.basename(f)
            ext = os.path.splitext(fname)[1].lower().strip('.')
            emoji = FILE_EMOJIS.get(ext, 'πŸ“¦')
            
            # Get file metadata
            mod_time = datetime.fromtimestamp(os.path.getmtime(f))
            time_str = mod_time.strftime("%Y-%m-%d %H:%M:%S")
            file_size = os.path.getsize(f) / 1024  # Size in KB

            with st.sidebar.expander(f"{emoji} {fname}"):
                st.write(f"**Modified:** {time_str}")
                st.write(f"**Size:** {file_size:.1f} KB")
                
                if ext == "md":
                    try:
                        with open(f, "r", encoding="utf-8") as file_in:
                            snippet = file_in.read(200).replace("\n", " ")
                        if len(snippet) == 200:
                            snippet += "..."
                        st.write(snippet)
                        st.markdown(
                            get_download_link(f, file_type="md"),
                            unsafe_allow_html=True
                        )
                    except Exception as e:
                        st.error(f"Error reading markdown file: {str(e)}")
                
                elif ext in ["mp3", "wav"]:
                    st.audio(f)
                    st.markdown(
                        get_download_link(f, file_type=ext),
                        unsafe_allow_html=True
                    )
                
                else:
                    st.markdown(get_download_link(f), unsafe_allow_html=True)

# ─────────────────────────────────────────────────────────
# 8. MAIN APPLICATION
# ─────────────────────────────────────────────────────────

def main():
    """Main application entry point with enhanced UI and error handling."""
    try:
        # 1. Setup marquee UI in sidebar
        update_marquee_settings_ui()
        marquee_settings = get_marquee_settings()

        # 2. Display welcome marquee
        display_marquee(
            st.session_state['marquee_content'],
            {**marquee_settings, "font-size": "28px", "lineHeight": "50px"},
            key_suffix="welcome"
        )

        # 3. Main action tabs
        tab_main = st.radio(
            "Action:",
            ["🎀 Voice", "πŸ“Έ Media", "πŸ” ArXiv", "πŸ“ Editor"],
            horizontal=True
        )

        # Custom component usage
        mycomponent = components.declare_component(
            "mycomponent",
            path="mycomponent"
        )
        val = mycomponent(my_input_value="Hello")

        if val:
            # Process input value
            val_stripped = val.replace('\\n', ' ')
            edited_input = st.text_area(
                "✏️ Edit Input:",
                value=val_stripped,
                height=100
            )

            # Model selection and options
            run_option = st.selectbox("Model:", ["Arxiv"])
            col1, col2 = st.columns(2)
            
            with col1:
                #autorun = st.checkbox("βš™ AutoRun", value=True)
                autorun = st.checkbox("βš™ AutoRun", value=False)
            with col2:
                full_audio = st.checkbox("πŸ“š FullAudio", value=False)

            # Check for input changes
            input_changed = (val != st.session_state.old_val)

            if autorun and input_changed:
                st.session_state.old_val = val
                st.session_state.last_query = edited_input
                result, timings = perform_ai_lookup(
                    edited_input,
                    vocal_summary=True,
                    extended_refs=False,
                    titles_summary=True,
                    full_audio=full_audio
                )
                
                # Display performance metrics
                display_performance_metrics(timings)
            
            else:
                if st.button("β–Ά Run"):
                    st.session_state.old_val = val
                    st.session_state.last_query = edited_input
                    result, timings = perform_ai_lookup(
                        edited_input,
                        vocal_summary=True,
                        extended_refs=False,
                        titles_summary=True,
                        full_audio=full_audio
                    )
                    
                    # Display performance metrics
                    display_performance_metrics(timings)

        # Tab-specific content
        if tab_main == "πŸ” ArXiv":
            display_arxiv_tab()
        elif tab_main == "🎀 Voice":
            display_voice_tab()
        elif tab_main == "πŸ“Έ Media":
            display_media_tab()
        elif tab_main == "πŸ“ Editor":
            display_editor_tab()

        # Display file history
        display_file_history_in_sidebar()

        # Apply styling
        apply_custom_styling()

        # Check for rerun
        if st.session_state.should_rerun:
            st.session_state.should_rerun = False
            st.rerun()

    except Exception as e:
        st.error(f"An error occurred in the main application: {str(e)}")
        st.info("Please try refreshing the page or contact support if the issue persists.")

if __name__ == "__main__":
    main()