File size: 2,001 Bytes
417f770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a299284
417f770
a299284
417f770
 
 
 
 
9726882
 
 
b08c991
417f770
9726882
 
417f770
9726882
 
417f770
9726882
 
 
417f770
 
 
 
 
 
1842c19
417f770
 
b08c991
417f770
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#Importing all the necessary packages
import nltk
import librosa
import torch
import gradio as gr
from transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTC
nltk.download("punkt")


def asr_transcript(audio_file, language):
    """Generating transcripts for the audio input
    """
    
    #Selecting the language and loading the model and the tokenizer
    if language == "English":
        model = "facebook/wav2vec2-large-960h-lv60-self"
    elif language == "Russian":
        model = "jonatasgrosman/wav2vec2-large-xlsr-53-russian"
    
    tokenizer = Wav2Vec2Tokenizer.from_pretrained(model)
    model = Wav2Vec2ForCTC.from_pretrained(model)
  
    #read the file and resample to 16KHz
    #stream = librosa.stream(audio_file.name, block_length=20, frame_length=16000, hop_length=16000)
    
    #read the file
    speech, sample_rate = librosa.load(audio_file, 16000)

    if len(speech.shape) > 1:
        speech = speech[:, 0] + speech[:, 1]

    input_values = tokenizer(speech, return_tensors="pt").input_values
    logits = model(input_values).logits

    predicted_ids = torch.argmax(logits, dim=-1)
    transcription = tokenizer.batch_decode(predicted_ids)[0]
    transcript = transcription.lower()

    return transcript
  

gr.Interface(asr_transcript,
             inputs = [gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Please record your message/Пожалуйста, введите Ваше сообщение"), 
             gr.inputs.Radio(label="Pick a language/Выберите язык", choices=["English", "Russian"])],
             outputs = gr.outputs.Textbox(label="Output Text/Результат"),
             title="Automatic speech recognition with voice recorder in Russian and English",
             description = "This application displays transcribed text for given audio input/Это приложение отображает текст Вашего голосового сообщения",
             theme="grass").launch()