File size: 39,253 Bytes
0203722
5d2baa5
0203722
 
6210885
0203722
 
 
 
 
 
 
 
4622e54
 
0203722
 
 
 
 
 
 
 
788a499
1fa352f
0203722
 
6210885
0203722
 
 
5d2baa5
0203722
6210885
5d2baa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0203722
 
 
292c2fc
0203722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b57247
4622e54
 
 
 
 
 
 
 
 
0b57247
 
4622e54
 
0b57247
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b57247
 
4622e54
 
 
 
 
 
0b57247
 
 
4622e54
 
 
 
 
 
 
a67873a
0203722
 
 
 
 
 
 
 
 
292c2fc
4622e54
 
 
0b57247
4b8ea71
 
 
2c71769
0203722
 
 
 
788a499
0203722
 
 
4622e54
 
 
 
 
 
 
 
2c71769
4622e54
 
 
 
 
 
 
0203722
292c2fc
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
0b57247
 
 
0203722
 
f52a712
0203722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292c2fc
310c080
4622e54
788a499
4622e54
 
 
 
 
 
 
0203722
4622e54
3daaddd
 
0203722
 
 
3daaddd
 
 
 
 
4622e54
de943de
 
4622e54
3daaddd
 
 
4622e54
 
 
 
 
 
0203722
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
0203722
4622e54
 
 
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3daaddd
4622e54
 
 
 
 
 
 
 
0203722
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3daaddd
 
 
4622e54
 
 
 
 
3daaddd
310c080
3daaddd
 
 
 
 
 
 
 
 
 
 
0203722
 
4622e54
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
0203722
4622e54
 
 
 
 
 
 
0203722
4622e54
0322abf
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0203722
4622e54
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0203722
 
 
 
 
 
 
 
 
 
97bf543
0203722
 
 
 
 
 
 
 
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
 
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
 
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
 
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
 
4622e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
310c080
4622e54
 
 
 
 
 
 
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
import os
import requests
import re
from num2words import num2words
import gradio as gr
import torch
import torchaudio
from data.tokenizer import (
    AudioTokenizer,
    TextTokenizer,
)
from edit_utils_en import parse_edit_en
from edit_utils_en import parse_tts_en
from edit_utils_zh import parse_edit_zh
from edit_utils_zh import parse_tts_zh
from inference_scale import inference_one_sample
import librosa
import soundfile as sf
from models import ssr
import io
import numpy as np
import random
import uuid
import opencc
import spaces
import nltk
nltk.download('punkt')

DEMO_PATH = os.getenv("DEMO_PATH", "./demo")
TMP_PATH = os.getenv("TMP_PATH", "./demo/temp")
MODELS_PATH = os.getenv("MODELS_PATH", "./pretrained_models")
os.makedirs(MODELS_PATH, exist_ok=True)
device = "cuda" if torch.cuda.is_available() else "cpu"

# download wmencodec
url = "https://huggingface.co/westbrook/SSR-Speech-English/resolve/main/wmencodec.th"
filename = os.path.join(MODELS_PATH, "wmencodec.th")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(filename, "wb") as file:
    for chunk in response.iter_content(chunk_size=8192):
        file.write(chunk)
print(f"File downloaded to: {filename}")

# download english model
url = "https://huggingface.co/westbrook/SSR-Speech-English/resolve/main/English.pth"
filename = os.path.join(MODELS_PATH, "English.th")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(filename, "wb") as file:
    for chunk in response.iter_content(chunk_size=8192):
        file.write(chunk)
print(f"File downloaded to: {filename}")

# download mandarin model
url = "https://huggingface.co/westbrook/SSR-Speech-Mandarin/resolve/main/Mandarin.pth"
filename = os.path.join(MODELS_PATH, "Mandarin.pth")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(filename, "wb") as file:
    for chunk in response.iter_content(chunk_size=8192):
        file.write(chunk)
print(f"File downloaded to: {filename}")

def get_random_string():
    return "".join(str(uuid.uuid4()).split("-"))

@spaces.GPU
def seed_everything(seed):
    if seed != -1:
        os.environ['PYTHONHASHSEED'] = str(seed)
        random.seed(seed)
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed(seed)
        torch.backends.cudnn.benchmark = False
        torch.backends.cudnn.deterministic = True

def get_mask_interval(transcribe_state, word_span):
    print(transcribe_state)
    seg_num = len(transcribe_state['segments'])
    data = []
    for i in range(seg_num):
      words = transcribe_state['segments'][i]['words']
      for item in words:
        data.append([item['start'], item['end'], item['word']])

    s, e = word_span[0], word_span[1]
    assert s <= e, f"s:{s}, e:{e}"
    assert s >= 0, f"s:{s}"
    assert e <= len(data), f"e:{e}"
    if e == 0: # start
        start = 0.
        end = float(data[0][0])
    elif s == len(data): # end
        start = float(data[-1][1])
        end = float(data[-1][1]) # don't know the end yet
    elif s == e: # insert
        start = float(data[s-1][1])
        end = float(data[s][0])
    else:
        start = float(data[s-1][1]) if s > 0 else float(data[s][0])
        end = float(data[e][0]) if e < len(data) else float(data[-1][1])

    return (start, end)

def traditional_to_simplified(segments):
    converter = opencc.OpenCC('t2s') 
    seg_num = len(segments)
    for i in range(seg_num):
        words = segments[i]['words']
        for j in range(len(words)):
            segments[i]['words'][j]['word'] = converter.convert(segments[i]['words'][j]['word'])
        segments[i]['text'] = converter.convert(segments[i]['text'])
    return segments


from whisperx import load_align_model, load_model, load_audio
from whisperx import align as align_func

# Load models
text_tokenizer_en = TextTokenizer(backend="espeak")
text_tokenizer_zh = TextTokenizer(backend="espeak", language='cmn')

ssrspeech_fn_en = f"{MODELS_PATH}/English.pth"
ckpt_en = torch.load(ssrspeech_fn_en)
model_en = ssr.SSR_Speech(ckpt_en["config"])
model_en.load_state_dict(ckpt_en["model"])
config_en = model_en.args
phn2num_en = ckpt_en["phn2num"]
model_en.to(device)

ssrspeech_fn_zh = f"{MODELS_PATH}/Mandarin.pth"
ckpt_zh = torch.load(ssrspeech_fn_zh)
model_zh = ssr.SSR_Speech(ckpt_zh["config"])
model_zh.load_state_dict(ckpt_zh["model"])
config_zh = model_zh.args
phn2num_zh = ckpt_zh["phn2num"]
model_zh.to(device)

encodec_fn = f"{MODELS_PATH}/wmencodec.th"

ssrspeech_model_en = {
    "config": config_en,
    "phn2num": phn2num_en,
    "model": model_en,
    "text_tokenizer": text_tokenizer_en,
    "audio_tokenizer": AudioTokenizer(signature=encodec_fn)
}

ssrspeech_model_zh = {
    "config": config_zh,
    "phn2num": phn2num_zh,
    "model": model_zh,
    "text_tokenizer": text_tokenizer_zh,
    "audio_tokenizer": AudioTokenizer(signature=encodec_fn)
}


def get_transcribe_state(segments):
    transcript = " ".join([segment["text"] for segment in segments])
    transcript = transcript[1:] if transcript[0] == " " else transcript
    return {
        "segments": segments,
        "transcript": transcript,
    }

@spaces.GPU
def transcribe_en(audio_path):
    language = "en"
    transcribe_model_name = "base.en"
    transcribe_model = load_model(transcribe_model_name, device, asr_options={"suppress_numerals": True, "max_new_tokens": None, "clip_timestamps": None, "hallucination_silence_threshold": None}, language=language)
    segments = transcribe_model.transcribe(audio_path, batch_size=8)["segments"]
    for segment in segments:
        segment['text'] = replace_numbers_with_words(segment['text'])
    _, segments = align_en(segments, audio_path)
    state = get_transcribe_state(segments)
    success_message = "<span style='color:green;'>Success: Transcribe completed successfully!</span>"

    return [
        state["transcript"], state['segments'],
        state, success_message
    ]

@spaces.GPU
def transcribe_zh(audio_path):
    language = "zh"
    transcribe_model_name = "base"
    transcribe_model = load_model(transcribe_model_name, device, asr_options={"suppress_numerals": True, "max_new_tokens": None, "clip_timestamps": None, "hallucination_silence_threshold": None}, language=language)
    segments = transcribe_model.transcribe(audio_path, batch_size=8)["segments"]
    for segment in segments:
        segment['text'] = replace_numbers_with_words(segment['text'])
    _, segments = align_zh(segments, audio_path)
    state = get_transcribe_state(segments)
    success_message = "<span style='color:green;'>Success: Transcribe completed successfully!</span>"

    return [
        state["transcript"], state['segments'],
        state, success_message
    ]

@spaces.GPU
def align_en(segments, audio_path):
    language = "en"
    align_model, metadata = load_align_model(language_code=language, device=device)
    audio = load_audio(audio_path)
    segments = align_func(segments, align_model, metadata, audio, device, return_char_alignments=False)["segments"]
    state = get_transcribe_state(segments)

    return state, segments


@spaces.GPU
def align_zh(segments, audio_path):
    language = "zh"
    align_model, metadata = load_align_model(language_code=language, device=device)
    audio = load_audio(audio_path)
    segments = align_func(segments, align_model, metadata, audio, device, return_char_alignments=False)["segments"]
    state = get_transcribe_state(segments)

    return state, segments


def get_output_audio(audio_tensors, codec_audio_sr):
    result = torch.cat(audio_tensors, 1)
    buffer = io.BytesIO()
    torchaudio.save(buffer, result, int(codec_audio_sr), format="wav")
    buffer.seek(0)
    return buffer.read()

def replace_numbers_with_words(sentence):
    sentence = re.sub(r'(\d+)', r' \1 ', sentence) # add spaces around numbers
    def replace_with_words(match):
        num = match.group(0)
        try:
            return num2words(num) # Convert numbers to words
        except:
            return num # In case num2words fails (unlikely with digits but just to be safe)
    return re.sub(r'\b\d+\b', replace_with_words, sentence) # Regular expression that matches numbers

@spaces.GPU
def run_edit_en(seed, sub_amount, aug_text, cfg_coef, cfg_stride, prompt_length,
        audio_path, original_transcript, transcript):
    
    codec_audio_sr = 16000
    codec_sr = 50
    top_k = 0
    top_p = 0.8
    temperature = 1
    kvcache = 1
    stop_repetition = 2
    
    aug_text = True if aug_text == 1 else False
    seed_everything(seed)

    # resample audio
    audio, _ = librosa.load(audio_path, sr=16000)
    sf.write(audio_path, audio, 16000)
    
    # text normalization
    target_transcript = replace_numbers_with_words(transcript).replace("  ", " ").replace("  ", " ").replace("\n", " ")
    orig_transcript = replace_numbers_with_words(original_transcript).replace("  ", " ").replace("  ", " ").replace("\n", " ")

    [orig_transcript, segments, _, _] = transcribe_en(audio_path)
    orig_transcript = orig_transcript.lower()
    target_transcript = target_transcript.lower()
    transcribe_state,_ = align_en(segments, audio_path)
    print(orig_transcript)
    print(target_transcript)

    operations, orig_spans = parse_edit_en(orig_transcript, target_transcript)
    print(operations)
    print("orig_spans: ", orig_spans)
    
    if len(orig_spans) > 3:
        raise gr.Error("Current model only supports maximum 3 editings")
        
    starting_intervals = []
    ending_intervals = []
    for orig_span in orig_spans:
        start, end = get_mask_interval(transcribe_state, orig_span)
        starting_intervals.append(start)
        ending_intervals.append(end)

    print("intervals: ", starting_intervals, ending_intervals)

    info = torchaudio.info(audio_path)
    audio_dur = info.num_frames / info.sample_rate
    
    def combine_spans(spans, threshold=0.2):
        spans.sort(key=lambda x: x[0])
        combined_spans = []
        current_span = spans[0]

        for i in range(1, len(spans)):
            next_span = spans[i]
            if current_span[1] >= next_span[0] - threshold:
                current_span[1] = max(current_span[1], next_span[1])
            else:
                combined_spans.append(current_span)
                current_span = next_span
        combined_spans.append(current_span)
        return combined_spans
    
    morphed_span = [[max(start - sub_amount, 0), min(end + sub_amount, audio_dur)]
                    for start, end in zip(starting_intervals, ending_intervals)] # in seconds
    morphed_span = combine_spans(morphed_span, threshold=0.2)
    print("morphed_spans: ", morphed_span)
    mask_interval = [[round(span[0]*codec_sr), round(span[1]*codec_sr)] for span in morphed_span]
    mask_interval = torch.LongTensor(mask_interval) # [M,2], M==1 for now
    
    decode_config = {'top_k': top_k, 'top_p': top_p, 'temperature': temperature, 'stop_repetition': stop_repetition, 'kvcache': kvcache, "codec_audio_sr": codec_audio_sr, "codec_sr": codec_sr}
    
    new_audio = inference_one_sample(
        ssrspeech_model_en["model"],
        ssrspeech_model_en["config"],
        ssrspeech_model_en["phn2num"],
        ssrspeech_model_en["text_tokenizer"], 
        ssrspeech_model_en["audio_tokenizer"],
        audio_path, orig_transcript, target_transcript, mask_interval,
        cfg_coef, cfg_stride, aug_text, False, True, False,
        device, decode_config
    )
    audio_tensors = []
    # save segments for comparison
    new_audio = new_audio[0].cpu()
    torchaudio.save(audio_path, new_audio, codec_audio_sr)

    audio_tensors.append(new_audio)
    output_audio = get_output_audio(audio_tensors, codec_audio_sr)
    
    success_message = "<span style='color:green;'>Success: Inference successfully!</span>"
    return output_audio, success_message


@spaces.GPU
def run_tts_en(seed, sub_amount, aug_text, cfg_coef, cfg_stride, prompt_length,
        audio_path, original_transcript, transcript):
    
    codec_audio_sr = 16000
    codec_sr = 50
    top_k = 0
    top_p = 0.8
    temperature = 1
    kvcache = 1
    stop_repetition = 2
    
    aug_text = True if aug_text == 1 else False
    seed_everything(seed)

    # resample audio
    audio, _ = librosa.load(audio_path, sr=16000)
    sf.write(audio_path, audio, 16000)
    
    # text normalization
    target_transcript = replace_numbers_with_words(transcript).replace("  ", " ").replace("  ", " ").replace("\n", " ")
    orig_transcript = replace_numbers_with_words(original_transcript).replace("  ", " ").replace("  ", " ").replace("\n", " ")

    [orig_transcript, segments, _, _] = transcribe_en(audio_path)
    orig_transcript = orig_transcript.lower()
    target_transcript = target_transcript.lower()
    transcribe_state,_ = align_en(segments, audio_path)
    print(orig_transcript)
    print(target_transcript)

    
    info = torchaudio.info(audio_path)
    duration = info.num_frames / info.sample_rate
    cut_length = duration
    # Cut long audio for tts
    if duration > prompt_length:
        seg_num = len(transcribe_state['segments'])
        for i in range(seg_num):
            words = transcribe_state['segments'][i]['words']
            for item in words:
                if item['end'] >= prompt_length:
                    cut_length = min(item['end'], cut_length)

    audio, _ = librosa.load(audio_path, sr=16000, duration=cut_length)
    sf.write(audio_path, audio, 16000)
    [orig_transcript, segments, _, _] = transcribe_en(audio_path)
    
    
    orig_transcript = orig_transcript.lower()
    target_transcript = target_transcript.lower()
    transcribe_state,_ = align_en(segments, audio_path)
    print(orig_transcript)
    target_transcript_copy = target_transcript # for tts cut out
    target_transcript_copy = target_transcript_copy.split(' ')[0]
    target_transcript = orig_transcript + ' ' + target_transcript
    print(target_transcript)


    info = torchaudio.info(audio_path)
    audio_dur = info.num_frames / info.sample_rate
    
    morphed_span = [(audio_dur, audio_dur)] # in seconds
    mask_interval = [[round(span[0]*codec_sr), round(span[1]*codec_sr)] for span in morphed_span]
    mask_interval = torch.LongTensor(mask_interval) # [M,2], M==1 for now
    print("mask_interval: ", mask_interval)

    decode_config = {'top_k': top_k, 'top_p': top_p, 'temperature': temperature, 'stop_repetition': stop_repetition, 'kvcache': kvcache, "codec_audio_sr": codec_audio_sr, "codec_sr": codec_sr}
    
    new_audio = inference_one_sample(
        ssrspeech_model_en["model"],
        ssrspeech_model_en["config"],
        ssrspeech_model_en["phn2num"],
        ssrspeech_model_en["text_tokenizer"], 
        ssrspeech_model_en["audio_tokenizer"],
        audio_path, orig_transcript, target_transcript, mask_interval,
        cfg_coef, cfg_stride, aug_text, False, True, True,
        device, decode_config
    )
    audio_tensors = []
    # save segments for comparison
    new_audio = new_audio[0].cpu()
    torchaudio.save(audio_path, new_audio, codec_audio_sr)
    
    [new_transcript, new_segments, _, _] = transcribe_en(audio_path)
    transcribe_state,_ = align_en(new_segments, audio_path)
    tmp1 = transcribe_state['segments'][0]['words'][0]['word'].lower()
    tmp2 = target_transcript_copy.lower()
    if tmp1 == tmp2:
        offset = transcribe_state['segments'][0]['words'][0]['start']
    else:
        offset = transcribe_state['segments'][0]['words'][1]['start']
    
    new_audio, _ = torchaudio.load(audio_path, frame_offset=int(offset*codec_audio_sr))
    audio_tensors.append(new_audio)
    output_audio = get_output_audio(audio_tensors, codec_audio_sr)
    
    success_message = "<span style='color:green;'>Success: Inference successfully!</span>"
    return output_audio, success_message


@spaces.GPU
def run_edit_zh(seed, sub_amount, aug_text, cfg_coef, cfg_stride, prompt_length,
        audio_path, original_transcript, transcript):
    
    codec_audio_sr = 16000
    codec_sr = 50
    top_k = 0
    top_p = 0.8
    temperature = 1
    kvcache = 1
    stop_repetition = 2
    
    aug_text = True if aug_text == 1 else False
    
    seed_everything(seed)

    # resample audio
    audio, _ = librosa.load(audio_path, sr=16000)
    sf.write(audio_path, audio, 16000)
    
    # text normalization
    target_transcript = replace_numbers_with_words(transcript).replace("  ", " ").replace("  ", " ").replace("\n", " ")
    orig_transcript = replace_numbers_with_words(original_transcript).replace("  ", " ").replace("  ", " ").replace("\n", " ")

    [orig_transcript, segments, _] = transcribe_zh(audio_path)

    converter = opencc.OpenCC('t2s')
    orig_transcript = converter.convert(orig_transcript)
    transcribe_state = align_zh(traditional_to_simplified(segments), audio_path)
    transcribe_state['segments'] = traditional_to_simplified(transcribe_state['segments'])

    print(orig_transcript)
    print(target_transcript)

    operations, orig_spans = parse_edit_zh(orig_transcript, target_transcript)
    print(operations)
    print("orig_spans: ", orig_spans)
    
    if len(orig_spans) > 3:
        raise gr.Error("Current model only supports maximum 3 editings")
        
    starting_intervals = []
    ending_intervals = []
    for orig_span in orig_spans:
        start, end = get_mask_interval(transcribe_state, orig_span)
        starting_intervals.append(start)
        ending_intervals.append(end)

    print("intervals: ", starting_intervals, ending_intervals)

    info = torchaudio.info(audio_path)
    audio_dur = info.num_frames / info.sample_rate
    
    def combine_spans(spans, threshold=0.2):
        spans.sort(key=lambda x: x[0])
        combined_spans = []
        current_span = spans[0]

        for i in range(1, len(spans)):
            next_span = spans[i]
            if current_span[1] >= next_span[0] - threshold:
                current_span[1] = max(current_span[1], next_span[1])
            else:
                combined_spans.append(current_span)
                current_span = next_span
        combined_spans.append(current_span)
        return combined_spans
    
    morphed_span = [[max(start - sub_amount, 0), min(end + sub_amount, audio_dur)]
                    for start, end in zip(starting_intervals, ending_intervals)] # in seconds
    morphed_span = combine_spans(morphed_span, threshold=0.2)
    print("morphed_spans: ", morphed_span)
    mask_interval = [[round(span[0]*codec_sr), round(span[1]*codec_sr)] for span in morphed_span]
    mask_interval = torch.LongTensor(mask_interval) # [M,2], M==1 for now
    
    decode_config = {'top_k': top_k, 'top_p': top_p, 'temperature': temperature, 'stop_repetition': stop_repetition, 'kvcache': kvcache, "codec_audio_sr": codec_audio_sr, "codec_sr": codec_sr}
    
    new_audio = inference_one_sample(
        ssrspeech_model_zh["model"],
        ssrspeech_model_zh["config"],
        ssrspeech_model_zh["phn2num"],
        ssrspeech_model_zh["text_tokenizer"], 
        ssrspeech_model_zh["audio_tokenizer"],
        audio_path, orig_transcript, target_transcript, mask_interval,
        cfg_coef, cfg_stride, aug_text, False, True, False,
        device, decode_config
    )
    audio_tensors = []
    # save segments for comparison
    new_audio = new_audio[0].cpu()
    torchaudio.save(audio_path, new_audio, codec_audio_sr)
    audio_tensors.append(new_audio)
    output_audio = get_output_audio(audio_tensors, codec_audio_sr)
    
    success_message = "<span style='color:green;'>Success: Inference successfully!</span>"
    return output_audio, success_message


@spaces.GPU
def run_tts_zh(seed, sub_amount, aug_text, cfg_coef, cfg_stride, prompt_length,
        audio_path, original_transcript, transcript):
    
    codec_audio_sr = 16000
    codec_sr = 50
    top_k = 0
    top_p = 0.8
    temperature = 1
    kvcache = 1
    stop_repetition = 2
    
    aug_text = True if aug_text == 1 else False
    
    seed_everything(seed)

    # resample audio
    audio, _ = librosa.load(audio_path, sr=16000)
    sf.write(audio_path, audio, 16000)
    
    # text normalization
    target_transcript = replace_numbers_with_words(transcript).replace("  ", " ").replace("  ", " ").replace("\n", " ")
    orig_transcript = replace_numbers_with_words(original_transcript).replace("  ", " ").replace("  ", " ").replace("\n", " ")

    [orig_transcript, segments, _] = transcribe_zh(audio_path)

    converter = opencc.OpenCC('t2s')
    orig_transcript = converter.convert(orig_transcript)
    transcribe_state = align_zh(traditional_to_simplified(segments), audio_path)
    transcribe_state['segments'] = traditional_to_simplified(transcribe_state['segments'])

    print(orig_transcript)
    print(target_transcript)

    info = torchaudio.info(audio_path)
    duration = info.num_frames / info.sample_rate
    cut_length = duration
    # Cut long audio for tts
    if duration > prompt_length:
        seg_num = len(transcribe_state['segments'])
        for i in range(seg_num):
            words = transcribe_state['segments'][i]['words']
            for item in words:
                if item['end'] >= prompt_length:
                    cut_length = min(item['end'], cut_length)

    audio, _ = librosa.load(audio_path, sr=16000, duration=cut_length)
    sf.write(audio_path, audio, 16000)
    [orig_transcript, segments, _] = transcribe_zh(audio_path)
    

    converter = opencc.OpenCC('t2s')
    orig_transcript = converter.convert(orig_transcript)
    transcribe_state = align_zh(traditional_to_simplified(segments), audio_path)
    transcribe_state['segments'] = traditional_to_simplified(transcribe_state['segments'])
    
    print(orig_transcript)
    target_transcript_copy = target_transcript # for tts cut out
    target_transcript_copy = target_transcript_copy[0]
    target_transcript = orig_transcript + target_transcript
    print(target_transcript)

    
    info = torchaudio.info(audio_path)
    audio_dur = info.num_frames / info.sample_rate
    
    morphed_span = [(audio_dur, audio_dur)] # in seconds
    mask_interval = [[round(span[0]*codec_sr), round(span[1]*codec_sr)] for span in morphed_span]
    mask_interval = torch.LongTensor(mask_interval) # [M,2], M==1 for now
    print("mask_interval: ", mask_interval)

    decode_config = {'top_k': top_k, 'top_p': top_p, 'temperature': temperature, 'stop_repetition': stop_repetition, 'kvcache': kvcache, "codec_audio_sr": codec_audio_sr, "codec_sr": codec_sr}
    
    new_audio = inference_one_sample(
        ssrspeech_model_zh["model"],
        ssrspeech_model_zh["config"],
        ssrspeech_model_zh["phn2num"],
        ssrspeech_model_zh["text_tokenizer"], 
        ssrspeech_model_zh["audio_tokenizer"],
        audio_path, orig_transcript, target_transcript, mask_interval,
        cfg_coef, cfg_stride, aug_text, False, True, True,
        device, decode_config
    )
    audio_tensors = []
    # save segments for comparison
    new_audio = new_audio[0].cpu()
    torchaudio.save(audio_path, new_audio, codec_audio_sr)
    
    [new_transcript, new_segments, _] = transcribe_zh(audio_path)
    
    transcribe_state = align_zh(traditional_to_simplified(new_segments), audio_path)
    transcribe_state['segments'] = traditional_to_simplified(transcribe_state['segments'])
    tmp1 = transcribe_state['segments'][0]['words'][0]['word']
    tmp2 = target_transcript_copy
    
    if tmp1 == tmp2:
        offset = transcribe_state['segments'][0]['words'][0]['start']
    else:
        offset = transcribe_state['segments'][0]['words'][1]['start']
    
    new_audio, _ = torchaudio.load(audio_path, frame_offset=int(offset*codec_audio_sr))
    audio_tensors.append(new_audio)
    output_audio = get_output_audio(audio_tensors, codec_audio_sr)
    
    success_message = "<span style='color:green;'>Success: Inference successfully!</span>"
    return output_audio, success_message


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Ssrspeech gradio app.")
    
    parser.add_argument("--demo-path", default="./demo", help="Path to demo directory")
    parser.add_argument("--tmp-path", default="./demo/temp", help="Path to tmp directory")
    parser.add_argument("--models-path", default="./pretrained_models", help="Path to ssrspeech models directory")
    parser.add_argument("--port", default=7860, type=int, help="App port")
    parser.add_argument("--share", action="store_true", help="Launch with public url")

    os.environ["USER"] = os.getenv("USER", "user")
    args = parser.parse_args()
    DEMO_PATH = args.demo_path
    TMP_PATH = args.tmp_path
    MODELS_PATH = args.models_path

    # app = get_app()
    # app.queue().launch(share=args.share, server_port=args.port)
    
    # CSS styling (optional)
    css = """
    #col-container {
        margin: 0 auto;
        max-width: 1280px;
    }
    """
    
    # Gradio Blocks layout
    with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
        with gr.Column(elem_id="col-container"):
            gr.Markdown("""
                # SSR-Speech: High-quality Speech Editor and Text-to-Speech Synthesizer
                Generate and edit speech from text. Adjust advanced settings for more control.
                
                Learn more about 🚀**SSR-Speech** on the [SSR-Speech Homepage](https://wanghelin1997.github.io/SSR-Speech-Demo/).
            """)


            # Tabs for Generate and Edit
            with gr.Tab("English Speech Editing"):
                
                with gr.Row():
                    with gr.Column(scale=2):
                        input_audio = gr.Audio(value=f"{DEMO_PATH}/84_121550_000074_000000.wav", label="Input Audio", type="filepath", interactive=True)
                        with gr.Group():
                            original_transcript = gr.Textbox(label="Original transcript", lines=5, value="Debug",
                                                            info="Use whisperx model to get the transcript.")
                            transcribe_btn = gr.Button(value="Transcribe")

                    with gr.Column(scale=3):
                        with gr.Group():
                            transcript = gr.Textbox(label="Text", lines=7, value="Debug", interactive=True)
                            run_btn = gr.Button(value="Run")

                    with gr.Column(scale=2):
                        output_audio = gr.Audio(label="Output Audio")
                        
                with gr.Row():
                    with gr.Accordion("Advanced Settings", open=False):
                        seed = gr.Number(label="seed", value=-1, precision=0, info="random seeds always works :)")
                        aug_text = gr.Radio(label="aug_text", choices=[0, 1], value=1,
                                            info="set to 1 to use classifer-free guidance, change if you don't like the results")
                        cfg_coef = gr.Number(label="cfg_coef", value=1.5,
                                            info="cfg guidance scale, 1.5 is a good value, change if you don't like the results")
                        cfg_stride = gr.Number(label="cfg_stride", value=5,
                                            info="cfg stride, 5 is a good value for English, change if you don't like the results")
                        prompt_length = gr.Number(label="prompt_length", value=3,
                                            info="used for tts prompt, will automatically cut the prompt audio to this length")
                        sub_amount = gr.Number(label="sub_amount", value=0.12, info="margin to the left and right of the editing segment, change if you don't like the results")

                success_output = gr.HTML()

                semgents = gr.State() # not used
                state = gr.State() # not used
                transcribe_btn.click(fn=transcribe_en,
                                    inputs=[input_audio],
                                    outputs=[original_transcript, semgents, state, success_output])
                
                run_btn.click(fn=run_edit_en,
                            inputs=[
                                seed, sub_amount,
                                aug_text, cfg_coef, cfg_stride, prompt_length, 
                                input_audio, original_transcript, transcript,
                            ],
                            outputs=[output_audio, success_output])

                transcript.submit(fn=run_edit_en,
                        inputs=[
                                seed, sub_amount,
                                aug_text, cfg_coef, cfg_stride, prompt_length, 
                                input_audio, original_transcript, transcript,
                        ],
                    outputs=[output_audio, success_output]
                )

            with gr.Tab("English TTS"):
                
                with gr.Row():
                    with gr.Column(scale=2):
                        input_audio = gr.Audio(value=f"{DEMO_PATH}/84_121550_000074_000000.wav", label="Input Audio", type="filepath", interactive=True)
                        with gr.Group():
                            original_transcript = gr.Textbox(label="Original transcript", lines=5, value="Debug",
                                                            info="Use whisperx model to get the transcript.")
                            transcribe_btn = gr.Button(value="Transcribe")

                    with gr.Column(scale=3):
                        with gr.Group():
                            transcript = gr.Textbox(label="Text", lines=7, value="Debug", interactive=True)
                            run_btn = gr.Button(value="Run")

                    with gr.Column(scale=2):
                        output_audio = gr.Audio(label="Output Audio")
                        
                with gr.Row():
                    with gr.Accordion("Advanced Settings", open=False):
                        seed = gr.Number(label="seed", value=-1, precision=0, info="random seeds always works :)")
                        aug_text = gr.Radio(label="aug_text", choices=[0, 1], value=1,
                                            info="set to 1 to use classifer-free guidance, change if you don't like the results")
                        cfg_coef = gr.Number(label="cfg_coef", value=1.5,
                                            info="cfg guidance scale, 1.5 is a good value, change if you don't like the results")
                        cfg_stride = gr.Number(label="cfg_stride", value=5,
                                            info="cfg stride, 5 is a good value for English, change if you don't like the results")
                        prompt_length = gr.Number(label="prompt_length", value=3,
                                            info="used for tts prompt, will automatically cut the prompt audio to this length")
                        sub_amount = gr.Number(label="sub_amount", value=0.12, info="margin to the left and right of the editing segment, change if you don't like the results")

                success_output = gr.HTML()

                semgents = gr.State() # not used
                state = gr.State() # not used
                transcribe_btn.click(fn=transcribe_en,
                                    inputs=[input_audio],
                                    outputs=[original_transcript, semgents, state, success_output])
                
                run_btn.click(fn=run_tts_en,
                            inputs=[
                                seed, sub_amount,
                                aug_text, cfg_coef, cfg_stride, prompt_length, 
                                input_audio, original_transcript, transcript,
                            ],
                            outputs=[output_audio, success_output])

                transcript.submit(fn=run_tts_en,
                        inputs=[
                                seed, sub_amount,
                                aug_text, cfg_coef, cfg_stride, prompt_length, 
                                input_audio, original_transcript, transcript,
                        ],
                    outputs=[output_audio, success_output]
                )
                
            with gr.Tab("Mandarin Speech Editing"):
                
                with gr.Row():
                    with gr.Column(scale=2):
                        input_audio = gr.Audio(value=f"{DEMO_PATH}/aishell3_test.wav", label="Input Audio", type="filepath", interactive=True)
                        with gr.Group():
                            original_transcript = gr.Textbox(label="Original transcript", lines=5, value="Debug",
                                                            info="Use whisperx model to get the transcript.")
                            transcribe_btn = gr.Button(value="Transcribe")

                    with gr.Column(scale=3):
                        with gr.Group():
                            transcript = gr.Textbox(label="Text", lines=7, value="Debug", interactive=True)
                            run_btn = gr.Button(value="Run")

                    with gr.Column(scale=2):
                        output_audio = gr.Audio(label="Output Audio")
                        
                with gr.Row():
                    with gr.Accordion("Advanced Settings", open=False):
                        seed = gr.Number(label="seed", value=-1, precision=0, info="random seeds always works :)")
                        aug_text = gr.Radio(label="aug_text", choices=[0, 1], value=1,
                                            info="set to 1 to use classifer-free guidance, change if you don't like the results")
                        cfg_coef = gr.Number(label="cfg_coef", value=1.5,
                                            info="cfg guidance scale, 1.5 is a good value, change if you don't like the results")
                        cfg_stride = gr.Number(label="cfg_stride", value=1,
                                            info="cfg stride, 1 is a good value for Mandarin, change if you don't like the results")
                        prompt_length = gr.Number(label="prompt_length", value=3,
                                            info="used for tts prompt, will automatically cut the prompt audio to this length")
                        sub_amount = gr.Number(label="sub_amount", value=0.12, info="margin to the left and right of the editing segment, change if you don't like the results")

                success_output = gr.HTML()

                semgents = gr.State() # not used
                state = gr.State() # not used
                transcribe_btn.click(fn=transcribe_zh,
                                    inputs=[input_audio],
                                    outputs=[original_transcript, semgents, state, success_output])
                
                run_btn.click(fn=run_edit_zh,
                            inputs=[
                                seed, sub_amount,
                                aug_text, cfg_coef, cfg_stride, prompt_length, 
                                input_audio, original_transcript, transcript,
                            ],
                            outputs=[output_audio, success_output])

                transcript.submit(fn=run_edit_zh,
                        inputs=[
                                seed, sub_amount,
                                aug_text, cfg_coef, cfg_stride, prompt_length, 
                                input_audio, original_transcript, transcript,
                        ],
                    outputs=[output_audio, success_output]
                )
                
            with gr.Tab("Mandarin TTS"):
                
                with gr.Row():
                    with gr.Column(scale=2):
                        input_audio = gr.Audio(value=f"{DEMO_PATH}/aishell3_test.wav", label="Input Audio", type="filepath", interactive=True)
                        with gr.Group():
                            original_transcript = gr.Textbox(label="Original transcript", lines=5, value="Debug",
                                                            info="Use whisperx model to get the transcript.")
                            transcribe_btn = gr.Button(value="Transcribe")

                    with gr.Column(scale=3):
                        with gr.Group():
                            transcript = gr.Textbox(label="Text", lines=7, value="Debug", interactive=True)
                            run_btn = gr.Button(value="Run")

                    with gr.Column(scale=2):
                        output_audio = gr.Audio(label="Output Audio")
                        
                with gr.Row():
                    with gr.Accordion("Advanced Settings", open=False):
                        seed = gr.Number(label="seed", value=-1, precision=0, info="random seeds always works :)")
                        aug_text = gr.Radio(label="aug_text", choices=[0, 1], value=1,
                                            info="set to 1 to use classifer-free guidance, change if you don't like the results")
                        cfg_coef = gr.Number(label="cfg_coef", value=1.5,
                                            info="cfg guidance scale, 1.5 is a good value, change if you don't like the results")
                        cfg_stride = gr.Number(label="cfg_stride", value=1,
                                            info="cfg stride, 1 is a good value for Mandarin, change if you don't like the results")
                        prompt_length = gr.Number(label="prompt_length", value=3,
                                            info="used for tts prompt, will automatically cut the prompt audio to this length")
                        sub_amount = gr.Number(label="sub_amount", value=0.12, info="margin to the left and right of the editing segment, change if you don't like the results")

                success_output = gr.HTML()

                semgents = gr.State() # not used
                state = gr.State() # not used
                transcribe_btn.click(fn=transcribe_zh,
                                    inputs=[input_audio],
                                    outputs=[original_transcript, semgents, state, success_output])
                
                run_btn.click(fn=run_tts_zh,
                            inputs=[
                                seed, sub_amount,
                                aug_text, cfg_coef, cfg_stride, prompt_length, 
                                input_audio, original_transcript, transcript,
                            ],
                            outputs=[output_audio, success_output])

                transcript.submit(fn=run_tts_zh,
                        inputs=[
                                seed, sub_amount,
                                aug_text, cfg_coef, cfg_stride, prompt_length, 
                                input_audio, original_transcript, transcript,
                        ],
                    outputs=[output_audio, success_output]
                )

        # Launch the Gradio demo
        demo.launch()