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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
#!/usr/bin/env python3
import os
import uuid
import json
import time
import tempfile
import threading
from datetime import datetime
from pathlib import Path
import atexit

import numpy as np
import tensorflow as tf
import librosa
from flask import Flask, request, jsonify, send_file, g
from flask_cors import CORS
import mido
from pydub import AudioSegment
import boto3
from werkzeug.utils import secure_filename
import logging
import gc
import subprocess
import shutil
from scipy import ndimage
from scipy.signal import find_peaks

from typing import List, Tuple, Dict, Any

# Import your existing modules
from models.model_loader import ModelLoader
from utils.utils import weighted_binary_crossentropy, focal_loss, F1Score
from models.architecture import acoustic_feature_extractor, vertical_dependencies_layer, lstm_with_attention, \
    onset_subnetwork, frame_subnetwork, offset_subnetwork, velocity_subnetwork, build_model
from postprocessing.postprocessing import MusicTranscriptionPostprocessor

# Configure logging for better debugging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

print(f"Python executable: {os.sys.executable}")

# =============================================================================
# CONFIGURATION & SETUP - ALL ORIGINAL SETTINGS
# =============================================================================

# Use absolute paths for directories
UPLOAD_FOLDER = '/app/uploads'
OUTPUT_FOLDER = '/app/output'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)

# AWS S3 Configuration - EXACTLY as original
AWS_REGION = os.environ.get("AWS_REGION", "us-east-1")
AWS_BUCKET_NAME = "flutter-audio-uploads"
AWS_ACCESS_KEY = os.environ.get("AWS_ACCESS_KEY")
AWS_SECRET_KEY = os.environ.get("AWS_SECRET_KEY")

s3_client = None
if AWS_ACCESS_KEY and AWS_SECRET_KEY:
    try:
        s3_client = boto3.client(
            's3',
            aws_access_key_id=AWS_ACCESS_KEY,
            aws_secret_access_key=AWS_SECRET_KEY,
            region_name=AWS_REGION
        )
        logger.info("βœ… S3 client initialized")
    except Exception as e:
        logger.warning(f"⚠️ S3 initialization failed: {e}")


# =============================================================================
# ENHANCED STARTUP SEQUENCE - ALL ORIGINAL FUNCTIONALITY
# =============================================================================

def setup_virtual_display():
    """Set up virtual display for MuseScore in headless environment"""
    display = ':99'

    try:
        os.environ['DISPLAY'] = display

        try:
            result = subprocess.run(['pgrep', 'Xvfb'], capture_output=True)
            if result.returncode != 0:
                logger.info("πŸ–₯️ Starting virtual display...")
                xvfb_process = subprocess.Popen([
                    'Xvfb', display,
                    '-screen', '0', '1024x768x24',
                    '-ac', '+extension', 'GLX'
                ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

                time.sleep(2)
                atexit.register(lambda: xvfb_process.terminate())
                logger.info(f"βœ… Virtual display started: {display}")
            else:
                logger.info(f"βœ… Virtual display already running: {display}")

        except Exception as e:
            logger.warning(f"⚠️ Virtual display setup warning: {e}")

    except Exception as e:
        logger.error(f"❌ Display setup error: {e}")

    return display


def comprehensive_musescore_test():
    """Complete MuseScore test including conversion - ORIGINAL FUNCTION"""
    logger.info("🎼 Running comprehensive MuseScore test...")

    commands_to_test = ['musescore3', 'musescore', 'mscore3', 'mscore']
    working_command = None

    for cmd in commands_to_test:
        try:
            result = subprocess.run([cmd, '--version'],
                                    capture_output=True, text=True, timeout=10)
            if result.returncode == 0:
                working_command = cmd
                logger.info(f"βœ… {cmd} version check passed: {result.stdout.strip()}")
                break
        except Exception as e:
            logger.debug(f"   {cmd}: {e}")

    if not working_command:
        return False, "No working MuseScore command found"

    # Test conversion with minimal MIDI
    try:
        test_midi = os.path.join(OUTPUT_FOLDER, 'test_minimal.mid')
        minimal_midi_bytes = bytes([
            0x4D, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06,  # MThd header
            0x00, 0x00, 0x00, 0x01, 0x00, 0x60,  # Format 0, 1 track, 96 tpqn
            0x4D, 0x54, 0x72, 0x6B, 0x00, 0x00, 0x00, 0x0B,  # MTrk header
            0x00, 0x90, 0x40, 0x40,  # Note on C4
            0x48, 0x80, 0x40, 0x40,  # Note off C4
            0x00, 0xFF, 0x2F, 0x00  # End of track
        ])

        with open(test_midi, 'wb') as f:
            f.write(minimal_midi_bytes)

        test_xml = os.path.join(OUTPUT_FOLDER, 'test_output.musicxml')
        result = subprocess.run([working_command, '-o', test_xml, test_midi],
                                capture_output=True, text=True, timeout=20)

        if result.returncode == 0 and os.path.exists(test_xml):
            logger.info("βœ… MusicXML conversion test passed")

            test_pdf = os.path.join(OUTPUT_FOLDER, 'test_output.pdf')
            result = subprocess.run([working_command, '-o', test_pdf, test_xml],
                                    capture_output=True, text=True, timeout=20)

            if result.returncode == 0 and os.path.exists(test_pdf):
                logger.info("βœ… PDF conversion test passed")
                conversion_success = True
            else:
                logger.warning(f"⚠️ PDF conversion failed: {result.stderr}")
                conversion_success = False

            # Clean up test files
            for test_file in [test_midi, test_xml, test_pdf]:
                if os.path.exists(test_file):
                    os.remove(test_file)

            return conversion_success, working_command
        else:
            logger.error(f"❌ MusicXML conversion failed: {result.stderr}")
            return False, f"{working_command} conversion failed"

    except Exception as e:
        logger.error(f"❌ Conversion test error: {e}")
        return False, str(e)


# Run startup sequence
logger.info("=" * 60)
logger.info("πŸš€ STARTING WAVE2NOTES WITH MUSESCORE SUPPORT")
logger.info("=" * 60)

display = setup_virtual_display()
conversion_works, musescore_status = comprehensive_musescore_test()

if conversion_works:
    logger.info(f"🎼 βœ… MuseScore fully operational: {musescore_status}")
    logger.info("   πŸ“„ Sheet music generation enabled")
else:
    logger.info(f"🎼 ❌ MuseScore issues: {musescore_status}")
    logger.info("   πŸ“„ Sheet music generation disabled")

# Verify directories
logger.info("πŸ“ Checking directories...")
for folder_name, folder_path in [("Upload", UPLOAD_FOLDER), ("Output", OUTPUT_FOLDER)]:
    try:
        os.makedirs(folder_path, mode=0o755, exist_ok=True)
        if os.access(folder_path, os.W_OK):
            logger.info(f"βœ… {folder_name} folder ready: {folder_path}")
        else:
            logger.warning(f"⚠️ {folder_name} folder not writable: {folder_path}")
    except Exception as e:
        logger.error(f"❌ {folder_name} folder error: {e}")

logger.info("=" * 60)
logger.info("🎡 Ready for piano transcription!")
logger.info("=" * 60)


# =============================================================================
# IMPROVED MODEL LOADER WRAPPER
# =============================================================================

class StableModelLoader:
    """Wrapper around your existing ModelLoader for stability"""

    def __init__(self):
        self.model_loader = ModelLoader()
        self.lock = threading.Lock()
        self.last_reset_time = 0
        self.reset_cooldown = 30  # 30 seconds between resets

    def get_model(self):
        """Get model with stability improvements"""
        with self.lock:
            try:
                return self.model_loader.get_model()
            except Exception as e:
                logger.error(f"Model loading failed: {e}")
                # Only reset if cooldown period has passed
                current_time = time.time()
                if current_time - self.last_reset_time > self.reset_cooldown:
                    logger.info("Attempting model reset after cooldown")
                    try:
                        self.model_loader.reset()
                        self.last_reset_time = current_time
                        return self.model_loader.get_model()
                    except Exception as reset_error:
                        logger.error(f"Model reset failed: {reset_error}")
                        raise
                else:
                    logger.warning("Reset attempted too soon, using cooldown")
                    raise

    def is_model_ready(self):
        """Check model status safely"""
        try:
            return self.model_loader.is_model_ready()
        except Exception:
            return False

    def safe_reset(self):
        """Safe reset with cooldown"""
        with self.lock:
            current_time = time.time()
            if current_time - self.last_reset_time > self.reset_cooldown:
                try:
                    self.model_loader.reset()
                    self.last_reset_time = current_time
                    logger.info("πŸ”„ Model reset completed")
                    return True
                except Exception as e:
                    logger.error(f"Reset failed: {e}")
                    return False
            else:
                logger.warning("Reset blocked by cooldown")
                return False


# Initialize stable model loader
stable_model_loader = StableModelLoader()
logger.info("Model loader initialized - model will load on first recordings endpoint request")


# =============================================================================
# ALL ORIGINAL UTILITY FUNCTIONS - PRESERVED
# =============================================================================

def detect_request_platform(request):
    """Detect if request is from web browser or mobile app - ORIGINAL"""
    user_agent = request.headers.get('User-Agent', '').lower()

    is_web = any(browser in user_agent for browser in [
        'mozilla', 'chrome', 'safari', 'firefox', 'edge', 'webkit'
    ])

    is_mobile_app = 'flutter' in user_agent or 'dart' in user_agent

    return {
        'is_web': is_web,
        'is_mobile_app': is_mobile_app,
        'user_agent': user_agent
    }


def safe_file_processing(file, platform_type):
    """Safely process files based on platform - ORIGINAL"""
    try:
        filename = secure_filename(file.filename)
        unique_filename = f"{uuid.uuid4()}_{filename}"

        if platform_type == 'web':
            audio_path = os.path.join(UPLOAD_FOLDER, f"web_{unique_filename}")
        else:
            audio_path = os.path.join(UPLOAD_FOLDER, f"mobile_{unique_filename}")

        file.save(audio_path)

        if not os.path.exists(audio_path) or os.path.getsize(audio_path) == 0:
            raise Exception(f"File not saved properly: {audio_path}")

        logger.info(f"βœ… File saved safely: {audio_path}")
        return audio_path

    except Exception as e:
        logger.error(f"❌ File processing error: {e}")
        raise


def check_musescore_with_display():
    """Check MuseScore with proper display setup - ORIGINAL"""
    try:
        if not os.environ.get('DISPLAY'):
            os.environ['DISPLAY'] = ':99'

        try:
            subprocess.run(['Xvfb', ':99', '-screen', '0', '1024x768x24'],
                           stdout=subprocess.DEVNULL,
                           stderr=subprocess.DEVNULL,
                           timeout=2)
        except:
            pass

        commands_to_test = ['musescore3', 'musescore', 'mscore3', 'mscore']

        for cmd in commands_to_test:
            try:
                result = subprocess.run([cmd, '--version'],
                                        capture_output=True, text=True, timeout=10)
                if result.returncode == 0:
                    logger.info(f"βœ… {cmd} is available: {result.stdout.strip()}")
                    return True, cmd, result.stdout.strip()
            except Exception as e:
                logger.debug(f"   {cmd}: {e}")
                continue

        return False, None, "MuseScore not responding"

    except Exception as e:
        logger.error(f"❌ Display setup error: {e}")
        return False, None, str(e)


def check_musescore_installation():
    """Updated function that works with your Dockerfile setup - ORIGINAL"""
    return check_musescore_with_display()[:2]


def pitch_to_note_name(pitch):
    """Convert MIDI pitch number to note name - ORIGINAL"""
    note_names = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
    octave = (pitch // 12) - 1
    note = note_names[pitch % 12]
    return f"{note}{octave}"


def clean_up_notes(notes_list, min_duration=0.05, merge_gap=0.08, confidence_threshold=0.4):
    """Filter and merge notes to improve MIDI quality - ORIGINAL"""
    filtered_notes = []
    for note in notes_list:
        if note["duration"] >= min_duration and note["velocity"] >= confidence_threshold:
            filtered_notes.append(note)

    filtered_notes.sort(key=lambda x: (x["pitch"], x["time"]))

    merged_notes = []
    i = 0
    while i < len(filtered_notes):
        current_note = filtered_notes[i]

        j = i + 1
        while j < len(filtered_notes) and filtered_notes[j]["pitch"] == current_note["pitch"]:
            next_note = filtered_notes[j]

            gap = next_note["time"] - (current_note["time"] + current_note["duration"])
            if gap <= merge_gap:
                current_note["duration"] = (next_note["time"] + next_note["duration"]) - current_note["time"]
                current_note["velocity"] = max(current_note["velocity"], next_note["velocity"])
                current_note["velocity_midi"] = max(current_note["velocity_midi"], next_note["velocity_midi"])
                j += 1
            else:
                break

        merged_notes.append(current_note)
        i = j

    return merged_notes


def extract_notes_from_predictions(predictions):
    """Enhanced note extraction using sophisticated postprocessing - ORIGINAL"""
    logger.info("🎼 Extracting notes with enhanced postprocessing...")

    postprocessor = MusicTranscriptionPostprocessor(
        onset_threshold=0.3,
        frame_threshold=0.3,
        min_note_duration=0.05,
        max_note_duration=8.0,
        time_resolution=0.032
    )

    refined_notes = postprocessor.process_predictions(predictions)
    return refined_notes


def print_detailed_notes(notes):
    """Print detailed information about detected notes for debugging - ORIGINAL"""
    logger.info("\n===== DETECTED NOTES (BACKEND) =====")
    logger.info(f"Total notes detected: {len(notes)}")

    if len(notes) > 0:
        pitches = [note['pitch'] for note in notes]
        times = [note['time'] for note in notes]
        durations = [note['duration'] for note in notes]
        velocities = [note['velocity'] for note in notes]

        logger.info(f"Pitch range: {min(pitches)} to {max(pitches)}")
        logger.info(f"Time range: {min(times):.2f}s to {max(times):.2f}s")
        logger.info(f"Duration range: {min(durations):.2f}s to {max(durations):.2f}s")
        logger.info(f"Velocity range: {min(velocities):.2f} to {max(velocities):.2f}")

        for i, note in enumerate(notes[:5]):  # Show first 5 notes
            logger.info(f"Note {i + 1}: name={note['note_name']}, time={note['time']:.3f}s, "
                        f"duration={note['duration']:.3f}s, velocity={note['velocity']:.2f}, "
                        f"pitch={note['pitch']}")

    logger.info("===== END OF NOTES =====\n")


def create_midi_from_notes(notes, output_path):
    """Create a MIDI file from the detected notes - ORIGINAL"""
    logger.info(f"Creating MIDI file with {len(notes)} notes")

    for i, note in enumerate(notes[:5]):
        logger.debug(f"Note {i}: time={note.get('time', 'N/A')}, "
                     f"duration={note.get('duration', 'N/A')}, "
                     f"pitch={note.get('pitch', 'N/A')}, "
                     f"velocity={note.get('velocity_midi', 'N/A')}")

    mid = mido.MidiFile()
    track = mido.MidiTrack()
    mid.tracks.append(track)

    track.append(mido.MetaMessage('set_tempo', tempo=500000, time=0))

    ticks_per_beat = 480
    tempo = 500000
    ticks_per_second = ticks_per_beat / (tempo / 1000000)

    notes = sorted(notes, key=lambda x: x['time'])
    events = []

    for note in notes:
        if note['time'] < 0 or note['duration'] <= 0:
            logger.debug(f"Skipping invalid note: time={note['time']}, duration={note['duration']}")
            continue

        onset_time_ticks = int(max(0, note['time'] * ticks_per_second))
        offset_time_ticks = onset_time_ticks + int(max(1, note['duration'] * ticks_per_second))

        velocity_raw = note['velocity_midi']
        if velocity_raw > 5:
            velocity_raw = 100

        velocity = max(0, min(127, velocity_raw))

        events.append((onset_time_ticks, 'note_on', note['pitch'], velocity))
        events.append((offset_time_ticks, 'note_off', note['pitch'], 0))

    events.sort()

    last_time = 0
    for abs_time, msg_type, pitch, velocity in events:
        delta_time = max(0, abs_time - last_time)

        if msg_type == 'note_on':
            track.append(mido.Message('note_on', note=pitch, velocity=velocity, time=delta_time))
        else:
            track.append(mido.Message('note_off', note=pitch, velocity=velocity, time=delta_time))

        last_time = abs_time

    mid.save(output_path)
    return output_path


def extract_mel_spectrogram(audio_path, sr=16000, n_mels=229, hop_length=512, n_fft=2048):
    """Extract mel spectrogram - ORIGINAL"""
    y, _ = librosa.load(audio_path, sr=sr)
    mel_spec = librosa.feature.melspectrogram(
        y=y, sr=sr, n_mels=n_mels, hop_length=hop_length, n_fft=n_fft
    )
    log_mel_spec = librosa.power_to_db(mel_spec, ref=np.max)
    return log_mel_spec


def convert_audio_to_wav(input_path, output_path, sample_rate=16000):
    """Simple function to convert audio files to WAV format - ORIGINAL"""
    try:
        audio = AudioSegment.from_file(input_path)
        audio = audio.set_frame_rate(sample_rate)

        if audio.channels > 1:
            audio = audio.set_channels(1)

        audio.export(output_path, format="wav")
        logger.info(f"Converted {input_path} to {output_path}")
        return True

    except Exception as e:
        logger.error(f"Error converting audio: {e}")
        return False


def convert_m4a_to_wav(input_path, output_path, sample_rate=16000):
    """Convert specifically M4A to WAV format - ORIGINAL"""
    try:
        audio = AudioSegment.from_file(input_path, format="m4a")
        audio = audio.set_frame_rate(sample_rate)

        if audio.channels > 1:
            audio = audio.set_channels(1)

        audio.export(output_path, format="wav")
        logger.info(f"Converted {input_path} to {output_path}")
        return True

    except Exception as e:
        logger.error(f"Error converting audio: {e}")
        return False


def convert_midi_to_musicxml(midi_path, output_path):
    """Convert MIDI to MusicXML using your container's MuseScore - ORIGINAL"""
    try:
        if not os.environ.get('DISPLAY'):
            os.environ['DISPLAY'] = ':99'

        cmd = ['musescore3', '-o', output_path, midi_path]
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)

        if result.returncode == 0 and os.path.exists(output_path):
            return True, "Conversion successful"
        else:
            return False, f"MuseScore error: {result.stderr or 'Unknown error'}"

    except subprocess.TimeoutExpired:
        return False, "MuseScore conversion timed out"
    except Exception as e:
        return False, f"Conversion error: {str(e)}"


def convert_musicxml_to_pdf(musicxml_path, pdf_path):
    """Convert MusicXML to PDF using your container's MuseScore - ORIGINAL"""
    try:
        if not os.environ.get('DISPLAY'):
            os.environ['DISPLAY'] = ':99'

        cmd = ['musescore3', '-o', pdf_path, musicxml_path]
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)

        if result.returncode == 0 and os.path.exists(pdf_path):
            return True, "PDF conversion successful"
        else:
            return False, f"PDF conversion error: {result.stderr or 'Unknown error'}"

    except Exception as e:
        return False, f"PDF conversion error: {str(e)}"


def process_spectrogram_for_model(mel_spec):
    """Process the spectrogram to fit model input requirements - ORIGINAL"""
    expected_height = 229
    expected_width = 626

    if mel_spec.shape[0] != expected_height:
        mel_spec = tf.image.resize(
            tf.expand_dims(mel_spec, 0),
            [expected_height, mel_spec.shape[1]]
        )[0]

    if mel_spec.shape[1] < expected_width:
        padding = expected_width - mel_spec.shape[1]
        mel_spec = np.pad(mel_spec, ((0, 0), (0, padding)), mode='constant')
    elif mel_spec.shape[1] > expected_width:
        mel_spec = mel_spec[:, :expected_width]

    mel_spec = tf.transpose(mel_spec)
    mel_spec = tf.expand_dims(mel_spec, axis=0)
    mel_spec = tf.expand_dims(mel_spec, axis=-1)

    return mel_spec


# =============================================================================
# ALL ORIGINAL S3 HELPER FUNCTIONS - PRESERVED
# =============================================================================

def get_file_extension(filename):
    """Extract file extension from filename - ORIGINAL"""
    return '.' + filename.rsplit('.', 1)[1].lower() if '.' in filename else ''


def save_file_locally(file, filename):
    """Save uploaded file to local directory temporarily - ORIGINAL"""
    local_path = os.path.join(UPLOAD_FOLDER, filename)
    file.save(local_path)
    return local_path


def upload_file_to_s3(local_path, s3_path, content_type):
    """Upload file from local path to S3 - ORIGINAL"""
    if s3_client:
        s3_client.upload_file(
            local_path,
            AWS_BUCKET_NAME,
            s3_path,
            ExtraArgs={'ContentType': content_type}
        )


def clean_up_local_file(local_path):
    """Remove temporary local file - ORIGINAL"""
    try:
        if local_path and os.path.exists(local_path):
            os.remove(local_path)
    except Exception as e:
        logger.debug(f"Cleanup warning: {e}")


def save_metadata_to_s3(metadata, s3_path):
    """Save metadata JSON to S3 - ORIGINAL"""
    if not s3_client:
        return

    with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as temp_file:
        json.dump(metadata, temp_file, indent=2)
        temp_file_path = temp_file.name

    s3_client.upload_file(
        temp_file_path,
        AWS_BUCKET_NAME,
        s3_path,
        ExtraArgs={'ContentType': 'application/json'}
    )

    os.remove(temp_file_path)


def save_generated_files_to_s3(user_id, recording_id, result_data):
    """Save generated MIDI and PDF files to S3 and update metadata - ORIGINAL"""
    if not s3_client:
        return result_data

    try:
        logger.info("πŸ’Ύ Saving generated files to S3...")

        recording_folder = f"users/{user_id}/recordings/{recording_id}"
        metadata_key = f"{recording_folder}/metadata.json"

        try:
            metadata_response = s3_client.get_object(
                Bucket=AWS_BUCKET_NAME,
                Key=metadata_key
            )
            metadata = json.loads(metadata_response['Body'].read().decode('utf-8'))
        except Exception as e:
            logger.error(f"❌ Could not load metadata: {e}")
            return result_data

        files_saved = 0

        # Save MIDI file if it exists
        if 'midi_file' in result_data and result_data['midi_file']:
            midi_url = result_data['midi_file']
            if midi_url.startswith('/api/download/'):
                midi_filename = midi_url.replace('/api/download/', '')
                midi_local_path = os.path.join(OUTPUT_FOLDER, midi_filename)

                if os.path.exists(midi_local_path):
                    midi_s3_path = f"{recording_folder}/transcription.mid"
                    upload_file_to_s3(midi_local_path, midi_s3_path, 'audio/midi')

                    midi_s3_url = f"https://{AWS_BUCKET_NAME}.s3.amazonaws.com/{midi_s3_path}"

                    metadata['files']['midi'] = {
                        'filename': 'transcription.mid',
                        'original_name': 'AI_Generated_Transcription.mid',
                        'content_type': 'audio/midi',
                        's3_path': midi_s3_path,
                        'url': midi_s3_url,
                        'generated_date': datetime.now().isoformat(),
                        'generated_by': 'ai_transcription'
                    }

                    result_data['midi_file'] = midi_s3_url

                    logger.info(f"βœ… MIDI saved to S3: {midi_s3_path}")
                    files_saved += 1

                    try:
                        os.remove(midi_local_path)
                    except:
                        pass

        # Save PDF file if it exists
        if 'sheet_music' in result_data and result_data['sheet_music']:
            sheet_info = result_data['sheet_music']
            if 'fileUrl' in sheet_info and sheet_info['fileUrl']:
                pdf_url = sheet_info['fileUrl']
                if pdf_url.startswith('/api/download/'):
                    pdf_filename = pdf_url.replace('/api/download/', '')
                    pdf_local_path = os.path.join(OUTPUT_FOLDER, pdf_filename)

                    if os.path.exists(pdf_local_path):
                        pdf_s3_path = f"{recording_folder}/sheet_music.pdf"
                        upload_file_to_s3(pdf_local_path, pdf_s3_path, 'application/pdf')

                        pdf_s3_url = f"https://{AWS_BUCKET_NAME}.s3.amazonaws.com/{pdf_s3_path}"

                        metadata['files']['pdf'] = {
                            'filename': 'sheet_music.pdf',
                            'original_name': 'AI_Generated_Sheet_Music.pdf',
                            'content_type': 'application/pdf',
                            's3_path': pdf_s3_path,
                            'url': pdf_s3_url,
                            'generated_date': datetime.now().isoformat(),
                            'generated_by': 'ai_transcription'
                        }

                        result_data['sheet_music']['fileUrl'] = pdf_s3_url

                        logger.info(f"βœ… PDF saved to S3: {pdf_s3_path}")
                        files_saved += 1

                        try:
                            os.remove(pdf_local_path)
                        except:
                            pass

        if files_saved > 0:
            metadata['last_transcription'] = {
                'date': datetime.now().isoformat(),
                'files_saved': files_saved
            }

            save_metadata_to_s3(metadata, metadata_key)
            logger.info(f"βœ… Metadata updated with {files_saved} new files")

        return result_data

    except Exception as e:
        logger.error(f"❌ Error saving files to S3: {e}")
        return result_data


# =============================================================================
# CORE TRANSCRIPTION FUNCTION - ALL ORIGINAL LOGIC
# =============================================================================

def calculate_chunk_duration_and_overlap():
    """
    Calculate the optimal chunk duration and overlap for processing.
    
    Returns:
        chunk_duration (float): Duration of each chunk in seconds
        overlap_duration (float): Overlap between chunks to avoid missing notes
    """
    # Based on your model's expected input size
    expected_width = 626  # time frames
    hop_length = 512
    sr = 16000
    
    # Calculate actual duration the model can handle
    chunk_duration = (expected_width * hop_length) / sr  # β‰ˆ 20.032 seconds
    
    # Use 2-second overlap to catch notes that might be split between chunks
    overlap_duration = 2.0
    
    logger.info(f"πŸ“ Chunk settings: {chunk_duration:.1f}s duration, {overlap_duration:.1f}s overlap")
    
    return chunk_duration, overlap_duration


def split_audio_into_chunks(audio_path: str) -> List[Tuple[str, float, float]]:
    """
    Split a long audio file into processable chunks.
    
    Args:
        audio_path (str): Path to the audio file
        
    Returns:
        List of tuples: (chunk_file_path, start_time, end_time)
    """
    try:
        # Load the full audio to get its duration
        y, sr = librosa.load(audio_path, sr=16000)
        total_duration = len(y) / sr
        
        logger.info(f"🎡 Audio duration: {total_duration:.1f} seconds")
        
        chunk_duration, overlap_duration = calculate_chunk_duration_and_overlap()
        
        # If audio is short enough, no chunking needed
        if total_duration <= chunk_duration:
            logger.info("βœ… Audio fits in single chunk, no splitting needed")
            return [(audio_path, 0.0, total_duration)]
        
        # Calculate chunk boundaries
        chunks = []
        start_time = 0.0
        chunk_index = 0
        
        while start_time < total_duration:
            # Calculate end time for this chunk
            end_time = min(start_time + chunk_duration, total_duration)
            
            # Extract chunk audio data
            start_sample = int(start_time * sr)
            end_sample = int(end_time * sr)
            chunk_audio = y[start_sample:end_sample]
            
            # Save chunk to temporary file
            chunk_filename = f"chunk_{chunk_index}_{uuid.uuid4()}.wav"
            chunk_path = os.path.join(UPLOAD_FOLDER, chunk_filename)
            
            # Save the chunk as WAV file
            import soundfile as sf
            sf.write(chunk_path, chunk_audio, sr)
            
            chunks.append((chunk_path, start_time, end_time))
            
            logger.info(f"πŸ“¦ Chunk {chunk_index}: {start_time:.1f}s - {end_time:.1f}s -> {chunk_path}")
            
            # Move to next chunk with overlap
            # For the last chunk, don't add overlap
            if end_time < total_duration:
                start_time = end_time - overlap_duration
            else:
                break
                
            chunk_index += 1
        
        logger.info(f"βœ‚οΈ Split audio into {len(chunks)} chunks")
        return chunks
        
    except Exception as e:
        logger.error(f"❌ Error splitting audio: {e}")
        # Fallback: return original file as single chunk
        return [(audio_path, 0.0, 0.0)]


def process_single_chunk(chunk_path: str, start_offset: float) -> List[Dict]:
    """
    Process a single audio chunk and return notes with adjusted timing.
    
    Args:
        chunk_path (str): Path to the chunk audio file
        start_offset (float): Time offset of this chunk in the original audio
        
    Returns:
        List of note dictionaries with corrected timestamps
    """
    try:
        logger.info(f"πŸ”„ Processing chunk: {os.path.basename(chunk_path)} (offset: {start_offset:.1f}s)")
        
        # Extract mel spectrogram for this chunk
        mel_spec = extract_mel_spectrogram(chunk_path)
        mel_spec = process_spectrogram_for_model(mel_spec)
        
        # Get current model and make prediction
        current_model = stable_model_loader.get_model()
        predictions = current_model.predict(mel_spec)
        
        # Extract notes from predictions
        notes = extract_notes_from_predictions(predictions)
        
        # Adjust note timing by adding the chunk's start offset
        adjusted_notes = []
        for note in notes:
            adjusted_note = note.copy()
            adjusted_note['time'] = note['time'] + start_offset
            adjusted_notes.append(adjusted_note)
        
        logger.info(f"βœ… Chunk processed: {len(adjusted_notes)} notes found")
        return adjusted_notes
        
    except Exception as e:
        logger.error(f"❌ Error processing chunk {chunk_path}: {e}")
        return []


def remove_duplicate_notes(all_notes: List[Dict], overlap_duration: float = 2.0) -> List[Dict]:
    """
    Remove duplicate notes that appear in overlapping regions between chunks.
    
    Args:
        all_notes: Combined list of all notes from all chunks
        overlap_duration: Duration of overlap between chunks
        
    Returns:
        List of unique notes with duplicates removed
    """
    if not all_notes:
        return []
    
    logger.info(f"πŸ” Removing duplicates from {len(all_notes)} total notes...")
    
    # Sort notes by time first
    all_notes.sort(key=lambda x: x['time'])
    
    unique_notes = []
    
    for note in all_notes:
        is_duplicate = False
        
        # Check if this note is too similar to any recent note
        for existing_note in unique_notes[-10:]:  # Only check last 10 notes for efficiency
            time_diff = abs(note['time'] - existing_note['time'])
            pitch_diff = abs(note['pitch'] - existing_note['pitch'])
            
            # Consider it a duplicate if:
            # - Same pitch and very close in time (within overlap region)
            # - Time difference is less than 0.5 seconds
            if pitch_diff == 0 and time_diff < 0.5:
                is_duplicate = True
                logger.debug(f"   Duplicate found: {note['note_name']} at {note['time']:.2f}s")
                break
        
        if not is_duplicate:
            unique_notes.append(note)
    
    removed_count = len(all_notes) - len(unique_notes)
    logger.info(f"βœ… Removed {removed_count} duplicate notes, {len(unique_notes)} unique notes remain")
    
    return unique_notes


def process_long_audio_in_chunks(audio_path: str) -> List[Dict]:
    """
    Main function to process long audio files by splitting into chunks.
    
    Args:
        audio_path (str): Path to the audio file
        
    Returns:
        List of all detected notes with correct timing
    """
    try:
        logger.info(f"🎼 Starting chunked processing for: {audio_path}")
        
        # Split audio into manageable chunks
        chunks = split_audio_into_chunks(audio_path)
        
        if len(chunks) == 1:
            # No chunking needed, process normally
            logger.info("πŸ“‹ Single chunk processing")
            mel_spec = extract_mel_spectrogram(audio_path)
            mel_spec = process_spectrogram_for_model(mel_spec)
            current_model = stable_model_loader.get_model()
            predictions = current_model.predict(mel_spec)
            return extract_notes_from_predictions(predictions)
        
        # Process each chunk
        all_notes = []
        chunk_files_to_cleanup = []
        
        for i, (chunk_path, start_time, end_time) in enumerate(chunks):
            logger.info(f"πŸ”„ Processing chunk {i+1}/{len(chunks)}: {start_time:.1f}s - {end_time:.1f}s")
            
            # Process this chunk
            chunk_notes = process_single_chunk(chunk_path, start_time)
            all_notes.extend(chunk_notes)
            
            # Mark chunk file for cleanup (but not if it's the original file)
            if chunk_path != audio_path:
                chunk_files_to_cleanup.append(chunk_path)
        
        # Remove duplicate notes from overlapping regions
        _, overlap_duration = calculate_chunk_duration_and_overlap()
        unique_notes = remove_duplicate_notes(all_notes, overlap_duration)
        
        # Clean up temporary chunk files
        for chunk_file in chunk_files_to_cleanup:
            try:
                if os.path.exists(chunk_file):
                    os.remove(chunk_file)
                    logger.debug(f"🧹 Cleaned up chunk file: {os.path.basename(chunk_file)}")
            except Exception as cleanup_e:
                logger.warning(f"⚠️ Could not clean up {chunk_file}: {cleanup_e}")
        
        logger.info(f"πŸŽ‰ Chunked processing complete: {len(unique_notes)} total notes from {len(chunks)} chunks")
        return unique_notes
        
    except Exception as e:
        logger.error(f"❌ Error in chunked processing: {e}")
        import traceback
        traceback.print_exc()
        # Fallback to regular processing
        logger.info("πŸ”„ Falling back to regular processing...")
        mel_spec = extract_mel_spectrogram(audio_path)
        mel_spec = process_spectrogram_for_model(mel_spec)
        current_model = stable_model_loader.get_model()
        predictions = current_model.predict(mel_spec)
        return extract_notes_from_predictions(predictions)


# Modified perform_transcription function to use chunking
def perform_transcription_with_chunking(audio_file_path, title="Piano Transcription", sheet_format="pdf", tempo=120):
    """
    Enhanced transcription function that handles long audio files by chunking.
    This replaces your original perform_transcription function.
    """
    try:
        logger.info(f"🎡 Starting enhanced transcription for: {audio_file_path}")

        # Convert to WAV if needed
        wav_path = os.path.splitext(audio_file_path)[0] + ".wav"
        if not audio_file_path.lower().endswith('.wav'):
            convert_audio_to_wav(audio_file_path, wav_path)
        else:
            wav_path = audio_file_path

        # Check audio duration to decide processing method
        y, sr = librosa.load(wav_path, sr=16000)
        total_duration = len(y) / sr
        
        logger.info(f"⏱️ Audio duration: {total_duration:.1f} seconds")
        
        if total_duration > 22:
            logger.info(f"πŸ“ Long audio detected ({total_duration:.1f}s), using chunked processing")
            notes = process_long_audio_in_chunks(wav_path)
        else:
            logger.info(f"πŸ“ Short audio ({total_duration:.1f}s), using standard processing")
            # Use original processing method for short audio
            mel_spec = extract_mel_spectrogram(wav_path)
            mel_spec = process_spectrogram_for_model(mel_spec)
            current_model = stable_model_loader.get_model()
            predictions = current_model.predict(mel_spec)
            notes = extract_notes_from_predictions(predictions)

        logger.info(f"🎼 Total notes extracted: {len(notes)}")

        # Create MIDI file from all notes
        midi_filename = f"{uuid.uuid4()}.mid"
        midi_path = os.path.join(OUTPUT_FOLDER, midi_filename)
        create_midi_from_notes(notes, midi_path)

        # Generate sheet music if possible
        musescore_available, musescore_info = check_musescore_installation()
        sheet_music_result = None

        if musescore_available and os.path.exists(midi_path):
            try:
                logger.info("🎼 Generating sheet music from full-length MIDI...")

                sheet_uuid = str(uuid.uuid4())
                musicxml_filename = f"{sheet_uuid}.musicxml"
                musicxml_path = os.path.join(OUTPUT_FOLDER, musicxml_filename)

                pdf_filename = f"{sheet_uuid}.pdf"
                pdf_path = os.path.join(OUTPUT_FOLDER, pdf_filename)

                success, message = convert_midi_to_musicxml(midi_path, musicxml_path)

                if success and sheet_format.lower() == 'pdf':
                    pdf_success, pdf_message = convert_musicxml_to_pdf(musicxml_path, pdf_path)

                    if pdf_success:
                        sheet_music_result = {
                            "fileUrl": f"/api/download/{pdf_filename}",
                            "format": "pdf",
                            "title": title
                        }
                        logger.info(f"βœ… Full-length sheet music generated: {pdf_filename}")
                    else:
                        logger.error(f"❌ PDF generation failed: {pdf_message}")
                elif success:
                    sheet_music_result = {
                        "fileUrl": f"/api/download/{musicxml_filename}",
                        "format": "musicxml",
                        "title": title
                    }
                    logger.info(f"βœ… Full-length MusicXML generated: {musicxml_filename}")
                else:
                    logger.error(f"❌ Sheet music generation failed: {message}")

            except Exception as sheet_e:
                logger.error(f"❌ Sheet music generation error: {sheet_e}")

        # Clean up temporary WAV file
        try:
            if wav_path != audio_file_path and os.path.exists(wav_path):
                os.remove(wav_path)
        except Exception as cleanup_e:
            logger.warning(f"⚠️ Warning: Could not clean up temporary wav file: {cleanup_e}")

        result_data = {
            "success": True,
            "notes": notes,
            "midi_file": f"/api/download/{midi_filename}",
            "musescore_available": musescore_available,
            "sheet_music": sheet_music_result,
            "debug_info": {
                "total_duration": total_duration,
                "processing_method": "chunked" if total_duration > 22 else "standard",
                "notes_extracted": len(notes),
                "sheet_music_generated": sheet_music_result is not None
            }
        }

        logger.info(f"πŸŽ‰ Enhanced transcription complete: {len(notes)} notes, Duration: {total_duration:.1f}s")
        return True, result_data, None

    except Exception as e:
        logger.error(f"❌ Error in enhanced transcription: {e}")
        import traceback
        traceback.print_exc()
        return False, None, str(e)

def perform_transcription(audio_file_path, title="Piano Transcription", sheet_format="pdf", tempo=120):
    """Core transcription logic - ALL ORIGINAL FUNCTIONALITY"""
    try:
        logger.info(f"🎡 Starting transcription for: {audio_file_path}")

        wav_path = os.path.splitext(audio_file_path)[0] + ".wav"
        if not audio_file_path.lower().endswith('.wav'):
            convert_audio_to_wav(audio_file_path, wav_path)
        else:
            wav_path = audio_file_path

        logger.info("πŸ”Š Extracting mel spectrogram...")
        mel_spec = extract_mel_spectrogram(wav_path)
        mel_spec = process_spectrogram_for_model(mel_spec)
        logger.info(f"πŸ“Š Processed spectrogram shape: {mel_spec.shape}")

        try:
            logger.info("πŸ€– Loading AI model...")
            current_model = stable_model_loader.get_model()
            logger.info("βœ… Model loaded successfully for transcription")
        except Exception as model_e:
            logger.error(f"❌ Error loading model: {model_e}")
            return False, None, f"Model loading failed: {str(model_e)}"

        logger.info("🧠 Running AI model prediction...")
        predictions = current_model.predict(mel_spec)
        logger.info("βœ… Model prediction completed")

        logger.info(f"πŸ” Model output debug:")
        logger.info(f"  - Predictions type: {type(predictions)}")
        logger.info(f"  - Number of outputs: {len(predictions)}")
        for i, pred in enumerate(predictions):
            logger.info(f"  - Output {i} shape: {pred.shape}")

        try:
            notes = extract_notes_from_predictions(predictions)
            logger.info(f"🎼 Extracted {len(notes)} notes successfully")
        except Exception as extraction_error:
            logger.error(f"❌ Note extraction failed: {extraction_error}")
            return False, None, f"Note extraction failed: {str(extraction_error)}"

        midi_filename = f"{uuid.uuid4()}.mid"
        midi_path = os.path.join(OUTPUT_FOLDER, midi_filename)
        create_midi_from_notes(notes, midi_path)

        musescore_available, musescore_info = check_musescore_installation()

        sheet_music_result = None

        if musescore_available and os.path.exists(midi_path):
            try:
                logger.info("🎼 Generating sheet music...")

                sheet_uuid = str(uuid.uuid4())
                musicxml_filename = f"{sheet_uuid}.musicxml"
                musicxml_path = os.path.join(OUTPUT_FOLDER, musicxml_filename)

                pdf_filename = f"{sheet_uuid}.pdf"
                pdf_path = os.path.join(OUTPUT_FOLDER, pdf_filename)

                success, message = convert_midi_to_musicxml(midi_path, musicxml_path)

                if success and sheet_format.lower() == 'pdf':
                    pdf_success, pdf_message = convert_musicxml_to_pdf(musicxml_path, pdf_path)

                    if pdf_success:
                        sheet_music_result = {
                            "fileUrl": f"/api/download/{pdf_filename}",
                            "format": "pdf",
                            "title": title
                        }
                        logger.info(f"βœ… Sheet music generated: {pdf_filename}")
                    else:
                        logger.error(f"❌ PDF generation failed: {pdf_message}")
                elif success:
                    sheet_music_result = {
                        "fileUrl": f"/api/download/{musicxml_filename}",
                        "format": "musicxml",
                        "title": title
                    }
                    logger.info(f"βœ… MusicXML generated: {musicxml_filename}")
                else:
                    logger.error(f"❌ Sheet music generation failed: {message}")

            except Exception as sheet_e:
                logger.error(f"❌ Sheet music generation error: {sheet_e}")

        try:
            if wav_path != audio_file_path and os.path.exists(wav_path):
                os.remove(wav_path)
        except Exception as cleanup_e:
            logger.warning(f"⚠️ Warning: Could not clean up temporary wav file: {cleanup_e}")

        result_data = {
            "success": True,
            "notes": notes,
            "midi_file": f"/api/download/{midi_filename}",
            "musescore_available": musescore_available,
            "sheet_music": sheet_music_result,
            "debug_info": {
                "model_outputs": len(predictions),
                "notes_extracted": len(notes),
                "sheet_music_generated": sheet_music_result is not None
            }
        }

        logger.info(
            f"πŸŽ‰ Transcription complete: {len(notes)} notes, MIDI: βœ…, Sheet: {'βœ…' if sheet_music_result else '❌'}")

        return True, result_data, None

    except Exception as e:
        logger.error(f"❌ Error in transcription: {e}")
        import traceback
        traceback.print_exc()
        return False, None, str(e)


# =============================================================================
# FLASK APPLICATION SETUP
# =============================================================================

app = Flask(__name__)
CORS(app)


@app.before_request
def before_request():
    """Handle platform differences and periodic cleanup"""
    platform_info = detect_request_platform(request)
    g.platform_info = platform_info

    logger.debug(f"πŸ” Request from: {platform_info['user_agent'][:50]}...")
    logger.debug(f"πŸ“± Platform: {'Web Browser' if platform_info['is_web'] else 'Mobile App'}")

    # Periodic cleanup (10% chance)
    if np.random.random() < 0.1:
        try:
            # Clean old files
            current_time = time.time()
            for folder in [UPLOAD_FOLDER, OUTPUT_FOLDER]:
                for file_path in Path(folder).glob('*'):
                    if file_path.is_file():
                        age_minutes = (current_time - file_path.stat().st_mtime) / 60
                        if age_minutes > 30:  # Remove files older than 30 minutes
                            try:
                                file_path.unlink()
                            except:
                                pass
        except Exception as e:
            logger.debug(f"Cleanup warning: {e}")

    if request.method == 'OPTIONS':
        return '', 200


@app.after_request
def after_request(response):
    """Ensure proper CORS headers for all responses"""
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
    return response


@app.errorhandler(Exception)
def handle_exception(e):
    """IMPROVED error handling - no more cascading failures"""
    logger.error(f"❌ Error occurred: {e}", exc_info=True)

    # Don't automatically reset model - this was causing cascades
    # Only log and return clean JSON response

    return jsonify({
        "success": False,
        "error": "Server error occurred",
        "details": str(e)
    }), 500


# =============================================================================
# ALL ORIGINAL ENDPOINTS - COMPLETE PRESERVATION
# =============================================================================

@app.route('/hello', methods=['GET'])
def hello():
    """Hello endpoint - ORIGINAL"""
    return jsonify({"message": "Hello, World!"}), 200


@app.route('/api/musescore-status', methods=['GET'])
def check_musescore_status():
    """Check if MuseScore is available for sheet music generation - ORIGINAL"""
    try:
        is_available, version_info = check_musescore_installation()
        return jsonify({
            "available": is_available,
            "version": version_info,
            "features": ["pdf", "musicxml"] if is_available else []
        })
    except Exception as e:
        return jsonify({
            "available": False,
            "error": str(e),
            "features": []
        })


@app.route('/upload', methods=['POST'])
def upload_recording_with_files():
    """Enhanced upload endpoint that handles multiple file types - ALL ORIGINAL"""
    try:
        logger.info("πŸ“€ Enhanced upload request received")

        if 'userId' not in request.form:
            return jsonify({"error": "User ID is required"}), 400

        user_id = request.form['userId']
        title = request.form.get('title', 'Untitled Recording')
        description = request.form.get('description', '')

        if 'audio_file' not in request.files:
            return jsonify({"error": "Audio file is required"}), 400

        audio_file = request.files['audio_file']
        if audio_file.filename == '':
            return jsonify({"error": "No audio file selected"}), 400

        image_file = request.files.get('image_file')
        pdf_file = request.files.get('pdf_file')
        midi_file = request.files.get('midi_file')

        logger.info(f"πŸ“‹ Upload details:")
        logger.info(f"   User: {user_id}")
        logger.info(f"   Title: {title}")
        logger.info(f"   Audio: {audio_file.filename}")
        logger.info(f"   Image: {image_file.filename if image_file else 'None'}")
        logger.info(f"   PDF: {pdf_file.filename if pdf_file else 'None'}")
        logger.info(f"   MIDI: {midi_file.filename if midi_file else 'None'}")

        recording_id = str(uuid.uuid4())
        timestamp = datetime.now()

        recording_folder = f"users/{user_id}/recordings/{recording_id}"

        metadata = {
            'recording_id': recording_id,
            'user_id': user_id,
            'title': title,
            'description': description,
            'upload_date': timestamp.isoformat(),
            'created_date': timestamp.strftime('%Y-%m-%d'),
            'files': {}
        }

        uploaded_files = {}

        # Process audio file (required)
        audio_extension = get_file_extension(audio_file.filename)
        audio_s3_path = f"{recording_folder}/audio{audio_extension}"
        local_audio_path = save_file_locally(audio_file, f"audio_{recording_id}{audio_extension}")

        upload_file_to_s3(local_audio_path, audio_s3_path, audio_file.content_type)
        metadata['files']['audio'] = {
            'filename': f"audio{audio_extension}",
            'original_name': audio_file.filename,
            'content_type': audio_file.content_type,
            's3_path': audio_s3_path,
            'url': f"https://{AWS_BUCKET_NAME}.s3.amazonaws.com/{audio_s3_path}"
        }
        uploaded_files['audio'] = metadata['files']['audio']['url']
        clean_up_local_file(local_audio_path)

        # Process image file (optional)
        if image_file and image_file.filename:
            image_extension = get_file_extension(image_file.filename)
            image_s3_path = f"{recording_folder}/image{image_extension}"
            local_image_path = save_file_locally(image_file, f"image_{recording_id}{image_extension}")

            upload_file_to_s3(local_image_path, image_s3_path, image_file.content_type)
            metadata['files']['image'] = {
                'filename': f"image{image_extension}",
                'original_name': image_file.filename,
                'content_type': image_file.content_type,
                's3_path': image_s3_path,
                'url': f"https://{AWS_BUCKET_NAME}.s3.amazonaws.com/{image_s3_path}"
            }
            uploaded_files['image'] = metadata['files']['image']['url']
            clean_up_local_file(local_image_path)

        # Process PDF file (optional)
        if pdf_file and pdf_file.filename:
            pdf_s3_path = f"{recording_folder}/sheet_music.pdf"
            local_pdf_path = save_file_locally(pdf_file, f"pdf_{recording_id}.pdf")

            upload_file_to_s3(local_pdf_path, pdf_s3_path, 'application/pdf')
            metadata['files']['pdf'] = {
                'filename': 'sheet_music.pdf',
                'original_name': pdf_file.filename,
                'content_type': 'application/pdf',
                's3_path': pdf_s3_path,
                'url': f"https://{AWS_BUCKET_NAME}.s3.amazonaws.com/{pdf_s3_path}"
            }
            uploaded_files['pdf'] = metadata['files']['pdf']['url']
            clean_up_local_file(local_pdf_path)

        # Process MIDI file (optional)
        if midi_file and midi_file.filename:
            midi_s3_path = f"{recording_folder}/transcription.mid"
            local_midi_path = save_file_locally(midi_file, f"midi_{recording_id}.mid")

            upload_file_to_s3(local_midi_path, midi_s3_path, 'audio/midi')
            metadata['files']['midi'] = {
                'filename': 'transcription.mid',
                'original_name': midi_file.filename,
                'content_type': 'audio/midi',
                's3_path': midi_s3_path,
                'url': f"https://{AWS_BUCKET_NAME}.s3.amazonaws.com/{midi_s3_path}"
            }
            uploaded_files['midi'] = metadata['files']['midi']['url']
            clean_up_local_file(local_midi_path)

        # Save metadata.json to S3
        metadata_s3_path = f"{recording_folder}/metadata.json"
        save_metadata_to_s3(metadata, metadata_s3_path)

        logger.info(f"βœ… Upload successful for recording {recording_id}")

        return jsonify({
            "success": True,
            "message": f"Recording '{title}' uploaded successfully",
            "recording_id": recording_id,
            "files": uploaded_files,
            "metadata": metadata
        }), 200

    except Exception as e:
        logger.error(f"❌ Upload error: {e}")
        import traceback
        traceback.print_exc()

        return jsonify({
            "success": False,
            "error": str(e)
        }), 500


@app.route('/recordings/<user_id>', methods=['GET'])
def get_user_recordings_enhanced(user_id):
    """Enhanced endpoint to get all recordings with their files - ALL ORIGINAL"""
    try:
        logger.info(f"πŸ“‹ Getting recordings for user: {user_id}")

        # Load model on first recordings request
        if not stable_model_loader.is_model_ready():
            logger.info("Loading model on recordings endpoint request...")
            try:
                stable_model_loader.get_model()
                logger.info("Model loaded successfully!")
            except Exception as e:
                logger.warning(f"Model loading failed: {e}")

        if not s3_client:
            return jsonify({
                "success": True,
                "userId": user_id,
                "recordings": [],
                "model_ready": stable_model_loader.is_model_ready(),
                "total_recordings": 0,
                "message": "S3 not configured"
            }), 200

        user_recordings_prefix = f"users/{user_id}/recordings/"

        response = s3_client.list_objects_v2(
            Bucket=AWS_BUCKET_NAME,
            Prefix=user_recordings_prefix,
            Delimiter='/'
        )

        recordings = []

        if 'CommonPrefixes' in response:
            for prefix in response['CommonPrefixes']:
                recording_folder = prefix['Prefix']
                recording_id = recording_folder.split('/')[-2]

                try:
                    metadata_key = f"{recording_folder}metadata.json"
                    metadata_response = s3_client.get_object(
                        Bucket=AWS_BUCKET_NAME,
                        Key=metadata_key
                    )
                    metadata = json.loads(metadata_response['Body'].read().decode('utf-8'))

                    recordings.append({
                        'recording_id': recording_id,
                        'metadata': metadata,
                        'files': metadata.get('files', {}),
                        'title': metadata.get('title', 'Untitled'),
                        'upload_date': metadata.get('upload_date'),
                        'description': metadata.get('description', ''),
                        'user_id': metadata.get('user_id', user_id),
                        # For backward compatibility with existing UI
                        'url': metadata.get('files', {}).get('audio', {}).get('url', ''),
                        'has_image': 'image' in metadata.get('files', {}),
                        'has_pdf': 'pdf' in metadata.get('files', {}),
                        'has_midi': 'midi' in metadata.get('files', {})
                    })

                except Exception as e:
                    logger.error(f"❌ Error reading metadata for recording {recording_id}: {e}")
                    continue

        recordings.sort(key=lambda x: x.get('upload_date', ''), reverse=True)

        logger.info(f"🎡 Found {len(recordings)} recordings for user {user_id}")

        return jsonify({
            "success": True,
            "userId": user_id,
            "recordings": recordings,
            "model_ready": stable_model_loader.is_model_ready(),
            "total_recordings": len(recordings)
        }), 200

    except Exception as e:
        logger.error(f"❌ Error in enhanced recordings endpoint: {e}")
        import traceback
        traceback.print_exc()

        return jsonify({
            "success": False,
            "error": str(e)
        }), 500


@app.route('/recordings/<recording_id>', methods=['PUT'])
def update_recording_metadata(recording_id):
    """Update recording metadata and optionally replace image - ALL ORIGINAL"""
    try:
        logger.info(f"πŸ“ Updating recording {recording_id}")

        if 'userId' not in request.form:
            return jsonify({"error": "User ID is required"}), 400

        user_id = request.form['userId']
        title = request.form.get('title', 'Untitled Recording')
        description = request.form.get('description', '')

        logger.info(f"πŸ‘€ User: {user_id}")
        logger.info(f"🏷️ New title: {title}")
        logger.info(f"πŸ“ New description: {description}")

        recording_folder = f"users/{user_id}/recordings/{recording_id}"
        metadata_key = f"{recording_folder}/metadata.json"

        if not s3_client:
            return jsonify({"error": "S3 not configured"}), 503

        try:
            metadata_response = s3_client.get_object(
                Bucket=AWS_BUCKET_NAME,
                Key=metadata_key
            )
            metadata = json.loads(metadata_response['Body'].read().decode('utf-8'))
            logger.info("πŸ“‹ Loaded existing metadata")
        except Exception as e:
            logger.error(f"❌ Could not load existing metadata: {e}")
            return jsonify({"error": "Recording not found or access denied"}), 404

        metadata['title'] = title
        metadata['description'] = description
        metadata['last_modified'] = datetime.now().isoformat()

        image_file = request.files.get('image_file')
        if image_file and image_file.filename:
            logger.info(f"πŸ–ΌοΈ Processing new image: {image_file.filename}")

            image_extension = get_file_extension(image_file.filename)
            local_image_path = save_file_locally(image_file, f"image_update_{recording_id}{image_extension}")

            image_s3_path = f"{recording_folder}/image{image_extension}"
            upload_file_to_s3(local_image_path, image_s3_path, image_file.content_type)

            metadata['files']['image'] = {
                'filename': f"image{image_extension}",
                'original_name': image_file.filename,
                'content_type': image_file.content_type,
                's3_path': image_s3_path,
                'url': f"https://{AWS_BUCKET_NAME}.s3.amazonaws.com/{image_s3_path}"
            }

            clean_up_local_file(local_image_path)
            logger.info("βœ… Image updated successfully")

        save_metadata_to_s3(metadata, metadata_key)
        logger.info("βœ… Metadata updated successfully")

        return jsonify({
            "success": True,
            "message": f"Recording '{title}' updated successfully",
            "recording_id": recording_id,
            "metadata": metadata
        }), 200

    except Exception as e:
        logger.error(f"❌ Update error: {e}")
        import traceback
        traceback.print_exc()

        return jsonify({
            "success": False,
            "error": str(e)
        }), 500


@app.route('/recordings/<user_id>/<recording_id>/transcribe', methods=['POST'])
def generate_transcription_for_recording(user_id, recording_id):
    """Generate AI transcription for an existing recording - ALL ORIGINAL"""
    try:
        logger.info(f"πŸ€– Generating transcription for recording {recording_id}")

        data = request.get_json() or {}
        title = data.get('title', 'Piano Transcription')
        sheet_format = data.get('sheet_format', 'pdf')
        tempo = int(data.get('tempo', 120))

        if not s3_client:
            return jsonify({"error": "S3 not configured"}), 503

        recording_folder = f"users/{user_id}/recordings/{recording_id}"
        metadata_key = f"{recording_folder}/metadata.json"

        try:
            metadata_response = s3_client.get_object(
                Bucket=AWS_BUCKET_NAME,
                Key=metadata_key
            )
            metadata = json.loads(metadata_response['Body'].read().decode('utf-8'))
            logger.info("πŸ“‹ Loaded recording metadata")
        except Exception as e:
            logger.error(f"❌ Could not load recording metadata: {e}")
            return jsonify({"error": "Recording not found"}), 404

        audio_info = metadata.get('files', {}).get('audio')
        if not audio_info:
            return jsonify({"error": "Audio file not found in recording"}), 404

        audio_s3_path = audio_info['s3_path']
        logger.info(f"πŸ“₯ Audio file S3 path: {audio_s3_path}")

        temp_audio_path = None

        try:
            audio_extension = audio_info.get('filename', 'audio.m4a').split('.')[-1]
            with tempfile.NamedTemporaryFile(delete=False, suffix=f'.{audio_extension}') as temp_audio:
                temp_audio_path = temp_audio.name

            s3_client.download_file(
                AWS_BUCKET_NAME,
                audio_s3_path,
                temp_audio_path
            )

            logger.info(f"πŸ“₯ Downloaded audio file for transcription: {temp_audio_path}")

            success, result_data, error_message = perform_transcription_with_chunking(
                temp_audio_path, title, sheet_format, tempo
            )

            if success:
                result_data['recording_id'] = recording_id
                result_data['user_id'] = user_id

                updated_result_data = save_generated_files_to_s3(user_id, recording_id, result_data)

                logger.info(f"βœ… Transcription completed for recording {recording_id}")
                if 'midi_file' in updated_result_data:
                    logger.info(f"   MIDI: {updated_result_data['midi_file']}")
                if 'sheet_music' in updated_result_data and updated_result_data['sheet_music']:
                    logger.info(f"   PDF: {updated_result_data['sheet_music'].get('fileUrl', 'None')}")

                return jsonify(updated_result_data), 200
            else:
                logger.error(f"❌ Transcription failed: {error_message}")
                return jsonify({"error": error_message}), 500

        finally:
            if temp_audio_path and os.path.exists(temp_audio_path):
                try:
                    os.unlink(temp_audio_path)
                    logger.info("🧹 Cleaned up temporary audio file")
                except Exception as cleanup_e:
                    logger.warning(f"⚠️ Warning: Could not clean up temp file: {cleanup_e}")

    except Exception as e:
        logger.error(f"❌ Transcription error: {e}")
        import traceback
        traceback.print_exc()

        return jsonify({
            "success": False,
            "error": str(e)
        }), 500


@app.route('/api/health', methods=['GET'])
def health_check():
    """Simple health check endpoint - ORIGINAL"""
    return jsonify({
        "status": "ok",
        "model_loaded": stable_model_loader.is_model_ready(),
        "timestamp": datetime.now().isoformat(),
        "message": "Model loads on first recordings endpoint request"
    })


@app.route('/api/transcribe', methods=['POST'])
def transcribe_audio_with_sheet_music():
    """Enhanced transcribe endpoint that includes sheet music generation - ALL ORIGINAL"""
    if 'audio' not in request.files:
        return jsonify({"error": "No audio file provided"}), 400

    file = request.files['audio']
    if file.filename == '':
        return jsonify({"error": "Empty filename"}), 400

    try:
        platform_info = g.get('platform_info', detect_request_platform(request))
        platform_type = 'web' if platform_info['is_web'] else 'mobile'

        logger.info(f"🎡 Processing audio from: {platform_type}")

        # Check if model needs reset due to platform switch
        if hasattr(g, 'last_platform') and g.last_platform != platform_type:
            logger.info(f"πŸ”„ Platform switch detected ({g.last_platform} -> {platform_type})")
            # Don't automatically reset - just log the switch

        g.last_platform = platform_type

        sheet_format = request.form.get('sheet_format', 'pdf')
        title = request.form.get('title', 'Piano Transcription')
        tempo = int(request.form.get('tempo', '120'))

        audio_path = safe_file_processing(file, platform_type)

        logger.info(f"🎡 Processing uploaded audio file from {platform_type}: {os.path.basename(audio_path)}")

        success, result_data, error_message = perform_transcription_with_chunking(
            audio_path, title, sheet_format, tempo
        )

        try:
            if os.path.exists(audio_path):
                os.remove(audio_path)
        except Exception as cleanup_e:
            logger.warning(f"⚠️ Warning: Could not clean up uploaded file: {cleanup_e}")

        if success:
            result_data['platform'] = platform_type
            return jsonify(result_data)
        else:
            return jsonify({"error": error_message}), 500

    except Exception as e:
        logger.error(f"❌ Error in transcription endpoint: {e}")
        import traceback
        traceback.print_exc()

        return jsonify({"error": str(e)}), 500


@app.route('/api/convert-midi-to-sheet', methods=['POST'])
def convert_midi_to_sheet():
    """Convert an existing MIDI file to sheet music - ALL ORIGINAL"""
    if 'midi' not in request.files:
        return jsonify({"error": "No MIDI file provided"}), 400

    file = request.files['midi']
    if file.filename == '':
        return jsonify({"error": "Empty filename"}), 400

    try:
        format_type = request.form.get('format', 'pdf')
        title = request.form.get('title', 'Piano Sheet Music')

        musescore_available, musescore_info = check_musescore_installation()

        if not musescore_available:
            return jsonify({
                "error": "MuseScore is not available for sheet music generation",
                "musescore_info": musescore_info
            }), 400

        original_filename = secure_filename(file.filename)
        filename = f"{uuid.uuid4()}_{original_filename}"
        midi_path = os.path.join(UPLOAD_FOLDER, filename)
        file.save(midi_path)

        if format_type.lower() == 'pdf':
            musicxml_filename = f"{os.path.splitext(filename)[0]}.musicxml"
            musicxml_path = os.path.join(OUTPUT_FOLDER, musicxml_filename)

            pdf_filename = f"{os.path.splitext(filename)[0]}.pdf"
            pdf_path = os.path.join(OUTPUT_FOLDER, pdf_filename)

            success, message = convert_midi_to_musicxml(midi_path, musicxml_path)

            if success:
                pdf_success, pdf_message = convert_musicxml_to_pdf(musicxml_path, pdf_path)

                if pdf_success:
                    sheet_music_result = {
                        "fileUrl": f"/api/download/{pdf_filename}",
                        "format": "pdf",
                        "title": title
                    }
                else:
                    return jsonify({"error": f"PDF conversion failed: {pdf_message}"}), 500
            else:
                return jsonify({"error": f"MusicXML conversion failed: {message}"}), 500
        else:
            musicxml_filename = f"{os.path.splitext(filename)[0]}.musicxml"
            musicxml_path = os.path.join(OUTPUT_FOLDER, musicxml_filename)

            success, message = convert_midi_to_musicxml(midi_path, musicxml_path)

            if success:
                sheet_music_result = {
                    "fileUrl": f"/api/download/{musicxml_filename}",
                    "format": "musicxml",
                    "title": title
                }
            else:
                return jsonify({"error": f"MusicXML conversion failed: {message}"}), 500

        try:
            if os.path.exists(midi_path):
                os.remove(midi_path)
        except Exception as cleanup_e:
            logger.warning(f"Warning: Could not clean up MIDI file: {cleanup_e}")

        return jsonify({
            "success": True,
            "sheet_music": sheet_music_result,
            "musescore_available": True
        })

    except Exception as e:
        logger.error(f"Error in MIDI to sheet conversion: {e}")
        return jsonify({"error": str(e)}), 500


@app.route('/api/download/<filename>', methods=['GET'])
def download_midi(filename):
    """Download the generated MIDI file - ORIGINAL"""
    try:
        file_path = os.path.join(OUTPUT_FOLDER, secure_filename(filename))
        if not os.path.exists(file_path):
            return jsonify({"error": "File not found"}), 404
        return send_file(file_path, as_attachment=True)
    except Exception as e:
        logger.error(f"Error downloading file {filename}: {e}")
        return jsonify({"error": str(e)}), 404


@app.route('/process-audio', methods=['POST'])
def process_audio():
    """Process uploaded audio file and return detected notes - ALL ORIGINAL"""
    if 'file' not in request.files:
        logger.error("No file part in the request.")
        return jsonify({"error": "No file part in the request"}), 400

    file = request.files['file']
    if file.filename == '':
        logger.error("No selected file.")
        return jsonify({"error": "No selected file"}), 400

    try:
        file_ext = os.path.splitext(file.filename)[1].lower()
        unique_filename = f"{uuid.uuid4()}_{secure_filename(file.filename)}"
        audio_path = os.path.join(UPLOAD_FOLDER, unique_filename)
        file.save(audio_path)
        logger.info(f"File saved at: {audio_path}")

        wav_file_path = os.path.splitext(audio_path)[0] + ".wav"
        if file_ext != '.wav':
            logger.info(f"Converting {file_ext} to WAV...")
            if file_ext == '.m4a':
                success = convert_m4a_to_wav(audio_path, wav_file_path)
            else:
                success = convert_audio_to_wav(audio_path, wav_file_path)

            if not success:
                return jsonify({"error": "Failed to convert audio file"}), 500
        else:
            wav_file_path = audio_path

        mel_spec = extract_mel_spectrogram(wav_file_path)
        expected_height = 229
        expected_width = 625

        logger.info(f"Original spectrogram shape: {mel_spec.shape}")

        if mel_spec.shape[0] != expected_height:
            logger.info(f"Resizing frequency dimension from {mel_spec.shape[0]} to {expected_height}")
            mel_spec = tf.image.resize(
                tf.expand_dims(mel_spec, 0),
                [expected_height, mel_spec.shape[1]]
            )[0]

        if mel_spec.shape[1] < expected_width:
            padding = expected_width - mel_spec.shape[1]
            mel_spec = np.pad(mel_spec, ((0, 0), (0, padding)), mode='constant')
            logger.info(f"Padded time dimension to {mel_spec.shape}")
        elif mel_spec.shape[1] > expected_width:
            mel_spec = mel_spec[:, :expected_width]
            logger.info(f"Trimmed time dimension to {mel_spec.shape}")

        mel_spec = tf.transpose(mel_spec)
        mel_spec = tf.expand_dims(mel_spec, axis=0)
        mel_spec = tf.expand_dims(mel_spec, axis=-1)
        logger.info(f"Spectrogram shape for model input: {mel_spec.shape}")

        try:
            current_model = stable_model_loader.get_model()
            logger.info("Model loaded successfully for processing")
            logger.info("Running model prediction...")
            predictions = current_model.predict(mel_spec)
            logger.info("Model prediction completed")
            notes = extract_notes_from_predictions(predictions)
            logger.info(f"Extracted {len(notes)} notes")

        except Exception as model_error:
            logger.error(f"Model failed: {model_error}, generating test notes...")
            logger.info("Creating test notes (C major scale)...")
            notes = []
            for i, pitch in enumerate([60, 62, 64, 65, 67, 69, 71, 72]):
                notes.append({
                    "note_name": pitch_to_note_name(pitch),
                    "time": float(i * 0.5),
                    "duration": 0.4,
                    "velocity": 0.8,
                    "velocity_midi": 100,
                    "pitch": pitch,
                    "frequency": librosa.midi_to_hz(pitch)
                })

        logger.info("\n===== EXTRACTED NOTES SUMMARY =====")
        logger.info(f"Total notes extracted: {len(notes)}")
        if len(notes) > 0:
            logger.info(f"Time range: {notes[0]['time']:.2f}s to {notes[-1]['time']:.2f}s")
            logger.info(f"Pitch range: {min([n['pitch'] for n in notes])} to {max([n['pitch'] for n in notes])}")

        midi_filename = f"{os.path.splitext(unique_filename)[0]}.mid"
        midi_output_path = os.path.join(OUTPUT_FOLDER, midi_filename)
        create_midi_from_notes(notes, midi_output_path)
        logger.info(f"MIDI file created at: {midi_output_path}")

        try:
            if os.path.exists(audio_path):
                os.remove(audio_path)
            if os.path.exists(wav_file_path) and wav_file_path != audio_path:
                os.remove(wav_file_path)
        except Exception as cleanup_e:
            logger.warning(f"Warning: Could not clean up temporary files: {cleanup_e}")

        return jsonify({
            "success": True,
            "notes": notes,
            "midi_file": f"/api/download/{midi_filename}"
        }), 200

    except Exception as e:
        logger.error(f"Error processing audio: {e}")
        import traceback
        traceback.print_exc()
        return jsonify({"error": str(e)}), 500


@app.route('/api/reset-server-state', methods=['POST'])
def reset_server_state():
    """Emergency endpoint to reset server state when corruption occurs - IMPROVED"""
    try:
        logger.info("πŸ”„ Manually resetting server state...")

        # Safe reset with cooldown
        reset_success = stable_model_loader.safe_reset()

        # Clear temporary files
        temp_files_cleared = 0
        for folder in [UPLOAD_FOLDER, OUTPUT_FOLDER]:
            try:
                for filename in os.listdir(folder):
                    if filename.startswith(('temp_', 'web_', 'mobile_', 'test_')):
                        try:
                            file_path = os.path.join(folder, filename)
                            os.remove(file_path)
                            temp_files_cleared += 1
                        except Exception as file_error:
                            logger.warning(f"⚠️ Could not remove {filename}: {file_error}")
            except Exception as folder_error:
                logger.warning(f"⚠️ Could not access folder {folder}: {folder_error}")

        try:
            gc.collect()
        except:
            pass

        logger.info(f"βœ… Server state reset complete - cleared {temp_files_cleared} temp files")

        return jsonify({
            "success": True,
            "message": "Server state reset successfully",
            "temp_files_cleared": temp_files_cleared,
            "model_reset": reset_success,
            "model_ready": stable_model_loader.is_model_ready()
        })

    except Exception as e:
        logger.error(f"❌ Reset failed: {e}")
        return jsonify({
            "success": False,
            "error": str(e)
        }), 500


# =============================================================================
# MAIN APPLICATION ENTRY POINT
# =============================================================================

if __name__ == '__main__':
    # For Hugging Face Spaces, use the PORT environment variable
    port = int(os.environ.get('PORT', 7860))
    app.run(host='0.0.0.0', port=port, debug=False)