anuragshas commited on
Commit
609963b
1 Parent(s): 30ad04a

add application file

Browse files
Files changed (2) hide show
  1. app.py +178 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from tensorflow import keras
5
+ import tensorflow_io as tfio
6
+ from huggingface_hub import from_pretrained_keras
7
+
8
+
9
+ model = from_pretrained_keras("keras-io/ctc_asr", compile=False)
10
+
11
+ characters = [x for x in "abcdefghijklmnopqrstuvwxyz'?! "]
12
+ # Mapping characters to integers
13
+ char_to_num = keras.layers.StringLookup(vocabulary=characters, oov_token="")
14
+ # Mapping integers back to original characters
15
+ num_to_char = keras.layers.StringLookup(
16
+ vocabulary=char_to_num.get_vocabulary(), oov_token="", invert=True
17
+ )
18
+
19
+ # An integer scalar Tensor. The window length in samples.
20
+ frame_length = 256
21
+ # An integer scalar Tensor. The number of samples to step.
22
+ frame_step = 160
23
+ # An integer scalar Tensor. The size of the FFT to apply.
24
+ # If not provided, uses the smallest power of 2 enclosing frame_length.
25
+ fft_length = 384
26
+
27
+ SAMPLE_RATE = 22050
28
+
29
+
30
+ def decode_batch_predictions(pred):
31
+ input_len = np.ones(pred.shape[0]) * pred.shape[1]
32
+ # Use greedy search. For complex tasks, you can use beam search
33
+ results = keras.backend.ctc_decode(pred, input_length=input_len, greedy=True)[0][0]
34
+ # Iterate over the results and get back the text
35
+ output_text = []
36
+ for result in results:
37
+ result = tf.strings.reduce_join(num_to_char(result)).numpy().decode("utf-8")
38
+ output_text.append(result)
39
+ return output_text
40
+
41
+
42
+ def load_16k_audio_wav(filename):
43
+ # Read file content
44
+ file_content = tf.io.read_file(filename)
45
+
46
+ # Decode audio wave
47
+ audio_wav, sample_rate = tf.audio.decode_wav(file_content, desired_channels=1)
48
+ audio_wav = tf.squeeze(audio_wav, axis=-1)
49
+ sample_rate = tf.cast(sample_rate, dtype=tf.int64)
50
+
51
+ # Resample to 16k
52
+ audio_wav = tfio.audio.resample(
53
+ audio_wav, rate_in=sample_rate, rate_out=SAMPLE_RATE
54
+ )
55
+
56
+ return audio_wav
57
+
58
+
59
+ def mic_to_tensor(recorded_audio_file):
60
+ sample_rate, audio = recorded_audio_file
61
+
62
+ audio_wav = tf.constant(audio, dtype=tf.float32)
63
+ if tf.rank(audio_wav) > 1:
64
+ audio_wav = tf.reduce_mean(audio_wav, axis=1)
65
+ audio_wav = tfio.audio.resample(
66
+ audio_wav, rate_in=sample_rate, rate_out=SAMPLE_RATE
67
+ )
68
+
69
+ audio_wav = tf.divide(audio_wav, tf.reduce_max(tf.abs(audio_wav)))
70
+
71
+ return audio_wav
72
+
73
+
74
+ def tensor_to_predictions(audio_tensor):
75
+ # 3. Change type to float
76
+ audio_tensor = tf.cast(audio_tensor, tf.float32)
77
+
78
+ # 4. Get the spectrogram
79
+ spectrogram = tf.signal.stft(
80
+ audio_tensor,
81
+ frame_length=frame_length,
82
+ frame_step=frame_step,
83
+ fft_length=fft_length,
84
+ )
85
+
86
+ # 5. We only need the magnitude, which can be derived by applying tf.abs
87
+ spectrogram = tf.abs(spectrogram)
88
+ spectrogram = tf.math.pow(spectrogram, 0.5)
89
+
90
+ # 6. normalisation
91
+ means = tf.math.reduce_mean(spectrogram, 1, keepdims=True)
92
+ stddevs = tf.math.reduce_std(spectrogram, 1, keepdims=True)
93
+ spectrogram = (spectrogram - means) / (stddevs + 1e-10)
94
+
95
+ spectrogram = tf.expand_dims(spectrogram, axis=0)
96
+
97
+ batch_predictions = model.predict(spectrogram)
98
+ batch_predictions = decode_batch_predictions(batch_predictions)
99
+ return batch_predictions
100
+
101
+
102
+ def clear_inputs_and_outputs():
103
+ return [None, None, None]
104
+
105
+
106
+ def predict(recorded_audio_file, uploaded_audio_file):
107
+ # 1. Read wav file
108
+ if recorded_audio_file:
109
+ audio_tensor = mic_to_tensor(recorded_audio_file)
110
+ else:
111
+ audio_tensor = load_16k_audio_wav(uploaded_audio_file)
112
+
113
+ prediction = tensor_to_predictions(audio_tensor)[0]
114
+ return prediction
115
+
116
+
117
+ # gr.Interface(
118
+ # infer,
119
+ # inputs=gr.Audio(source="microphone", type="filepath"),
120
+ # outputs=gr.Textbox(lines=5, label="Input Text"),
121
+ # #title=title,
122
+ # #description=description,
123
+ # #article=article,
124
+ # #examples=examples,
125
+ # enable_queue=True,
126
+ # ).launch(debug=True)
127
+
128
+ # Main function
129
+ if __name__ == "__main__":
130
+ demo = gr.Blocks()
131
+
132
+ with demo:
133
+ gr.Markdown(
134
+ """
135
+ <center><h1>Automatic Speech Recognition using CTC</h1></center> \
136
+ This space is a demo of Automatic Speech Recognition using Keras trained on LJSpeech dataset.<br> \
137
+ In this space, you can record your voice or upload a wav file and the model will predict the words spoken in English<br><br>
138
+ """
139
+ )
140
+ with gr.Row():
141
+ ## Input
142
+ with gr.Column():
143
+ mic_input = gr.Audio(source="microphone", label="Record your own voice")
144
+ upl_input = gr.Audio(
145
+ source="upload", type="filepath", label="Upload a wav file"
146
+ )
147
+
148
+ with gr.Row():
149
+ clr_btn = gr.Button(value="Clear", variant="secondary")
150
+ prd_btn = gr.Button(value="Predict")
151
+
152
+ # Outputs
153
+ with gr.Column():
154
+ lbl_output = gr.Label(label="Text")
155
+
156
+ # Credits
157
+ with gr.Row():
158
+ gr.Markdown(
159
+ """
160
+ <h4>Credits</h4>
161
+ Author: <a href="https://twitter.com/anuragcomm"> Anurag Singh</a>.<br>
162
+ Based on the following Keras example <a href="https://keras.io/examples/audio/ctc_asr">Automatic Speech Recognition using CTC</a> by <a href="https://rbouadjenek.github.io/">Mohamed Reda Bouadjenek</a> and <a href="https://www.linkedin.com/in/parkerhuynh/">Ngoc Dung Huynh</a><br>
163
+ Check out the model <a href="https://huggingface.co/keras-io/ctc_asr">here</a>
164
+ """
165
+ )
166
+
167
+ clr_btn.click(
168
+ fn=clear_inputs_and_outputs,
169
+ inputs=[],
170
+ outputs=[mic_input, upl_input, lbl_output],
171
+ )
172
+ prd_btn.click(
173
+ fn=predict,
174
+ inputs=[mic_input, upl_input],
175
+ outputs=[lbl_output],
176
+ )
177
+
178
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy
2
+ matplotlib
3
+ tensorflow==2.8.2
4
+ tensorflow_io==0.25.0