alvi123 commited on
Commit
e4f7433
β€’
1 Parent(s): c12a349
Files changed (1) hide show
  1. app.py +107 -19
app.py CHANGED
@@ -1,20 +1,108 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
-
4
-
5
- model = pipeline(task="automatic-speech-recognition")
6
- def predict_speech_to_text(audio):
7
- prediction = model(audio)
8
- text = prediction['text']
9
- return text
10
-
11
- gr.Interface.load("models/facebook/s2t-medium-librispeech-asr")
12
- gr.Interface(fn=predict_speech_to_text,
13
- title="πŸ§‘πŸ½β€πŸŽ€ PROLOVE πŸ₯°πŸ˜˜ ",
14
- inputs=gr.inputs.Audio(
15
- source="microphone", type="filepath", label="Input"),
16
- outputs=gr.outputs.Textbox(label="Output"),
17
- description="This application was created to help correct pronouncation",
18
- examples=['Good night_alvi.wav'],
19
- allow_flagging='never'
20
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import librosa
3
+ import torch
4
+
5
+ from transformers import SpeechT5Processor, SpeechT5ForSpeechToText
6
+
7
+
8
+ checkpoint = "microsoft/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
+ # limit to 30 seconds
26
+ waveform = waveform[:16000*30]
27
+
28
+ # make PyTorch tensor
29
+ waveform = torch.tensor(waveform)
30
+ return waveform
31
+
32
+
33
+ def predict(audio, mic_audio=None):
34
+ # audio = tuple (sample_rate, frames) or (sample_rate, (frames, channels))
35
+ if mic_audio is not None:
36
+ sampling_rate, waveform = mic_audio
37
+ elif audio is not None:
38
+ sampling_rate, waveform = audio
39
+ else:
40
+ return "(please provide audio)"
41
+
42
+ waveform = process_audio(sampling_rate, waveform)
43
+ inputs = processor(audio=waveform, sampling_rate=16000, return_tensors="pt")
44
+ predicted_ids = model.generate(**inputs, max_length=400)
45
+ transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
46
+ return transcription[0]
47
+
48
+
49
+ title = "SpeechT5: Automatic Speech Recognition"
50
+
51
+ description = """
52
+ 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.
53
+ By pre-training on text and speech at the same time, it learns unified representations for both, resulting in improved modeling capabilities.
54
+ SpeechT5 can be fine-tuned for different speech tasks. This space demonstrates the <b>speech-to-text</b>
55
+ or automatic speech recognition (ASR) checkpoint for the English language.
56
+ See also the <a href="https://huggingface.co/spaces/Matthijs/speecht5-tts-demo">text-to-speech (TTS) demo</a>
57
+ and the <a href="https://huggingface.co/spaces/Matthijs/speecht5-vc-demo">voice conversion demo</a>.
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
+ <p>References: <a href="https://arxiv.org/abs/2110.07205">SpeechT5 paper</a> |
66
+ <a href="https://github.com/microsoft/SpeechT5/">original GitHub</a> |
67
+ <a href="https://huggingface.co/ajyy/SpeechT5">original weights</a></p>
68
+ <pre>
69
+ @article{Ao2021SpeechT5,
70
+ title = {SpeechT5: Unified-Modal Encoder-Decoder Pre-training for Spoken Language Processing},
71
+ 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},
72
+ eprint={2110.07205},
73
+ archivePrefix={arXiv},
74
+ primaryClass={eess.AS},
75
+ year={2021}
76
+ }
77
+ </pre>
78
+ <p>Example sound credits:<p>
79
+ <ul>
80
+ <li>"Hmm, I don't know" from <a href="https://freesound.org/people/InspectorJ/sounds/519189/">InspectorJ</a> (CC BY 4.0 license)
81
+ <li>"Henry V" excerpt from <a href="https://freesound.org/people/acclivity/sounds/24096/">acclivity</a> (CC BY-NC 4.0 license)
82
+ <li>"You can see it in the eyes" from <a href="https://freesound.org/people/JoyOhJoy/sounds/165348/">JoyOhJoy</a> (CC0 license)
83
+ <li>"We yearn for time" from <a href="https://freesound.org/people/Sample_Me/sounds/610529/">Sample_Me</a> (CC0 license)
84
+ </ul>
85
+ </div>
86
+ """
87
+
88
+ examples = [
89
+ ["examples/hmm_i_dont_know.wav", None],
90
+ ["examples/henry5.mp3", None],
91
+ ["examples/yearn_for_time.mp3", None],
92
+ ["examples/see_in_eyes.wav", None],
93
+ ]
94
+
95
+ gr.Interface(
96
+ fn=predict,
97
+ inputs=[
98
+ gr.Audio(label="Upload Speech", source="upload", type="numpy"),
99
+ gr.Audio(label="Record Speech", source="microphone", type="numpy"),
100
+ ],
101
+ outputs=[
102
+ gr.Text(label="Transcription"),
103
+ ],
104
+ title=title,
105
+ description=description,
106
+ article=article,
107
+ examples=examples,
108
+ ).launch()