Update app.py
Browse files
app.py
CHANGED
@@ -1,109 +1,105 @@
|
|
1 |
-
import torch
|
2 |
-
import gradio as gr
|
3 |
-
import speechbrain as sb
|
4 |
-
import torchaudio
|
5 |
-
from hyperpyyaml import load_hyperpyyaml
|
6 |
-
from pyctcdecode import build_ctcdecoder
|
7 |
-
import os
|
8 |
-
|
9 |
-
# Load hyperparameters and initialize the ASR model
|
10 |
-
hparams_file = "train.yaml"
|
11 |
-
with open(hparams_file, "r") as fin:
|
12 |
-
hparams = load_hyperpyyaml(fin)
|
13 |
-
|
14 |
-
# Initialize the label encoder
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
)
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
labels
|
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 |
-
asr_brain
|
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 |
-
gr.
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
],
|
106 |
-
outputs="text",
|
107 |
-
title=title,
|
108 |
-
description=description
|
109 |
).launch()
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import speechbrain as sb
|
4 |
+
import torchaudio
|
5 |
+
from hyperpyyaml import load_hyperpyyaml
|
6 |
+
from pyctcdecode import build_ctcdecoder
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Load hyperparameters and initialize the ASR model
|
10 |
+
hparams_file = "train.yaml"
|
11 |
+
with open(hparams_file, "r") as fin:
|
12 |
+
hparams = load_hyperpyyaml(fin)
|
13 |
+
|
14 |
+
# Initialize the label encoder
|
15 |
+
encoder = sb.dataio.encoder.CTCTextEncoder()
|
16 |
+
|
17 |
+
encoder.load_or_create(
|
18 |
+
path=hparams["encoder_file"],
|
19 |
+
from_didatasets=[[]],
|
20 |
+
output_key="char_list",
|
21 |
+
special_labels=special_labels = {"blank_label":0,"unk_label": 1},
|
22 |
+
sequence_input=True,
|
23 |
+
)
|
24 |
+
|
25 |
+
# Prepare labels for the CTC decoder
|
26 |
+
ind2lab = encoder.ind2lab
|
27 |
+
labels = [ind2lab[x] for x in range(len(ind2lab))]
|
28 |
+
labels = [""] + labels[1:-1] + ["1"]
|
29 |
+
|
30 |
+
# Initialize the CTC decoder
|
31 |
+
decoder = build_ctcdecoder(
|
32 |
+
labels,
|
33 |
+
kenlm_model_path=hparams["ngram_lm_path"],
|
34 |
+
alpha=0.5,
|
35 |
+
beta=1.0,
|
36 |
+
)
|
37 |
+
|
38 |
+
|
39 |
+
# Define the ASR class with the `treat_wav` method
|
40 |
+
class ASR(sb.core.Brain):
|
41 |
+
def treat_wav(self, sig):
|
42 |
+
"""Process a waveform and return the transcribed text."""
|
43 |
+
feats = self.modules.wav2vec2(sig.to("cpu"), torch.tensor([1]).to("cpu"))
|
44 |
+
feats = self.modules.enc(feats)
|
45 |
+
logits = self.modules.ctc_lin(feats)
|
46 |
+
p_ctc = self.hparams.log_softmax(logits)
|
47 |
+
predicted_words = []
|
48 |
+
for logs in p_ctc:
|
49 |
+
text = decoder.decode(logs.detach().cpu().numpy())
|
50 |
+
predicted_words.append(text.split(" "))
|
51 |
+
return " ".join(predicted_words[0])
|
52 |
+
|
53 |
+
|
54 |
+
# Initialize the ASR model
|
55 |
+
asr_brain = ASR(
|
56 |
+
modules=hparams["modules"],
|
57 |
+
hparams=hparams,
|
58 |
+
run_opts={"device": "cpu"},
|
59 |
+
checkpointer=hparams["checkpointer"],
|
60 |
+
)
|
61 |
+
asr_brain.tokenizer = encoder
|
62 |
+
asr_brain.checkpointer.recover_if_possible()
|
63 |
+
asr_brain.modules.eval()
|
64 |
+
|
65 |
+
|
66 |
+
# Function to process audio files
|
67 |
+
def treat_wav_file(file_mic, file_upload, asr=asr_brain, device="cpu"):
|
68 |
+
if file_mic is not None:
|
69 |
+
wav = file_mic
|
70 |
+
elif file_upload is not None:
|
71 |
+
wav = file_upload
|
72 |
+
else:
|
73 |
+
return "ERROR: You have to either use the microphone or upload an audio file"
|
74 |
+
|
75 |
+
# Read and preprocess the audio file
|
76 |
+
info = torchaudio.info(wav)
|
77 |
+
sr = info.sample_rate
|
78 |
+
sig = sb.dataio.dataio.read_audio(wav)
|
79 |
+
if len(sig.shape) > 1:
|
80 |
+
sig = torch.mean(sig, dim=1)
|
81 |
+
sig = torch.unsqueeze(sig, 0)
|
82 |
+
tensor_wav = sig.to(device)
|
83 |
+
resampled = torchaudio.functional.resample(tensor_wav, sr, 16000)
|
84 |
+
|
85 |
+
# Transcribe the audio
|
86 |
+
sentence = asr.treat_wav(resampled)
|
87 |
+
return sentence
|
88 |
+
|
89 |
+
|
90 |
+
# Gradio interface
|
91 |
+
title = "Tunisian Speech Recognition"
|
92 |
+
description = ''' This is a Tunisian ASR based on WavLM Model, fine-tuned on a dataset of 2.5 Hours resulting in a W.E.R of 24% and a C.E.R of 9 %.
|
93 |
+
\n
|
94 |
+
\n Interesting isn\'t it !'''
|
95 |
+
|
96 |
+
gr.Interface(
|
97 |
+
fn=treat_wav_file,
|
98 |
+
inputs=[
|
99 |
+
gr.Audio(sources="microphone", type='filepath', label="Record"),
|
100 |
+
gr.Audio(sources="upload", type='filepath', label="Upload File")
|
101 |
+
],
|
102 |
+
outputs="text",
|
103 |
+
title=title,
|
104 |
+
description=description
|
|
|
|
|
|
|
|
|
105 |
).launch()
|