File size: 138,304 Bytes
9a2046b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys

# Set UTF-8 encoding for Windows
if sys.platform == 'win32':
    import codecs
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
    sys.stderr = codecs.getwriter('utf-8')(sys.stderr.detach())

# Load environment variables from .env file (optimized for HF Spaces)
try:
    # Only load .env in local development, skip in production
    if not os.environ.get("SPACE_ID") and not os.environ.get("HF_SPACE_ID"):
        from dotenv import load_dotenv
        load_dotenv()
        print("✅ Environment variables loaded from .env file")
    else:
        print("🏭 Production environment - using system environment variables")
except ImportError:
    print("⚠️  python-dotenv not installed. Using system environment variables only.")
except Exception as e:
    print(f"⚠️  Error loading .env file: {e}")

# Essential imports for HF Spaces
import numpy as np
import gradio as gr

# Try to import google-generativeai with fallback
try:
    import google.generativeai as genai
    GENAI_AVAILABLE = True
except ImportError as e:
    print(f"⚠️ google-generativeai not available: {e}")
    GENAI_AVAILABLE = False
    genai = None

try:
    from gtts import gTTS, lang
    GTTS_AVAILABLE = True
except ImportError as e:
    print(f"⚠️ gtts not available: {e}")
    GTTS_AVAILABLE = False

import tempfile
# import soundfile as sf  # Import locally to avoid startup overhead
# Kokoro not used - removed for performance
import time
import base64

# Try to import optional dependencies
try:
    import edge_tts
    EDGE_TTS_AVAILABLE = True
except ImportError as e:
    print(f"⚠️ edge-tts not available: {e}")
    EDGE_TTS_AVAILABLE = False

import asyncio
import io

try:
    import PyPDF2
    PDF_AVAILABLE = True
except ImportError:
    PDF_AVAILABLE = False

try:
    import docx
    DOCX_AVAILABLE = True
except ImportError:
    DOCX_AVAILABLE = False

import shutil
import atexit
import glob
import datetime

# Librosa not used - removed for performance

# === RECORD DATA MANAGEMENT ===
RECORD_DATA_DIR = "record_data"

def create_record_data_directory():
    """Create record_data directory if it doesn't exist"""
    if not os.path.exists(RECORD_DATA_DIR):
        os.makedirs(RECORD_DATA_DIR)
        print(f"✅ Created directory: {RECORD_DATA_DIR}")
    return RECORD_DATA_DIR

def cleanup_record_data():
    """Clean up record_data directory when app closes (disabled for production)"""
    try:
        # Disable cleanup for HF Spaces and production environments
        if os.environ.get("SPACE_ID") or os.environ.get("HF_SPACE_ID"):
            print(f"🏭 Production environment detected - keeping {RECORD_DATA_DIR} directory")
            return
            
        # Only cleanup in local development
        if os.path.exists(RECORD_DATA_DIR):
            shutil.rmtree(RECORD_DATA_DIR)
            print(f"🧹 Cleaned up {RECORD_DATA_DIR} directory")
    except Exception as e:
        print(f"⚠️ Error cleaning up {RECORD_DATA_DIR}: {e}")

def save_recorded_audio(audio_data, original_filename=None):
    """Save audio data to record_data directory"""
    try:
        # Create directory if needed
        create_record_data_directory()
        
        # Generate filename with timestamp
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        if original_filename:
            name_part = os.path.splitext(os.path.basename(original_filename))[0]
            filename = f"recorded_{name_part}_{timestamp}.wav"
        else:
            filename = f"recorded_{timestamp}.wav"
        
        filepath = os.path.join(RECORD_DATA_DIR, filename)
        
        # Handle different audio data types
        if isinstance(audio_data, str) and os.path.exists(audio_data):
            # File path - copy the file
            shutil.copy2(audio_data, filepath)
        elif isinstance(audio_data, tuple) and len(audio_data) == 2:
            # Numpy array format (sample_rate, audio_array)
            sample_rate, audio_array = audio_data
            import soundfile as sf
            sf.write(filepath, audio_array, sample_rate)
            print(f"📊 Saved numpy audio: sr={sample_rate}, shape={audio_array.shape}")
        else:
            # Raw data
            with open(filepath, 'wb') as f:
                f.write(audio_data)
        
        print(f"✅ Saved recorded audio: {filepath}")
        return filepath
        
    except Exception as e:
        print(f"❌ Error saving recorded audio: {e}")
        import traceback
        traceback.print_exc()
        return None

def get_recorded_files():
    """Get list of recorded audio files"""
    try:
        if not os.path.exists(RECORD_DATA_DIR):
            print(f"📁 Record directory does not exist: {RECORD_DATA_DIR}")
            return []
        
        # Get all audio files in record_data
        pattern = os.path.join(RECORD_DATA_DIR, "*.wav")
        files = glob.glob(pattern)
        print(f"🔍 Found {len(files)} files in {RECORD_DATA_DIR}")
        
        # Sort by modification time (newest first)
        files.sort(key=os.path.getmtime, reverse=True)
        
        # Return just filenames for display
        filenames = [os.path.basename(f) for f in files]
        print(f"📂 Returning filenames: {filenames}")
        return filenames
        
    except Exception as e:
        print(f"❌ Error getting recorded files: {e}")
        return []

def get_recorded_file_path(filename):
    """Get full path of recorded file"""
    return os.path.join(RECORD_DATA_DIR, filename)


def delete_recorded_file(filename):
    """Delete recorded file from record_data directory"""
    try:
        if not filename or not filename.strip():
            return "❌ Không có file nào được chọn để xóa"
            
        file_path = get_recorded_file_path(filename)
        print(f"🗑️ Attempting to delete: {file_path}")
        
        if os.path.exists(file_path):
            os.remove(file_path)
            print(f"✅ Successfully deleted: {filename}")
            return f"✅ Đã xóa file: {filename}"
        else:
            print(f"❌ File not found: {file_path}")
            return f"❌ Không tìm thấy file: {filename}"
            
    except Exception as e:
        print(f"❌ Error deleting file: {e}")
        return f"❌ Lỗi khi xóa file: {str(e)}"

# Register cleanup function to run when app exits (disabled for stability)
# atexit.register(cleanup_record_data)  # Disabled to prevent data loss on deployment

# DOCX support already checked above

# Configure Gemini API - Delayed configuration for faster startup
GEMINI_API_KEY = None

def configure_gemini_api():
    """Configure Gemini API on first use to speed up startup"""
    global GEMINI_API_KEY
    if not GENAI_AVAILABLE:
        print("❌ google-generativeai not available")
        return None
        
    if GEMINI_API_KEY is None:
        GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
        if GEMINI_API_KEY:
            genai.configure(api_key=GEMINI_API_KEY)
            print("✅ Gemini API configured successfully")
        else:
            print("⚠️  GEMINI_API_KEY or GOOGLE_API_KEY not found in environment variables")
    return GEMINI_API_KEY

# Language configurations for Audio Translation (simplified)
if GTTS_AVAILABLE:
    GTTS_LANGUAGES = lang.tts_langs()
    GTTS_LANGUAGES['ja'] = 'Japanese'
else:
    GTTS_LANGUAGES = {'en': 'English', 'vi': 'Vietnamese'}

SUPPORTED_LANGUAGES = sorted(list(GTTS_LANGUAGES.values()))

# Voice mapping for Edge TTS - defined once for performance
VOICE_MAP = {
    "🇻🇳 HoaiMy - Nữ Việt Chuẩn": "vi-VN-HoaiMyNeural",
    "🇻🇳 NamMinh - Nam Việt Chuẩn": "vi-VN-NamMinhNeural",
    "🇺🇸 Aria - Nữ Mỹ": "en-US-AriaNeural",
    "🇺🇸 Guy - Nam Mỹ": "en-US-GuyNeural",
    "🇬🇧 Sonia - Nữ Anh": "en-GB-SoniaNeural",
    "🇬🇧 Ryan - Nam Anh": "en-GB-RyanNeural",
    "🇩🇪 Katja - Deutsche Frau": "de-DE-KatjaNeural",
    "🇩🇪 Conrad - Deutscher Mann": "de-DE-ConradNeural",
    "🇫🇷 Denise - Française": "fr-FR-DeniseNeural",
    "🇫🇷 Henri - Français": "fr-FR-HenriNeural",
    "🇪🇸 Elvira - Española": "es-ES-ElviraNeural",
    "🇪🇸 Alvaro - Español": "es-ES-AlvaroNeural",
    "🇮🇹 Elsa - Italiana": "it-IT-ElsaNeural",
    "🇮🇹 Diego - Italiano": "it-IT-DiegoNeural",
    "🇯🇵 Nanami - 日本女性": "ja-JP-NanamiNeural",
    "🇯🇵 Keita - 日本男性": "ja-JP-KeitaNeural",
    "🇰🇷 SunHi - 한국 여성": "ko-KR-SunHiNeural",
    "🇰🇷 BongJin - 한국 남성": "ko-KR-BongJinNeural",
    "🇨🇳 Xiaoxiao - 中文女声": "zh-CN-XiaoxiaoNeural",
    "🇨🇳 Yunxi - 中文男声": "zh-CN-YunxiNeural",
    "🇷🇺 Svetlana - Русская": "ru-RU-SvetlanaNeural",
    "🇷🇺 Dmitry - Русский": "ru-RU-DmitryNeural",
    "🇵🇹 Francisca - Portuguesa": "pt-BR-FranciscaNeural",
    "🇵🇹 Antonio - Português": "pt-BR-AntonioNeural",
    "🇸🇦 Zariyah - عربية": "ar-SA-ZariyahNeural",
    "🇸🇦 Hamed - عربي": "ar-SA-HamedNeural"
}

# Voice RAG Functions (Tích hợp từ hf_Voice_Audio_Translation)
def read_pdf(file_path):
    """Extract text from PDF file"""
    try:
        with open(file_path, 'rb') as file:
            reader = PyPDF2.PdfReader(file)
            text = ""
            for page in reader.pages:
                text += page.extract_text()
        return text
    except Exception as e:
        return f"Error reading PDF: {str(e)}"

def read_docx(file_path):
    """Extract text from Word document"""
    try:
        if not DOCX_AVAILABLE:
            return "❌ python-docx not available"
        doc = docx.Document(file_path)
        text = ""
        for paragraph in doc.paragraphs:
            text += paragraph.text + "\n"
        return text
    except Exception as e:
        return f"Error reading DOCX: {str(e)}"

def read_txt(file_path):
    """Extract text from TXT file"""
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            return file.read()
    except Exception as e:
        return f"Error reading TXT: {str(e)}"

def extract_text_from_file(file_path):
    """Extract text from various file formats"""
    if file_path is None:
        return "No file uploaded"
    
    file_extension = os.path.splitext(file_path)[1].lower()
    
    if file_extension == '.pdf':
        return read_pdf(file_path)
    elif file_extension == '.docx':
        return read_docx(file_path)
    elif file_extension == '.txt':
        return read_txt(file_path)
    else:
        return f"Unsupported file format: {file_extension}"

def detect_language_from_text(text):
    """Detect language from text content"""
    # Vietnamese detection
    vietnamese_chars = 'àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ'
    if any(char in text.lower() for char in vietnamese_chars):
        return "Vietnamese"
    
    # Chinese detection
    chinese_chars = '中文汉字學習语言'
    if any(char in text for char in chinese_chars):
        return "Chinese"
    
    # Japanese detection
    japanese_chars = 'ひらがなカタカナ日本語'
    if any(char in text for char in japanese_chars):
        return "Japanese"
    
    # Korean detection
    korean_chars = '한국어문자'
    if any(char in text for char in korean_chars):
        return "Korean"
    
    # French detection
    french_words = ['le', 'la', 'les', 'de', 'et', 'à', 'un', 'une', 'ce', 'qui', 'que']
    french_chars = 'àâäéèêëïîôöùûüÿç'
    if any(word in text.lower() for word in french_words) or any(char in text.lower() for char in french_chars):
        return "French"
    
    # German detection
    german_words = ['der', 'die', 'das', 'und', 'ist', 'ich', 'bin', 'haben', 'sein', 'werden']
    german_chars = 'äöüß'
    if any(word in text.lower() for word in german_words) or any(char in text.lower() for char in german_chars):
        return "German"
    
    # Spanish detection
    spanish_words = ['el', 'la', 'de', 'que', 'y', 'a', 'en', 'un', 'es', 'se', 'no', 'te', 'lo']
    spanish_chars = 'ñáéíóúü'
    if any(word in text.lower() for word in spanish_words) or any(char in text.lower() for char in spanish_chars):
        return "Spanish"
    
    # English detection (default)
    english_words = ['the', 'and', 'is', 'are', 'have', 'has', 'will', 'would', 'can', 'could']
    if any(word in text.lower() for word in english_words):
        return "English"
    
    return "English"  # Default fallback

def process_with_gemini(text, question, answer_language="Vietnamese"):
    """Process text and question using Gemini with multi-language support"""
    try:
        api_key = configure_gemini_api()
        if not api_key:
            return "❌ Lỗi: Chưa cấu hình GEMINI_API_KEY hoặc GOOGLE_API_KEY trong environment variables"
            
        model = genai.GenerativeModel("gemini-2.0-flash")
        
        # Detect document language
        detected_doc_language = detect_language_from_text(text)
        
        prompt = f"""

        Based on the following document content, please answer the question in {answer_language}:

        

        Document Content (detected language: {detected_doc_language}):

        {text}

        

        Question: {question}

        

        Please provide a comprehensive and accurate answer in {answer_language}. 

        If the document is in a different language than the question, please still answer in {answer_language}.

        Maintain the factual accuracy while adapting cultural context appropriately.

        """
        
        response = model.generate_content(prompt)
        return response.text
        
    except Exception as e:
        return f"Error processing with Gemini: {str(e)}"

def text_to_speech_rag(text, voice_selection):
    """Convert text to speech using Edge TTS for RAG results"""
    try:
        if not text or text.startswith("Error"):
            return None
        
        # Use global VOICE_MAP for performance
        voice_name = VOICE_MAP.get(voice_selection, "vi-VN-HoaiMyNeural")
        text_limited = text[:2000] if len(text) > 2000 else text
        
        # Generate speech using Edge TTS
        audio_data = asyncio.run(generate_speech(text_limited, voice_name, 0.0))
        
        # Save to temporary file
        fd, temp_output_path = tempfile.mkstemp(suffix=".wav", prefix="voice_rag_audio_")
        os.close(fd)
        
        # Write raw audio data to temporary file
        with open(temp_output_path, 'wb') as f:
            f.write(audio_data)
        
        return temp_output_path
        
    except Exception as e:
        print(f"TTS Error: {str(e)}")
        return None

def voice_rag_pipeline(uploaded_file, question, answer_language="Vietnamese", voice_selection="🇻🇳 HoaiMy - Nữ Việt Chuẩn", text_format="txt"):
    """Complete Voice RAG pipeline with multi-language support and downloadable text"""
    if uploaded_file is None:
        return "Please upload a document first.", "N/A", None, None
    
    if not question.strip():
        return "Please enter a question.", "N/A", None, None
    
    # Extract text from uploaded file
    extracted_text = extract_text_from_file(uploaded_file)
    
    if extracted_text.startswith("Error"):
        return extracted_text, "Error", None, None
    
    # Detect document language
    detected_doc_language = detect_language_from_text(extracted_text)
    
    # Process with Gemini using selected answer language
    answer = process_with_gemini(extracted_text, question, answer_language)
    
    # Generate speech using selected voice
    audio_file = text_to_speech_rag(answer, voice_selection)
    
    # Create formatted content for download
    if text_format.lower() == "md":
        # Create beautiful Markdown format
        formatted_content = format_voice_rag_response(
            question, answer, detected_doc_language, voice_selection
        )
        text_file_path = create_text_file(formatted_content, "md", "voice_rag_response")
    else:
        # Create standard text file
        text_file_path = create_text_file(answer, text_format, "voice_rag_answer")
    
    return answer, detected_doc_language, audio_file, text_file_path

def detect_language(text):
    """Detect language of input text with improved accuracy"""
    if not text.strip():
        return "unknown"
    
    text_lower = text.lower()
    
    # Vietnamese detection (more comprehensive)
    vietnamese_chars = 'àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ'
    vietnamese_words = ['và', 'của', 'là', 'có', 'này', 'được', 'cho', 'từ', 'một', 'những', 'tôi', 'bạn']
    vietnamese_score = sum(1 for char in text if char in vietnamese_chars) + sum(2 for word in vietnamese_words if word in text_lower)
    
    # English detection (more comprehensive)
    english_words = ['the', 'and', 'is', 'are', 'have', 'has', 'will', 'would', 'can', 'could', 'that', 'this', 'with', 'for', 'you', 'he', 'she', 'it', 'they', 'we']
    english_score = sum(1 for word in english_words if word in text_lower)
    
    # German detection
    german_words = ['der', 'die', 'das', 'und', 'ist', 'ich', 'bin', 'haben', 'sein', 'werden', 'mit', 'auf', 'für', 'von']
    german_chars = 'äöüß'
    german_score = sum(1 for word in german_words if word in text_lower) + sum(1 for char in text if char in german_chars)
    
    # French detection
    french_words = ['le', 'la', 'les', 'de', 'et', 'à', 'un', 'une', 'ce', 'qui', 'que', 'avec', 'pour', 'dans']
    french_chars = 'àâäéèêëïîôöùûüÿç'
    french_score = sum(1 for word in french_words if word in text_lower) + sum(0.5 for char in text if char in french_chars)
    
    # Spanish detection
    spanish_words = ['el', 'la', 'de', 'que', 'y', 'a', 'en', 'un', 'es', 'se', 'no', 'te', 'lo', 'con', 'para']
    spanish_chars = 'ñáéíóúü'
    spanish_score = sum(1 for word in spanish_words if word in text_lower) + sum(0.5 for char in text if char in spanish_chars)
    
    # Score-based detection
    scores = {
        'Vietnamese': vietnamese_score,
        'English': english_score,
        'German': german_score,
        'French': french_score,
        'Spanish': spanish_score
    }
    
    # Find the language with highest score
    max_score = max(scores.values())
    if max_score > 0:
        detected = max(scores, key=scores.get)
        print(f"🔍 Language detection scores: {scores}")
        print(f"🎯 Detected language: {detected} (score: {max_score})")
        return detected
    
    # Default fallback
    print(f"⚠️ Could not detect language, defaulting to English")
    return "English"

async def generate_speech(text, voice_name, rate):
    """Generate speech using Edge TTS"""
    communicate = edge_tts.Communicate(text, voice_name, rate=f"{rate:+.0%}")
    
    # Create in-memory buffer
    audio_buffer = io.BytesIO()
    
    async for chunk in communicate.stream():
        if chunk["type"] == "audio":
            audio_buffer.write(chunk["data"])
    
    audio_buffer.seek(0)
    return audio_buffer.getvalue()

def create_text_file(content, file_format="txt", filename_prefix="translated_text"):
    """

    Create a downloadable text file from content in TXT, DOCX, or MD format

    """
    if not content or content.startswith("Lỗi:") or content.startswith("❌"):
        return None
    
    try:
        if file_format.lower() == "docx" and DOCX_AVAILABLE:
            # Create Word document
            fd, temp_file_path = tempfile.mkstemp(suffix=".docx", prefix=f"{filename_prefix}_")
            os.close(fd)
            
            if not DOCX_AVAILABLE:
                return None
            from docx import Document
            doc = Document()
            doc.add_heading('Nội dung đã dịch', 0)
            doc.add_paragraph(content)
            doc.save(temp_file_path)
            
            return temp_file_path
        elif file_format.lower() == "md":
            # Create Markdown file
            fd, temp_file_path = tempfile.mkstemp(suffix=".md", prefix=f"{filename_prefix}_")
            os.close(fd)
            
            with open(temp_file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            
            return temp_file_path
        else:
            # Create TXT file (default)
            fd, temp_file_path = tempfile.mkstemp(suffix=".txt", prefix=f"{filename_prefix}_")
            os.close(fd)
            
            with open(temp_file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            
            return temp_file_path
    except Exception as e:
        return None

def format_voice_rag_response(question, answer, detected_language, voice_selection, timestamp=None):
    """

    Format Voice RAG response as beautiful Markdown

    """
    if timestamp is None:
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    
    # Clean and format the answer
    formatted_answer = answer.strip()
    
    # Create beautiful Markdown document
    markdown_content = f"""# 📚 Voice RAG - Intelligent Document Q&A



---



## 📄 **Session Information**



| **Field** | **Details** |

|-----------|-------------|

| 🕒 **Timestamp** | {timestamp} |

| 🌍 **Document Language** | {detected_language} |

| 🎭 **Voice Selection** | {voice_selection} |

| 🤖 **AI Model** | Google Gemini 2.0 Flash |



---



## ❓ **Question**



> {question}



---



## 💬 **AI Response**



{formatted_answer}



---





---



## 📱 **Generated by**



**🎙️ Voice AI Platform** - Digitized Brains  

*Powered by Claude Code & Google Gemini 2.0 Flash*



> 🌐 **Voice RAG Technology** - Combining document intelligence with premium voice synthesis



---



*Generated on {timestamp} | Voice: {voice_selection} | Language: {detected_language}*

"""
    
    return markdown_content

def format_voice_studio_response(text, voice_selection, speed, detected_language="Auto-detected", timestamp=None):
    """

    Format Voice Studio response as simple Markdown

    """
    if timestamp is None:
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    
    # Clean and format the text
    formatted_text = text.strip()
    
    # Create simple Markdown document
    markdown_content = f"""# Voice Studio Result



## Input Text ({detected_language})



{formatted_text}



---



*Generated on {timestamp} | Voice: {voice_selection} | Speed: {speed:.1f}x*

"""
    
    return markdown_content

def format_audio_translation_response(original_text, translated_text, source_language, target_language, voice_selection, timestamp=None):
    """

    Format Audio Translation response as simple Markdown

    """
    if timestamp is None:
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    
    # Clean and format the texts
    formatted_original = original_text.strip()
    formatted_translated = translated_text.strip()
    
    # Create simple Markdown document
    markdown_content = f"""# Audio Translation Result



## Original Text ({source_language})



{formatted_original}



## Translated Text ({target_language})



{formatted_translated}



---



*Generated on {timestamp} | {source_language}{target_language} | Voice: {voice_selection}*

"""
    
    return markdown_content

def create_audio_voice_studio(text, voice_selection, speed, text_format="txt"):
    """Voice Studio functionality with text file generation"""
    if not text.strip():
        return "❌ Vui lòng nhập văn bản / Please enter text / Bitte Text eingeben", None
    
    try:
        # Use global VOICE_MAP for performance (avoiding recreation on each call)
        voice_name = VOICE_MAP.get(voice_selection, "vi-VN-HoaiMyNeural")
        text_limited = text[:1000] if len(text) > 1000 else text
        
        # Convert speed (0.5-2.0) to rate percentage (-50% to +100%)
        rate_percent = (speed - 1.0)
        
        # Generate speech using Edge TTS
        audio_data = asyncio.run(generate_speech(text_limited, voice_name, rate_percent))
        
        # Convert to base64
        audio_base64 = base64.b64encode(audio_data).decode('utf-8')
        
        timestamp = int(time.time())
        filename = f"voice_{voice_name}_{speed}x_{timestamp}.mp3"
        
        # Detect language
        detected_lang = detect_language(text_limited)
        
        # Mobile-optimized HTML player
        html_player = f'''

            <div style="

                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

                border-radius: 20px; 

                padding: 20px; 

                margin: 10px 0; 

                box-shadow: 0 8px 32px rgba(0,0,0,0.2);

                color: white;

                text-align: center;

            ">

                <div style="margin-bottom: 20px;">

                    <h3 style="color: #fff; margin: 0 0 15px 0; font-size: 1.3em; text-shadow: 1px 1px 2px rgba(0,0,0,0.3);">

                        🎵 Âm thanh hoàn thành!

                    </h3>

                    <div style="

                        background: rgba(255,255,255,0.2); 

                        border-radius: 12px; 

                        padding: 12px; 

                        font-size: 0.9em; 

                        line-height: 1.5;

                        backdrop-filter: blur(10px);

                    ">

                        <div><strong>🎭 Giọng:</strong> {voice_selection}</div>

                        <div><strong>⚡ Tốc độ:</strong> {speed:.1f}x | <strong>🌍 Ngôn ngữ:</strong> {detected_lang.title()}</div>

                        <div><strong>📝 Độ dài:</strong> {len(text_limited)} ký tự</div>

                    </div>

                </div>

                

                <audio controls style="

                    width: 100%; 

                    max-width: 100%;

                    height: 50px;

                    margin: 20px 0; 

                    border-radius: 25px;

                    background: rgba(255,255,255,0.95);

                    box-shadow: 0 4px 15px rgba(0,0,0,0.2);

                ">

                    <source src="data:audio/mpeg;base64,{audio_base64}" type="audio/mpeg">

                    Trình duyệt không hỗ trợ audio.

                </audio>

                

                <div style="

                    display: flex; 

                    justify-content: center;

                    margin-top: 20px;

                ">

                    <a href="data:audio/mpeg;base64,{audio_base64}" download="{filename}"

                       style="

                           background: linear-gradient(45deg, #28a745, #20c997);

                           color: white; 

                           padding: 15px 30px; 

                           text-decoration: none;

                           border-radius: 25px; 

                           font-weight: 700; 

                           font-size: 1.1em;

                           display: flex;

                           align-items: center;

                           justify-content: center;

                           box-shadow: 0 4px 15px rgba(40,167,69,0.3);

                           transition: all 0.3s ease;

                           min-height: 48px;

                           min-width: 200px;

                       "

                       ontouchstart=""

                       onmouseover="this.style.transform='translateY(-2px)'; this.style.boxShadow='0 6px 20px rgba(40,167,69,0.4)'"

                       onmouseout="this.style.transform='translateY(0)'; this.style.boxShadow='0 4px 15px rgba(40,167,69,0.3)'">

                        📥 TẢI XUỐNG MP3

                    </a>

                </div>

            </div>

            '''
        
        # Create text file based on format
        text_file_path = None
        if text_format == "md":
            # Use Markdown formatting function
            detected_language = detect_language(text_limited)
            markdown_content = format_voice_studio_response(text_limited, voice_selection, speed, detected_language)
            text_file_path = create_text_file(markdown_content, "md", "voice_studio")
        elif text_format == "docx":
            # Create Word document with Voice Studio formatting
            detected_language = detect_language(text_limited)
            markdown_content = format_voice_studio_response(text_limited, voice_selection, speed, detected_language)
            text_file_path = create_text_file(markdown_content, "docx", "voice_studio")
        elif text_format == "txt":
            # Create simple text file
            text_file_path = create_text_file(text_limited, "txt", "voice_studio")
            
        return html_player, text_file_path
            
    except Exception as e:
        return f"❌ Error: {str(e)}", None

# Language mapping for voices - defined once for performance
VOICE_TO_LANGUAGE = {
    # Vietnamese
    "🇻🇳 HoaiMy - Nữ Việt Chuẩn": "Vietnamese",
    "🇻🇳 NamMinh - Nam Việt Chuẩn": "Vietnamese",
    # English
    "🇺🇸 Aria - Nữ Mỹ": "English",
    "🇺🇸 Guy - Nam Mỹ": "English", 
    "🇬🇧 Sonia - Nữ Anh": "English",
    "🇬🇧 Ryan - Nam Anh": "English",
    # German
    "🇩🇪 Katja - Deutsche Frau": "German",
    "🇩🇪 Conrad - Deutscher Mann": "German",
    # French
    "🇫🇷 Denise - Française": "French",
    "🇫🇷 Henri - Français": "French",
    # Spanish
    "🇪🇸 Elvira - Española": "Spanish",
    "🇪🇸 Alvaro - Español": "Spanish",
    # Italian
    "🇮🇹 Elsa - Italiana": "Italian",
    "🇮🇹 Diego - Italiano": "Italian",
    # Japanese
    "🇯🇵 Nanami - 日本女性": "Japanese",
    "🇯🇵 Keita - 日本男性": "Japanese",
    # Korean
    "🇰🇷 SunHi - 한국 여성": "Korean",
    "🇰🇷 BongJin - 한국 남성": "Korean",
    # Chinese
    "🇨🇳 Xiaoxiao - 中文女声": "Chinese",
    "🇨🇳 Yunxi - 中文男声": "Chinese",
    # Russian
    "🇷🇺 Svetlana - Русская": "Russian",
    "🇷🇺 Dmitry - Русский": "Russian",
    # Portuguese
    "🇵🇹 Francisca - Portuguesa": "Portuguese",
    "🇵🇹 Antonio - Português": "Portuguese",
    # Arabic
    "🇸🇦 Zariyah - عربية": "Arabic",
    "🇸🇦 Hamed - عربي": "Arabic"
}

def get_target_language_from_voice(voice_selection):
    """Map voice selection to target language for translation"""
    return VOICE_TO_LANGUAGE.get(voice_selection, "Vietnamese")

def translate_text_with_gemini(text, target_language):
    """Translate text using Gemini API"""
    try:
        api_key = configure_gemini_api()
        if not api_key:
            return f"❌ Lỗi: Chưa cấu hình GEMINI_API_KEY hoặc GOOGLE_API_KEY trong environment variables"
            
        if not text.strip():
            return ""
            
        model = genai.GenerativeModel("gemini-2.0-flash")
        
        prompt = f"""Translate the following text to {target_language}. Return ONLY the translated text, nothing else:



{text}"""

        response = model.generate_content(prompt)
        translated_text = response.text.strip()
        
        # Clean up any unwanted text that might be included
        if translated_text.lower().startswith("translation:"):
            translated_text = translated_text[12:].strip()
        if translated_text.lower().startswith("here is"):
            lines = translated_text.split('\n')
            if len(lines) > 1:
                translated_text = '\n'.join(lines[1:]).strip()
        
        return translated_text
        
    except Exception as e:
        return f"Lỗi dịch thuật: {str(e)}"

def translate_audio(audio_file, target_country, voice_selection, text_format="txt"):
    """

    Transcribe, translate and synthesize audio to target language with Voice Studio integration

    """
    try:
        api_key = configure_gemini_api()
        if not api_key:
            return "❌ Lỗi: Chưa cấu hình GEMINI_API_KEY hoặc GOOGLE_API_KEY trong environment variables", "Không xác định", "", target_country, None, None, "", "", None
            
        if audio_file is None:
            return "Lỗi: Vui lòng tải lên file audio", "Không xác định", "", target_country, None, None, "", "", None
        
        # Save recorded audio to record_data directory
        print(f"🔍 Processing audio file type: {type(audio_file)}")
        saved_audio_path = save_recorded_audio(audio_file)
        if saved_audio_path:
            print(f"🎤 Audio saved to record_data: {os.path.basename(saved_audio_path)}")
            # Debug: check if file really exists
            if os.path.exists(saved_audio_path):
                file_size = os.path.getsize(saved_audio_path)
                print(f"✅ File confirmed: {saved_audio_path} ({file_size} bytes)")
            else:
                print(f"❌ File not found after save: {saved_audio_path}")
                return "❌ Lỗi: Không thể lưu file audio", "Không xác định", "", target_country, None, None, "", "", None
        else:
            print("❌ Failed to save audio file")
            return "❌ Lỗi: Không thể lưu file audio", "Không xác định", "", target_country, None, None, "", "", None
            
        # Get target language from voice selection
        target_language = get_target_language_from_voice(voice_selection)
            
        # Transcribe audio using Gemini
        model = genai.GenerativeModel("gemini-2.0-flash")
        
        # Read audio file using saved path
        with open(saved_audio_path, 'rb') as f:
            audio_data = f.read()
        
        # Create audio blob
        audio_blob = {
            'mime_type': 'audio/wav',
            'data': audio_data
        }

        # Step 1: Transcribe audio only first
        transcribe_prompt = """Transcribe this audio accurately in its original language. Return only the transcribed text, nothing else."""
        
        response = model.generate_content([transcribe_prompt, audio_blob])
        transcription = response.text.strip()
        
        # Step 2: Detect language of transcription
        detected_lang = detect_language(transcription)
        
        # Step 3: Translate if needed (only if source is different from target)
        if detected_lang.lower() != target_language.lower():
            print(f"🔄 Translating from {detected_lang} to {target_language}")
            translated_text = translate_text_with_gemini(transcription, target_language)
            
            # Check if translation was successful
            if translated_text.startswith("❌") or translated_text.startswith("Lỗi"):
                print(f"❌ Translation failed: {translated_text}")
                # Use original transcription if translation fails
                translated_text = transcription
            else:
                print(f"✅ Translation successful")
        else:
            print(f"ℹ️ No translation needed - same language ({detected_lang})")
            translated_text = transcription
        
        # Generate audio using Edge TTS (use global VOICE_MAP for performance)
        edge_voice = VOICE_MAP.get(voice_selection, "vi-VN-HoaiMyNeural")
        print(f"🎙️ Generating audio with voice: {edge_voice}")
        audio_data = asyncio.run(generate_speech(translated_text, edge_voice, 0.0))
        print(f"🎵 Generated audio data: {len(audio_data)} bytes")
        
        # Save audio file
        fd, temp_output_path = tempfile.mkstemp(suffix=".wav", prefix="translated_audio_")
        os.close(fd)
        print(f"📁 Created temp audio file: {temp_output_path}")
        
        # Write raw audio data to temporary file
        with open(temp_output_path, 'wb') as f:
            f.write(audio_data)
        
        # Verify file was created
        if os.path.exists(temp_output_path):
            file_size = os.path.getsize(temp_output_path)
            print(f"✅ Audio file created successfully: {file_size} bytes")
        else:
            print(f"❌ Failed to create audio file: {temp_output_path}")
        
        # Create text file for download with proper formatting
        text_file_path = None
        if text_format == "md":
            # Use Markdown formatting function for Audio Translation
            markdown_content = format_audio_translation_response(
                transcription, translated_text, detected_lang, target_language, voice_selection
            )
            text_file_path = create_text_file(markdown_content, "md", "audio_translation")
        elif text_format == "docx":
            # Create Word document with Audio Translation formatting
            markdown_content = format_audio_translation_response(
                transcription, translated_text, detected_lang, target_language, voice_selection
            )
            text_file_path = create_text_file(markdown_content, "docx", "audio_translation")
        else:
            # Create simple text file
            text_file_path = create_text_file(translated_text, "txt", "audio_translation")
        
        return transcription, detected_lang, translated_text, target_language, temp_output_path, temp_output_path, transcription, translated_text, text_file_path
        
    except Exception as e:
        # Get target language for error response
        target_language = get_target_language_from_voice(voice_selection) if 'voice_selection' in locals() else "Vietnamese"
        return f"Lỗi: {str(e)}", "Lỗi", "", target_language, None, None, "", "", None

# Voice choices organized by country - ONLY OFFICIAL VOICES  
voice_choices_by_country = {
    "🇻🇳 Việt Nam": [
        "🇻🇳 HoaiMy - Nữ Việt Chuẩn",
        "🇻🇳 NamMinh - Nam Việt Chuẩn"
    ],
    "🇺🇸 Hoa Kỳ": [
        "🇺🇸 Aria - Nữ Mỹ",
        "🇺🇸 Guy - Nam Mỹ"
    ],
    "🇬🇧 Anh": [
        "🇬🇧 Sonia - Nữ Anh",
        "🇬🇧 Ryan - Nam Anh"
    ],
    "🇩🇪 Đức": [
        "🇩🇪 Katja - Deutsche Frau",
        "🇩🇪 Conrad - Deutscher Mann"
    ],
    "🇫🇷 Pháp": [
        "🇫🇷 Denise - Française",
        "🇫🇷 Henri - Français"
    ],
    "🇪🇸 Tây Ban Nha": [
        "🇪🇸 Elvira - Española",
        "🇪🇸 Alvaro - Español"
    ],
    "🇮🇹 Ý": [
        "🇮🇹 Elsa - Italiana",
        "🇮🇹 Diego - Italiano"
    ],
    "🇯🇵 Nhật Bản": [
        "🇯🇵 Nanami - 日本女性",
        "🇯🇵 Keita - 日本男性"
    ],
    "🇰🇷 Hàn Quốc": [
        "🇰🇷 SunHi - 한국 여성",
        "🇰🇷 BongJin - 한국 남성"
    ],
    "🇨🇳 Trung Quốc": [
        "🇨🇳 Xiaoxiao - 中文女声",
        "🇨🇳 Yunxi - 中文男声"
    ],
    "🇷🇺 Nga": [
        "🇷🇺 Svetlana - Русская",
        "🇷🇺 Dmitry - Русский"
    ],
    "🇵🇹 Bồ Đào Nha": [
        "🇵🇹 Francisca - Portuguesa",
        "🇵🇹 Antonio - Português"
    ],
    "🇸🇦 Ả Rập": [
        "🇸🇦 Zariyah - عربية",
        "🇸🇦 Hamed - عربي"
    ]
}

def update_voices(country):
    """Update voice choices based on selected country"""
    if country in voice_choices_by_country:
        voices = voice_choices_by_country[country]
        return gr.Dropdown(choices=voices, value=voices[0])
    else:
        # Default to Vietnamese voices
        default_voices = voice_choices_by_country["🇻🇳 Việt Nam"]
        return gr.Dropdown(choices=default_voices, value=default_voices[0])

# Lightweight CSS - optimized for performance  
css = """

* {

    font-family: system-ui, -apple-system, 'Segoe UI', Arial, sans-serif;

}



.gradio-container {

    max-width: 1200px;

    margin: 0 auto;

    position: relative;

}



/* Critical fix for dropdown interaction */

.gradio-container * {

    pointer-events: auto;

}



/* Hide Gradio footer */

.footer {

    display: none !important;

}



/* Pulsing animation for processing status */

@keyframes pulse-processing {

    0% {

        opacity: 1;

        transform: scale(1);

        box-shadow: 0 4px 15px rgba(255, 193, 7, 0.3);

    }

    50% {

        opacity: 0.8;

        transform: scale(1.02);

        box-shadow: 0 6px 25px rgba(255, 193, 7, 0.6);

    }

    100% {

        opacity: 1;

        transform: scale(1);

        box-shadow: 0 4px 15px rgba(255, 193, 7, 0.3);

    }

}



.status-processing {

    animation: pulse-processing 1.5s ease-in-out infinite;

    background: linear-gradient(135deg, #FFC107 0%, #FF9800 100%) !important;

}



/* Success status animation */

@keyframes pulse-success {

    0% {

        opacity: 1;

        transform: scale(1);

    }

    50% {

        opacity: 0.9;

        transform: scale(1.01);

    }

    100% {

        opacity: 1;

        transform: scale(1);

    }

}



.status-success {

    animation: pulse-success 2s ease-in-out 3;

    background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%) !important;

}



/* Custom footer to cover Gradio attribution */

.custom-footer {

    position: fixed;

    bottom: 0;

    left: 0;

    right: 0;

    background: linear-gradient(135deg, #4A90E2 0%, #2E86AB 70%, #FF8A65 85%, #FF6B9D 100%);

    color: white;

    padding: 15px;

    text-align: center;

    font-weight: bold;

    z-index: 1000;

    box-shadow: 0 -2px 10px rgba(0,0,0,0.1);

}



/* Add padding to body to account for fixed footer */

body {

    padding-bottom: 60px;

}



/* Mobile-first responsive design */

.input-card {

    background: rgba(255,255,255,0.95);

    border-radius: 16px;

    padding: 16px;

    margin: 10px 0;

    box-shadow: 0 4px 20px rgba(0,0,0,0.1);

    backdrop-filter: blur(10px);

}



.output-area {

    background: rgba(255,255,255,0.95);

    border-radius: 16px;

    padding: 16px;

    margin: 15px 0;

    min-height: 200px;

    box-shadow: 0 4px 20px rgba(0,0,0,0.1);

}



.examples-section {

    background: rgba(255,255,255,0.9);

    border-radius: 16px;

    padding: 16px;

    margin: 20px 0;

}



.main-header {

    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

    color: white;

    padding: 20px;

    border-radius: 10px;

    margin-bottom: 20px;

    text-align: center;

}



.feature-box {

    background: #f8f9fa;

    padding: 15px;

    border-radius: 8px;

    margin: 10px 0;

    border-left: 4px solid #667eea;

}



.status-indicator {

    display: inline-block;

    padding: 5px 10px;

    border-radius: 15px;

    font-size: 12px;

    font-weight: bold;

    margin: 5px;

}



.status-success {

    background-color: #d4edda;

    color: #155724;

}



.status-processing {

    background-color: #fff3cd;

    color: #856404;

}



.comparison-section {

    border: 1px solid #e0e0e0;

    border-radius: 8px;

    padding: 15px;

    margin: 10px 0;

    background: #fafafa;

}



.language-label {

    font-weight: bold;

    color: #667eea;

    padding: 5px 10px;

    background: #f0f2ff;

    border-radius: 15px;

    display: inline-block;

    margin-bottom: 10px;

    font-size: 14px;

}



.content-compare {

    background: white;

    border: 1px solid #ddd;

    border-radius: 6px;

    padding: 12px;

    min-height: 120px;

    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

    line-height: 1.5;

}



/* Reset any problematic dropdown styles */

.gradio-container * {

    pointer-events: auto;

}



/* Remove any potential blocking overlays */

.gradio-container::before,

.gradio-container::after {

    display: none;

}



/* Ensure all interactive elements work */

button, select, input, textarea, .gr-dropdown {

    pointer-events: auto !important;

    position: relative !important;

}



/* Simple dropdown fix without complex selectors */

[class*="dropdown"] {

    position: relative !important;

    z-index: 999 !important;

}



[class*="dropdown"] * {

    pointer-events: auto !important;

}



/* Make sure no overlay blocks clicks */

.gradio-container .gr-form {

    position: relative;

    z-index: 1;

}



.gradio-container .gr-block {

    position: relative;

    z-index: 1;

}



.mobile-button {

    width: 100% !important;

    padding: 15px !important;

    font-size: 1.1em !important;

    margin: 20px 0 !important;

    border-radius: 12px !important;

    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;

    border: none !important;

    color: white !important;

    font-weight: bold !important;

    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3) !important;

    transition: all 0.3s ease !important;

    cursor: pointer !important;

    position: relative !important;

    overflow: hidden !important;

}



.mobile-button:hover {

    transform: translateY(-2px) !important;

    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4) !important;

    background: linear-gradient(135deg, #5a6fd8 0%, #6b4190 100%) !important;

}



.mobile-button:active {

    transform: translateY(0px) !important;

    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3) !important;

}



/* Ripple effect for button */

.mobile-button::before {

    content: '';

    position: absolute;

    top: 50%;

    left: 50%;

    width: 0;

    height: 0;

    border-radius: 50%;

    background: rgba(255, 255, 255, 0.3);

    transform: translate(-50%, -50%);

    transition: width 0.6s, height 0.6s;

}



.mobile-button:active::before {

    width: 300px;

    height: 300px;

}



/* Loading spinner animation */

@keyframes spin {

    0% { transform: rotate(0deg); }

    100% { transform: rotate(360deg); }

}



.loading-spinner {

    display: inline-block;

    width: 20px;

    height: 20px;

    border: 3px solid rgba(255,255,255,0.3);

    border-radius: 50%;

    border-top-color: white;

    animation: spin 1s ease-in-out infinite;

    margin-right: 10px;

}



/* Button pulse effect when processing */

@keyframes pulse {

    0% {

        box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);

    }

    50% {

        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);

    }

    100% {

        box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);

    }

}



.button-processing {

    animation: pulse 2s ease-in-out infinite;

    background: linear-gradient(135deg, #FF8E53 0%, #FF6B6B 100%) !important;

}



.mobile-textbox textarea {

    border-radius: 10px !important;

    border: 2px solid #e0e0e0 !important;

    padding: 12px !important;

    font-size: 1em !important;

    line-height: 1.5 !important;

}



.mobile-compare textarea {

    border-radius: 8px !important;

    border: 1px solid #ddd !important;

    padding: 10px !important;

    background: #fafafa !important;

    font-size: 0.95em !important;

}



.mobile-audio {

    margin: 10px 0 !important;

    border-radius: 10px !important;

}



.mobile-file {

    margin: 10px 0 !important;

    border-radius: 10px !important;

}



/* Beautiful Markdown styling for Voice RAG responses */

.markdown-response {

    background: linear-gradient(135deg, #ffffff 0%, #f8fffe 100%);

    border-radius: 12px;

    padding: 20px;

    margin: 15px 0;

    box-shadow: 0 4px 20px rgba(0,0,0,0.1);

    border-left: 4px solid #4CAF50;

}



.markdown-response h1 {

    color: #2e7d32;

    border-bottom: 2px solid #4CAF50;

    padding-bottom: 10px;

    margin-bottom: 20px;

    font-size: 1.8em;

}



.markdown-response h2 {

    color: #388E3C;

    margin-top: 25px;

    margin-bottom: 15px;

    font-size: 1.4em;

    border-left: 3px solid #4CAF50;

    padding-left: 15px;

}



.markdown-response h3 {

    color: #43A047;

    margin-top: 20px;

    margin-bottom: 12px;

    font-size: 1.2em;

}



.markdown-response p {

    line-height: 1.6;

    margin-bottom: 12px;

    color: #333;

}



.markdown-response blockquote {

    background: linear-gradient(135deg, #e8f5e8 0%, #c8e6c9 100%);

    border-left: 4px solid #4CAF50;

    padding: 15px 20px;

    margin: 15px 0;

    border-radius: 8px;

    font-style: italic;

    color: #2e7d32;

}



.markdown-response table {

    width: 100%;

    border-collapse: collapse;

    margin: 15px 0;

    box-shadow: 0 2px 10px rgba(0,0,0,0.1);

    border-radius: 8px;

    overflow: hidden;

}



.markdown-response table th {

    background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);

    color: white;

    padding: 12px 15px;

    text-align: left;

    font-weight: bold;

}



.markdown-response table td {

    padding: 12px 15px;

    border-bottom: 1px solid #e0e0e0;

    background: white;

}



.markdown-response table tr:nth-child(even) td {

    background: #f8fffe;

}



.markdown-response table tr:hover td {

    background: #e8f5e8;

    transition: background 0.3s ease;

}



.markdown-response ul, .markdown-response ol {

    margin: 15px 0;

    padding-left: 25px;

}



.markdown-response li {

    margin-bottom: 8px;

    line-height: 1.5;

}



.markdown-response code {

    background: #f5f5f5;

    border: 1px solid #e0e0e0;

    border-radius: 4px;

    padding: 2px 6px;

    font-family: 'Courier New', monospace;

    color: #d32f2f;

}



.markdown-response pre {

    background: #f5f5f5;

    border: 1px solid #e0e0e0;

    border-radius: 8px;

    padding: 15px;

    overflow-x: auto;

    margin: 15px 0;

}



.markdown-response pre code {

    background: none;

    border: none;

    padding: 0;

    color: #333;

}



.markdown-response hr {

    border: none;

    height: 2px;

    background: linear-gradient(90deg, transparent, #4CAF50, transparent);

    margin: 25px 0;

}



.markdown-response strong {

    color: #2e7d32;

    font-weight: bold;

}



.markdown-response em {

    color: #388E3C;

    font-style: italic;

}



/* Responsive design for markdown */

@media (max-width: 768px) {

    .markdown-response {

        padding: 15px;

        margin: 10px 0;

    }

    

    .markdown-response table {

        font-size: 0.9em;

    }

    

    .markdown-response h1 {

        font-size: 1.6em;

    }

    

    .markdown-response h2 {

        font-size: 1.3em;

    }

}



/* Mobile responsive breakpoints */

@media (max-width: 768px) {

    .gradio-container {

        padding: 10px !important;

    }

    

    .input-card {

        padding: 12px !important;

        margin: 8px 0 !important;

    }

    

    .output-area {

        padding: 12px !important;

        margin: 10px 0 !important;

    }

    

    .examples-section {

        padding: 12px !important;

    }

    

    .main-header h2 {

        font-size: 1.5em !important;

    }

    

    .main-header p {

        font-size: 1em !important;

    }

    

    /* Mobile layout adjustments - less aggressive */

    .gr-row {

        flex-direction: column;

    }

    

    .gr-column {

        width: 100%;

        margin-bottom: 15px;

    }

}



@media (max-width: 480px) {

    .gradio-container {

        padding: 5px !important;

    }

    

    .input-card {

        padding: 10px !important;

        margin: 5px 0 !important;

    }

    

    .main-header {

        padding: 15px !important;

    }

    

    .main-header h2 {

        font-size: 1.3em !important;

    }

    

    .mobile-button {

        padding: 12px !important;

        font-size: 1em !important;

    }

}



/* JavaScript for button interactions */

"""

# Add JavaScript for button effects
js_code = """

<script>

function addButtonEffects() {

    // Find button by class since Gradio might change IDs

    const buttons = document.querySelectorAll('.mobile-button');

    

    buttons.forEach(button => {

        // Remove existing listeners to avoid duplicates

        button.removeEventListener('click', handleClick);

        

        // Add enhanced click effect

        button.addEventListener('click', handleClick);

        

        // Add hover effects for better interaction

        button.addEventListener('mouseenter', function() {

            if (!this.disabled) {

                this.style.transform = 'translateY(-2px) scale(1.02)';

            }

        });

        

        button.addEventListener('mouseleave', function() {

            if (!this.disabled) {

                this.style.transform = 'translateY(0) scale(1)';

            }

        });

    });

}



function handleClick(e) {

    const button = e.target;

    

    // Immediate visual feedback

    button.style.transform = 'scale(0.98)';

    button.style.transition = 'all 0.1s ease';

    

    setTimeout(() => {

        button.style.transform = 'scale(1)';

        button.style.transition = 'all 0.3s ease';

    }, 100);

    

    // Add processing state

    const originalText = button.innerHTML;

    button.innerHTML = '<span class="loading-spinner"></span>⏳ ĐANG XỬ LÝ...';

    button.classList.add('button-processing');

    button.disabled = true;

    

    // Monitor for completion and reset

    let checkCount = 0;

    const checkInterval = setInterval(() => {

        checkCount++;

        

        // Reset after 15 seconds max or if status changes

        const statusElements = document.querySelectorAll('[style*="Hoàn thành"]');

        if (statusElements.length > 0 || checkCount > 50) {

            clearInterval(checkInterval);

            button.innerHTML = originalText;

            button.classList.remove('button-processing');

            button.disabled = false;

            button.style.transform = 'scale(1)';

        }

    }, 300);

}



// Initialize when DOM is ready

if (document.readyState === 'loading') {

    document.addEventListener('DOMContentLoaded', addButtonEffects);

} else {

    addButtonEffects();

}



// Re-initialize periodically for Gradio updates

setInterval(addButtonEffects, 2000);

</script>

"""

# Create interface with tabs
with gr.Blocks(css=css, title="🎙️ Voice AI Platform - Voice RAG & Audio Translation") as demo:
    # Simplified header for faster loading on HF Spaces
    if not (os.environ.get("SPACE_ID") or os.environ.get("HF_SPACE_ID")):
        # Only load complex microphone permissions in local development
        gr.HTML("""

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <meta http-equiv="Permissions-Policy" content="microphone=*, camera=*, display-capture=*, autoplay=*">

        <meta http-equiv="Feature-Policy" content="microphone 'self' *; camera 'self' *; autoplay 'self' *">

        <meta name="theme-color" content="#4A90E2">

    

    <script>

    // Global microphone management

    window.microphoneStatus = {

        granted: false,

        requested: false,

        supported: false

    };

    

    // Enhanced microphone permission request for iframe and main window

    function initializeMicrophoneSupport() {

        console.log('🎤 Initializing microphone support...');

        

        // Check browser support

        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {

            window.microphoneStatus.supported = true;

            console.log('✅ Browser supports microphone');

            

            // Check current permission status

            if (navigator.permissions) {

                navigator.permissions.query({name: 'microphone'}).then(function(result) {

                    console.log('🔐 Current microphone permission:', result.state);

                    window.microphoneStatus.granted = (result.state === 'granted');

                    

                    // Update UI based on permission status

                    updateMicrophoneUI(result.state);

                    

                    // Listen for permission changes

                    result.onchange = function() {

                        console.log('🔄 Microphone permission changed to:', this.state);

                        window.microphoneStatus.granted = (this.state === 'granted');

                        updateMicrophoneUI(this.state);

                    };

                }).catch(function(err) {

                    console.log('⚠️ Permission query failed:', err);

                });

            }

            

            // Auto-request permissions if we're in iframe (with user gesture simulation)

            if (window.location !== window.parent.location && !window.microphoneStatus.requested) {

                console.log('🖼️ Running in iframe - preparing microphone access');

                window.microphoneStatus.requested = true;

                

                // Add a global click listener to request permissions on first interaction

                document.addEventListener('click', function requestOnFirstClick() {

                    if (!window.microphoneStatus.granted) {

                        console.log('👆 First click detected - requesting microphone access');

                        requestMicrophonePermission();

                        document.removeEventListener('click', requestOnFirstClick);

                    }

                }, { once: true });

            }

        } else {

            console.log('❌ Browser does not support microphone');

            window.microphoneStatus.supported = false;

            updateMicrophoneUI('unsupported');

        }

    }

    

    function requestMicrophonePermission() {

        console.log('🎤 Requesting microphone permission...');

        

        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {

            navigator.mediaDevices.getUserMedia({ 

                audio: {

                    echoCancellation: true,

                    noiseSuppression: true,

                    autoGainControl: true,

                    sampleRate: 44100

                } 

            })

            .then(function(stream) {

                console.log('✅ Microphone access granted');

                window.microphoneStatus.granted = true;

                

                // Stop the stream immediately (we just wanted permission)

                stream.getTracks().forEach(track => track.stop());

                

                updateMicrophoneUI('granted');

                

                // Notify other parts of the app

                window.dispatchEvent(new CustomEvent('microphoneGranted'));

                

            })

            .catch(function(err) {

                console.log('❌ Microphone access denied:', err);

                window.microphoneStatus.granted = false;

                updateMicrophoneUI('denied', err.message);

            });

        }

    }

    

    function updateMicrophoneUI(status, errorMessage = '') {

        // This will be called by the specific UI components

        console.log('🎛️ Updating microphone UI for status:', status);

        window.dispatchEvent(new CustomEvent('microphoneStatusChanged', { 

            detail: { status, errorMessage } 

        }));

    }

    

    // Initialize when DOM is ready

    if (document.readyState === 'loading') {

        document.addEventListener('DOMContentLoaded', initializeMicrophoneSupport);

    } else {

        initializeMicrophoneSupport();

    }

    

    // Also initialize on any dynamic content changes (for Gradio updates)

    if (window.MutationObserver) {

        const observer = new MutationObserver(function(mutations) {

            mutations.forEach(function(mutation) {

                if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {

                    // Check if audio components were added

                    const hasAudioComponent = Array.from(mutation.addedNodes).some(node => 

                        node.nodeType === 1 && (

                            node.querySelector && (

                                node.querySelector('audio') || 

                                node.querySelector('[data-testid*="audio"]') ||

                                node.classList.contains('audio')

                            )

                        )

                    );

                    

                    if (hasAudioComponent) {

                        console.log('🔄 Audio component detected, re-initializing microphone');

                        setTimeout(initializeMicrophoneSupport, 500);

                    }

                }

            });

        });

        

        observer.observe(document.body, {

            childList: true,

            subtree: true

        });

    }

    </script>

    

    <div style="text-align: center; background: linear-gradient(135deg, #4A90E2 0%, #FF6B9D 100%); color: white; padding: 20px; border-radius: 10px; margin-bottom: 20px;">

        <h1>🎙️ Voice AI Platform</h1>

        <p>Voice RAG, Audio Translation và Voice Studio - Nền tảng AI giọng nói toàn diện</p>

        <div style="margin-top: 10px; font-size: 14px; opacity: 0.9;">

            ✨ Tính năng mới: Voice RAG với 24 giọng nói đa ngôn ngữ

        </div>

        <div style="margin-top: 8px;">🧠 <strong>Digitized Brains</strong></div>

    </div>

    """)
    else:
        # Production mode - minimal header
        gr.HTML('<div style="text-align:center;"><h1>🎙️ Voice AI Platform</h1></div>')
    
    with gr.Tabs():
        # Tab 1: Voice RAG
        with gr.TabItem("📚 Voice RAG"):
            # Header section with hf_voice style
            gr.HTML("""

            <div style="display: flex; justify-content: center; gap: 15px; margin: 20px 0; flex-wrap: wrap;">

                <div style="background: linear-gradient(135deg, #FF6B6B 0%, #FF8E53 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 140px;">

                    <h4>📚 Voice RAG</h4>

                    <p style="margin: 0; font-size: 12px;">Hỏi đáp tài liệu thông minh</p>

                </div>

                <div style="background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 140px;">

                    <h4>🌍 Multi-Language</h4>

                    <p style="margin: 0; font-size: 12px;">13 ngôn ngữ trả lời</p>

                </div>

                <div style="background: linear-gradient(135deg, #45B7D1 0%, #96C93D 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 140px;">

                    <h4>🎤 Voice Output</h4>

                    <p style="margin: 0; font-size: 12px;">24 giọng nói đa dạng</p>

                </div>

                <div style="background: linear-gradient(135deg, #A8E6CF 0%, #88D8A3 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 140px;">

                    <h4>🔄 AI Gemini</h4>

                    <p style="margin: 0; font-size: 12px;">Gemini 2.0 Flash</p>

                </div>

            </div>

            """)
            
            gr.Markdown("### 📝 Upload tài liệu và đặt câu hỏi")
            
            # Input section - Mobile optimized
            with gr.Column():
                # Document upload
                with gr.Row():
                    file_upload_rag = gr.File(
                        label="📎 Tải lên tài liệu (PDF, DOCX, TXT)", 
                        file_types=[".pdf", ".docx", ".txt"]
                    )
                
                # Question input
                with gr.Row():
                    question_input_rag = gr.Textbox(
                        label="❓ Câu hỏi của bạn",
                        placeholder="Hãy đặt câu hỏi về nội dung tài liệu...",
                        lines=3
                    )
                
                # Language selection for answer
                with gr.Row():
                    answer_language_dropdown_rag = gr.Dropdown(
                        choices=SUPPORTED_LANGUAGES,
                        value="Vietnamese",
                        label="🌍 Ngôn ngữ trả lời"
                    )
                
                # Voice selection từ Voice Studio
                with gr.Row():
                    with gr.Column(scale=1):
                        rag_country_dropdown = gr.Dropdown(
                            choices=list(voice_choices_by_country.keys()),
                            value="🇻🇳 Việt Nam",
                            label="🌍 Chọn quốc gia giọng nói"
                        )
                    
                    with gr.Column(scale=1):
                        rag_voice_dropdown = gr.Dropdown(
                            choices=voice_choices_by_country["🇻🇳 Việt Nam"],
                            value="🇻🇳 HoaiMy - Nữ Việt Chuẩn",
                            label="🎭 Chọn giọng nói"
                        )
                
                # Format selection for download
                with gr.Row():
                    rag_text_format_dropdown = gr.Dropdown(
                        choices=["Markdown (.md)", "TXT (.txt)", "Word (.docx)"] if DOCX_AVAILABLE else ["Markdown (.md)", "TXT (.txt)"],
                        value="Markdown (.md)",
                        label="📄 Định dạng file trả lời"
                    )
                
                # Process button
                with gr.Row():
                    submit_btn_rag = gr.Button(
                        "🚀 Xử lý tài liệu và trả lời", 
                        variant="primary",
                        size="lg"
                    )
            
            # Results section - Mobile optimized
            with gr.Column():
                # Document info section
                with gr.Accordion("📄 Thông tin tài liệu", open=True):
                    detected_doc_language_rag = gr.Textbox(
                        label="🌐 Ngôn ngữ tài liệu được phát hiện",
                        lines=1,
                        interactive=False,
                        placeholder="Tự động nhận diện ngôn ngữ tài liệu..."
                    )
                
                # Text answer section
                with gr.Accordion("💬 Câu trả lời", open=True):
                    gr.HTML("""

                    <div style="

                        background: linear-gradient(135deg, #e8f5e8 0%, #c8e6c9 100%);

                        padding: 15px;

                        border-radius: 12px;

                        margin: 15px 0;

                        border-left: 4px solid #4CAF50;

                        text-align: center;

                    ">

                        <h4 style="margin: 0 0 10px 0; color: #2e7d32;">💬 AI Response with Markdown Formatting</h4>

                        <p style="color: #388E3C; margin: 0; font-style: italic;">

                            Formatted response with tables, headers, and beautiful layout

                        </p>

                    </div>

                    """)
                    
                    answer_output_rag = gr.Markdown(
                        value="**Câu trả lời sẽ xuất hiện ở đây sau khi xử lý...**\n\n*Hỗ trợ format Markdown với tables, headers, lists và nhiều style khác*",
                        label="",
                        show_label=False,
                        elem_classes=["markdown-response"]
                    )
                
                # Downloads section - Mobile optimized
                with gr.Accordion("💾 Tải xuống kết quả", open=True):
                    gr.HTML("""

                    <div style="text-align: center; margin-bottom: 15px;">

                        <p style="color: #666; font-style: italic;">Tải xuống câu trả lời dưới dạng file và audio</p>

                    </div>

                    """)
                    
                    # Stack vertically on mobile
                    with gr.Column():
                        # Audio download section
                        with gr.Row():
                            audio_output_rag = gr.Audio(
                                label="🔊 Audio câu trả lời",
                                type="filepath"
                            )
                        
                        # Text download section  
                        with gr.Row():
                            text_output_rag = gr.File(
                                label="📄 Văn bản câu trả lời",
                                file_count="single",
                                file_types=[".md", ".txt", ".docx"]
                            )
            
            # Status indicator for RAG
            rag_status_text = gr.HTML("""

            <div style="text-align: center; margin: 20px 0;">

                <div style="

                    background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);

                    color: white;

                    padding: 15px;

                    border-radius: 12px;

                    box-shadow: 0 4px 15px rgba(78,205,196,0.3);

                ">

                    <span style="font-weight: bold; font-size: 1.1em;">✅ Sẵn sàng xử lý tài liệu</span>

                </div>

            </div>

            """)
            
            # Helper function for RAG format
            def get_rag_format_from_dropdown(format_choice):
                if "Word" in format_choice or "docx" in format_choice:
                    return "docx"
                elif "Markdown" in format_choice or "md" in format_choice:
                    return "md"
                return "txt"
            
            # RAG processing function
            def update_rag_status_processing():
                return """

                <div style="text-align: center; margin: 20px 0;">

                    <div style="

                        background: linear-gradient(135deg, #FF8E53 0%, #FF6B6B 100%);

                        color: white;

                        padding: 15px;

                        border-radius: 12px;

                        box-shadow: 0 4px 15px rgba(255,142,83,0.3);

                    ">

                        <span style="font-weight: bold; font-size: 1.1em;">⏳ Đang xử lý tài liệu...</span>

                    </div>

                </div>

                """
            
            def update_rag_status_complete():
                return """

                <div style="text-align: center; margin: 20px 0;">

                    <div style="

                        background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);

                        color: white;

                        padding: 15px;

                        border-radius: 12px;

                        box-shadow: 0 4px 15px rgba(78,205,196,0.3);

                    ">

                        <span style="font-weight: bold; font-size: 1.1em;">✅ Xử lý hoàn thành!</span>

                    </div>

                </div>

                """
            
            # Event handlers for Voice RAG
            rag_country_dropdown.change(
                fn=update_voices,
                inputs=[rag_country_dropdown],
                outputs=[rag_voice_dropdown]
            )
            
            submit_btn_rag.click(
                fn=lambda: update_rag_status_processing(),
                outputs=[rag_status_text]
            ).then(
                fn=lambda file, question, lang, voice, fmt: voice_rag_pipeline(file, question, lang, voice, get_rag_format_from_dropdown(fmt)),
                inputs=[file_upload_rag, question_input_rag, answer_language_dropdown_rag, rag_voice_dropdown, rag_text_format_dropdown],
                outputs=[answer_output_rag, detected_doc_language_rag, audio_output_rag, text_output_rag]
            ).then(
                fn=lambda: update_rag_status_complete(),
                outputs=[rag_status_text]
            )
        
        # Voice Studio Tab
        with gr.TabItem("🎤 Voice Studio"):
            gr.HTML("""

            <div style="display: flex; justify-content: center; gap: 15px; margin: 20px 0; flex-wrap: wrap;">

                <div style="background: linear-gradient(135deg, #FF6B6B 0%, #FF8E53 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 150px;">

                    <h4>🇻🇳 Tiếng Việt</h4>

                    <p style="margin: 0; font-size: 12px;">2 giọng chuẩn</p>

                    <p style="margin: 0; font-size: 10px;">HoaiMy • NamMinh</p>

                </div>

                <div style="background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 150px;">

                    <h4>🇺🇸🇬🇧 English</h4>

                    <p style="margin: 0; font-size: 12px;">4 giọng chuẩn</p>

                    <p style="margin: 0; font-size: 10px;">US • UK</p>

                </div>

                <div style="background: linear-gradient(135deg, #45B7D1 0%, #96C93D 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 150px;">

                    <h4>🌍 Đa ngôn ngữ</h4>

                    <p style="margin: 0; font-size: 12px;">20 giọng chuẩn</p>

                    <p style="margin: 0; font-size: 10px;">10 ngôn ngữ</p>

                </div>

            </div>

            """)
            
            gr.Markdown("### 📝 Nhập nội dung và chọn giọng nói")
            
            with gr.Row():
                text_input = gr.Textbox(
                    placeholder="Nhập văn bản cần chuyển thành giọng nói...",
                    lines=4,
                    label="Văn bản",
                    scale=2
                )
            
            with gr.Row():
                with gr.Column(scale=1):
                    country_dropdown = gr.Dropdown(
                        choices=list(voice_choices_by_country.keys()),
                        value="🇻🇳 Việt Nam",
                        label="🌍 Chọn quốc gia"
                    )
                
                with gr.Column(scale=1):
                    voice_dropdown = gr.Dropdown(
                        choices=voice_choices_by_country["🇻🇳 Việt Nam"],
                        value="🇻🇳 HoaiMy - Nữ Việt Chuẩn",
                        label="🎭 Chọn giọng nói"
                    )
            
            with gr.Row():
                with gr.Column(scale=2):
                    speed_slider = gr.Slider(
                        minimum=0.5,
                        maximum=2.0,
                        value=1.0,
                        step=0.1,
                        label="⚡ Tốc độ phát"
                    )
                with gr.Column(scale=1):
                    voice_studio_format_dropdown = gr.Dropdown(
                        choices=["Markdown (.md)", "TXT (.txt)", "Word (.docx)"] if DOCX_AVAILABLE else ["Markdown (.md)", "TXT (.txt)"],
                        value="Markdown (.md)",
                        label="📄 Định dạng file tải xuống"
                    )
            
            # Translation feature
            with gr.Row():
                with gr.Column(scale=1):
                    translate_checkbox = gr.Checkbox(
                        label="🌍 Dịch văn bản trước khi tạo giọng nói",
                        value=False
                    )
                with gr.Column(scale=2):
                    translate_btn = gr.Button("🔄 DỊCH VĂN BẢN", variant="secondary", size="lg", visible=False)
                
            # Show translated text when translation is enabled
            translated_text_output = gr.Textbox(
                label="📝 Văn bản đã dịch",
                lines=3,
                interactive=True,
                visible=False,
                placeholder="Văn bản sau khi dịch sẽ hiển thị ở đây..."
            )
                
            generate_btn = gr.Button("🎵 TẠO GIỌNG NÓI", variant="primary", size="lg")
            
            # Status indicator for Voice Studio
            studio_status_text = gr.HTML("""

            <div style="text-align: center; margin: 20px 0;">

                <div style="

                    background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);

                    color: white;

                    padding: 15px;

                    border-radius: 12px;

                    box-shadow: 0 4px 15px rgba(78,205,196,0.3);

                ">

                    <span style="font-weight: bold; font-size: 1.1em;">⚡ Sẵn sàng tạo giọng nói</span>

                </div>

            </div>

            """)
            
            gr.Markdown("### 🎧 Kết quả âm thanh")
            audio_output_vs = gr.HTML(
                value="<p style='text-align: center; color: #666; padding: 40px;'>Nhấn 'TẠO GIỌNG NÓI' để bắt đầu 🎤</p>"
            )
            
            # Download section for Voice Studio
            with gr.Accordion("💾 Tải xuống kết quả", open=False):
                gr.HTML("""

                <div style="

                    background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);

                    padding: 15px;

                    border-radius: 12px;

                    margin: 15px 0;

                    border-left: 4px solid #2196F3;

                    text-align: center;

                ">

                    <h4 style="margin: 0 0 10px 0; color: #1976D2;">📄 Tải xuống văn bản với Markdown formatting</h4>

                    <p style="color: #1565C0; margin: 0; font-style: italic;">

                        File chứa thông tin session, cấu hình giọng nói và technical details

                    </p>

                </div>

                """)
                
                voice_studio_text_output = gr.File(
                    label="📄 Văn bản với thông tin chi tiết",
                    file_count="single",
                    file_types=[".md", ".txt", ".docx"]
                )
            
            # Examples section
            gr.Markdown("### 📚 Ví dụ nhanh")
            with gr.Row():
                example_vn = gr.Button("🇻🇳 Tiếng Việt", size="sm")
                example_en = gr.Button("🇺🇸 English", size="sm") 
                example_de = gr.Button("🇩🇪 Deutsch", size="sm")
                example_translate = gr.Button("🌍 Dịch thuật", size="sm")
            
            # Example button functions
            def load_vn_example():
                return "Xin chào! Chào mừng bạn đến với studio giọng nói.", "🇻🇳 Việt Nam"
            
            def load_en_example():
                return "Hello! Welcome to our voice studio.", "🇺🇸 Hoa Kỳ"
                
            def load_de_example():
                return "Hallo! Willkommen in unserem Sprachstudio.", "🇩🇪 Đức"
            
            def load_translate_example():
                return "Hello! This is an example text for translation.", "🇺🇸 Hoa Kỳ", True
            
            # Translation functions
            def toggle_translation_ui(translate_enabled):
                """Show/hide translation UI elements"""
                return (
                    gr.update(visible=translate_enabled),  # translate_btn
                    gr.update(visible=translate_enabled)   # translated_text_output
                )
            
            def translate_text_interface(text, voice_selection):
                """Translate text for Voice Studio"""
                if not text.strip():
                    return "Vui lòng nhập văn bản trước khi dịch"
                
                target_language = get_target_language_from_voice(voice_selection)
                translated = translate_text_with_gemini(text, target_language)
                return translated
            
            def create_voice_with_translation(original_text, translated_text, translate_enabled, voice_selection, speed, text_format="txt"):
                """Create voice using original or translated text"""
                if translate_enabled and translated_text.strip() and not translated_text.startswith("Lỗi"):
                    # Use translated text
                    return create_audio_voice_studio(translated_text, voice_selection, speed, text_format)
                else:
                    # Use original text
                    return create_audio_voice_studio(original_text, voice_selection, speed, text_format)
                
            # Event handlers for Voice Studio
            country_dropdown.change(
                fn=update_voices,
                inputs=[country_dropdown],
                outputs=[voice_dropdown]
            )
            
            example_vn.click(
                fn=load_vn_example,
                outputs=[text_input, country_dropdown]
            )
            
            example_en.click(
                fn=load_en_example,
                outputs=[text_input, country_dropdown]
            )
            
            example_de.click(
                fn=load_de_example,
                outputs=[text_input, country_dropdown]
            )
            
            example_translate.click(
                fn=load_translate_example,
                outputs=[text_input, country_dropdown, translate_checkbox]
            )
            
            # Translation UI toggle
            translate_checkbox.change(
                fn=toggle_translation_ui,
                inputs=[translate_checkbox],
                outputs=[translate_btn, translated_text_output]
            )
            
            # Translation button
            translate_btn.click(
                fn=translate_text_interface,
                inputs=[text_input, voice_dropdown],
                outputs=[translated_text_output]
            )
            
            # Helper function to extract format and process Voice Studio
            def process_voice_studio(original_text, translated_text, translate_enabled, voice_selection, speed, format_choice):
                """Process Voice Studio with format support"""
                # Extract format from dropdown
                if "Markdown" in format_choice:
                    text_format = "md"
                elif "Word" in format_choice:
                    text_format = "docx"
                else:
                    text_format = "txt"
                
                return create_voice_with_translation(original_text, translated_text, translate_enabled, voice_selection, speed, text_format)
            
            # Generate voice with translation support
            generate_btn.click(
                fn=process_voice_studio,
                inputs=[text_input, translated_text_output, translate_checkbox, voice_dropdown, speed_slider, voice_studio_format_dropdown],
                outputs=[audio_output_vs, voice_studio_text_output]
            )
        
        # Audio Translation Tab
        with gr.TabItem("🎙️ Audio Translation"):
            # Colorful feature cards like Voice Studio
            gr.HTML("""

            <div style="display: flex; justify-content: center; gap: 15px; margin: 20px 0; flex-wrap: wrap;">

                <div style="background: linear-gradient(135deg, #FF6B6B 0%, #FF8E53 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 150px;">

                    <h4>🎤 Ghi âm</h4>

                    <p style="margin: 0; font-size: 12px;">Microphone</p>

                    <p style="margin: 0; font-size: 10px;">Real-time</p>

                </div>

                <div style="background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 150px;">

                    <h4>📁 Upload</h4>

                    <p style="margin: 0; font-size: 12px;">Audio Files</p>

                    <p style="margin: 0; font-size: 10px;">WAV • MP3</p>

                </div>

                <div style="background: linear-gradient(135deg, #45B7D1 0%, #96C93D 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 150px;">

                    <h4>🔄 AI Dịch</h4>

                    <p style="margin: 0; font-size: 12px;">13 ngôn ngữ</p>

                    <p style="margin: 0; font-size: 10px;">Gemini 2.0</p>

                </div>

                <div style="background: linear-gradient(135deg, #A855F7 0%, #EC4899 100%); padding: 15px; border-radius: 10px; color: white; text-align: center; min-width: 150px;">

                    <h4>🎵 Tổng hợp</h4>

                    <p style="margin: 0; font-size: 12px;">Neural TTS</p>

                    <p style="margin: 0; font-size: 10px;">26 giọng</p>

                </div>

            </div>

            """)
            
            # Input section with colorful design
            gr.HTML("""

            <div style="

                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

                color: white;

                padding: 20px;

                border-radius: 15px;

                margin: 20px 0;

                text-align: center;

                box-shadow: 0 8px 32px rgba(0,0,0,0.2);

            ">

                <h3 style="margin: 0 0 10px 0;">🎤 Tải lên file audio hoặc ghi âm trực tiếp</h3>

                <p style="margin: 0; opacity: 0.9; font-size: 0.95em;">

                    Hỗ trợ file WAV, MP3 hoặc ghi âm real-time qua microphone

                </p>

            </div>

            """)
            
            # Enhanced microphone permission notice and controls
            if not (os.environ.get("SPACE_ID") or os.environ.get("HF_SPACE_ID")):
                gr.HTML("""

                <div id="microphone-section" style="margin: 15px 0;">

                    <!-- Microphone Status Indicator -->

                    <div id="mic-status" style="

                    background: linear-gradient(135deg, #e8f5e8 0%, #c8e6c9 100%);

                    color: #2e7d32;

                    padding: 12px;

                    border-radius: 8px;

                    margin-bottom: 10px;

                    text-align: center;

                    border: 1px solid #4caf50;

                    display: none;

                ">

                    <strong>🎤 Microphone Ready</strong> - Bạn có thể ghi âm trực tiếp

                </div>

                

                <!-- Microphone Error/Permission Notice -->

                <div id="microphone-notice" style="

                    background: linear-gradient(135deg, #fff3cd 0%, #ffeaa7 100%);

                    color: #856404;

                    padding: 15px;

                    border-radius: 10px;

                    border: 1px solid #ffeaa7;

                    text-align: center;

                    display: none;

                ">

                    <strong>🎤 Microphone Access Required</strong><br>

                    Để sử dụng ghi âm, vui lòng cho phép truy cập microphone.<br>

                    <button onclick="requestMicrophoneAccess()" style="

                        background: #4caf50; 

                        color: white; 

                        padding: 8px 16px; 

                        border: none;

                        border-radius: 6px;

                        cursor: pointer;

                        margin: 8px 4px;

                    ">🎤 Kích hoạt Microphone</button>

                    <a href="#" onclick="window.open(window.location.href, '_blank')" style="

                        background: #667eea; 

                        color: white; 

                        padding: 8px 16px; 

                        text-decoration: none; 

                        border-radius: 6px;

                        display: inline-block;

                        margin: 8px 4px;

                    ">🔗 Mở cửa sổ mới</a>

                </div>

                

                <!-- Iframe Warning -->

                <div id="iframe-warning" style="

                    background: linear-gradient(135deg, #ffebee 0%, #ffcdd2 100%);

                    color: #c62828;

                    padding: 12px;

                    border-radius: 8px;

                    border: 1px solid #f44336;

                    text-align: center;

                    display: none;

                ">

                    <strong>⚠️ Iframe Restriction</strong><br>

                    Microphone có thể bị hạn chế trong iframe. 

                    <a href="#" onclick="window.open(window.location.href, '_blank')" style="color: #c62828; text-decoration: underline;">

                        Mở trong cửa sổ mới

                    </a> để sử dụng đầy đủ tính năng.

                </div>

            </div>

            

            <script>

            // Enhanced microphone permission handling

            let microphoneAccess = false;

            

            function requestMicrophoneAccess() {

                console.log('🎤 Audio Translation: Requesting microphone access...');

                

                // Use global microphone function if available

                if (window.requestMicrophonePermission) {

                    window.requestMicrophonePermission();

                    return;

                }

                

                // Fallback to local implementation

                if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {

                    navigator.mediaDevices.getUserMedia({ 

                        audio: {

                            echoCancellation: true,

                            noiseSuppression: true,

                            autoGainControl: true,

                            sampleRate: 44100

                        } 

                    })

                    .then(function(stream) {

                        console.log('✅ Audio Translation: Microphone access granted');

                        microphoneAccess = true;

                        

                        // Show success status

                        updateLocalMicrophoneUI('granted');

                        

                        // Stop the stream (we just wanted permission)

                        stream.getTracks().forEach(track => track.stop());

                        

                        // Trigger Gradio audio component refresh

                        setTimeout(() => {

                            const audioComponents = document.querySelectorAll('[data-testid*="audio"]');

                            audioComponents.forEach(comp => {

                                // Try to trigger a refresh or re-initialization

                                if (comp.click) comp.click();

                            });

                        }, 500);

                        

                        // Update global status if available

                        if (window.microphoneStatus) {

                            window.microphoneStatus.granted = true;

                        }

                        

                    })

                    .catch(function(err) {

                        console.log('❌ Audio Translation: Microphone access denied:', err);

                        updateLocalMicrophoneUI('denied', err.message);

                    });

                } else {

                    console.log('❌ getUserMedia not supported');

                    updateLocalMicrophoneUI('unsupported');

                }

            }

            

            function updateLocalMicrophoneUI(status, errorMessage = '') {

                const micStatus = document.getElementById('mic-status');

                const micNotice = document.getElementById('microphone-notice');

                

                switch(status) {

                    case 'granted':

                        if (micStatus) micStatus.style.display = 'block';

                        if (micNotice) micNotice.style.display = 'none';

                        microphoneAccess = true;

                        break;

                        

                    case 'denied':

                        if (micNotice) {

                            micNotice.style.display = 'block';

                            micNotice.innerHTML = `

                                <strong>❌ Microphone Access Denied</strong><br>

                                Lỗi: ${errorMessage}<br>

                                Vui lòng kiểm tra cài đặt trình duyệt và cho phép microphone.

                                <br><br>

                                <button onclick="requestMicrophoneAccess()" style="

                                    background: #ff9800; 

                                    color: white; 

                                    padding: 8px 16px; 

                                    border: none;

                                    border-radius: 6px;

                                    cursor: pointer;

                                    margin: 4px;

                                ">🔄 Thử lại</button>

                                <button onclick="window.open(window.location.href, '_blank')" style="

                                    background: #2196f3; 

                                    color: white; 

                                    padding: 8px 16px; 

                                    border: none;

                                    border-radius: 6px;

                                    cursor: pointer;

                                    margin: 4px;

                                ">🔗 Mở cửa sổ mới</button>

                            `;

                        }

                        break;

                        

                    case 'unsupported':

                        if (micNotice) {

                            micNotice.style.display = 'block';

                            micNotice.innerHTML = `

                                <strong>❌ Microphone Not Supported</strong><br>

                                Trình duyệt của bạn không hỗ trợ ghi âm.<br>

                                Vui lòng sử dụng Chrome, Firefox, Safari hoặc Edge phiên bản mới.

                                <br><br>

                                <a href="https://caniuse.com/stream" target="_blank" style="

                                    color: #856404;

                                    text-decoration: underline;

                                ">Kiểm tra tương thích trình duyệt</a>

                            `;

                        }

                        break;

                        

                    default:

                        if (micNotice) {

                            micNotice.style.display = 'block';

                        }

                        break;

                }

            }

            

            // Listen for global microphone events

            window.addEventListener('microphoneStatusChanged', function(event) {

                console.log('🔄 Audio Translation: Received microphone status update:', event.detail);

                updateLocalMicrophoneUI(event.detail.status, event.detail.errorMessage);

            });

            

            window.addEventListener('microphoneGranted', function() {

                console.log('✅ Audio Translation: Global microphone granted');

                updateLocalMicrophoneUI('granted');

            });

            

            // Check microphone availability on load

            function checkMicrophoneAvailability() {

                console.log('🔍 Audio Translation: Checking microphone availability...');

                

                // Check global status first

                if (window.microphoneStatus) {

                    if (window.microphoneStatus.granted) {

                        updateLocalMicrophoneUI('granted');

                        return;

                    } else if (!window.microphoneStatus.supported) {

                        updateLocalMicrophoneUI('unsupported');

                        return;

                    }

                }

                

                // Check if we're in an iframe

                if (window.location !== window.parent.location) {

                    console.log('Running in iframe');

                    const iframeWarning = document.getElementById('iframe-warning');

                    if (iframeWarning) {

                        setTimeout(() => {

                            iframeWarning.style.display = 'block';

                        }, 1000);

                    }

                }

                

                // Try to get microphone permissions

                if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {

                    // Check if we already have permission

                    navigator.permissions.query({name: 'microphone'}).then(function(result) {

                        console.log('Microphone permission status:', result.state);

                        

                        if (result.state === 'granted') {

                            const micStatus = document.getElementById('mic-status');

                            if (micStatus) micStatus.style.display = 'block';

                            microphoneAccess = true;

                        } else if (result.state === 'prompt' || result.state === 'denied') {

                            const micNotice = document.getElementById('microphone-notice');

                            if (micNotice) {

                                setTimeout(() => {

                                    micNotice.style.display = 'block';

                                }, 1500);

                            }

                        }

                        

                        // Listen for permission changes

                        result.onchange = function() {

                            console.log('Microphone permission changed to:', this.state);

                            if (this.state === 'granted') {

                                const micStatus = document.getElementById('mic-status');

                                const micNotice = document.getElementById('microphone-notice');

                                if (micStatus) micStatus.style.display = 'block';

                                if (micNotice) micNotice.style.display = 'none';

                                microphoneAccess = true;

                            }

                        };

                    }).catch(function(err) {

                        console.log('Permission query failed:', err);

                        // Fallback to showing the notice

                        setTimeout(() => {

                            const micNotice = document.getElementById('microphone-notice');

                            if (micNotice) micNotice.style.display = 'block';

                        }, 2000);

                    });

                } else {

                    // Browser doesn't support getUserMedia

                    setTimeout(() => {

                        const micNotice = document.getElementById('microphone-notice');

                        if (micNotice) {

                            micNotice.style.display = 'block';

                            micNotice.innerHTML = `

                                <strong>❌ Microphone Not Supported</strong><br>

                                Trình duyệt không hỗ trợ ghi âm. Vui lòng cập nhật trình duyệt.

                            `;

                        }

                    }, 1000);

                }

            }

            

            // Initialize when DOM is ready

            if (document.readyState === 'loading') {

                document.addEventListener('DOMContentLoaded', checkMicrophoneAvailability);

            } else {

                checkMicrophoneAvailability();

            }

            

            // Re-check periodically for dynamic content

            setInterval(checkMicrophoneAvailability, 5000);

            </script>

            """)
            else:
                # Production mode - simple microphone notice
                gr.HTML('<div style="text-align:center;color:#666;padding:10px;">📎 Upload audio file or use microphone</div>')
            
            audio_input = gr.Audio(
                label="📎 Tải lên file audio hoặc ghi âm trực tiếp",
                type="numpy",  # Use numpy to avoid temp file issues
                sources=["upload", "microphone"],
                show_label=True,
                interactive=True,
                elem_id="audio-input-translation"
            )
            
            # Audio Recording Control Buttons
            with gr.Row():
                save_recording_btn = gr.Button(
                    "💾 Save Recording",
                    variant="secondary",
                    size="sm"
                )
                new_recording_btn = gr.Button(
                    "🎙️ New Record", 
                    variant="primary",
                    size="sm"
                )
            
            # Button descriptions
            gr.HTML("""

            <div style="display: flex; justify-content: space-between; margin: 5px 0 15px 0; font-size: 0.8em; color: #666;">

                <span>💾 Lưu file audio hiện tại vào record_data</span>

                <span>🎙️ Xóa audio hiện tại để ghi âm mới</span>

            </div>

            """)
            
            # Status for recording actions
            recording_status = gr.HTML(
                value="<p style='text-align: center; color: #666; font-style: italic;'>Sẵn sàng ghi âm hoặc tải lên file</p>"
            )
            
            # === RECORDED FILES FUNCTIONS ===
            def refresh_recorded_files():
                """Refresh the list of recorded files"""
                files = get_recorded_files()
                print(f"🔄 Refreshing dropdown - found files: {files}")
                return gr.Dropdown(choices=files, value=None)
            
            def load_recorded_file(filename):
                """Load selected recorded file for playback"""
                print(f"🎵 Loading recorded file: {filename}")
                if filename and filename.strip():
                    file_path = get_recorded_file_path(filename)
                    print(f"📁 Full path: {file_path}")
                    if os.path.exists(file_path):
                        file_size = os.path.getsize(file_path)
                        print(f"✅ File exists, size: {file_size} bytes")
                        
                        try:
                            # Load audio as numpy array for Gradio compatibility
                            import soundfile as sf
                            audio_data, sample_rate = sf.read(file_path)
                            print(f"🎵 Loaded audio: shape={audio_data.shape}, sr={sample_rate}")
                            # Return tuple (sample_rate, audio_data) for Gradio numpy type
                            return (sample_rate, audio_data)
                        except Exception as e:
                            print(f"❌ Error loading audio: {e}")
                            return None
                    else:
                        print(f"❌ File not found: {file_path}")
                        print(f"📁 Directory contents: {os.listdir(os.path.dirname(file_path)) if os.path.exists(os.path.dirname(file_path)) else 'Directory not found'}")
                else:
                    print("❌ No filename provided")
                return None
            
            def use_recorded_for_translation(filename, country, voice, fmt):
                """Use selected recorded file for translation"""
                print(f"🔄 Using recorded file for translation: {filename}")
                if filename and filename.strip():
                    file_path = get_recorded_file_path(filename)
                    print(f"📁 Translation file path: {file_path}")
                    if os.path.exists(file_path):
                        print(f"✅ Starting translation for: {filename}")
                        # Use the same translation function
                        return translate_audio(file_path, country, voice, get_format_from_dropdown(fmt))
                    else:
                        print(f"❌ File not found for translation: {file_path}")
                # Return empty results if no file selected
                print("❌ No file selected for translation")
                return "", "", "", "", None, "", "", None
            
            def prepare_recorded_file_download(filename):
                """Prepare recorded file for download"""
                print(f"📥 Preparing download for: {filename}")
                if filename and filename.strip():
                    file_path = get_recorded_file_path(filename)
                    print(f"📁 Download file path: {file_path}")
                    if os.path.exists(file_path):
                        print(f"✅ File ready for download: {filename}")
                        return file_path
                    else:
                        print(f"❌ Download file not found: {file_path}")
                print("❌ No file selected for download")
                return None
            
            def save_current_recording(audio_file):
                """Save current audio recording to record_data"""
                if audio_file is None:
                    current_files = get_recorded_files()
                    return (
                        "<p style='color: #e74c3c; text-align: center;'>❌ Không có file audio để lưu</p>",
                        gr.Dropdown(choices=current_files, value=None)
                    )
                
                try:
                    saved_path = save_recorded_audio(audio_file)
                    if saved_path:
                        saved_filename = os.path.basename(saved_path)
                        # Get updated file list after saving
                        updated_files = get_recorded_files()
                        print(f"🔄 After save - updated files: {updated_files}")
                        return (
                            f"<p style='color: #27ae60; text-align: center;'>✅ Đã lưu: {saved_filename}</p>",
                            gr.Dropdown(choices=updated_files, value=saved_filename)
                        )
                    else:
                        current_files = get_recorded_files()
                        return (
                            "<p style='color: #e74c3c; text-align: center;'>❌ Lỗi khi lưu file</p>",
                            gr.Dropdown(choices=current_files, value=None)
                        )
                except Exception as e:
                    current_files = get_recorded_files()
                    return (
                        f"<p style='color: #e74c3c; text-align: center;'>❌ Lỗi: {str(e)}</p>",
                        gr.Dropdown(choices=current_files, value=None)
                    )
            
            def clear_audio_for_new_recording():
                """Clear audio input for new recording"""
                return (
                    None,  # Clear audio input
                    "<p style='color: #3498db; text-align: center;'>🎙️ Sẵn sàng ghi âm mới</p>"
                )
            
            def delete_selected_file(filename):
                """Delete selected file and refresh dropdown"""
                if not filename or not filename.strip():
                    current_files = get_recorded_files()
                    return (
                        "<p style='color: #e74c3c; text-align: center;'>❌ Vui lòng chọn file để xóa</p>",
                        gr.Dropdown(choices=current_files, value=None),
                        None  # Clear audio player
                    )
                
                # Delete the file
                delete_result = delete_recorded_file(filename)
                
                # Refresh file list
                updated_files = get_recorded_files()
                
                # Determine status color based on result
                if "✅" in delete_result:
                    status_html = f"<p style='color: #27ae60; text-align: center;'>{delete_result}</p>"
                else:
                    status_html = f"<p style='color: #e74c3c; text-align: center;'>{delete_result}</p>"
                
                return (
                    status_html,
                    gr.Dropdown(choices=updated_files, value=None),
                    None  # Clear audio player
                )
            
            # Recorded Files Management Section
            with gr.Accordion("🎤 File đã ghi âm", open=False):
                gr.HTML("""

                <div style="

                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

                    color: white;

                    padding: 15px;

                    border-radius: 12px;

                    margin: 15px 0;

                    text-align: center;

                ">

                    <h4 style="margin: 0 0 8px 0;">📁 Quản lý file đã ghi</h4>

                    <p style="margin: 0; opacity: 0.9; font-size: 0.9em;">

                        Chọn file từ danh sách để phát lại hoặc dịch thuật

                    </p>

                </div>

                """)
                
                # Refresh button for recorded files
                refresh_files_btn = gr.Button(
                    "🔄 Làm mới danh sách",
                    variant="secondary",
                    size="sm"
                )
                
                # Status display for file operations
                file_operation_status = gr.HTML(
                    value="<p style='text-align: center; color: #666; font-style: italic;'>Chọn file để thực hiện thao tác</p>"
                )
                
                # Dropdown for recorded files  
                initial_files = get_recorded_files()
                print(f"🔍 Initial recorded files: {initial_files}")
                recorded_files_dropdown = gr.Dropdown(
                    choices=initial_files,
                    label="📂 Chọn file đã ghi",
                    info="Các file audio đã được ghi âm trước đó"
                )
                
                # Preview and controls for selected file
                with gr.Row():
                    with gr.Column():
                        # Audio player for selected file
                        recorded_audio_player = gr.Audio(
                            label="🎵 Phát lại file đã chọn",
                            interactive=False,
                            show_label=True,
                            type="numpy"  # Use numpy for better compatibility
                        )
                    
                    with gr.Column():
                        # Action buttons
                        use_for_translation_btn = gr.Button(
                            "🔄 Sử dụng để dịch thuật",
                            variant="primary",
                            size="sm"
                        )
                        
                        with gr.Row():
                            download_recorded_btn = gr.Button(
                                "📥 Tải xuống",
                                variant="secondary", 
                                size="sm"
                            )
                            
                            delete_recorded_btn = gr.Button(
                                "🗑️ Xóa file",
                                variant="stop",
                                size="sm"
                            )
                        
                        # Download link for recorded file
                        download_recorded_file = gr.File(
                            label="📥 File tải xuống",
                            visible=True,
                            file_count="single"
                        )
            
            # Settings section with gradient header
            gr.HTML("""

            <div style="

                background: linear-gradient(135deg, #FF6B6B 0%, #FF8E53 100%);

                color: white;

                padding: 18px;

                border-radius: 12px;

                margin: 25px 0 20px 0;

                text-align: center;

                box-shadow: 0 6px 24px rgba(255,107,107,0.3);

            ">

                <h3 style="margin: 0 0 8px 0;">🌍 Cài đặt dịch thuật</h3>

                <p style="margin: 0; opacity: 0.9; font-size: 0.9em;">

                    Chọn ngôn ngữ đích và giọng nói cho kết quả dịch thuật

                </p>

            </div>

            """)
                
            # Separate dropdowns without complex wrappers to avoid CSS conflicts
            target_country_dropdown = gr.Dropdown(
                choices=list(voice_choices_by_country.keys()),
                value="🇻🇳 Việt Nam",
                label="🌍 Chọn quốc gia đích"
            )
            
            target_voice_dropdown = gr.Dropdown(
                choices=voice_choices_by_country["🇻🇳 Việt Nam"],
                value="🇻🇳 HoaiMy - Nữ Việt Chuẩn",
                label="🎭 Chọn giọng nói đích"
            )
            
            text_format_dropdown = gr.Dropdown(
                choices=["Markdown (.md)", "TXT (.txt)", "Word (.docx)"] if DOCX_AVAILABLE else ["Markdown (.md)", "TXT (.txt)"],
                value="Markdown (.md)",
                label="📄 Định dạng file văn bản"
            )
            
            # Colorful action button
            gr.HTML("""

            """)
            
            # Auto-translate on audio upload - no manual button needed
            
            # Results section with colorful headers
            gr.HTML("""

            <div style="

                background: linear-gradient(135deg, #45B7D1 0%, #96C93D 100%);

                color: white;

                padding: 18px;

                border-radius: 12px;

                margin: 30px 0 20px 0;

                text-align: center;

                box-shadow: 0 6px 24px rgba(69,183,209,0.3);

            ">

                <h3 style="margin: 0 0 8px 0;">📊 Kết quả xử lý</h3>

                <p style="margin: 0; opacity: 0.9; font-size: 0.9em;">

                    Phiên âm, dịch thuật và tổng hợp giọng nói

                </p>

            </div>

            """)
            
            # Dynamic status indicator
            status_text = gr.HTML("")
            
            # Card-based layout for mobile
            with gr.Column(elem_classes=["output-area"]):
                # Original content card
                gr.HTML("""

                <div style="

                    background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);

                    padding: 15px;

                    border-radius: 12px;

                    margin: 15px 0;

                    border-left: 4px solid #2196F3;

                ">

                    <h4 style="margin: 0 0 10px 0; color: #1976D2;">📝 Nội dung gốc từ audio</h4>

                </div>

                """)
                
                transcription_output = gr.Textbox(
                    label="🎯 Phiên âm từ audio",
                    lines=4,
                    interactive=False,
                    placeholder="Nội dung phiên âm từ file audio sẽ hiển thị ở đây...",
                    elem_classes=["mobile-textbox"]
                )
                
                detected_language = gr.Textbox(
                    label="🌐 Ngôn ngữ được phát hiện",
                    lines=1,
                    interactive=False,
                    placeholder="Tự động nhận diện...",
                    elem_classes=["mobile-textbox"]
                )
                            
                
                # Translation result card
                gr.HTML("""

                <div style="

                    background: linear-gradient(135deg, #e8f5e8 0%, #c8e6c9 100%);

                    padding: 15px;

                    border-radius: 12px;

                    margin: 15px 0;

                    border-left: 4px solid #4CAF50;

                ">

                    <h4 style="margin: 0 0 10px 0; color: #388E3C;">✨ Kết quả dịch thuật</h4>

                </div>

                """)
                
                translation_output = gr.Textbox(
                    label="🔄 Nội dung đã dịch",
                    lines=4,
                    interactive=False,
                    placeholder="Bản dịch sẽ hiển thị ở đây...",
                    elem_classes=["mobile-textbox"]
                )
                
                target_language_display = gr.Textbox(
                    label="🎯 Ngôn ngữ đích",
                    lines=1,
                    interactive=False,
                    placeholder="Chưa chọn...",
                    elem_classes=["mobile-textbox"]
                )
                
                # Mobile-friendly comparison section
                with gr.Accordion("🔍 So sánh nội dung", open=False):
                    gr.HTML("""

                    <div style="

                        text-align: center; 

                        margin-bottom: 15px;

                        padding: 10px;

                        background: #f5f5f5;

                        border-radius: 8px;

                    ">

                        <p style="color: #666; font-style: italic; margin: 0;">

                            Xem nội dung gốc và bản dịch để so sánh

                        </p>

                    </div>

                    """)
                    
                    # Stack vertically on mobile for better readability
                    with gr.Column():
                        gr.HTML("""

                        <div style="

                            background: #e3f2fd;

                            padding: 10px;

                            border-radius: 8px;

                            margin: 10px 0;

                            text-align: center;

                            font-weight: bold;

                            color: #1976D2;

                        ">📝 Ngôn ngữ gốc</div>

                        """)
                        original_compare = gr.Textbox(
                            label="",
                            lines=4,
                            interactive=False,
                            show_label=False,
                            placeholder="Nội dung phiên âm từ audio sẽ hiển thị ở đây...",
                            elem_classes=["mobile-compare"]
                        )
                        
                        gr.HTML("""

                        <div style="

                            background: #e8f5e8;

                            padding: 10px;

                            border-radius: 8px;

                            margin: 15px 0 5px 0;

                            text-align: center;

                            font-weight: bold;

                            color: #388E3C;

                        ">✨ Sau khi dịch</div>

                        """)
                        translated_compare = gr.Textbox(
                            label="",
                            lines=4,
                            interactive=False,
                            show_label=False,
                            placeholder="Nội dung sau khi dịch sẽ hiển thị ở đây...",
                            elem_classes=["mobile-compare"]
                        )
                
                # Mobile-optimized download section
                with gr.Accordion("💾 Tải xuống kết quả", open=True):
                    gr.HTML("""

                    <div style="

                        background: linear-gradient(135deg, #fff3e0 0%, #ffcc80 100%);

                        padding: 15px;

                        border-radius: 12px;

                        margin: 15px 0;

                        border-left: 4px solid #FF9800;

                        text-align: center;

                    ">

                        <h4 style="margin: 0 0 10px 0; color: #E65100;">💾 Tải xuống kết quả</h4>

                        <p style="color: #BF360C; margin: 0; font-style: italic;">

                            File audio và văn bản đã dịch

                        </p>

                    </div>

                    """)
                    
                    # Stack downloads vertically for mobile
                    with gr.Column():
                        gr.HTML("""

                        <div style="

                            background: #e3f2fd;

                            padding: 12px;

                            border-radius: 8px;

                            margin: 15px 0 10px 0;

                            text-align: center;

                            font-weight: bold;

                            color: #1976D2;

                        ">🔊 Audio đã dịch</div>

                        """)
                        audio_output_at = gr.Audio(
                            label="🎵 Audio đã dịch",
                            type="filepath",
                            show_label=True,
                            elem_classes=["mobile-audio"],
                            format="wav"  # Specify format explicitly
                        )
                        
                        # Explicit download component for translated audio
                        audio_download_at = gr.File(
                            label="📥 Tải xuống audio đã dịch",
                            file_count="single",
                            file_types=[".wav"],
                            visible=True
                        )
                        
                        gr.HTML("""

                        <div style="

                            background: #e8f5e8;

                            padding: 12px;

                            border-radius: 8px;

                            margin: 25px 0 10px 0;

                            text-align: center;

                            font-weight: bold;

                            color: #388E3C;

                        ">📄 Văn bản đã dịch</div>

                        """)
                        text_output = gr.File(
                            label="",
                            file_count="single",
                            file_types=[".txt", ".docx"],
                            show_label=False,
                            elem_classes=["mobile-file"]
                        )
            
            # Event handlers for Audio Translation with colorful status
            def update_status_processing():
                return """

                <div class="status-processing" style="

                    text-align: center; 

                    margin: 20px 0;

                    padding: 15px;

                    border-radius: 12px;

                    color: white;

                    transition: all 0.3s ease;

                ">

                    <span style="font-weight: bold; font-size: 1.1em;">

                        ⚡ Đang tự động dịch thuật...

                    </span>

                </div>

                """
            
            def update_status_complete():
                return """

                <div class="status-success" style="

                    text-align: center; 

                    margin: 20px 0;

                    padding: 15px;

                    border-radius: 12px;

                    color: white;

                    transition: all 0.3s ease;

                ">

                    <span style="font-weight: bold; font-size: 1.1em;">

                        ✅ Dịch thuật hoàn thành!

                    </span>

                </div>

                """
            
            target_country_dropdown.change(
                fn=update_voices,
                inputs=[target_country_dropdown],
                outputs=[target_voice_dropdown]
            )
            
            # Update target language display when dropdown changes
            target_voice_dropdown.change(
                fn=lambda voice: voice,
                inputs=[target_voice_dropdown],
                outputs=[target_language_display]
            )
            
            # Helper function to extract format
            def get_format_from_dropdown(format_choice):
                if "Markdown" in format_choice:
                    return "md"
                elif "Word" in format_choice:
                    return "docx"
                return "txt"
            
            # Auto-translate when audio is uploaded or changed
            audio_input.change(
                fn=lambda: update_status_processing(),
                outputs=[status_text]
            ).then(
                fn=lambda audio, country, voice, fmt: translate_audio(audio, country, voice, get_format_from_dropdown(fmt)) if audio is not None else ("", "", "📎 Vui lòng tải lên file audio hoặc ghi âm", country, None, "", "", None),
                inputs=[audio_input, target_country_dropdown, target_voice_dropdown, text_format_dropdown],
                outputs=[
                    transcription_output, 
                    detected_language, 
                    translation_output, 
                    target_language_display, 
                    audio_output_at,
                    audio_download_at,
                    original_compare,
                    translated_compare,
                    text_output
                ]
            ).then(
                fn=lambda: update_status_complete(),
                outputs=[status_text]
            ).then(
                fn=refresh_recorded_files,
                outputs=[recorded_files_dropdown]
            )
            
            # === RECORDED FILES EVENT HANDLERS ===
            
            # Save current recording
            save_recording_btn.click(
                fn=save_current_recording,
                inputs=[audio_input],
                outputs=[recording_status, recorded_files_dropdown]
            )
            
            # New recording (clear audio)
            new_recording_btn.click(
                fn=clear_audio_for_new_recording,
                outputs=[audio_input, recording_status]
            )
            
            refresh_files_btn.click(
                fn=refresh_recorded_files,
                outputs=[recorded_files_dropdown]
            )
            
            recorded_files_dropdown.change(
                fn=load_recorded_file,
                inputs=[recorded_files_dropdown],
                outputs=[recorded_audio_player]
            )
            
            use_for_translation_btn.click(
                fn=lambda: update_status_processing(),
                outputs=[status_text]
            ).then(
                fn=use_recorded_for_translation,
                inputs=[recorded_files_dropdown, target_country_dropdown, target_voice_dropdown, text_format_dropdown],
                outputs=[
                    transcription_output, 
                    detected_language, 
                    translation_output, 
                    target_language_display, 
                    audio_output_at,
                    audio_download_at,
                    original_compare,
                    translated_compare,
                    text_output
                ]
            ).then(
                fn=lambda: update_status_complete(),
                outputs=[status_text]
            ).then(
                fn=refresh_recorded_files,
                outputs=[recorded_files_dropdown]
            )
            
            download_recorded_btn.click(
                fn=prepare_recorded_file_download,
                inputs=[recorded_files_dropdown],
                outputs=[download_recorded_file]
            )
            
            delete_recorded_btn.click(
                fn=delete_selected_file,
                inputs=[recorded_files_dropdown],
                outputs=[file_operation_status, recorded_files_dropdown, recorded_audio_player]
            )
    
    # Features section cho Voice RAG
    gr.Markdown("### 📚 Tính năng chính")
    
    with gr.Row():
        with gr.Column():
            gr.HTML("""

            <div style="background: linear-gradient(135deg, #FF6B6B 0%, #FF8E53 100%); padding: 20px; border-radius: 15px; color: white; text-align: center; margin: 10px;">

                <h3>📚 Voice RAG</h3>

                <p>Upload tài liệu và đặt câu hỏi. Nhận trả lời bằng giọng nói đa ngôn ngữ.</p>

                <div style="margin-top: 15px;">

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ Hỗ trợ PDF, DOCX, TXT

                    </div>

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ AI Gemini 2.0 Flash

                    </div>

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ 24 giọng nói đa quốc gia

                    </div>

                </div>

            </div>

            """)
        
        with gr.Column():
            gr.HTML("""

            <div style="background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%); padding: 20px; border-radius: 15px; color: white; text-align: center; margin: 10px;">

                <h3>🌍 Audio Translation</h3>

                <p>Dịch thuật âm thanh sang nhiều ngôn ngữ với giọng nói tự nhiên.</p>

                <div style="margin-top: 15px;">

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ Ghi âm real-time

                    </div>

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ 13 ngôn ngữ chính

                    </div>

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ Edge TTS Neural

                    </div>

                </div>

            </div>

            """)
    
    with gr.Row():
        with gr.Column():
            gr.HTML("""

            <div style="background: linear-gradient(135deg, #45B7D1 0%, #96C93D 100%); padding: 20px; border-radius: 15px; color: white; text-align: center; margin: 10px;">

                <h3>🎤 Voice Studio</h3>

                <p>Chuyển văn bản thành giọng nói với nhiều lựa chọn quốc gia và giọng nói.</p>

                <div style="margin-top: 15px;">

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ 13 quốc gia

                    </div>

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ Tích hợp dịch thuật

                    </div>

                    <div style="background: rgba(255,255,255,0.2); padding: 8px; border-radius: 8px; margin: 5px 0;">

                        ✓ Điều chỉnh tốc độ

                    </div>

                </div>

            </div>

            """)
    
    
    # Footer
    gr.HTML("""

    <div class="custom-footer">

        <div style="display: flex; justify-content: center; align-items: center; gap: 15px; flex-wrap: wrap;">

            <div style="display: flex; align-items: center; gap: 8px;">

                <div style="background: rgba(255,255,255,0.2); padding: 8px 15px; border-radius: 20px; font-size: 16px;">

                    🧠 DB

                </div>

                <span style="font-size: 18px; font-weight: bold;">Digitized Brains</span>

            </div>

            <div style="font-size: 14px; opacity: 0.9;">

                Voice Studio - AI Powered

            </div>

        </div>

    </div>

    """)
    
    # Add JavaScript for button effects
    gr.HTML(js_code)

if __name__ == "__main__":
    import sys
    import locale
    import os
    
    # Ensure UTF-8 encoding
    if sys.platform == 'win32':
        os.environ['PYTHONIOENCODING'] = 'utf-8'
    
    # Optimize startup for HF Spaces
    print(f"===== Application Startup at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
    
    # Only create record_data directory when actually needed to speed up startup
    if not os.environ.get("SPACE_ID") and not os.environ.get("HF_SPACE_ID"):
        create_record_data_directory()
        print(f"📁 Record data directory ready: {RECORD_DATA_DIR}")
    else:
        print(f"🏭 Production mode - record_data will be created on first use")
    
    # Set environment variables for iframe support
    os.environ['GRADIO_ALLOW_FLAGGING'] = 'never'
    # Disable Gradio temp directory to prevent file serving issues
    # os.environ['GRADIO_TEMP_DIR'] = '/tmp'
    
    # Hugging Face Spaces configuration - Use standard port 7860 for HF
    if os.environ.get("SPACE_ID") or os.environ.get("HF_SPACE_ID"):
        # HF Spaces standard configuration
        port = 7860
        print("🏭 Using HF Spaces standard port 7860")
    else:
        # Local development
        port = int(os.environ.get("GRADIO_SERVER_PORT", 7880))
        print(f"🖥️ Using local development port {port}")
    
    demo.launch(
        server_name="0.0.0.0",
        server_port=port,
        share=False,
        show_error=True,
        ssr_mode=False,  # Disable SSR to prevent timeout issues on HF Spaces
        enable_monitoring=False  # Disable monitoring for faster startup
    )