str20tbl commited on
Commit
e715e06
·
1 Parent(s): db8fa51
Files changed (5) hide show
  1. app.py +115 -0
  2. requirements.txt +3 -0
  3. spkemb/speaker0.npy +3 -0
  4. spkemb/speaker1.npy +3 -0
  5. spkemb/speaker2.npy +3 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import librosa
3
+ import numpy as np
4
+ import torch
5
+
6
+ from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
7
+
8
+
9
+ checkpoint = "microsoft/speecht5_tts"
10
+ processor = SpeechT5Processor.from_pretrained(checkpoint)
11
+ model = SpeechT5ForTextToSpeech.from_pretrained(checkpoint)
12
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
13
+
14
+
15
+ speaker_embeddings = {
16
+ "GGP": "spkemb/speaker0.npy",
17
+ "BGP": "spkemb/speaker1.npy",
18
+ "BDP": "spkemb/speaker2.npy",
19
+ }
20
+
21
+
22
+ def predict(text, speaker):
23
+ if len(text.strip()) == 0:
24
+ return (16000, np.zeros(0).astype(np.int16))
25
+
26
+ inputs = processor(text=text, return_tensors="pt")
27
+
28
+ # limit input length
29
+ input_ids = inputs["input_ids"]
30
+ input_ids = input_ids[..., :model.config.max_text_positions]
31
+
32
+ if speaker == "Surprise Me!":
33
+ # load one of the provided speaker embeddings at random
34
+ idx = np.random.randint(len(speaker_embeddings))
35
+ key = list(speaker_embeddings.keys())[idx]
36
+ speaker_embedding = np.load(speaker_embeddings[key])
37
+
38
+ # randomly shuffle the elements
39
+ np.random.shuffle(speaker_embedding)
40
+
41
+ # randomly flip half the values
42
+ x = (np.random.rand(512) >= 0.5) * 1.0
43
+ x[x == 0] = -1.0
44
+ speaker_embedding *= x
45
+
46
+ #speaker_embedding = np.random.rand(512).astype(np.float32) * 0.3 - 0.15
47
+ else:
48
+ speaker_embedding = np.load(speaker_embeddings[speaker[:3]])
49
+
50
+ speaker_embedding = torch.tensor(speaker_embedding).unsqueeze(0)
51
+
52
+ speech = model.generate_speech(input_ids, speaker_embedding, vocoder=vocoder)
53
+
54
+ speech = (speech.numpy() * 32767).astype(np.int16)
55
+ return (16000, speech)
56
+
57
+
58
+ title = "SpeechT5: Speech Synthesis"
59
+
60
+ description = """
61
+ The <b>SpeechT5</b> model is pre-trained on text as well as speech inputs, with targets that are also a mix of text and speech.
62
+ By pre-training on text and speech at the same time, it learns unified representations for both, resulting in improved modeling capabilities.
63
+ SpeechT5 can be fine-tuned for different speech tasks. This space demonstrates the <b>text-to-speech</b> (TTS) checkpoint for the English language.
64
+ See also the <a href="https://huggingface.co/spaces/Matthijs/speecht5-asr-demo">speech recognition (ASR) demo</a>
65
+ and the <a href="https://huggingface.co/spaces/Matthijs/speecht5-vc-demo">voice conversion demo</a>.
66
+ Refer to <a href="https://colab.research.google.com/drive/1i7I5pzBcU3WDFarDnzweIj4-sVVoIUFJ">this Colab notebook</a> to learn how to fine-tune the SpeechT5 TTS model on your own dataset or language.
67
+ <b>How to use:</b> Enter some English text and choose a speaker. The output is a mel spectrogram, which is converted to a mono 16 kHz waveform by the
68
+ HiFi-GAN vocoder. Because the model always applies random dropout, each attempt will give slightly different results.
69
+ The <em>Surprise Me!</em> option creates a completely randomized speaker.
70
+ """
71
+
72
+ article = """
73
+ <div style='margin:20px auto;'>
74
+ <p>References: <a href="https://arxiv.org/abs/2110.07205">SpeechT5 paper</a> |
75
+ <a href="https://github.com/microsoft/SpeechT5/">original GitHub</a> |
76
+ <a href="https://huggingface.co/mechanicalsea/speecht5-tts">original weights</a></p>
77
+ <pre>
78
+ @article{Ao2021SpeechT5,
79
+ title = {SpeechT5: Unified-Modal Encoder-Decoder Pre-training for Spoken Language Processing},
80
+ author = {Junyi Ao and Rui Wang and Long Zhou and Chengyi Wang and Shuo Ren and Yu Wu and Shujie Liu and Tom Ko and Qing Li and Yu Zhang and Zhihua Wei and Yao Qian and Jinyu Li and Furu Wei},
81
+ eprint={2110.07205},
82
+ archivePrefix={arXiv},
83
+ primaryClass={eess.AS},
84
+ year={2021}
85
+ }
86
+ </pre>
87
+ </div>
88
+ """
89
+
90
+ examples = [
91
+ ["Rhyfeddod neu ffenomenon optegol a meteorolegol yw enfys, pan fydd sbectrwm o olau yn ymddangos yn yr awyr pan fo'r haul yn disgleirio ar ddiferion o leithder yn atmosffer y ddaear.", "GGP (gwryw-gogledd-pro)"],
92
+ ["Rhyfeddod neu ffenomenon optegol a meteorolegol yw enfys, pan fydd sbectrwm o olau yn ymddangos yn yr awyr pan fo'r haul yn disgleirio ar ddiferion o leithder yn atmosffer y ddaear.", "BGP (benyw-gogledd-pro)"],
93
+ ["Rhyfeddod neu ffenomenon optegol a meteorolegol yw enfys, pan fydd sbectrwm o olau yn ymddangos yn yr awyr pan fo'r haul yn disgleirio ar ddiferion o leithder yn atmosffer y ddaear.", "BDP (benyw-de-pro)"],
94
+ ]
95
+
96
+ gr.Interface(
97
+ fn=predict,
98
+ inputs=[
99
+ gr.Text(label="Input Text"),
100
+ gr.Radio(label="Speaker", choices=[
101
+ "GGP (gwryw-gogledd-pro)",
102
+ "BGP (benyw-gogledd-pro)",
103
+ "BDP (benyw-de-pro)",
104
+ "Surprise Me!"
105
+ ],
106
+ value="GGP (gwryw-gogledd-pro)"),
107
+ ],
108
+ outputs=[
109
+ gr.Audio(label="Generated Speech", type="numpy"),
110
+ ],
111
+ title=title,
112
+ description=description,
113
+ article=article,
114
+ examples=examples,
115
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ sentencepiece
3
+ datasets[audio]
spkemb/speaker0.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:29a89ec165f28301a1dda1ef5e1a5c8b5ddd60c0bfa4094f7fb6b88035812ca3
3
+ size 229448
spkemb/speaker1.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cc9f3a6e58a1e2f52c9258a7e9372b30a0966dd5b3fdc82de23cd2e38fd61c67
3
+ size 599888
spkemb/speaker2.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e61aa815909de199d2261c806674d5cbdc264120c930838dffbe4c21d957845c
3
+ size 158888