Spaces:
Sleeping
Sleeping
File size: 9,747 Bytes
3c4f822 642245b 3c4f822 144f8dd 3c4f822 6484054 3c4f822 6484054 a002fb8 6484054 a002fb8 6484054 18baff1 6484054 a002fb8 3c4f822 144f8dd 3c4f822 2b3036d 144f8dd 2b3036d 144f8dd a002fb8 144f8dd cc25526 144f8dd 3c4f822 2b4d292 a002fb8 4766ef6 2e77406 3c4f822 b0f6dcb 3c4f822 cc25526 144f8dd cc25526 144f8dd 3c4f822 cc25526 3c4f822 |
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
import warnings
warnings.filterwarnings("ignore")
import os
import re
import pywt
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 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
from scipy.signal import butter, lfilter, wiener
asr_model_telugu = pipeline("automatic-speech-recognition", model="cdactvm/telugu_w2v-bert_model")
asr_model_kannada = pipeline("automatic-speech-recognition", model="cdactvm/w2v_bert_kannada_030125")
def createlex(filename):
#filename = "num_map.txt"
# Initialize an empty dictionary
data_dict = {}
# Open the file and read it line by line
with open(filename, "r", encoding="utf-8") as f:
for line in f:
# Strip newline characters and split by tab
key, value = line.strip().split("\t")
# Add to dictionary
data_dict[key] = value
return data_dict
tellex=createlex("num_words_tel.txt")
kanlex=createlex("num_words_kn.txt")
def addnum(inlist):
sum=0
for num in inlist:
sum+=int(num)
return sum
from rapidfuzz import process
def get_val(word, lexicon):
threshold = 80 # Minimum similarity score
length_difference = 4
#length_range = (4, 6) # Acceptable character length range (min, max)
# Find the best match above the similarity threshold
result = process.extractOne(word, lexicon.keys(), score_cutoff=threshold)
print (result)
if result:
match, score, _ = result
#print(lexicon[match])
#return lexicon[match]
if abs(len(match) - len(word)) <= length_difference:
#if length_range[0] <= len(match) <= length_range[1]:
return lexicon[match]
else:
return None
else:
return None
def convert2numtel(input, lex):
input += " #" # Add a period for termination
words = input.split()
i = 0
num = 0
outstr = ""
digit_end = True
numlist = []
addflag = False
prevword=""
single_list=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,17,18,19]
# Process the words
while i < len(words):
#checkwordlist = handleSpecialnum(words[i])
# Handle special numbers
#if len(checkwordlist) == 2:
# words[i] = checkwordlist[0]
# words.insert(i + 1, checkwordlist[1]) # Collect new word for later processing
# Get numerical value of the word
numval = get_val(words[i], lex)
if numval is not None:
if prevword not in single_list:
addflag = True
numlist.append(numval)
else:
if addflag:
numlist.append(numval)
num = addnum(numlist)
outstr += str(num) + " "
addflag = False
numlist = []
else:
outstr += " " + str(numval) + " "
digit_end = False
prevword=numval
else:
prevword=""
if addflag:
num = addnum(numlist)
outstr += str(num) + " " + words[i] + " "
addflag = False
numlist = []
else:
outstr += words[i] + " "
if not digit_end:
digit_end = True
# Move to the next word
i += 1
# Final processing
outstr = outstr.replace('#','') # Remove trailing spaces
return outstr
def convert2numkn(input, lex):
input += " ######" # Add a period for termination
words = input.split()
i = 0
num = 0
outstr = ""
digit_end = True
numlist = []
addflag = False
prevword = []
# Process the words
while i < len(words):
# Get numerical value of the word
numval = get_val(words[i], lex)
if len(prevword)>=3:
prevword.pop(0)
prevword.append(words[i])
else:
prevword.append(words[i])
if numval is not None:
addflag = True
numlist.append(numval)
else:
#print("word--->"+words[i])
#print("addflagword--->"+str(addflag))
prevwords=" ".join(prevword)
#print("prev word--->"+prevwords)
numval=get_val(prevwords,lex)
if numval is not None:
#addflag=True
#print("numval " +numval)
numlist=[]
#print("First outstr--->"+outstr)
outwords = outstr.split()
outstr=" ".join(outwords[:-1])
#print("outstr--->"+outstr)
outstr += " " + str(numval) + " "
#print(" aoutstr--->"+outstr)
numval=0
addflag=False
else:
if addflag:
num = addnum(numlist)
outstr += str(num) + " " + words[i] + " "
#print("penlast outstr--->"+outstr)
addflag = False
numlist = []
else:
outstr += words[i] + " "
#print("last outstr--->"+outstr)
if not digit_end:
digit_end = True
# Move to the next word
i += 1
# Final processing
outstr = outstr.replace('#','') # Remove trailing spaces
return outstr
# Function to apply a high-pass filter
def high_pass_filter(audio, sr, cutoff=300):
nyquist = 0.5 * sr
normal_cutoff = cutoff / nyquist
b, a = butter(1, normal_cutoff, btype='high', analog=False)
filtered_audio = lfilter(b, a, audio)
return filtered_audio
# Function to apply wavelet denoising
def wavelet_denoise(audio, wavelet='db1', level=1):
coeffs = pywt.wavedec(audio, wavelet, mode='per')
sigma = np.median(np.abs(coeffs[-level])) / 0.5
uthresh = sigma * np.sqrt(2 * np.log(len(audio)))
coeffs[1:] = [pywt.threshold(i, value=uthresh, mode='soft') for i in coeffs[1:]]
return pywt.waverec(coeffs, wavelet, mode='per')
# Function to apply a Wiener filter for noise reduction
def apply_wiener_filter(audio):
return wiener(audio)
# Function to handle speech recognition
def recognize_speech_telugu(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_telugu(denoised_audio)
result = asr_model_telugu(audio)
text_value = result['text']
print (text_value)
cleaned_text = text_value.replace("<s>", "")
converted_text=convert2numtel(cleaned_text,tellex)
# cleaned_text=convert2num(cleaned_text,lex)
# converted_to_list = convert_to_list(cleaned_text, text_to_list())
# processed_doubles = process_doubles(converted_to_list)
# replaced_words = replace_words(processed_doubles)
# converted_text = text_to_int(replaced_words)
return cleaned_text +" -----------------> " + converted_text
#return cleaned_text
# Function to handle speech recognition
def recognize_speech_kannada(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_kannada(denoised_audio)
text_value = result['text']
cleaned_text = text_value.replace("[UNK]", "")
converted_text=convert2numkn(cleaned_text,kanlex)
#converted_text=convert2num(cleaned_text,lex)
# cleaned_text=convert2num(cleaned_text,lex)
# converted_to_list = convert_to_list(cleaned_text, text_to_list())
# processed_doubles = process_doubles(converted_to_list)
# replaced_words = replace_words(processed_doubles)
# converted_text = text_to_int(replaced_words)
return cleaned_text +" -----------------> " + 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 == "Telugu":
return recognize_speech_telugu(audio)
elif lng == "Kannada":
return recognize_speech_kannada(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([
"Telugu","Kannada"],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()
|