File size: 58,775 Bytes
9bb75d7
 
 
 
 
 
 
 
122fe8e
9bb75d7
 
 
 
 
122fe8e
 
9bb75d7
 
 
122fe8e
9bb75d7
 
122fe8e
9bb75d7
122fe8e
9bb75d7
 
 
122fe8e
9bb75d7
 
 
 
 
 
 
122fe8e
 
9bb75d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122fe8e
9bb75d7
 
 
 
 
122fe8e
9bb75d7
 
 
122fe8e
9bb75d7
 
 
122fe8e
9bb75d7
 
 
 
122fe8e
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
122fe8e
 
9bb75d7
 
 
 
 
 
122fe8e
 
9bb75d7
 
 
122fe8e
9bb75d7
122fe8e
9bb75d7
122fe8e
 
 
 
 
 
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
122fe8e
 
9bb75d7
122fe8e
 
 
 
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
 
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
9bb75d7
 
122fe8e
9bb75d7
122fe8e
 
 
 
 
9bb75d7
122fe8e
 
9bb75d7
122fe8e
9bb75d7
122fe8e
9bb75d7
122fe8e
 
 
9bb75d7
122fe8e
9bb75d7
 
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
122fe8e
 
 
 
 
 
 
 
9bb75d7
122fe8e
9bb75d7
122fe8e
9bb75d7
122fe8e
9bb75d7
122fe8e
9bb75d7
122fe8e
 
9bb75d7
122fe8e
 
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
 
122fe8e
 
9bb75d7
122fe8e
 
 
9bb75d7
122fe8e
 
 
 
 
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
 
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
122fe8e
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
 
122fe8e
 
 
 
 
 
 
 
 
 
9bb75d7
 
 
122fe8e
9bb75d7
122fe8e
 
9bb75d7
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb76ccb
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1c24e5
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
 
 
122fe8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb75d7
 
 
 
122fe8e
 
 
 
 
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
import os
import time
import tempfile
import subprocess
import threading
import json
import base64
import io
import shutil
import random
import logging
from queue import Queue
from threading import Thread

import numpy as np
import matplotlib.pyplot as plt
import gradio as gr
import torch
import soundfile as sf
import librosa
import requests
from transformers import pipeline, AutoTokenizer, AutoModel
from scipy import signal

# Cấu hình logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Kiểm tra và tạo thư mục cho dữ liệu
os.makedirs("data", exist_ok=True)
os.makedirs("data/audio", exist_ok=True)
os.makedirs("data/reports", exist_ok=True)
os.makedirs("data/models", exist_ok=True)


class AsyncProcessor:
    """Xử lý các tác vụ nặng trong thread riêng để không làm đơ giao diện"""

    def __init__(self):
        self.task_queue = Queue()
        self.result_queue = Queue()
        self.running = True
        self.worker_thread = Thread(target=self._worker)
        self.worker_thread.daemon = True
        self.worker_thread.start()

    def _worker(self):
        while self.running:
            if not self.task_queue.empty():
                task_id, func, args, kwargs = self.task_queue.get()
                try:
                    result = func(*args, **kwargs)
                    self.result_queue.put((task_id, result, None))
                except Exception as e:
                    logger.error(f"Lỗi trong xử lý tác vụ {task_id}: {str(e)}")
                    self.result_queue.put((task_id, None, str(e)))
                self.task_queue.task_done()
            time.sleep(0.1)

    def add_task(self, task_id, func, *args, **kwargs):
        self.task_queue.put((task_id, func, args, kwargs))

    def get_result(self):
        if not self.result_queue.empty():
            return self.result_queue.get()
        return None

    def stop(self):
        self.running = False
        if self.worker_thread.is_alive():
            self.worker_thread.join(timeout=1)


class VietSpeechTrainer:
    def __init__(self):
        # Cấu hình từ biến môi trường hoặc file cấu hình
        self.config = self._load_config()

        # Khởi tạo bộ xử lý bất đồng bộ
        self.async_processor = AsyncProcessor()

        # Lưu trữ lịch sử
        self.session_history = []
        self.current_session_id = int(time.time())

        # Trạng thái hội thoại
        self.current_scenario = None
        self.current_prompt_index = 0

        # Khởi tạo các mô hình
        logger.info("Đang tải các mô hình...")
        self._initialize_models()

    def _load_config(self):
        """Tải cấu hình từ file hoặc biến môi trường"""
        config = {
            # STT config
            "stt_model": os.environ.get("STT_MODEL", "nguyenvulebinh/wav2vec2-base-vietnamese-250h"),
            "use_phowhisper": os.environ.get("USE_PHOWHISPER", "false").lower() == "true",
            # NLP config
            "use_phobert": os.environ.get("USE_PHOBERT", "false").lower() == "true",
            "use_vncorenlp": os.environ.get("USE_VNCORENLP", "false").lower() == "true",
            # LLM config
            "llm_provider": os.environ.get("LLM_PROVIDER", "none"),  # "openai", "gemini", "local", "none"
            "openai_api_key": os.environ.get("OPENAI_API_KEY", ""),
            "gemini_api_key": os.environ.get("GEMINI_API_KEY", ""),
            "local_llm_endpoint": os.environ.get("LOCAL_LLM_ENDPOINT", "http://localhost:8080/v1"),
            # TTS config
            "use_viettts": os.environ.get("USE_VIETTTS", "false").lower() == "true",
            "tts_api_url": os.environ.get("TTS_API_URL", ""),
            # Application settings
            "default_dialect": os.environ.get("DEFAULT_DIALECT", "Bắc"),
            "enable_pronunciation_eval": os.environ.get("ENABLE_PRONUNCIATION_EVAL", "false").lower() == "true",
            # Advanced settings
            "preprocess_audio": os.environ.get("PREPROCESS_AUDIO", "true").lower() == "true",
            "save_history": os.environ.get("SAVE_HISTORY", "true").lower() == "true",
        }

        # Nếu tồn tại file cấu hình, đọc thêm từ đó
        if os.path.exists("config.json"):
            try:
                with open("config.json", "r", encoding="utf-8") as f:
                    file_config = json.load(f)
                    config.update(file_config)
            except Exception as e:
                logger.error(f"Lỗi khi đọc file cấu hình: {e}")

        return config

    def _initialize_models(self):
        """Khởi tạo các mô hình AI cần thiết"""
        try:
            # 1. Khởi tạo mô hình STT
            if self.config["use_phowhisper"]:
                logger.info("Đang tải PhoWhisper...")
                self.stt_model = pipeline(
                    "automatic-speech-recognition",
                    model="vinai/PhoWhisper-small",
                    device=0 if torch.cuda.is_available() else -1,
                )
            else:
                logger.info(f"Đang tải mô hình STT: {self.config['stt_model']}")
                self.stt_model = pipeline(
                    "automatic-speech-recognition",
                    model=self.config["stt_model"],
                    device=0 if torch.cuda.is_available() else -1,
                )

            # 2. Khởi tạo PhoBERT và VnCoreNLP nếu được cấu hình
            self.phobert_model = None
            self.phobert_tokenizer = None
            self.rdrsegmenter = None

            if self.config["use_phobert"]:
                logger.info("Đang tải PhoBERT...")
                try:
                    self.phobert_tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base")
                    self.phobert_model = AutoModel.from_pretrained("vinai/phobert-base")
                except Exception as e:
                    logger.error(f"Lỗi khi tải PhoBERT: {e}")
                    self.config["use_phobert"] = False

            if self.config["use_vncorenlp"]:
                logger.info("Đang chuẩn bị VnCoreNLP...")
                try:
                    vncorenlp_path = self._setup_vncorenlp()
                    from py_vncorenlp import VnCoreNLP

                    self.rdrsegmenter = VnCoreNLP(vncorenlp_path, annotators="wseg", max_heap_size="-Xmx500m")
                except Exception as e:
                    logger.error(f"Lỗi khi chuẩn bị VnCoreNLP: {e}")
                    self.config["use_vncorenlp"] = False

            # 3. Chuẩn bị VietTTS nếu được cấu hình
            self.viettts_ready = False
            if self.config["use_viettts"]:
                logger.info("Đang chuẩn bị VietTTS...")
                try:
                    self.viettts_ready = self._setup_viettts()
                except Exception as e:
                    logger.error(f"Lỗi khi chuẩn bị VietTTS: {e}")
                    self.config["use_viettts"] = False

            logger.info("Khởi tạo mô hình hoàn tất")
        except Exception as e:
            logger.error(f"Lỗi khi khởi tạo mô hình: {e}")
            raise

    def _setup_vncorenlp(self):
        """Tải và cài đặt VnCoreNLP"""
        vncorenlp_dir = "data/models/vncorenlp"
        vncorenlp_jar = f"{vncorenlp_dir}/VnCoreNLP-1.1.1.jar"

        os.makedirs(vncorenlp_dir, exist_ok=True)

        if not os.path.exists(vncorenlp_jar):
            logger.info("Đang tải VnCoreNLP...")

            # Tải jar file
            url = "https://raw.githubusercontent.com/vncorenlp/VnCoreNLP/master/VnCoreNLP-1.1.1.jar"
            response = requests.get(url)
            with open(vncorenlp_jar, "wb") as f:
                f.write(response.content)

            # Tạo thư mục models
            os.makedirs(f"{vncorenlp_dir}/models/wordsegmenter", exist_ok=True)

            # Tải models
            for model_file in ["vi-vocab", "wordsegmenter.rdr"]:
                url = f"https://raw.githubusercontent.com/vncorenlp/VnCoreNLP/master/models/wordsegmenter/{model_file}"
                response = requests.get(url)
                with open(f"{vncorenlp_dir}/models/wordsegmenter/{model_file}", "wb") as f:
                    f.write(response.content)

        return vncorenlp_jar

    def _setup_viettts(self):
        """Cài đặt và chuẩn bị VietTTS"""
        viettts_dir = "data/models/viettts"

        # Nếu đã tải VietTTS rồi
        if os.path.exists(f"{viettts_dir}/pretrained"):
            return True

        # Clone repo nếu chưa có
        os.makedirs(viettts_dir, exist_ok=True)
        if not os.path.exists(f"{viettts_dir}/.git"):
            logger.info("Đang clone VietTTS repository...")
            result = subprocess.run(
                ["git", "clone", "https://github.com/NTT123/vietTTS.git", viettts_dir],
                capture_output=True,
                text=True,
            )
            if result.returncode != 0:
                logger.error(f"Lỗi khi clone VietTTS: {result.stderr}")
                return False

        # Cài đặt VietTTS
        logger.info("Đang cài đặt VietTTS...")
        os.chdir(viettts_dir)
        result = subprocess.run(["pip", "install", "-e", "."], capture_output=True, text=True)
        if result.returncode != 0:
            logger.error(f"Lỗi khi cài đặt VietTTS: {result.stderr}")
            os.chdir("..")
            return False

        # Tải mô hình pretrained
        if not os.path.exists("pretrained"):
            logger.info("Đang tải mô hình pretrained...")
            result = subprocess.run(["bash", "scripts/quick_start.sh"], capture_output=True, text=True)
            if result.returncode != 0:
                logger.error(f"Lỗi khi tải mô hình pretrained: {result.stderr}")
                os.chdir("..")
                return False

        os.chdir("..")
        return True

    def preprocess_audio(self, audio_path):
        """Tiền xử lý âm thanh để cải thiện chất lượng"""
        if not self.config["preprocess_audio"]:
            return audio_path

        try:
            # Đọc âm thanh
            y, sr = librosa.load(audio_path, sr=16000)

            # Chuẩn hóa âm lượng
            y_normalized = librosa.util.normalize(y)

            # Xử lý nhiễu (đơn giản)
            y_filtered = self._simple_noise_reduction(y_normalized)

            # Lưu file mới
            processed_path = audio_path.replace(".wav", "_processed.wav")
            sf.write(processed_path, y_filtered, sr)

            return processed_path
        except Exception as e:
            logger.error(f"Lỗi khi tiền xử lý âm thanh: {e}")
            return audio_path

    def _simple_noise_reduction(self, y):
        """Áp dụng lọc nhiễu đơn giản"""
        # Áp dụng high-pass filter để giảm nhiễu tần số thấp
        b, a = signal.butter(5, 80 / (16000 / 2), "highpass")
        y_filtered = signal.filtfilt(b, a, y)
        return y_filtered

    def transcribe_audio(self, audio_path):
        """Chuyển đổi âm thanh thành văn bản"""
        try:
            # Tiền xử lý audio nếu cần
            if self.config["preprocess_audio"]:
                audio_path = self.preprocess_audio(audio_path)

            # Thực hiện nhận dạng giọng nói
            result = self.stt_model(audio_path)

            # Kết quả có thể có cấu trúc khác nhau tùy mô hình
            if isinstance(result, dict) and "text" in result:
                text = result["text"]
            elif isinstance(result, list):
                text = " ".join([chunk.get("text", "") for chunk in result])
            else:
                text = str(result)

            return text
        except Exception as e:
            logger.error(f"Lỗi khi chuyển đổi âm thanh: {e}")
            return f"Lỗi: {str(e)}"

    def segment_text(self, text):
        """Tách từ văn bản tiếng Việt"""
        if not text or not text.strip():
            return text

        # Nếu có VnCoreNLP, sử dụng RDRSegmenter
        if self.config["use_vncorenlp"] and self.rdrsegmenter:
            try:
                sentences = self.rdrsegmenter.tokenize(text)
                segmented_text = " ".join([" ".join(sentence) for sentence in sentences])
                return segmented_text
            except Exception as e:
                logger.error(f"Lỗi khi tách từ với VnCoreNLP: {e}")

        # Nếu không có VnCoreNLP hoặc lỗi, trả về nguyên bản
        return text

    def analyze_text(self, transcript, dialect="Bắc"):
        """Phân tích văn bản và đưa ra gợi ý cải thiện"""
        if not transcript or not transcript.strip():
            return "Không nhận được văn bản để phân tích."

        # Tách từ
        segmented_text = self.segment_text(transcript)

        # Phân tích với LLM nếu có cấu hình
        llm_provider = self.config["llm_provider"]

        if llm_provider == "openai" and self.config["openai_api_key"]:
            return self._analyze_with_openai(transcript, segmented_text, dialect)
        elif llm_provider == "gemini" and self.config["gemini_api_key"]:
            return self._analyze_with_gemini(transcript, segmented_text, dialect)
        elif llm_provider == "local" and self.config["local_llm_endpoint"]:
            return self._analyze_with_local_llm(transcript, segmented_text, dialect)
        else:
            # Sử dụng phân tích dựa trên quy tắc
            return self._rule_based_analysis(transcript, segmented_text, dialect)

    def _analyze_with_openai(self, transcript, segmented_text, dialect):
        """Phân tích văn bản sử dụng OpenAI API"""
        try:
            headers = {
                "Authorization": f"Bearer {self.config['openai_api_key']}",
                "Content-Type": "application/json",
            }

            # Tạo prompt
            prompt = self._create_analysis_prompt(transcript, segmented_text, dialect)

            # Gọi API
            response = requests.post(
                "https://api.openai.com/v1/chat/completions",
                headers=headers,
                json={
                    "model": "gpt-3.5-turbo",
                    "messages": [
                        {
                            "role": "system",
                            "content": "Bạn là trợ lý dạy tiếng Việt, chuyên phân tích và đưa ra gợi ý cải thiện kỹ năng nói.",
                        },
                        {"role": "user", "content": prompt},
                    ],
                    "temperature": 0.5,
                    "max_tokens": 800,
                },
            )

            if response.status_code == 200:
                result = response.json()
                analysis = result["choices"][0]["message"]["content"]
                return analysis
            else:
                logger.error(f"Lỗi khi gọi OpenAI API: {response.text}")
                return self._rule_based_analysis(transcript, segmented_text, dialect)

        except Exception as e:
            logger.error(f"Lỗi khi phân tích với OpenAI: {e}")
            return self._rule_based_analysis(transcript, segmented_text, dialect)

    def _analyze_with_gemini(self, transcript, segmented_text, dialect):
        """Phân tích văn bản sử dụng Gemini API"""
        try:
            headers = {
                "Content-Type": "application/json",
            }

            # Tạo prompt
            prompt = self._create_analysis_prompt(transcript, segmented_text, dialect)

            # Endpoint Gemini
            url = (
                f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent?key={self.config['gemini_api_key']}"
            )

            # Gọi API
            response = requests.post(
                url,
                headers=headers,
                json={
                    "contents": [
                        {
                            "role": "user",
                            "parts": [{"text": prompt}],
                        }
                    ],
                    "generationConfig": {
                        "temperature": 0.4,
                        "maxOutputTokens": 800,
                    },
                },
            )

            if response.status_code == 200:
                result = response.json()
                if "candidates" in result and len(result["candidates"]) > 0:
                    analysis = result["candidates"][0]["content"]["parts"][0]["text"]
                    return analysis
                else:
                    logger.error(f"Định dạng phản hồi Gemini không như mong đợi: {result}")
                    return self._rule_based_analysis(transcript, segmented_text, dialect)
            else:
                logger.error(f"Lỗi khi gọi Gemini API: {response.text}")
                return self._rule_based_analysis(transcript, segmented_text, dialect)

        except Exception as e:
            logger.error(f"Lỗi khi phân tích với Gemini: {e}")
            return self._rule_based_analysis(transcript, segmented_text, dialect)

    def _analyze_with_local_llm(self, transcript, segmented_text, dialect):
        """Phân tích văn bản sử dụng LLM mã nguồn mở local"""
        try:
            headers = {
                "Content-Type": "application/json",
            }

            # Tạo prompt
            prompt = self._create_analysis_prompt(transcript, segmented_text, dialect)

            # Endpoint local LLM
            url = f"{self.config['local_llm_endpoint']}/chat/completions"

            # Gọi API
            response = requests.post(
                url,
                headers=headers,
                json={
                    "model": "local-model",
                    "messages": [
                        {
                            "role": "system",
                            "content": "Bạn là trợ lý dạy tiếng Việt, chuyên phân tích và đưa ra gợi ý cải thiện kỹ năng nói.",
                        },
                        {"role": "user", "content": prompt},
                    ],
                    "temperature": 0.5,
                    "max_tokens": 800,
                },
            )

            if response.status_code == 200:
                result = response.json()
                analysis = result["choices"][0]["message"]["content"]
                return analysis
            else:
                logger.error(f"Lỗi khi gọi Local LLM API: {response.text}")
                return self._rule_based_analysis(transcript, segmented_text, dialect)

        except Exception as e:
            logger.error(f"Lỗi khi phân tích với Local LLM: {e}")
            return self._rule_based_analysis(transcript, segmented_text, dialect)

    def _create_analysis_prompt(self, transcript, segmented_text, dialect):
        """Tạo prompt cho việc phân tích văn bản"""
        return f"""Bạn là trợ lý dạy tiếng Việt. Hãy phân tích câu nói sau và đưa ra gợi ý cải thiện:

Câu nói: "{transcript}"
Câu đã tách từ: "{segmented_text}"
Phương ngữ: {dialect}

Hãy phân tích theo các khía cạnh sau:
1. Ngữ pháp: Cấu trúc câu, thì, cách sử dụng từ nối
2. Từ vựng: Từ không phù hợp, từ dùng không đúng ngữ cảnh, từ viết tắt
3. Phong cách: Mức độ trang trọng, thân mật, văn phong
4. Tính mạch lạc: Tính rõ ràng, dễ hiểu của câu

Đưa ra gợi ý cụ thể để cải thiện cách diễn đạt.
Viết câu mẫu cải thiện.

Định dạng phản hồi:
- Sử dụng Markdown
- Đặt các vấn đề vào danh sách có đánh dấu
- Đưa ra câu mẫu cải thiện ở cuối"""

    def _rule_based_analysis(self, transcript, segmented_text, dialect):
        """Phân tích dựa trên quy tắc đơn giản"""
        # Phân tích cơ bản khi không có LLM
        words = transcript.split()
        analysis = []

        # 1. Phân tích độ dài câu
        if len(words) < 3:
            analysis.append("⚠️ **Câu quá ngắn**: Thử mở rộng ý với các chi tiết hơn.")
        elif len(words) > 20:
            analysis.append("⚠️ **Câu dài**: Cân nhắc chia thành các câu ngắn hơn.")
        else:
            analysis.append("✅ **Độ dài câu**: Phù hợp.")

        # 2. Kiểm tra từ ngữ phổ biến
        common_errors = {
            "ko": "không",
            "k": "không",
            "bik": "biết",
            "j": "gì",
            "z": "vậy",
            "ntn": "như thế nào",
            "dc": "được",
            "vs": "với",
            "nc": "nước",
            "ng": "người",
            "trc": "trước",
            "sao": "sao",
        }

        errors_found = []
        for word in words:
            word_lower = word.lower()
            if word_lower in common_errors:
                errors_found.append(f"'{word}' → '{common_errors[word_lower]}'")

        if errors_found:
            analysis.append(f"⚠️ **Từ viết tắt**: Nên dùng từ đầy đủ thay vì: {', '.join(errors_found)}")
        else:
            analysis.append("✅ **Sử dụng từ**: Không phát hiện từ viết tắt phổ biến.")

        # 3. Tính trùng lặp
        word_counts = {}
        for word in words:
            word_lower = word.lower()
            if len(word_lower) > 1:  # Bỏ qua các từ ngắn
                word_counts[word_lower] = word_counts.get(word_lower, 0) + 1

        duplicates = [w for w, c in word_counts.items() if c > 2]
        if duplicates:
            analysis.append(
                f"⚠️ **Trùng lặp từ**: Từ '{', '.join(duplicates)}' lặp lại nhiều lần. Hãy thử dùng từ đồng nghĩa."
            )

        # 4. Gợi ý cải thiện phụ thuộc phương ngữ
        if dialect == "Bắc":
            suggestions = [
                "Phát âm rõ ràng phụ âm cuối, tránh nuốt âm",
                "Chú ý tới thanh điệu, đặc biệt là thanh hỏi và thanh ngã",
                "Phát âm 'r' và 'gi' phân biệt theo phong cách Bắc Bộ",
            ]
        elif dialect == "Trung":
            suggestions = [
                "Chú ý đến nhịp điệu đặc trưng của giọng Trung",
                "Phát âm rõ phụ âm đầu, đặc biệt là 'tr' và 'ch'",
                "Kéo dài nguyên âm một cách tự nhiên",
            ]
        else:  # Nam
            suggestions = [
                "Giữ nguyên âm ổn định, tránh biến đổi nguyên âm",
                "Phân biệt rõ 'v' và 'gi' theo phong cách Nam Bộ",
                "Tránh nhấn quá mạnh vào các phụ âm cuối",
            ]

        # 5. Câu mẫu cải thiện
        improved = transcript
        for word, replacement in common_errors.items():
            improved = improved.replace(f" {word} ", f" {replacement} ")

        # Ghép tất cả phân tích lại
        full_analysis = "### Phân tích\n\n" + "\n\n".join(analysis)
        full_analysis += "\n\n### Gợi ý cải thiện\n\n" + "\n".join([f"- {s}" for s in suggestions])
        full_analysis += f"\n\n### Câu gợi ý\n\n{improved}"
        return full_analysis

    def text_to_speech(self, text, dialect="Bắc"):
        """Chuyển văn bản thành giọng nói"""
        # Nếu có API TTS
        if self.config["tts_api_url"]:
            try:
                # Gọi API TTS
                response = requests.post(
                    self.config["tts_api_url"], json={"text": text, "dialect": dialect.lower()}
                )

                if response.status_code == 200:
                    # Lưu audio vào file tạm
                    output_file = f"data/audio/tts_{int(time.time())}.wav"
                    with open(output_file, "wb") as f:
                        f.write(response.content)
                    return output_file
                else:
                    logger.error(f"Lỗi khi gọi API TTS: {response.text}")
                    return None
            except Exception as e:
                logger.error(f"Lỗi khi gọi API TTS: {e}")
                return None

        # Nếu có VietTTS
        elif self.config["use_viettts"] and self.viettts_ready:
            try:
                # Chuẩn bị VietTTS
                viettts_dir = "data/models/viettts"

                # Tạo file tạm thời để lưu văn bản
                with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt", encoding="utf-8") as f:
                    f.write(text)
                    text_file = f.name

                # Tạo tên file output
                output_file = f"data/audio/tts_{int(time.time())}.wav"

                # Lưu thư mục hiện tại
                current_dir = os.getcwd()

                try:
                    # Đổi thư mục làm việc sang viettts_dir
                    os.chdir(viettts_dir)

                    # Gọi VietTTS để tạo giọng nói
                    cmd = [
                        "python",
                        "-m",
                        "vietTTS.synthesizer",
                        "--lexicon-file=./train_data/lexicon.txt",
                        f"--text-file={text_file}",
                        f"--output={os.path.join(current_dir, output_file)}",
                    ]

                    result = subprocess.run(cmd, capture_output=True, text=True)

                    # Quay lại thư mục ban đầu
                    os.chdir(current_dir)

                    if result.returncode != 0:
                        logger.error(f"Lỗi khi chạy VietTTS: {result.stderr}")
                        return None

                    # Xóa file tạm
                    os.unlink(text_file)
                    return output_file

                except Exception as e:
                    # Đảm bảo quay lại thư mục ban đầu
                    os.chdir(current_dir)
                    logger.error(f"Lỗi khi sử dụng VietTTS: {e}")
                    os.unlink(text_file)
                    return None

            except Exception as e:
                logger.error(f"Lỗi khi tạo file tạm: {e}")
                return None

        return None

    def process_recording(self, audio_path, dialect="Bắc"):
        """Xử lý bản ghi âm: chuyển sang văn bản và phân tích"""
        if audio_path is None:
            return "Không có âm thanh được ghi.", "", None

        # 1. Chuyển đổi âm thanh thành văn bản
        transcript = self.transcribe_audio(audio_path)

        # 2. Phân tích văn bản
        analysis = self.analyze_text(transcript, dialect)

        # 3. Tạo mẫu phát âm (nếu có)
        sample_audio = self.text_to_speech(transcript, dialect)

        # 4. Lưu vào lịch sử phiên
        entry = {
            "id": len(self.session_history) + 1,
            "time": time.strftime("%Y-%m-%d %H:%M:%S"),
            "transcript": transcript,
            "analysis": analysis,
            "audio_path": audio_path,
            "sample_audio": sample_audio,
            "dialect": dialect,
        }
        self.session_history.append(entry)

        # 5. Lưu lịch sử nếu được cấu hình
        if self.config["save_history"]:
            self._save_session_history()

        return transcript, analysis, sample_audio

    def evaluate_pronunciation(self, original_audio, text, dialect="Bắc"):
        """Đánh giá chất lượng phát âm bằng cách so sánh với mẫu chuẩn"""
        if not self.config["enable_pronunciation_eval"]:
            return {"score": 0, "feedback": "Tính năng đánh giá phát âm không được bật"}

        try:
            # 1. Tạo phát âm mẫu từ text
            sample_audio = self.text_to_speech(text, dialect)
            if not sample_audio:
                return {"score": 0, "feedback": "Không thể tạo mẫu phát âm chuẩn"}

            # 2. Trích xuất đặc trưng từ cả hai file âm thanh
            # Trích xuất MFCCs (Mel-frequency cepstral coefficients)
            def extract_mfcc(audio_file):
                y, sr = librosa.load(audio_file, sr=16000)
                mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
                return mfccs

            original_mfccs = extract_mfcc(original_audio)
            sample_mfccs = extract_mfcc(sample_audio)

            # 3. So sánh bằng DTW (Dynamic Time Warping)
            # Đơn giản hóa: tính khoảng cách Euclidean giữa hai vector MFCC
            # Trong thực tế, nên dùng DTW hoặc thuật toán phức tạp hơn
            def dtw_distance(mfcc1, mfcc2):
                # Chỉ lấy một phần của các frames để so sánh
                min_len = min(mfcc1.shape[1], mfcc2.shape[1])
                dist = np.linalg.norm(mfcc1[:, :min_len] - mfcc2[:, :min_len])
                return dist

            distance = dtw_distance(original_mfccs, sample_mfccs)

            # 4. Tính điểm dựa trên khoảng cách
            max_distance = 100  # Giá trị tối đa để chuẩn hóa
            normalized_distance = min(distance, max_distance) / max_distance
            pronunciation_score = 100 * (1 - normalized_distance)

            # 5. Phản hồi
            feedback = self._get_pronunciation_feedback(pronunciation_score, dialect)

            evaluation = {
                "score": round(pronunciation_score, 2),
                "sample_audio": sample_audio,
                "feedback": feedback,
            }
            return evaluation

        except Exception as e:
            logger.error(f"Lỗi khi đánh giá phát âm: {e}")
            return {"score": 0, "feedback": f"Lỗi khi đánh giá: {str(e)}"}

    def _get_pronunciation_feedback(self, score, dialect):
        """Đưa ra phản hồi dựa trên điểm phát âm"""
        prefix = f"**Phương ngữ {dialect}**: "

        if score >= 90:
            return prefix + "Phát âm rất tốt! Gần như giống với mẫu chuẩn."
        elif score >= 80:
            return prefix + "Phát âm tốt. Có một vài điểm nhỏ cần cải thiện."
        elif score >= 70:
            return prefix + "Phát âm khá tốt. Hãy chú ý đến ngữ điệu và các phụ âm cuối."
        elif score >= 60:
            return prefix + "Phát âm trung bình. Cần luyện tập thêm về nhịp điệu và độ rõ ràng."
        else:
            return prefix + "Cần luyện tập nhiều hơn. Hãy tập trung vào từng âm tiết và chú ý các dấu."

    def _save_session_history(self):
        """Lưu lịch sử phiên hiện tại vào file"""
        try:
            history_file = f"data/reports/session_{self.current_session_id}.json"

            # Chuyển đổi thành JSON serializable
            serializable_history = []
            for entry in self.session_history:
                # Tạo bản sao để không thay đổi bản gốc
                entry_copy = entry.copy()

                # Chỉ lưu đường dẫn, không lưu nội dung file
                if "audio_path" in entry_copy and entry_copy["audio_path"]:
                    entry_copy["audio_path"] = os.path.basename(entry_copy["audio_path"])

                if "sample_audio" in entry_copy and entry_copy["sample_audio"]:
                    entry_copy["sample_audio"] = os.path.basename(entry_copy["sample_audio"])

                serializable_history.append(entry_copy)

            with open(history_file, "w", encoding="utf-8") as f:
                json.dump(
                    {
                        "session_id": self.current_session_id,
                        "start_time": time.strftime(
                            "%Y-%m-%d %H:%M:%S", time.localtime(self.current_session_id)
                        ),
                        "entries": serializable_history,
                    },
                    f,
                    ensure_ascii=False,
                    indent=2,
                )

        except Exception as e:
            logger.error(f"Lỗi khi lưu lịch sử phiên: {e}")

    def export_session(self, format="markdown"):
        """Xuất báo cáo buổi luyện tập"""
        if not self.session_history:
            return None

        try:
            if format == "markdown":
                return self._export_markdown()
            elif format == "html":
                return self._export_html()
            else:
                return self._export_markdown()  # Mặc định là markdown
        except Exception as e:
            logger.error(f"Lỗi khi xuất báo cáo: {e}")
            return None

    def _export_markdown(self):
        """Xuất báo cáo dạng Markdown"""
        # Tạo nội dung báo cáo
        content = "# BÁO CÁO LUYỆN NÓI TIẾNG VIỆT\n\n"
        content += f"Ngày: {time.strftime('%Y-%m-%d')}\n"
        content += f"Tổng số câu: {len(self.session_history)}\n\n"

        for entry in self.session_history:
            content += f"## Câu {entry['id']} ({entry['time']})\n\n"
            content += f"**Phương ngữ:** {entry['dialect']}\n\n"
            content += f"**Bạn nói:** {entry['transcript']}\n\n"
            content += f"**Phân tích:**\n{entry['analysis']}\n\n"
            content += "---\n\n"

        # Thêm thống kê tổng quát
        content += "## Thống kê tổng quát\n\n"

        # Tính số từ trung bình mỗi câu
        avg_words = sum(len(entry["transcript"].split()) for entry in self.session_history) / len(
            self.session_history
        )
        content += f"- Số từ trung bình mỗi câu: {avg_words:.2f}\n"

        # Lưu báo cáo
        filename = f"data/reports/bao_cao_{time.strftime('%Y%m%d_%H%M%S')}.md"
        with open(filename, "w", encoding="utf-8") as f:
            f.write(content)
        return filename

    def _export_html(self):
        """Xuất báo cáo dạng HTML"""
        # Tạo nội dung HTML
        html = """<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Báo cáo luyện nói tiếng Việt</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; }
        h1, h2 { color: #2c3e50; }
        .entry { margin-bottom: 30px; border-bottom: 1px solid #eee; padding-bottom: 20px; }
        .transcript { background-color: #f8f9fa; padding: 10px; border-left: 4px solid #4CAF50; }
        .analysis { margin-top: 10px; }
        .meta { color: #7f8c8d; font-size: 0.9em; }
        .dialect { display: inline-block; background-color: #e74c3c; color: white; padding: 2px 6px; border-radius: 3px; font-size: 0.8em; }
    </style>
</head>
<body>
    <h1>Báo cáo luyện nói tiếng Việt</h1>
    <p>Ngày: %s</p>
    <p>Tổng số câu: %d</p>
    
    <div class="entries">
""" % (
            time.strftime("%Y-%m-%d"),
            len(self.session_history),
        )

        for entry in self.session_history:
            html += f"""
        <div class="entry">
            <h2>Câu {entry['id']}</h2>
            <div class="meta">Thời gian: {entry['time']} | <span class="dialect">{entry['dialect']}</span></div>
            <div class="transcript">{entry['transcript']}</div>
            <div class="analysis">{entry['analysis']}</div>
        </div>
"""

        # Thêm thống kê
        avg_words = sum(len(entry["transcript"].split()) for entry in self.session_history) / len(
            self.session_history
        )

        html += f"""
    </div>
    
    <h2>Thống kê tổng quát</h2>
    <ul>
        <li>Số từ trung bình mỗi câu: {avg_words:.2f}</li>
    </ul>
</body>
</html>
"""

        # Lưu báo cáo
        filename = f"data/reports/bao_cao_{time.strftime('%Y%m%d_%H%M%S')}.html"
        with open(filename, "w", encoding="utf-8") as f:
            f.write(html)
        return filename

    def create_conversation_scenario(self):
        """Tạo một tình huống hội thoại thực tế cho người dùng luyện tập"""
        # Danh sách các tình huống
        scenarios = [
            {
                "title": "Chào hỏi và giới thiệu bản thân",
                "description": "Bạn gặp một người mới tại một sự kiện networking.",
                "prompts": [
                    "Chào bạn, mình là người tổ chức sự kiện. Bạn tên gì và đang làm việc ở đâu?",
                    "Bạn có thể chia sẻ một chút về công việc của mình được không?",
                    "Bạn quan tâm đến lĩnh vực nào trong sự kiện này?",
                ],
            },
            {
                "title": "Đặt món tại nhà hàng",
                "description": "Bạn đang ở một nhà hàng và muốn gọi món.",
                "prompts": [
                    "Xin chào, tôi có thể giúp gì cho bạn?",
                    "Bạn muốn đặt món gì? Hôm nay chúng tôi có món đặc biệt là cá hồi nướng.",
                    "Bạn muốn uống thêm gì không? Chúng tôi có nhiều loại nước và rượu vang.",
                ],
            },
            {
                "title": "Phỏng vấn công việc",
                "description": "Bạn đang trong một cuộc phỏng vấn xin việc.",
                "prompts": [
                    "Chào bạn, bạn có thể giới thiệu ngắn gọn về bản thân được không?",
                    "Tại sao bạn muốn làm việc tại công ty chúng tôi?",
                    "Bạn có kinh nghiệm gì liên quan đến vị trí này không?",
                ],
            },
            {
                "title": "Thuyết trình ý tưởng",
                "description": "Bạn đang thuyết trình một ý tưởng mới cho đồng nghiệp.",
                "prompts": [
                    "Hãy giới thiệu về ý tưởng của bạn một cách ngắn gọn.",
                    "Ý tưởng này giải quyết vấn đề gì và đối tượng hướng đến là ai?",
                    "Bạn cần những nguồn lực gì để thực hiện ý tưởng này?",
                ],
            },
            {
                "title": "Hỏi đường",
                "description": "Bạn đang du lịch và cần hỏi đường đến một địa điểm.",
                "prompts": [
                    "Xin chào, tôi có thể giúp gì cho bạn?",
                    "Bạn đang tìm đường đến đâu?",
                    "Bạn muốn đi bằng phương tiện gì? Đi bộ, xe buýt hay taxi?",
                ],
            },
        ]

        # Chọn ngẫu nhiên một tình huống
        scenario = random.choice(scenarios)
        return scenario

    def track_progress(self):
        """Theo dõi tiến độ của người dùng qua thời gian"""
        if not self.session_history:
            return {
                "message": "Chưa có dữ liệu để theo dõi tiến độ",
                "statistics": {},
                "charts": {},
            }

        # Tính toán các chỉ số tiến triển
        total_entries = len(self.session_history)

        # Phân tích độ dài câu qua thời gian
        sentence_lengths = [len(entry["transcript"].split()) for entry in self.session_history]
        avg_length = sum(sentence_lengths) / total_entries

        # Tính số từ độc đáo sử dụng
        all_words = []
        for entry in self.session_history:
            all_words.extend(entry["transcript"].lower().split())

        unique_words = set(all_words)
        vocabulary_size = len(unique_words)

        # Tạo báo cáo tiến độ
        progress_report = {
            "message": "Dữ liệu theo dõi tiến độ",
            "statistics": {
                "total_entries": total_entries,
                "avg_sentence_length": round(avg_length, 2),
                "vocabulary_size": vocabulary_size,
                "improvement_score": min(100, int(total_entries * 5 + vocabulary_size / 10)),
            },
            "charts": self._generate_progress_charts(),
        }
        return progress_report

    def _generate_progress_charts(self):
        """Tạo biểu đồ trực quan hóa tiến độ"""
        # Dữ liệu cho biểu đồ
        sentence_ids = [entry["id"] for entry in self.session_history]
        sentence_lengths = [len(entry["transcript"].split()) for entry in self.session_history]

        # Tạo biểu đồ độ dài câu
        plt.figure(figsize=(10, 5))
        plt.plot(sentence_ids, sentence_lengths, marker="o", linestyle="-")
        plt.title("Độ dài câu qua thời gian")
        plt.xlabel("Số thứ tự câu")
        plt.ylabel("Số từ trong câu")
        plt.grid(True, linestyle="--", alpha=0.7)

        # Lưu biểu đồ vào buffer
        length_chart_buf = io.BytesIO()
        plt.savefig(length_chart_buf, format="png", dpi=100)
        length_chart_buf.seek(0)
        length_chart_b64 = base64.b64encode(length_chart_buf.read()).decode("utf-8")
        plt.close()

        # Biểu đồ phân bố độ dài câu
        plt.figure(figsize=(8, 4))
        plt.hist(sentence_lengths, bins=10, alpha=0.7)
        plt.title("Phân bố độ dài câu")
        plt.xlabel("Số từ trong câu")
        plt.ylabel("Tần suất")
        plt.grid(True, linestyle="--", alpha=0.7)

        dist_chart_buf = io.BytesIO()
        plt.savefig(dist_chart_buf, format="png", dpi=100)
        dist_chart_buf.seek(0)
        dist_chart_b64 = base64.b64encode(dist_chart_buf.read()).decode("utf-8")
        plt.close()

        return {
            "length_chart": f"data:image/png;base64,{length_chart_b64}",
            "distribution_chart": f"data:image/png;base64,{dist_chart_b64}",
        }

    def clean_up(self):
        """Dọn dẹp tài nguyên trước khi thoát"""
        # Lưu lịch sử phiên cuối cùng
        if self.config["save_history"] and self.session_history:
            self._save_session_history()

        # Dừng bộ xử lý bất đồng bộ
        if hasattr(self, "async_processor"):
            self.async_processor.stop()

        # Giải phóng bộ nhớ GPU nếu cần
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

        logger.info("Đã dọn dẹp tài nguyên")


# Tạo giao diện Gradio
def create_demo():
    try:
        trainer = VietSpeechTrainer()

        with gr.Blocks(title="Công cụ Luyện Nói Tiếng Việt", theme=gr.themes.Soft(primary_hue="blue")) as demo:
            # Header
            with gr.Row(variant="panel"):
                with gr.Column(scale=6):
                    gr.Markdown(
                        """
                    # 🎤 Công cụ Luyện Nói Tiếng Việt AI
                    ### Nâng cao kỹ năng giao tiếp tiếng Việt với trợ lý AI thông minh
                    """
                    )
                with gr.Column(scale=1):
                    dialect_selector = gr.Radio(["Bắc", "Trung", "Nam"], label="Phương ngữ tiếng Việt", value="Bắc")

            # Tabs for different functions
            with gr.Tabs() as tabs:
                # Tab 1: Luyện phát âm
                with gr.TabItem("Luyện phát âm", id=0):
                    with gr.Row():
                        with gr.Column(scale=2):
                            # Khu vực đầu vào
                            audio_input = gr.Audio(
                                label="📝 Giọng nói của bạn",
                                type="filepath",
                            )

                            with gr.Row():
                                submit_btn = gr.Button("🔍 Phân tích", variant="primary")
                                clear_btn = gr.Button("🗑️ Xóa")

                            gr.Markdown(
                                """
                            ### Chủ đề gợi ý:
                            - 🎯 Giới thiệu bản thân
                            - 🎯 Kể về một trải nghiệm thú vị
                            - 🎯 Mô tả một địa điểm yêu thích
                            - 🎯 Trình bày quan điểm về một vấn đề
                            """
                            )
                        with gr.Column(scale=3):
                            # Khu vực kết quả
                            transcript_output = gr.Textbox(
                                label="Nội dung bạn vừa nói",
                                placeholder="Nội dung sẽ hiển thị ở đây...",
                                lines=3,
                            )
                            analysis_output = gr.Markdown(label="Phân tích và gợi ý cải thiện")

                            with gr.Row():
                                with gr.Column(scale=1):
                                    gr.Markdown("#### Phát âm của bạn:")
                                    playback_audio = gr.Audio(label="", type="filepath")

                                with gr.Column(scale=1):
                                    gr.Markdown("#### Phát âm mẫu:")
                                    sample_audio = gr.Audio(label="", type="filepath")

                    # Lịch sử phiên
                    with gr.Accordion("Lịch sử phiên luyện tập", open=False):
                        history_md = gr.Markdown("*Chưa có lịch sử luyện tập*")

                # Tab 2: Hội thoại
                with gr.TabItem("Hội thoại", id=1):
                    scenario_title = gr.Markdown("## Tình huống hội thoại")
                    scenario_desc = gr.Markdown("*Nhấn Tạo tình huống để bắt đầu*")
                    prompt_text = gr.Markdown("*Câu hỏi/lời thoại sẽ hiển thị ở đây*")

                    conversation_audio = gr.Audio("microphone", label="Trả lời của bạn", type="filepath")
                    conversation_transcript = gr.Textbox(label="Văn bản của bạn", lines=2)
                    conversation_feedback = gr.Markdown(label="Phản hồi")

                    with gr.Row():
                        new_scenario_btn = gr.Button("🔄 Tạo tình huống mới")
                        next_prompt_btn = gr.Button("➡️ Câu tiếp theo")
                        analyze_response_btn = gr.Button("🔍 Phân tích câu trả lời")

                # Tab 3: Tiến độ
                with gr.TabItem("Tiến độ", id=2):
                    refresh_stats_btn = gr.Button("🔄 Cập nhật thống kê")

                    with gr.Row():
                        with gr.Column():
                            stats_output = gr.JSON(label="Thống kê", value={"message": "Nhấn Cập nhật thống kê để xem"})

                    with gr.Row():
                        with gr.Column():
                            length_chart = gr.Image(label="Độ dài câu qua thời gian", show_download_button=False)
                        with gr.Column():
                            dist_chart = gr.Image(label="Phân bố độ dài câu", show_download_button=False)

                # Tab 4: Xuất báo cáo
                with gr.TabItem("Xuất báo cáo", id=3):
                    with gr.Row():
                        export_md_btn = gr.Button("📝 Xuất báo cáo Markdown")
                        export_html_btn = gr.Button("🌐 Xuất báo cáo HTML")

                    export_output = gr.File(label="Tải báo cáo")

                # Tab 5: Thông tin
                with gr.TabItem("Thông tin", id=4):
                    gr.Markdown(
                        """
                    ## Về công cụ luyện nói tiếng Việt

                    Công cụ này sử dụng các mô hình trí tuệ nhân tạo tiên tiến để giúp người dùng cải thiện kỹ năng nói tiếng Việt.

                    ### Công nghệ sử dụng

                    - **Speech-to-Text**: Chuyển đổi giọng nói thành văn bản với độ chính xác cao
                       - PhoWhisper hoặc wav2vec2-Vietnamese
                    - **Phân tích ngôn ngữ**: Phân tích cấu trúc câu, phát hiện lỗi
                       - PhoBERT kết hợp với LLM (Gemini/OpenAI/Local)
                    - **Text-to-Speech**: Tạo mẫu phát âm chuẩn
                       - VietTTS hoặc API TTS
                    
                    ### Tính năng chính

                    - Nhận dạng và phân tích giọng nói tiếng Việt
                    - Phát hiện lỗi ngữ pháp, từ vựng và cách diễn đạt
                    - Phát âm mẫu chuẩn với VietTTS
                    - Lưu trữ và theo dõi tiến độ
                    - Gợi ý cải thiện cá nhân hóa
                    - Hỗ trợ nhiều phương ngữ (Bắc, Trung, Nam)
                    - Luyện tập hội thoại với tình huống thực tế
                    
                    ### Mô hình AI sử dụng
                    
                    - **PhoWhisper**: Mô hình nhận dạng giọng nói tiếng Việt tiên tiến nhất (2024), được phát triển bởi VinAI Research.
                    - **PhoBERT**: Mô hình hiểu ngôn ngữ tự nhiên tiếng Việt SOTA, cũng được phát triển bởi VinAI Research.
                    - **VietTTS**: Mô hình chuyển văn bản tiếng Việt thành giọng nói.
                    
                    ### Hướng dẫn sử dụng
                    
                    1. Chọn tab "Luyện phát âm" hoặc "Hội thoại"
                    2. Thu âm giọng nói của bạn
                    3. Nhận phản hồi và gợi ý cải thiện từ AI
                    4. Theo dõi tiến độ trong tab "Tiến độ"
                    5. Xuất báo cáo để lưu lại kết quả học tập
                    """
                    )

            # Xử lý sự kiện
            # 1. Tab Luyện phát âm
            def process_and_display(audio, dialect):
                if audio is None:
                    return "Vui lòng thu âm trước khi phân tích.", "", None, None, None

                # Xử lý bản ghi âm
                transcript, analysis, sample_audio_path = trainer.process_recording(audio, dialect)

                # Cập nhật lịch sử
                history_html = update_history()
                return transcript, analysis, audio, sample_audio_path, history_html

            def update_history():
                if not trainer.session_history:
                    return "*Chưa có lịch sử luyện tập*"
                history = "### Lịch sử phiên\n\n"
                for entry in trainer.session_history[-10:]:  # Chỉ hiển thị 10 mục gần nhất
                    short_t = entry["transcript"][:50]
                    suffix = "..." if len(entry["transcript"]) > 50 else ""
                    history += f"{entry['id']}. **{entry['time']}**: {short_t}{suffix}\n"
                return history

            def clear_inputs():
                return None, "", "", None, None

            submit_btn.click(
                fn=process_and_display,
                inputs=[audio_input, dialect_selector],
                outputs=[transcript_output, analysis_output, playback_audio, sample_audio, history_md],
            )

            clear_btn.click(fn=clear_inputs, inputs=[], outputs=[audio_input, transcript_output, analysis_output, playback_audio, sample_audio])

            # 2. Tab Hội thoại
            current_scenario = gr.State(None)
            current_prompt_index = gr.State(0)

            def load_new_scenario():
                scenario = trainer.create_conversation_scenario()
                return (
                    f"## {scenario['title']}",
                    f"*{scenario['description']}*",
                    f"**Bot**: {scenario['prompts'][0]}",
                    scenario,
                    0,
                )

            def next_prompt(scenario, prompt_index):
                if scenario is None:
                    return "Vui lòng tạo tình huống trước", prompt_index
                next_index = prompt_index + 1
                if next_index >= len(scenario["prompts"]):
                    return "Đã hết các câu hỏi trong tình huống này. Hãy tạo tình huống mới!", prompt_index
                return f"**Bot**: {scenario['prompts'][next_index]}", next_index

            def analyze_conversation_response(audio, scenario, prompt_index, dialect):
                if audio is None:
                    return "Vui lòng ghi âm câu trả lời trước", ""
                if scenario is None or prompt_index >= len(scenario["prompts"]):
                    return "Không có tình huống hoặc câu hỏi hợp lệ", ""

                # Xử lý âm thanh -> văn bản
                transcript = trainer.transcribe_audio(audio)

                # Phân tích câu trả lời trong ngữ cảnh
                context = scenario["prompts"][prompt_index]
                prompt = f"""Phân tích câu trả lời trong cuộc hội thoại:

Ngữ cảnh: {context}
Câu trả lời: {transcript}
Phương ngữ: {dialect}

Hãy đánh giá tính phù hợp của câu trả lời với ngữ cảnh, cách diễn đạt, và đưa ra gợi ý cải thiện.
"""

                # Sử dụng hàm phân tích với LLM (nếu có)
                if trainer.config["llm_provider"] != "none":
                    if trainer.config["llm_provider"] == "openai":
                        analysis = trainer._analyze_with_openai(transcript, "", dialect)
                    elif trainer.config["llm_provider"] == "gemini":
                        analysis = trainer._analyze_with_gemini(transcript, "", dialect)
                    elif trainer.config["llm_provider"] == "local":
                        analysis = trainer._analyze_with_local_llm(transcript, "", dialect)
                else:
                    analysis = trainer._rule_based_analysis(transcript, "", dialect)

                return transcript, analysis

            new_scenario_btn.click(
                fn=load_new_scenario,
                inputs=[],
                outputs=[scenario_title, scenario_desc, prompt_text, current_scenario, current_prompt_index],
            )
            next_prompt_btn.click(fn=next_prompt, inputs=[current_scenario, current_prompt_index], outputs=[prompt_text, current_prompt_index])
            analyze_response_btn.click(
                fn=analyze_conversation_response,
                inputs=[conversation_audio, current_scenario, current_prompt_index, dialect_selector],
                outputs=[conversation_transcript, conversation_feedback],
            )

            # 3. Tab Tiến độ
            def update_statistics():
                progress_data = trainer.track_progress()
                stats = progress_data["statistics"]
                charts = progress_data["charts"]
                return stats, charts.get("length_chart", ""), charts.get("distribution_chart", "")

            refresh_stats_btn.click(fn=update_statistics, inputs=[], outputs=[stats_output, length_chart, dist_chart])

            # 4. Tab Xuất báo cáo
            def export_markdown():
                return trainer.export_session(format="markdown")

            def export_html():
                return trainer.export_session(format="html")

            export_md_btn.click(fn=export_markdown, inputs=[], outputs=[export_output])
            export_html_btn.click(fn=export_html, inputs=[], outputs=[export_output])

            # Xử lý khi đóng ứng dụng
            demo.load(lambda: None, inputs=None, outputs=None)

        return demo
    except Exception as e:
        logger.error(f"Lỗi khi tạo giao diện: {e}")
        raise


def main():
    try:
        # Kiểm tra và tạo thư mục dữ liệu
        os.makedirs("data", exist_ok=True)
        os.makedirs("data/audio", exist_ok=True)
        os.makedirs("data/reports", exist_ok=True)
        os.makedirs("data/models", exist_ok=True)

        # Tạo file cấu hình mẫu nếu chưa có
        if not os.path.exists("config.json"):
            sample_config = {
                "stt_model": "nguyenvulebinh/wav2vec2-base-vietnamese-250h",
                "use_phowhisper": False,
                "use_phobert": False,
                "use_vncorenlp": False,
                "llm_provider": "none",
                "use_viettts": False,
                "default_dialect": "Bắc",
                "preprocess_audio": True,
                "save_history": True,
            }
            with open("config.json", "w", encoding="utf-8") as f:
                json.dump(sample_config, f, ensure_ascii=False, indent=2)

        # Tạo và khởi chạy ứng dụng
        demo = create_demo()
        demo.queue()
        demo.launch(share=True)
    except Exception as e:
        logger.error(f"Lỗi khi khởi chạy ứng dụng: {e}")
        print(f"Lỗi: {e}")


if __name__ == "__main__":
    main()

# Cải tiến:
# - Đánh giá ngữ điệu: Phân tích cao độ, nhịp điệu và cảm xúc trong giọng nói
# - Tùy chỉnh giọng TTS: Cho phép người dùng chọn giọng đọc mẫu
# - Tạo bài tập cá nhân hóa: Dựa trên lỗi thường gặp của người dùng