DontFreakOut
commited on
Commit
·
19a2ea8
1
Parent(s):
4f6bcb0
adding program to app.py
Browse files- app.py +111 -4
- chinese-american.wav +3 -0
- indian.wav +3 -0
- mexican.wav +3 -0
- nigerian.wav +3 -0
- vietnamese.wav +3 -0
app.py
CHANGED
@@ -1,7 +1,114 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
import torchaudio
|
5 |
+
from speechbrain.pretrained import EncoderClassifier
|
6 |
|
7 |
+
# Set up pipe for whisper asr
|
8 |
+
asr_pipe = pipeline(
|
9 |
+
"automatic-speech-recognition",
|
10 |
+
model="openai/whisper-base.en",
|
11 |
+
torch_dtype=torch.float32,
|
12 |
+
device="cpu",
|
13 |
+
)
|
14 |
|
15 |
+
# Set up pipe for 2 phonemic transcription models
|
16 |
+
american_phoneme_pipe = pipeline("automatic-speech-recognition", model="vitouphy/wav2vec2-xls-r-300m-timit-phoneme")
|
17 |
+
esl_phoneme_pipe = pipeline("automatic-speech-recognition", model="mrrubino/wav2vec2-large-xlsr-53-l2-arctic-phoneme")
|
18 |
+
|
19 |
+
# Set up pipe for 2 accent classification models
|
20 |
+
classifier = EncoderClassifier.from_hparams(source="Jzuluaga/accent-id-commonaccent_ecapa", savedir="pretrained_models/accent-id-commonaccent_ecapa")
|
21 |
+
|
22 |
+
def native_accent_classifier(file):
|
23 |
+
out_prob, score, index, text_lab = classifier.classify_file(file)
|
24 |
+
return [{'accent': text_lab[0], 'score': round(score[0],2)}]
|
25 |
+
|
26 |
+
def esl_accent_classifier(file):
|
27 |
+
esl_accent_pipe = pipeline(
|
28 |
+
"audio-classification",
|
29 |
+
model="kaysrubio/accent-id-distilhubert-finetuned-l2-arctic2"
|
30 |
+
)
|
31 |
+
audio, sr = torchaudio.load(file) # Load audio
|
32 |
+
audio = torchaudio.transforms.Resample(orig_freq=sr, new_freq=16000)(audio)
|
33 |
+
audio = audio.squeeze().numpy()
|
34 |
+
result = esl_accent_pipe(audio, top_k=6)
|
35 |
+
return [{'accent': result[0]['label'], 'score': round(result[0]['score'],2)}]
|
36 |
+
|
37 |
+
def transcribe_and_classify_speech(audio):
|
38 |
+
try:
|
39 |
+
asr_output = asr_pipe(
|
40 |
+
audio,
|
41 |
+
max_new_tokens=256,
|
42 |
+
chunk_length_s=30,
|
43 |
+
batch_size=8,
|
44 |
+
)["text"]
|
45 |
+
except Exception as e:
|
46 |
+
print(f"An error occurred with openai/whisper-base.en: {e}")
|
47 |
+
asr_output = "Error, make sure your file is in mono format"
|
48 |
+
|
49 |
+
try:
|
50 |
+
american_phoneme_output = american_phoneme_pipe(audio)['text']
|
51 |
+
except Exception as e:
|
52 |
+
print(f"An error occurred with wav2vec2-xls-r-300m-timit-phoneme: {e}")
|
53 |
+
american_phoneme_output = "Error, make sure your file is in mono format"
|
54 |
+
|
55 |
+
try:
|
56 |
+
esl_phoneme_output = esl_phoneme_pipe(audio)['text']
|
57 |
+
except Exception as e:
|
58 |
+
print(f"An error occurred with mrrubino/wav2vec2-large-xlsr-53-l2-arctic-phoneme: {e}")
|
59 |
+
esl_phoneme_output = "Error"
|
60 |
+
|
61 |
+
try:
|
62 |
+
native_accent_output = native_accent_classifier(audio)
|
63 |
+
except Exception as e:
|
64 |
+
print(f"An error occurred with Jzuluaga/accent-id-commonaccent_ecapa: {e}")
|
65 |
+
native_accent_output = [{'accent': 'Unknown-please upload single channel audio'}, {'score': .0}]
|
66 |
+
|
67 |
+
try:
|
68 |
+
esl_accent_output = esl_accent_classifier(audio)
|
69 |
+
except Exception as e:
|
70 |
+
print(f"An error occurred with kaysrubio/accent-id-distilhubert-finetuned-l2-arctic2: {e}")
|
71 |
+
esl_accent_output = [{'accent': 'Unknown-please upload single channel audio'}, {'score': .0}]
|
72 |
+
|
73 |
+
output = [
|
74 |
+
{'transcription': asr_output},
|
75 |
+
{'phonemes_native_eng': american_phoneme_output},
|
76 |
+
{'phonemes_eng_second_lang': esl_phoneme_output},
|
77 |
+
{'native_eng_country': native_accent_output},
|
78 |
+
{'first_lang_if_not_eng': esl_accent_output}
|
79 |
+
]
|
80 |
+
return output
|
81 |
+
|
82 |
+
demo = gr.Blocks()
|
83 |
+
|
84 |
+
examples = [['chinese-american.wav'], ['mexican.wav'], ['vietnamese.wav'], ['indian.wav'], ['nigerian.wav']]
|
85 |
+
|
86 |
+
mic_transcribe = gr.Interface(
|
87 |
+
fn=transcribe_and_classify_speech,
|
88 |
+
inputs=gr.Audio(sources="microphone", type="filepath"),
|
89 |
+
outputs=gr.components.Textbox(),
|
90 |
+
examples=examples,
|
91 |
+
)
|
92 |
+
|
93 |
+
file_transcribe = gr.Interface(
|
94 |
+
fn=transcribe_and_classify_speech,
|
95 |
+
inputs=gr.Audio(sources="upload", type="filepath"),
|
96 |
+
outputs=gr.components.Textbox(),
|
97 |
+
examples=examples,
|
98 |
+
)
|
99 |
+
|
100 |
+
# Launch gradio app demo
|
101 |
+
with demo:
|
102 |
+
gr.TabbedInterface(
|
103 |
+
[mic_transcribe, file_transcribe],
|
104 |
+
["Transcribe Microphone", "Transcribe Audio File"],
|
105 |
+
)
|
106 |
+
|
107 |
+
demo.launch(debug=True)
|
108 |
+
|
109 |
+
|
110 |
+
#def greet(name):
|
111 |
+
# return "Hello " + name + "!!"
|
112 |
+
|
113 |
+
#demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
114 |
+
#demo.launch()
|
chinese-american.wav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cdce1a1d3cc295e1bacc7b336a113fbb50175ac6ce9e03ec8fb56592ab485e95
|
3 |
+
size 1922192
|
indian.wav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:935aca9237f0e5301f29e7a198d703448d1b700bd17ddfc2ca41a5ea87e1aebd
|
3 |
+
size 1929624
|
mexican.wav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c0656ce06d8b8afcf8618d398a636227abdad0c8b0310f8992ab7ce6cd30769c
|
3 |
+
size 1949312
|
nigerian.wav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:87f5b33de6ca632c5500ae9bb5bae83516e4845ae631f77bb4a91830226fdba8
|
3 |
+
size 1937144
|
vietnamese.wav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d71e309a48614c82fcf88ad60fab5dae593e408b8226836d1283d6dde2db4418
|
3 |
+
size 400472
|