File size: 5,229 Bytes
5b6f2f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad2d740
b161c5a
f155e22
5b6f2f8
 
3137a68
 
 
 
b161c5a
 
 
 
 
5b6f2f8
b161c5a
5b6f2f8
 
fe9f5d4
 
 
 
 
 
 
 
5b6f2f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad2d740
5b6f2f8
 
 
 
 
 
 
 
 
fe9f5d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b6f2f8
 
 
 
 
 
 
 
 
fe9f5d4
ad2d740
 
 
 
 
 
5b6f2f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import warnings
warnings.filterwarnings("ignore")

import os
import re
import librosa
import webrtcvad
import nbimporter
import torchaudio
import numpy as np
import gradio as gr
import scipy.signal
import soundfile as sf
from scipy.io.wavfile import write
from transformers import pipeline
from transformers import AutoProcessor
from pyctcdecode import build_ctcdecoder
from transformers import Wav2Vec2ProcessorWithLM
from scipy.signal import butter, lfilter, wiener

from text2int import text_to_int
from isNumber import is_number
from Text2List import text_to_list
from convert2list import convert_to_list
from processDoubles import process_doubles
from replaceWords import replace_words
from applyVad import apply_vad
from wienerFilter import wiener_filter
from highPassFilter import high_pass_filter
from waveletDenoise import wavelet_denoise

transcriber_taml_new = pipeline(task="automatic-speech-recognition", model="cdactvm/w2v-bert-tamil_new")
# transcriber_hindi_old = pipeline(task="automatic-speech-recognition", model="cdactvm/w2v-bert-tamil_new")
processor = AutoProcessor.from_pretrained("cdactvm/w2v-bert-tamil_new")
vocab_dict = processor.tokenizer.get_vocab()
sorted_vocab_dict = {k.lower(): v for k, v in sorted(vocab_dict.items(), key=lambda item: item[1])}
# decoder = build_ctcdecoder(
#     labels=list(sorted_vocab_dict.keys()),
#     kenlm_model_path="lm.binary",
#     )
# processor_with_lm = Wav2Vec2ProcessorWithLM(
#     feature_extractor=processor.feature_extractor,
#     tokenizer=processor.tokenizer,
#     decoder=decoder
#     )
processor.feature_extractor._processor_class = "Wav2Vec2ProcessorWithLM"
# transcriber_hindi_lm = pipeline("automatic-speech-recognition", model="cdactvm/w2v-bert-tamil_new", tokenizer=processor_with_lm, feature_extractor=processor_with_lm.feature_extractor, decoder=processor_with_lm.decoder)


# def transcribe_tamil_new(audio):
#     # # Process the audio file
#     transcript = transcriber_taml_new(audio)
#     text_value = transcript['text']
#     processd_doubles=process_doubles(text_value)
#     replaced_words = replace_words(processd_doubles)
#     converted_text=text_to_int(replaced_words)
#     return converted_text
    

###############################################
# implementation of noise reduction techniques.

# Function to apply a Wiener filter for noise reduction
def apply_wiener_filter(audio):
    return wiener(audio)

# Function to handle speech recognition
def Noise_cancellation_function(audio_file):
    # Load the audio file using librosa
    audio, sr = librosa.load(audio_file, sr=16000)

    # Step 1: Apply a high-pass filter
    audio = high_pass_filter(audio, sr)

    # Step 2: Apply Wiener filter for noise reduction
    audio = apply_wiener_filter(audio)

    # Step 3: Apply wavelet denoising
    denoised_audio = wavelet_denoise(audio)

    # Save the denoised audio to a temporary file
    temp_wav = "temp_denoised.wav"
    write(temp_wav, sr, denoised_audio)

    # Perform speech recognition on the denoised audio
    transcript = transcriber_taml_new(temp_wav)
    text_value = transcript['text']
    cleaned_text=text_value.replace("<s>","")
    processd_doubles=process_doubles(cleaned_text)
    replaced_words = replace_words(processd_doubles)
    converted_text=text_to_int(replaced_words)
    return converted_text

#################################################

# Function to handle speech recognition
def recognize_speech(audio_file):
    audio, sr = librosa.load(audio_file, sr=16000)
    audio = high_pass_filter(audio, sr)
    audio = apply_wiener_filter(audio)
    denoised_audio = wavelet_denoise(audio)
    result = asr_model(denoised_audio)
    text_value = result['text']
    cleaned_text = text_value.replace("<s>", "")
    print(cleaned_text)
    converted_to_list = convert_to_list(cleaned_text, text_to_list())
    print(converted_to_list)
    processed_doubles = process_doubles(converted_to_list)
    print(processed_doubles)
    replaced_words = replace_words(processed_doubles)
    print(replaced_words)
    converted_text = text_to_int(replaced_words)
    print(converted_text)
    return converted_text


def sel_lng(lng, mic=None, file=None):
    if mic is not None:
        audio = mic
    elif file is not None:
        audio = file
    else:
        return "You must either provide a mic recording or a file"
    
    if lng == "model_1":
        return recognize_speech(audio)
    # elif lng == "model_2":
    #     return transcribe_hindi_new(audio)
    # elif lng== "model_3":
    #     return transcribe_hindi_lm(audio)
    # elif lng== "model_4":
    #     return Noise_cancellation_function(audio)
            
        
demo=gr.Interface(
    fn=sel_lng, 
      
    inputs=[
        gr.Dropdown([
            "model_1","model_2","model_3","model_4"],label="Select Model"),
        gr.Audio(sources=["microphone","upload"], type="filepath"),
    ],
    outputs=[
        "textbox"
    ],
    title="Automatic Speech Recognition",
    description = "Demo for Automatic Speech Recognition. Use microphone to record speech. Please press Record button. Initially it will take some time to load the model. The recognized text will appear in the output textbox",
      ).launch()