Matthijs Hollemans commited on
Commit
1a582a6
β€’
1 Parent(s): 8af0e6f

add application

Browse files
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ *.pyc
2
+ __pycache__/
3
+ .DS_Store
4
+
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
- title: Speecht5 Asr Demo
3
- emoji: πŸ“Š
4
- colorFrom: purple
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 3.17.0
 
1
  ---
2
+ title: SpeechT5 Speech Recognition Demo
3
+ emoji: πŸ‘©β€πŸŽ€
4
+ colorFrom: yellow
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 3.17.0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import librosa
3
+ import torch
4
+
5
+ from transformers import SpeechT5Processor, SpeechT5ForSpeechToText
6
+
7
+
8
+ checkpoint = "Matthijs/speecht5_asr"
9
+ processor = SpeechT5Processor.from_pretrained(checkpoint)
10
+ model = SpeechT5ForSpeechToText.from_pretrained(checkpoint)
11
+
12
+
13
+ def process_audio(sampling_rate, waveform):
14
+ # convert from int16 to floating point
15
+ waveform = waveform / 32678.0
16
+
17
+ # convert to mono if stereo
18
+ if len(waveform.shape) > 1:
19
+ waveform = librosa.to_mono(waveform.T)
20
+
21
+ # resample to 16 kHz if necessary
22
+ if sampling_rate != 16000:
23
+ waveform = librosa.resample(waveform, orig_sr=sampling_rate, target_sr=16000)
24
+
25
+ # make PyTorch tensor
26
+ waveform = torch.tensor(waveform)
27
+ return waveform
28
+
29
+
30
+ def predict(audio, mic_audio):
31
+ # audio = tuple (sample_rate, frames) or (sample_rate, (frames, channels))
32
+ if mic_audio is not None:
33
+ sampling_rate, waveform = mic_audio
34
+ elif audio is not None:
35
+ sampling_rate, waveform = audio
36
+ else:
37
+ return "(please provide audio)"
38
+
39
+ waveform = process_audio(sampling_rate, waveform)
40
+ inputs = processor(audio=waveform, sampling_rate=16000, return_tensors="pt")
41
+ predicted_ids = model.generate(**inputs, max_length=400)
42
+ transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
43
+ return transcription[0]
44
+
45
+
46
+ title = "SpeechT5: Automatic Speech Recognition"
47
+
48
+ description = """
49
+ 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.
50
+ By pre-training on text and speech at the same time, it learns unified representations for both, resulting in improved modeling capabilities.
51
+
52
+ SpeechT5 can be fine-tuned for different speech tasks. This space demonstrates the <b>speech-to-text</b>
53
+ or automatic speech recognition (ASR) checkpoint for the English language.
54
+
55
+ See also the <a href="https://huggingface.co/spaces/Matthijs/speecht5-tts-demo">text-to-speech (TTS) demo</a>
56
+ and the <a href="https://huggingface.co/spaces/Matthijs/speecht5-vc-demo">voice conversion demo</a>.
57
+
58
+ <b>How to use:</b> Upload an audio file or record using the microphone. The audio is converted to mono and resampled to 16 kHz before
59
+ being passed into the model. The output is the text transcription of the audio. SpeechT5 uses a simple character-based tokenizer, with no
60
+ additional language model on top, so the output won't have punctuation or capitalization and may contain the occasional spelling error.
61
+ """
62
+
63
+ article = """
64
+ <div style='margin:20px auto;'>
65
+
66
+ <p>References: <a href="https://arxiv.org/abs/2110.07205">SpeechT5 paper</a> |
67
+ <a href="https://github.com/microsoft/SpeechT5/">original GitHub</a> |
68
+ <a href="https://huggingface.co/ajyy/SpeechT5">original weights</a></p>
69
+
70
+ <pre>
71
+ @article{Ao2021SpeechT5,
72
+ title = {SpeechT5: Unified-Modal Encoder-Decoder Pre-training for Spoken Language Processing},
73
+ 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},
74
+ eprint={2110.07205},
75
+ archivePrefix={arXiv},
76
+ primaryClass={eess.AS},
77
+ year={2021}
78
+ }
79
+ </pre>
80
+
81
+ <p>Example sound credits:<p>
82
+
83
+ <ul>
84
+ <li>"Hmm, I don't know" from <a href="https://freesound.org/people/InspectorJ/sounds/519189/">InspectorJ</a> (CC BY 4.0 license)
85
+ <li>"Henry V" excerpt from <a href="https://freesound.org/people/acclivity/sounds/24096/">acclivity</a> (CC BY-NC 4.0 license)
86
+ <li>"You can see it in the eyes" from <a href="https://freesound.org/people/JoyOhJoy/sounds/165348/">JoyOhJoy</a> (CC0 license)
87
+ <li>"We yearn for time" from <a href="https://freesound.org/people/Sample_Me/sounds/610529/">Sample_Me</a> (CC0 license)
88
+ </ul>
89
+
90
+ </div>
91
+ """
92
+
93
+ examples = [
94
+ ["examples/hmm_i_dont_know.wav", None],
95
+ ["examples/henry5.mp3", None],
96
+ ["examples/yearn_for_time.mp3", None],
97
+ ["examples/see_in_eyes.wav", None],
98
+ ]
99
+
100
+ gr.Interface(
101
+ fn=predict,
102
+ inputs=[
103
+ gr.Audio(label="Upload Speech", source="upload", type="numpy"),
104
+ gr.Audio(label="Record Speech", source="microphone", type="numpy"),
105
+ ],
106
+ outputs=[
107
+ gr.Text(label="Transcription"),
108
+ ],
109
+ title=title,
110
+ description=description,
111
+ article=article,
112
+ examples=examples,
113
+ ).launch()
examples/henry5.mp3 ADDED
Binary file (375 kB). View file
 
examples/hmm_i_dont_know.wav ADDED
Binary file (203 kB). View file
 
examples/see_in_eyes.wav ADDED
Binary file (65.2 kB). View file
 
examples/yearn_for_time.mp3 ADDED
Binary file (56.3 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ git+https://github.com/hollance/transformers.git@speecht5
2
+ torch
3
+ torchaudio
4
+ soundfile
5
+ librosa
6
+ samplerate
7
+ resampy
8
+ sentencepiece