Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Importing all the necessary packages
|
2 |
+
import nltk
|
3 |
+
import librosa
|
4 |
+
import torch
|
5 |
+
import gradio as gr
|
6 |
+
from transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTC
|
7 |
+
nltk.download("punkt")
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
def correct_casing(input_sentence):
|
12 |
+
""" This function is for correcting the casing of the generated transcribed text
|
13 |
+
"""
|
14 |
+
sentences = nltk.sent_tokenize(input_sentence)
|
15 |
+
return (' '.join([s.replace(s[0],s[0].capitalize(),1) for s in sentences]))
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
def asr_transcript(audio_file, language):
|
20 |
+
"""Generating transcripts for the audio input
|
21 |
+
"""
|
22 |
+
|
23 |
+
#Selecting the language and loading the model and the tokenizer
|
24 |
+
if language == "English":
|
25 |
+
model_name = "facebook/wav2vec2-large-960h-lv60-self"
|
26 |
+
elif language == "Russian":
|
27 |
+
model_name = "jonatasgrosman/wav2vec2-large-xlsr-53-russian"
|
28 |
+
|
29 |
+
tokenizer = Wav2Vec2Tokenizer.from_pretrained(model)
|
30 |
+
model = Wav2Vec2ForCTC.from_pretrained(model)
|
31 |
+
|
32 |
+
#read the file and resample to 16KHz
|
33 |
+
stream = librosa.stream(audio_file.name, block_length=20, frame_length=16000, hop_length=16000)
|
34 |
+
|
35 |
+
for speech in stream:
|
36 |
+
if len(speech.shape) > 1:
|
37 |
+
speech = speech[:, 0] + speech[:, 1]
|
38 |
+
|
39 |
+
input_values = tokenizer(speech, return_tensors="pt").input_values
|
40 |
+
logits = model(input_values).logits
|
41 |
+
|
42 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
43 |
+
transcription = tokenizer.batch_decode(predicted_ids)[0]
|
44 |
+
transcript += transcription.lower() + " "
|
45 |
+
|
46 |
+
return transcript
|
47 |
+
|
48 |
+
|
49 |
+
gr.Interface(asr_transcript,
|
50 |
+
inputs = [gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Please record your message/Пожалуйста, введите Ваше сообщение"),
|
51 |
+
gr.inputs.Radio(label="Pick a language/Выберите язык", choices=["English", "Russian"])
|
52 |
+
outputs = gr.outputs.Textbox(label="Output Text/Результат"),
|
53 |
+
title="Automatic speech recognition with voice recorder in Russian and English",
|
54 |
+
description = "This application displays transcribed text for given audio input",
|
55 |
+
theme="grass").launch()
|