File size: 48,918 Bytes
a775cf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
IndexTTS2 Handler for Ultimate TTS Studio
Provides integration with IndexTTS-2 Text-to-Speech system with advanced emotion control
"""

import os
import sys
import warnings
import numpy as np
import torch
import tempfile
import json
from pathlib import Path
from typing import Optional, Union, Tuple, Dict, Any, List
from datetime import datetime
import librosa
import soundfile as sf

# Suppress warnings
warnings.filterwarnings('ignore')

# Setup IndexTTS2 module path and cache directories
current_dir = os.path.dirname(os.path.abspath(__file__))
indextts2_base_path = os.path.join(current_dir, 'indextts2')
indextts2_module_path = os.path.join(indextts2_base_path, 'indextts')

# Set up cache directory early to avoid ModelScope conflicts
cache_dir = os.path.join(current_dir, 'checkpoints', 'indextts2', 'cache')
os.makedirs(cache_dir, exist_ok=True)

# Set environment variables before any imports
os.environ['HF_HOME'] = cache_dir
os.environ['TRANSFORMERS_CACHE'] = cache_dir
os.environ['HF_HUB_CACHE'] = cache_dir
os.environ['MODELSCOPE_CACHE'] = cache_dir
os.environ['HUGGINGFACE_HUB_CACHE'] = cache_dir

# Add paths to sys.path for proper module resolution
# Ensure the app root is included so implicit namespace package 'indextts2' works
paths_to_add = [current_dir, indextts2_base_path, indextts2_module_path]
for path in paths_to_add:
    if path not in sys.path:
        sys.path.insert(0, path)

# Global handler instance
_indextts2_handler = None
INDEXTTS2_AVAILABLE = False

# Try multiple import strategies
def try_import_indextts2():
    """Try different import strategies for IndexTTS2"""
    global INDEXTTS2_AVAILABLE
    
    # Strategy 1: Set up proper module structure and import
    try:
        import importlib
        # Import the bundled package and alias as top-level 'indextts' for internal absolute imports
        indextts_pkg = importlib.import_module('indextts2.indextts')
        sys.modules['indextts'] = indextts_pkg
        
        # Import IndexTTS2
        from indextts2.indextts.infer_v2 import IndexTTS2
        INDEXTTS2_AVAILABLE = True
        print("βœ… IndexTTS2 loaded successfully (Strategy 1)")
        return IndexTTS2
    except Exception as e1:
        print(f"⚠️ Strategy 1 failed: {e1}")
    
    # Strategy 2: Direct file loading with module setup
    try:
        import importlib
        import importlib.util
        
        # Ensure 'indextts' alias points to bundled package for internal imports
        try:
            indextts_pkg = importlib.import_module('indextts2.indextts')
            sys.modules['indextts'] = indextts_pkg
        except Exception:
            pass
        
        # Load infer_v2.py directly but set up the environment first
        infer_v2_path = os.path.join(indextts2_module_path, "infer_v2.py")
        spec = importlib.util.spec_from_file_location("indextts2.indextts.infer_v2", infer_v2_path)
        infer_v2_module = importlib.util.module_from_spec(spec)
        
        # Register both names for compatibility
        sys.modules['indextts2.indextts.infer_v2'] = infer_v2_module
        sys.modules['indextts.infer_v2'] = infer_v2_module
        
        # Execute the module
        spec.loader.exec_module(infer_v2_module)
        IndexTTS2 = infer_v2_module.IndexTTS2
        INDEXTTS2_AVAILABLE = True
        print("βœ… IndexTTS2 loaded successfully (Strategy 2)")
        return IndexTTS2
    except Exception as e2:
        print(f"⚠️ Strategy 2 failed: {e2}")
    
    # All strategies failed
    INDEXTTS2_AVAILABLE = False
    print("❌ All import strategies failed")
    print("   IndexTTS2 requires complex module dependencies that couldn't be resolved")
    print("   This is a known limitation with the current IndexTTS2 package structure")
    return None

# Try to import IndexTTS2
IndexTTS2 = try_import_indextts2()

def check_indextts2_models():
    """Check if IndexTTS2 models are available"""
    model_dir = Path("checkpoints/indextts2")
    config_path = model_dir / "config.yaml"
    
    if not config_path.exists():
        return False
    
    # Check for essential IndexTTS-2 model files
    essential_files = ["gpt.pth", "s2mel.pth", "bpe.model"]
    
    for filename in essential_files:
        if not (model_dir / filename).exists():
            return False
    
    return True

def ensure_indextts2_dependencies():
    """Ensure all IndexTTS2 dependencies are available"""
    try:
        from transformers import SeamlessM4TFeatureExtractor, Wav2Vec2BertModel
        from huggingface_hub import hf_hub_download, snapshot_download
        
        model_dir = Path("checkpoints/indextts2")
        cache_dir = model_dir / "cache"
        cache_dir.mkdir(parents=True, exist_ok=True)
        
        # Set environment variables to use local cache
        os.environ['HF_HOME'] = str(cache_dir)
        os.environ['TRANSFORMERS_CACHE'] = str(cache_dir)
        os.environ['HF_HUB_CACHE'] = str(cache_dir)
        os.environ['MODELSCOPE_CACHE'] = str(cache_dir)
        
        print("πŸ”§ Ensuring IndexTTS2 dependencies are available...")
        
        # Check and download facebook/w2v-bert-2.0 (feature extractor and model)
        try:
            print("   πŸ” Checking facebook/w2v-bert-2.0...")
            repo_id = "facebook/w2v-bert-2.0"
            # Prefer a concrete snapshot path inside our cache if it already exists
            snapshots_dir = cache_dir / f"models--{repo_id.replace('/', '--')}" / "snapshots"
            local_repo_path = None
            if snapshots_dir.exists() and any(snapshots_dir.iterdir()):
                # pick most recent snapshot
                local_repo_path = str(max(snapshots_dir.iterdir(), key=lambda p: p.stat().st_mtime))
            if local_repo_path is None:
                local_repo_path = snapshot_download(repo_id=repo_id, cache_dir=str(cache_dir))

            # Validate both processor and model load locally (no remote access)
            SeamlessM4TFeatureExtractor.from_pretrained(local_repo_path, local_files_only=True)
            Wav2Vec2BertModel.from_pretrained(local_repo_path, local_files_only=True)
            print("   βœ… facebook/w2v-bert-2.0 ready")
        except Exception:
            # Non-fatal: real loading will happen later with fallback logic
            print("   ⚠️ facebook/w2v-bert-2.0 not fully validated; will download on demand")
        
        # Check and download MaskGCT semantic codec
        try:
            print("   πŸ” Checking amphion/MaskGCT...")
            hf_hub_download(
                "amphion/MaskGCT",
                filename="semantic_codec/model.safetensors",
                cache_dir=str(cache_dir)
            )
            print("   βœ… MaskGCT semantic codec ready")
        except Exception as e:
            print(f"   ⚠️ MaskGCT issue: {e}")
        
        # Check and download campplus
        try:
            print("   πŸ” Checking funasr/campplus...")
            hf_hub_download(
                "funasr/campplus",
                filename="campplus_cn_common.bin",
                cache_dir=str(cache_dir)
            )
            print("   βœ… campplus ready")
        except Exception as e:
            print(f"   ⚠️ campplus issue: {e}")
        
        return True
        
    except Exception as e:
        print(f"❌ Error ensuring dependencies: {e}")
        return False

def download_indextts2_models():
    """Download IndexTTS2 models and dependencies from HuggingFace"""
    try:
        from huggingface_hub import hf_hub_download
        from transformers import SeamlessM4TFeatureExtractor
        import requests
    except ImportError:
        print("⚠️ Cannot auto-download IndexTTS2 models - missing huggingface_hub or transformers")
        print("   Install with: pip install huggingface_hub transformers requests")
        return False
    
    repo_id = "IndexTeam/IndexTTS-2"
    model_dir = Path("checkpoints/indextts2")
    
    # Create directory if it doesn't exist
    model_dir.mkdir(parents=True, exist_ok=True)
    
    print("🎯 Auto-downloading IndexTTS-2 models and dependencies...")
    print("   This may take several minutes on first run...")
    
    # Step 1: Download main IndexTTS-2 model files
    required_files = [
        "config.yaml",
        "bpe.model", 
        "gpt.pth",
        "s2mel.pth",
        "feat1.pt",
        "feat2.pt",
        "wav2vec2bert_stats.pt"
    ]
    
    # Qwen emotion model files (in subfolder)
    qwen_files = [
        "qwen0.6bemo4-merge/config.json",
        "qwen0.6bemo4-merge/generation_config.json",
        "qwen0.6bemo4-merge/model.safetensors",
        "qwen0.6bemo4-merge/tokenizer.json",
        "qwen0.6bemo4-merge/tokenizer_config.json",
        "qwen0.6bemo4-merge/vocab.json"
    ]
    
    all_files = required_files + qwen_files
    
    print("πŸ“₯ Downloading IndexTTS-2 main models...")
    for filename in all_files:
        file_path = model_dir / filename
        
        if file_path.exists():
            print(f"   βœ… {filename} already exists")
            continue
            
        try:
            print(f"   ⬇️ Downloading {filename}...")
            
            # Create subdirectories if needed
            file_path.parent.mkdir(parents=True, exist_ok=True)
            
            hf_hub_download(
                repo_id=repo_id,
                filename=filename,
                local_dir=str(model_dir),
                local_dir_use_symlinks=False
            )
            
            print(f"   βœ… {filename} downloaded")
            
        except Exception as e:
            print(f"   ❌ Failed to download {filename}: {e}")
            continue
    
    # Step 2: Download dependency models
    print("πŸ“₯ Downloading IndexTTS-2 dependencies...")
    
    try:
        print("   ⬇️ Downloading facebook/w2v-bert-2.0 feature extractor...")
        # Set cache directory before downloading
        cache_dir = str(model_dir / "cache")
        os.makedirs(cache_dir, exist_ok=True)
        
        # Set environment variables to use local cache
        os.environ['HF_HOME'] = cache_dir
        os.environ['TRANSFORMERS_CACHE'] = cache_dir
        os.environ['HF_HUB_CACHE'] = cache_dir
        
        # Download to local cache
        SeamlessM4TFeatureExtractor.from_pretrained(
            "facebook/w2v-bert-2.0",
            cache_dir=cache_dir
        )
        print("   βœ… facebook/w2v-bert-2.0 downloaded to local cache")
    except Exception as e:
        print(f"   ❌ Failed to download facebook/w2v-bert-2.0: {e}")
        print("   πŸ’‘ This model will be downloaded automatically when needed")
    
    try:
        print("   ⬇️ Downloading amphion/MaskGCT semantic codec...")
        # Download the semantic codec model
        semantic_codec_path = hf_hub_download(
            "amphion/MaskGCT", 
            filename="semantic_codec/model.safetensors",
            cache_dir=str(model_dir / "cache")
        )
        print(f"   βœ… MaskGCT semantic codec downloaded to {semantic_codec_path}")
    except Exception as e:
        print(f"   ❌ Failed to download MaskGCT semantic codec: {e}")
    
    try:
        print("   ⬇️ Downloading funasr/campplus model...")
        # Download campplus model
        campplus_path = hf_hub_download(
            "funasr/campplus",
            filename="campplus_cn_common.bin",
            cache_dir=str(model_dir / "cache")
        )
        print(f"   βœ… campplus model downloaded to {campplus_path}")
    except Exception as e:
        print(f"   ❌ Failed to download campplus model: {e}")
    
    # Step 3: Check if essential files are present
    essential_files = ["config.yaml", "gpt.pth", "s2mel.pth", "bpe.model"]
    missing_essential = []
    
    for filename in essential_files:
        if not (model_dir / filename).exists():
            missing_essential.append(filename)
    
    if missing_essential:
        print(f"❌ Essential files missing: {missing_essential}")
        return False
    
    print("πŸŽ‰ IndexTTS-2 models and dependencies ready!")
    print("πŸ’‘ Note: Some models are cached by transformers/huggingface_hub")
    return True

def get_indextts2_handler():
    """Get the global IndexTTS2 handler instance (singleton)"""
    global _indextts2_handler
    if _indextts2_handler is None:
        _indextts2_handler = IndexTTS2Handler()
    return _indextts2_handler

class IndexTTS2Handler:
    """Handler for IndexTTS2 TTS system with advanced emotion control"""
    
    def __init__(self):
        self.model = None
        self.device = self._get_device()
        self.sample_rate = 22050
        self.model_path = "IndexTeam/IndexTTS-2"
        self.checkpoints_dir = Path("checkpoints/indextts2")
        self.checkpoints_dir.mkdir(parents=True, exist_ok=True)
        
        # Emotion control modes
        self.emotion_modes = {
            'audio_reference': 'Use audio file for emotion reference',
            'vector_control': 'Manual emotion vector adjustment',
            'text_description': 'Natural language emotion description'
        }
        
        # Emotion vectors for manual control
        self.emotion_vectors = {
            'happy': 0.0,
            'angry': 0.0,
            'sad': 0.0,
            'afraid': 0.0,
            'disgusted': 0.0,
            'melancholic': 0.0,
            'surprised': 0.0,
            'calm': 0.0
        }
        
    def _get_device(self):
        """Get the appropriate device for inference"""
        if torch.cuda.is_available():
            return "cuda"
        elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
            return "mps"
        else:
            return "cpu"
    
    def initialize_model(self):
        """Initialize the IndexTTS2 model"""
        if not INDEXTTS2_AVAILABLE or IndexTTS2 is None:
            return False, "❌ IndexTTS2 not available"
        
        try:
            print("🎯 Initializing IndexTTS2 model...")
            
            # Set up cache directories to avoid ModelScope conflicts
            cache_dir = str(self.checkpoints_dir / "cache")
            os.makedirs(cache_dir, exist_ok=True)
            
            # Set environment variables to use local cache (multiple cache systems)
            os.environ['HF_HOME'] = cache_dir
            os.environ['TRANSFORMERS_CACHE'] = cache_dir
            os.environ['HF_HUB_CACHE'] = cache_dir
            os.environ['MODELSCOPE_CACHE'] = cache_dir
            os.environ['HUGGINGFACE_HUB_CACHE'] = cache_dir
            
            # Also set the cache directory in the IndexTTS2 infer_v2.py file
            # This overrides the hardcoded cache path
            original_hf_cache = os.environ.get('HF_HUB_CACHE', './checkpoints/hf_cache')
            os.environ['HF_HUB_CACHE'] = cache_dir
            
            # Check if models are available, download if needed
            if not check_indextts2_models():
                print("πŸ“₯ IndexTTS2 models not found, downloading...")
                if not download_indextts2_models():
                    return False, "❌ Failed to download IndexTTS2 models"
            
            # Ensure all dependencies are available
            print("πŸ”§ Ensuring dependencies are ready...")
            try:
                ensure_indextts2_dependencies()
            except Exception as dep_error:
                print(f"⚠️ Dependency check failed: {dep_error}")
                print("πŸ’‘ Continuing with initialization - dependencies will be downloaded as needed")
            
            # Initialize IndexTTS2 with config path and model directory
            config_path = str(self.checkpoints_dir / "config.yaml")
            model_dir = str(self.checkpoints_dir)
            
            print(f"πŸ”§ Using config: {config_path}")
            print(f"πŸ”§ Using model dir: {model_dir}")
            print(f"πŸ”§ Using cache dir: {cache_dir}")
            
            self.model = IndexTTS2(
                cfg_path=config_path,
                model_dir=model_dir,
                device=self.device,
                use_fp16=self.device != "cpu"
            )
            
            print(f"βœ… IndexTTS2 model loaded on {self.device}")
            return True, "βœ… IndexTTS2 model loaded successfully"
            
        except Exception as e:
            import traceback
            traceback.print_exc()
            print(f"❌ Error initializing IndexTTS2: {e}")
            return False, f"❌ Error initializing IndexTTS2: {str(e)}"
    
    def unload_model(self):
        """Unload the IndexTTS2 model to free memory"""
        try:
            if self.model is not None:
                del self.model
                self.model = None
            
            # Force garbage collection
            import gc
            gc.collect()
            
            if torch.cuda.is_available():
                torch.cuda.empty_cache()
            
            return "βœ… IndexTTS2 model unloaded successfully"
            
        except Exception as e:
            return f"⚠️ Error unloading IndexTTS2: {str(e)}"
    
    def is_model_loaded(self):
        """Check if the model is loaded"""
        return self.model is not None
    
    def preprocess_audio(self, audio_path: str, max_duration: float = 15.0):
        """Preprocess reference audio for optimal performance"""
        try:
            # Load audio
            audio, sr = librosa.load(audio_path, sr=self.sample_rate)
            
            # Trim silence
            audio, _ = librosa.effects.trim(audio, top_db=20)
            
            # Limit duration for optimal performance
            max_samples = int(max_duration * self.sample_rate)
            if len(audio) > max_samples:
                audio = audio[:max_samples]
            
            return audio, sr
            
        except Exception as e:
            print(f"❌ Error preprocessing audio: {e}")
            return None, None
    
    def _preprocess_text_for_tensor_safety(self, text: str) -> str:
        """Preprocess text to avoid tensor dimension mismatches in IndexTTS2"""
        import re
        
        # Remove excessive punctuation that might cause issues
        text = re.sub(r'[.]{3,}', '...', text)  # Limit ellipsis
        text = re.sub(r'[!]{2,}', '!', text)    # Limit exclamation marks
        text = re.sub(r'[?]{2,}', '?', text)    # Limit question marks
        
        # Clean up excessive whitespace
        text = re.sub(r'\s+', ' ', text)
        text = text.strip()
        
        # Remove or replace problematic character sequences
        text = re.sub(r'[^\w\s.,!?;:\'"()-]', '', text)  # Keep only safe characters
        
        # Ensure text doesn't end abruptly without punctuation
        if text and text[-1] not in '.!?':
            text += '.'
        
        # Limit very long sentences that might cause tensor issues
        sentences = re.split(r'([.!?]+)', text)
        processed_sentences = []
        
        for i in range(0, len(sentences), 2):
            if i < len(sentences):
                sentence = sentences[i].strip()
                punctuation = sentences[i + 1] if i + 1 < len(sentences) else '.'
                
                # If sentence is too long, split it at commas or conjunctions
                if len(sentence) > 150:
                    # Split at commas, semicolons, or conjunctions
                    parts = re.split(r'(,|;|\s+and\s+|\s+but\s+|\s+or\s+)', sentence)
                    current_part = ""
                    
                    for j, part in enumerate(parts):
                        if part.strip() in [',', ';', 'and', 'but', 'or']:
                            current_part += part
                            if len(current_part) > 80:  # Split here
                                processed_sentences.append(current_part.strip() + '.')
                                current_part = ""
                        else:
                            if len(current_part + part) > 120 and current_part:
                                processed_sentences.append(current_part.strip() + '.')
                                current_part = part
                            else:
                                current_part += part
                    
                    if current_part.strip():
                        processed_sentences.append(current_part.strip() + punctuation)
                else:
                    processed_sentences.append(sentence + punctuation)
        
        return ' '.join(processed_sentences)
    
    def generate_speech(
        self,
        text: str,
        reference_audio: Optional[str] = None,
        emotion_mode: str = "audio_reference",
        emotion_audio: Optional[str] = None,
        emotion_vectors: Optional[Dict[str, float]] = None,
        emotion_description: str = "",
        temperature: float = 0.8,
        top_p: float = 0.9,
        top_k: int = 50,
        repetition_penalty: float = 1.1,
        max_mel_tokens: int = 1500,
        seed: Optional[int] = None,
        use_random: bool = True,
        emo_alpha: float = 1.0
    ) -> Tuple[Optional[np.ndarray], str]:
        """
        Generate speech using IndexTTS2 with emotion control
        
        Args:
            text: Text to synthesize
            reference_audio: Path to reference audio for voice cloning
            emotion_mode: Emotion control mode ('audio_reference', 'vector_control', 'text_description')
            emotion_audio: Path to emotion reference audio
            emotion_vectors: Dictionary of emotion intensities
            emotion_description: Natural language emotion description
            temperature: Sampling temperature
            top_p: Top-p sampling parameter
            top_k: Top-k sampling parameter
            repetition_penalty: Repetition penalty
            max_mel_tokens: Maximum mel tokens to generate
            seed: Random seed for reproducibility
            use_random: Enable random sampling
            emo_alpha: Emotion blending alpha
        """
        if not self.is_model_loaded():
            return None, "❌ IndexTTS2 model not loaded. Please initialize first."
        
        if not reference_audio or not os.path.exists(reference_audio):
            return None, "❌ Reference audio is required for IndexTTS2"
        
        try:
            print(f"🎯 Generating speech with IndexTTS2...")
            print(f"   Text: {text[:50]}...")
            print(f"   Emotion mode: {emotion_mode}")
            
            # Preprocess long text to avoid tensor dimension mismatch
            # Split very long text into smaller chunks to prevent issues
            if len(text) > 500:  # If text is longer than 500 characters
                print(f"   ⚠️ Long text detected ({len(text)} chars), splitting into chunks...")
                # Split by sentences first, then by length if needed
                import re
                sentences = re.split(r'[.!?]+', text)
                processed_chunks = []
                current_chunk = ""
                
                for sentence in sentences:
                    sentence = sentence.strip()
                    if not sentence:
                        continue
                    
                    # If adding this sentence would make chunk too long, save current chunk
                    if len(current_chunk) + len(sentence) > 300 and current_chunk:
                        processed_chunks.append(current_chunk.strip())
                        current_chunk = sentence
                    else:
                        current_chunk += (" " + sentence if current_chunk else sentence)
                
                # Add the last chunk
                if current_chunk.strip():
                    processed_chunks.append(current_chunk.strip())
                
                # If we have multiple chunks, process them separately and concatenate
                if len(processed_chunks) > 1:
                    print(f"   πŸ“ Processing {len(processed_chunks)} text chunks...")
                    chunk_audios = []
                    
                    for i, chunk in enumerate(processed_chunks):
                        print(f"   πŸ”„ Processing chunk {i+1}/{len(processed_chunks)}: {chunk[:30]}...")
                        
                        # Recursive call with shorter text
                        chunk_audio, chunk_message = self.generate_speech(
                            chunk, reference_audio, emotion_mode, emotion_audio,
                            emotion_vectors, emotion_description, temperature, top_p, top_k,
                            repetition_penalty, min(max_mel_tokens, 800), seed, use_random, emo_alpha
                        )
                        
                        if chunk_audio is None:
                            return None, f"❌ Error processing chunk {i+1}: {chunk_message}"
                        
                        chunk_audios.append(chunk_audio)
                    
                    # Concatenate all chunk audios with small pauses
                    print(f"   🎡 Combining {len(chunk_audios)} audio chunks...")
                    pause_samples = int(0.3 * self.sample_rate)  # 0.3 second pause
                    pause_audio = np.zeros(pause_samples)
                    
                    combined_audio = chunk_audios[0]
                    for chunk_audio in chunk_audios[1:]:
                        combined_audio = np.concatenate([combined_audio, pause_audio, chunk_audio])
                    
                    return combined_audio, "βœ… Long text processed successfully in chunks"
                else:
                    # Single chunk, continue with normal processing
                    text = processed_chunks[0]
            
            # Set random seed if provided
            if seed is not None:
                torch.manual_seed(seed)
                np.random.seed(seed)
            
            # Prepare generation parameters
            generation_kwargs = {
                'do_sample': True,
                'temperature': temperature,
                'top_p': top_p,
                'top_k': top_k,
                'repetition_penalty': repetition_penalty,
                'max_mel_tokens': max_mel_tokens
            }
            
            # Handle emotion control based on mode
            emo_audio_prompt = None
            emo_vector = None
            use_emo_text = False
            emo_text = None
            
            if emotion_mode == "audio_reference" and emotion_audio and os.path.exists(emotion_audio):
                emo_audio_prompt = emotion_audio
                    
            elif emotion_mode == "vector_control" and emotion_vectors:
                # Convert emotion vectors to the format expected by IndexTTS2
                emo_vector = []
                for emotion in ['happy', 'angry', 'sad', 'afraid', 'disgusted', 'melancholic', 'surprised', 'calm']:
                    emo_vector.append(emotion_vectors.get(emotion, 0.0))
                
            elif emotion_mode == "text_description" and emotion_description:
                use_emo_text = True
                emo_text = emotion_description
            
            # Preprocess text to avoid tensor dimension issues
            # Clean up text that might cause tensor mismatches
            original_text = text
            text = self._preprocess_text_for_tensor_safety(text)
            if text != original_text:
                print(f"πŸ”§ Text preprocessed to avoid tensor issues")
            
            # Generate speech using the actual IndexTTS2 API with retry logic for tensor dimension issues
            # Use smaller max_text_tokens_per_segment to prevent tensor dimension issues
            max_text_tokens_per_segment = min(80, max_mel_tokens // 20) if len(text) > 200 else 120
            
            # Implement retry logic with progressively smaller parameters
            max_retries = 3
            retry_count = 0
            result = None
            
            while retry_count < max_retries and result is None:
                try:
                    # Adjust parameters based on retry count
                    if retry_count > 0:
                        print(f"   πŸ”„ Tensor dimension mismatch (attempt {retry_count}/{max_retries})")
                        # Progressively reduce parameters to avoid tensor issues
                        max_text_tokens_per_segment = max(20, max_text_tokens_per_segment // 2)
                        generation_kwargs['max_mel_tokens'] = max(300, generation_kwargs['max_mel_tokens'] // 2)
                        print(f"   πŸ”§ Retrying with max_text_tokens_per_segment={max_text_tokens_per_segment}, max_mel_tokens={generation_kwargs['max_mel_tokens']}")
                    
                    result = self.model.infer(
                        spk_audio_prompt=reference_audio,
                        text=text,
                        output_path=None,  # Return audio data instead of saving
                        emo_audio_prompt=emo_audio_prompt,
                        emo_alpha=emo_alpha,
                        emo_vector=emo_vector,
                        use_emo_text=use_emo_text,
                        emo_text=emo_text,
                        use_random=use_random,
                        max_text_tokens_per_segment=max_text_tokens_per_segment,
                        **generation_kwargs
                    )
                    
                except Exception as retry_error:
                    retry_error_msg = str(retry_error)
                    
                    # Check if this is a tensor dimension mismatch error
                    if ("Sizes of tensors must match" in retry_error_msg or 
                        "Expected size" in retry_error_msg or
                        "dimension" in retry_error_msg.lower()):
                        
                        retry_count += 1
                        if retry_count >= max_retries:
                            # Final attempt: force text chunking with very small segments
                            print(f"   πŸ”„ Final attempt: forcing text chunking...")
                            try:
                                # Split text into very small chunks and process separately
                                words = text.split()
                                chunk_size = max(5, len(words) // 4)  # Very small chunks
                                text_chunks = []
                                
                                for i in range(0, len(words), chunk_size):
                                    chunk = " ".join(words[i:i + chunk_size])
                                    text_chunks.append(chunk)
                                
                                if len(text_chunks) > 1:
                                    print(f"   πŸ“ Processing {len(text_chunks)} micro-chunks...")
                                    chunk_audios = []
                                    
                                    for j, chunk in enumerate(text_chunks):
                                        print(f"   πŸ”„ Micro-chunk {j+1}/{len(text_chunks)}: {chunk[:20]}...")
                                        
                                        chunk_result = self.model.infer(
                                            spk_audio_prompt=reference_audio,
                                            text=chunk,
                                            output_path=None,
                                            emo_audio_prompt=emo_audio_prompt,
                                            emo_alpha=emo_alpha,
                                            emo_vector=emo_vector,
                                            use_emo_text=use_emo_text,
                                            emo_text=emo_text,
                                            use_random=use_random,
                                            max_text_tokens_per_segment=20,
                                            max_mel_tokens=300
                                        )
                                        
                                        if chunk_result is not None:
                                            if isinstance(chunk_result, tuple) and len(chunk_result) == 2:
                                                _, chunk_audio = chunk_result
                                                if isinstance(chunk_audio, torch.Tensor):
                                                    chunk_audio = chunk_audio.cpu().numpy()
                                                
                                                # Ensure chunk_audio is 1D
                                                if chunk_audio.ndim == 2:
                                                    if chunk_audio.shape[0] == 1:
                                                        chunk_audio = chunk_audio.flatten()
                                                    elif chunk_audio.shape[1] == 1:
                                                        chunk_audio = chunk_audio.flatten()
                                                    else:
                                                        # Take first channel if stereo
                                                        chunk_audio = chunk_audio[0] if chunk_audio.shape[0] < chunk_audio.shape[1] else chunk_audio[:, 0]
                                                
                                                chunk_audios.append(chunk_audio)
                                    
                                    if chunk_audios:
                                        try:
                                            # Ensure all chunks are 1D arrays before combining
                                            normalized_chunks = []
                                            for i, chunk_audio in enumerate(chunk_audios):
                                                if chunk_audio.ndim > 1:
                                                    chunk_audio = chunk_audio.flatten()
                                                
                                                # Ensure it's a valid audio array
                                                if len(chunk_audio) == 0:
                                                    print(f"   ⚠️ Skipping empty chunk {i+1}")
                                                    continue
                                                
                                                normalized_chunks.append(chunk_audio)
                                            
                                            if normalized_chunks:
                                                # Combine chunks with small pauses
                                                pause_samples = int(0.1 * self.sample_rate)
                                                pause_audio = np.zeros(pause_samples)
                                                
                                                combined_audio = normalized_chunks[0]
                                                for chunk_audio in normalized_chunks[1:]:
                                                    combined_audio = np.concatenate([combined_audio, pause_audio, chunk_audio])
                                                
                                                print(f"   βœ… Successfully combined {len(normalized_chunks)} micro-chunks")
                                                result = (self.sample_rate, combined_audio)
                                                break
                                            else:
                                                print(f"   ❌ No valid chunks to combine")
                                        
                                        except Exception as combine_error:
                                            print(f"   ❌ Error combining chunks: {combine_error}")
                                            # Continue to raise the original error
                                
                            except Exception as chunk_error:
                                print(f"   ❌ Chunking also failed: {chunk_error}")
                                raise retry_error  # Re-raise original error
                        else:
                            continue  # Try again with smaller parameters
                    else:
                        # Not a tensor dimension error, re-raise immediately
                        raise retry_error
            
            if result is None:
                return None, "❌ Failed to generate audio"
            
            # IndexTTS2 returns (sample_rate, audio_data) tuple
            if isinstance(result, tuple) and len(result) == 2:
                sample_rate, audio_data = result
                
                # Convert to numpy array if needed
                if isinstance(audio_data, torch.Tensor):
                    audio_data = audio_data.cpu().numpy()
                
                # Handle different audio data formats more robustly
                if audio_data.ndim == 2:
                    # If stereo or transposed, take first channel or transpose
                    if audio_data.shape[0] == 2:
                        audio_data = audio_data[0]  # Take first channel
                    elif audio_data.shape[1] == 1:
                        audio_data = audio_data.flatten()  # Flatten single channel
                    elif audio_data.shape[0] == 1:
                        audio_data = audio_data.flatten()  # Flatten single channel
                    else:
                        # Choose the dimension that makes more sense for audio
                        if audio_data.shape[0] < audio_data.shape[1]:
                            audio_data = audio_data[0]  # Take first row
                        else:
                            audio_data = audio_data[:, 0]  # Take first column
                elif audio_data.ndim > 2:
                    # Handle higher dimensional arrays by flattening
                    audio_data = audio_data.flatten()
                
                # Normalize audio to prevent clipping
                if len(audio_data) > 0:
                    max_val = np.max(np.abs(audio_data))
                    if max_val > 0:
                        audio_data = audio_data / max_val * 0.95
                
                print(f"βœ… Generated {len(audio_data)} samples at {sample_rate}Hz")
                return audio_data, "βœ… Speech generated successfully"
            else:
                return None, "❌ Unexpected audio format returned"
            
        except Exception as e:
            import traceback
            error_msg = str(e)
            
            # Provide specific guidance for common tensor dimension errors
            if "Sizes of tensors must match" in error_msg:
                print("πŸ” Tensor dimension mismatch detected - this is likely due to long text processing")
                print("πŸ’‘ Try reducing max_mel_tokens or splitting your text into shorter segments")
                error_msg = f"Tensor dimension mismatch (likely due to long text): {error_msg}"
            elif "Expected size" in error_msg and "but got size" in error_msg:
                print("πŸ” Tensor size mismatch detected")
                print("πŸ’‘ This may be resolved by using shorter text segments")
                error_msg = f"Tensor size mismatch: {error_msg}"
            
            traceback.print_exc()
            return None, f"❌ Error generating speech: {error_msg}"
    
    def get_model_info(self):
        """Get information about the loaded model"""
        if not self.is_model_loaded():
            return "❌ No model loaded"
        
        info = {
            'model_name': 'IndexTTS-2',
            'device': self.device,
            'sample_rate': self.sample_rate,
            'emotion_modes': list(self.emotion_modes.keys()),
            'supported_languages': ['English', 'Chinese'],
            'features': [
                'Zero-shot voice cloning',
                'Advanced emotion control',
                'Emotion-speaker disentanglement',
                'Duration control',
                'Multi-modal emotion input'
            ]
        }
        
        return info

# Model management functions
def init_indextts2():
    """Initialize IndexTTS2 model"""
    if not INDEXTTS2_AVAILABLE:
        return False, "❌ IndexTTS2 not available"
    
    try:
        handler = get_indextts2_handler()
        success, message = handler.initialize_model()
        return success, message
    except Exception as e:
        return False, f"❌ Error initializing IndexTTS2: {str(e)}"

def unload_indextts2():
    """Unload IndexTTS2 model"""
    try:
        handler = get_indextts2_handler()
        return handler.unload_model()
    except Exception as e:
        return f"⚠️ Error unloading IndexTTS2: {str(e)}"

def generate_indextts2_tts(
    text: str,
    reference_audio: Optional[str] = None,
    emotion_mode: str = "audio_reference",
    emotion_audio: Optional[str] = None,
    emotion_vectors: Optional[Dict[str, float]] = None,
    emotion_description: str = "",
    temperature: float = 0.8,
    top_p: float = 0.9,
    top_k: int = 50,
    repetition_penalty: float = 1.1,
    max_mel_tokens: int = 1500,
    seed: Optional[int] = None,
    use_random: bool = True,
    emo_alpha: float = 1.0,
    effects_settings: Optional[Dict] = None,
    audio_format: str = "wav",
    skip_file_saving: bool = False
) -> Tuple[Optional[Union[str, Tuple]], str]:
    """
    Generate TTS using IndexTTS2 with comprehensive emotion control
    
    Returns:
        Tuple of (audio_data, info_message)
        audio_data can be either file path (str) or (sample_rate, audio_array) tuple
    """
    try:
        handler = get_indextts2_handler()
        
        if not handler.is_model_loaded():
            return None, "❌ IndexTTS2 model not loaded. Please load the model first."
        
        # Generate speech
        audio_array, message = handler.generate_speech(
            text=text,
            reference_audio=reference_audio,
            emotion_mode=emotion_mode,
            emotion_audio=emotion_audio,
            emotion_vectors=emotion_vectors,
            emotion_description=emotion_description,
            temperature=temperature,
            top_p=top_p,
            top_k=top_k,
            repetition_penalty=repetition_penalty,
            max_mel_tokens=max_mel_tokens,
            seed=seed,
            use_random=use_random,
            emo_alpha=emo_alpha
        )
        
        if audio_array is None:
            return None, message
        
        # Apply audio effects if specified
        if effects_settings:
            try:
                # Import audio effects processing (assuming it exists in the main app)
                from launch import apply_audio_effects
                audio_array = apply_audio_effects(audio_array, handler.sample_rate, effects_settings)
            except ImportError:
                print("⚠️ Audio effects not available")
        
        if skip_file_saving:
            # Return audio data directly as tuple
            return (handler.sample_rate, audio_array), message
        
        # Save to file
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"indextts2_output_{timestamp}.{audio_format}"
        output_path = os.path.join("outputs", filename)
        
        # Ensure outputs directory exists
        os.makedirs("outputs", exist_ok=True)
        
        # Save audio file
        if audio_format.lower() == "wav":
            sf.write(output_path, audio_array, handler.sample_rate)
        elif audio_format.lower() == "mp3":
            # Convert to MP3 using pydub
            try:
                from pydub import AudioSegment
                import tempfile
                
                # Create temporary WAV file
                temp_wav = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
                temp_wav.close()
                
                try:
                    # Save as high-quality WAV first
                    sf.write(temp_wav.name, audio_array, handler.sample_rate)
                    
                    # Convert WAV to MP3 with high quality settings
                    audio_segment = AudioSegment.from_wav(temp_wav.name)
                    
                    # Export with high quality settings
                    audio_segment.export(
                        output_path,
                        format="mp3",
                        bitrate="320k",  # High quality
                        parameters=["-q:a", "0"]  # Highest quality
                    )
                    
                finally:
                    # Clean up temporary file
                    try:
                        os.unlink(temp_wav.name)
                    except:
                        pass
                        
            except ImportError:
                # Fallback to WAV if pydub not available
                print("⚠️ Warning: pydub not available for MP3 conversion, saving as WAV instead")
                wav_filename = f"indextts2_output_{timestamp}.wav"
                wav_output_path = os.path.join("outputs", wav_filename)
                sf.write(wav_output_path, audio_array, handler.sample_rate)
                output_path = wav_output_path
            except Exception as e:
                print(f"⚠️ MP3 conversion failed: {e}, saving as WAV instead")
                wav_filename = f"indextts2_output_{timestamp}.wav"
                wav_output_path = os.path.join("outputs", wav_filename)
                sf.write(wav_output_path, audio_array, handler.sample_rate)
                output_path = wav_output_path
        else:
            # For other formats, try soundfile (may not work for all formats)
            try:
                sf.write(output_path, audio_array, handler.sample_rate, format=audio_format)
            except Exception as e:
                print(f"⚠️ Format {audio_format} not supported, saving as WAV instead")
                wav_filename = f"indextts2_output_{timestamp}.wav"
                wav_output_path = os.path.join("outputs", wav_filename)
                sf.write(wav_output_path, audio_array, handler.sample_rate)
                output_path = wav_output_path
        
        # Calculate duration and create enhanced status message
        duration = len(audio_array) / handler.sample_rate
        filename = os.path.basename(output_path)
        
        status_message = f"βœ… IndexTTS2 synthesis completed\n"
        status_message += f"πŸ“ Saved as: {filename}\n"
        status_message += f"⏱️ Duration: {duration:.2f}s\n"
        status_message += f"πŸ“Š Sample Rate: {handler.sample_rate}Hz"
        
        return output_path, status_message
        
    except Exception as e:
        import traceback
        traceback.print_exc()
        return None, f"❌ Error in IndexTTS2 generation: {str(e)}"

def get_indextts2_status():
    """Get IndexTTS2 model status"""
    if not INDEXTTS2_AVAILABLE:
        return "❌ IndexTTS2 not available"
    
    handler = get_indextts2_handler()
    if handler.is_model_loaded():
        return "βœ… IndexTTS2 model loaded and ready"
    else:
        return "⚠️ IndexTTS2 model not loaded"

# Emotion presets for easy use
EMOTION_PRESETS = {
    'neutral': {'happy': 0.0, 'angry': 0.0, 'sad': 0.0, 'afraid': 0.0, 'disgusted': 0.0, 'melancholic': 0.0, 'surprised': 0.0, 'calm': 1.0},
    'happy': {'happy': 1.0, 'angry': 0.0, 'sad': 0.0, 'afraid': 0.0, 'disgusted': 0.0, 'melancholic': 0.0, 'surprised': 0.2, 'calm': 0.0},
    'sad': {'happy': 0.0, 'angry': 0.0, 'sad': 1.0, 'afraid': 0.0, 'disgusted': 0.0, 'melancholic': 0.8, 'surprised': 0.0, 'calm': 0.0},
    'angry': {'happy': 0.0, 'angry': 1.0, 'sad': 0.0, 'afraid': 0.0, 'disgusted': 0.3, 'melancholic': 0.0, 'surprised': 0.0, 'calm': 0.0},
    'excited': {'happy': 0.8, 'angry': 0.0, 'sad': 0.0, 'afraid': 0.0, 'disgusted': 0.0, 'melancholic': 0.0, 'surprised': 0.6, 'calm': 0.0},
    'melancholic': {'happy': 0.0, 'angry': 0.0, 'sad': 0.6, 'afraid': 0.0, 'disgusted': 0.0, 'melancholic': 1.0, 'surprised': 0.0, 'calm': 0.2},
    'surprised': {'happy': 0.2, 'angry': 0.0, 'sad': 0.0, 'afraid': 0.3, 'disgusted': 0.0, 'melancholic': 0.0, 'surprised': 1.0, 'calm': 0.0},
    'afraid': {'happy': 0.0, 'angry': 0.0, 'sad': 0.3, 'afraid': 1.0, 'disgusted': 0.0, 'melancholic': 0.0, 'surprised': 0.4, 'calm': 0.0}
}