Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,7 @@ from waveletDenoise import wavelet_denoise
|
|
| 31 |
from scipy.signal import butter, lfilter, wiener
|
| 32 |
|
| 33 |
asr_model = pipeline("automatic-speech-recognition", model="cdactvm/w2v-bert-tamil_new")
|
| 34 |
-
|
| 35 |
# Function to apply a high-pass filter
|
| 36 |
def high_pass_filter(audio, sr, cutoff=300):
|
| 37 |
nyquist = 0.5 * sr
|
|
@@ -52,6 +52,101 @@ def wavelet_denoise(audio, wavelet='db1', level=1):
|
|
| 52 |
def apply_wiener_filter(audio):
|
| 53 |
return wiener(audio)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Function to handle speech recognition
|
| 56 |
def recognize_speech(audio_file):
|
| 57 |
audio, sr = librosa.load(audio_file, sr=16000)
|
|
@@ -61,6 +156,7 @@ def recognize_speech(audio_file):
|
|
| 61 |
result = asr_model(denoised_audio)
|
| 62 |
text_value = result['text']
|
| 63 |
cleaned_text = text_value.replace("<s>", "")
|
|
|
|
| 64 |
# converted_to_list = convert_to_list(cleaned_text, text_to_list())
|
| 65 |
# processed_doubles = process_doubles(converted_to_list)
|
| 66 |
# replaced_words = replace_words(processed_doubles)
|
|
|
|
| 31 |
from scipy.signal import butter, lfilter, wiener
|
| 32 |
|
| 33 |
asr_model = pipeline("automatic-speech-recognition", model="cdactvm/w2v-bert-tamil_new")
|
| 34 |
+
lex=createlex("num_words_ta.txt")
|
| 35 |
# Function to apply a high-pass filter
|
| 36 |
def high_pass_filter(audio, sr, cutoff=300):
|
| 37 |
nyquist = 0.5 * sr
|
|
|
|
| 52 |
def apply_wiener_filter(audio):
|
| 53 |
return wiener(audio)
|
| 54 |
|
| 55 |
+
def createlex(filename):
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Initialize an empty dictionary
|
| 59 |
+
data_dict = {}
|
| 60 |
+
|
| 61 |
+
# Open the file and read it line by line
|
| 62 |
+
with open(filename, "r", encoding="utf-8") as f:
|
| 63 |
+
for line in f:
|
| 64 |
+
# Strip newline characters and split by tab
|
| 65 |
+
key, value = line.strip().split("\t")
|
| 66 |
+
# Add to dictionary
|
| 67 |
+
data_dict[key] = value
|
| 68 |
+
return data_dict
|
| 69 |
+
|
| 70 |
+
def addnum(inlist):
|
| 71 |
+
sum=0
|
| 72 |
+
for num in inlist:
|
| 73 |
+
sum+=int(num)
|
| 74 |
+
|
| 75 |
+
return sum
|
| 76 |
+
from rapidfuzz import process
|
| 77 |
+
def get_val(word, lexicon):
|
| 78 |
+
threshold = 80 # Minimum similarity score
|
| 79 |
+
length_difference = 4
|
| 80 |
+
#length_range = (4, 6) # Acceptable character length range (min, max)
|
| 81 |
+
|
| 82 |
+
# Find the best match above the similarity threshold
|
| 83 |
+
result = process.extractOne(word, lexicon.keys(), score_cutoff=threshold)
|
| 84 |
+
#print (result)
|
| 85 |
+
if result:
|
| 86 |
+
match, score, _ = result
|
| 87 |
+
#print(lexicon[match])
|
| 88 |
+
#return lexicon[match]
|
| 89 |
+
if abs(len(match) - len(word)) <= length_difference:
|
| 90 |
+
#if length_range[0] <= len(match) <= length_range[1]:
|
| 91 |
+
return lexicon[match]
|
| 92 |
+
else:
|
| 93 |
+
return None
|
| 94 |
+
else:
|
| 95 |
+
return None
|
| 96 |
+
|
| 97 |
+
def convert2num(input, lex):
|
| 98 |
+
input += " #" # Add a period for termination
|
| 99 |
+
words = input.split()
|
| 100 |
+
i = 0
|
| 101 |
+
num = 0
|
| 102 |
+
outstr = ""
|
| 103 |
+
digit_end = True
|
| 104 |
+
numlist = []
|
| 105 |
+
addflag = False
|
| 106 |
+
|
| 107 |
+
# Process the words
|
| 108 |
+
while i < len(words):
|
| 109 |
+
#checkwordlist = handleSpecialnum(words[i])
|
| 110 |
+
|
| 111 |
+
# Handle special numbers
|
| 112 |
+
#if len(checkwordlist) == 2:
|
| 113 |
+
# words[i] = checkwordlist[0]
|
| 114 |
+
# words.insert(i + 1, checkwordlist[1]) # Collect new word for later processing
|
| 115 |
+
|
| 116 |
+
# Get numerical value of the word
|
| 117 |
+
numval = get_val(words[i], lex)
|
| 118 |
+
if numval is not None:
|
| 119 |
+
if words[i][-4:] in ('த்து', 'ற்று'):
|
| 120 |
+
addflag = True
|
| 121 |
+
numlist.append(numval)
|
| 122 |
+
else:
|
| 123 |
+
if addflag:
|
| 124 |
+
numlist.append(numval)
|
| 125 |
+
num = addnum(numlist)
|
| 126 |
+
outstr += str(num) + " "
|
| 127 |
+
addflag = False
|
| 128 |
+
numlist = []
|
| 129 |
+
else:
|
| 130 |
+
outstr += " " + str(numval) + " "
|
| 131 |
+
digit_end = False
|
| 132 |
+
else:
|
| 133 |
+
if addflag:
|
| 134 |
+
num = addnum(numlist)
|
| 135 |
+
outstr += str(num) + " " + words[i] + " "
|
| 136 |
+
addflag = False
|
| 137 |
+
numlist = []
|
| 138 |
+
else:
|
| 139 |
+
outstr += words[i] + " "
|
| 140 |
+
if not digit_end:
|
| 141 |
+
digit_end = True
|
| 142 |
+
|
| 143 |
+
# Move to the next word
|
| 144 |
+
i += 1
|
| 145 |
+
|
| 146 |
+
# Final processing
|
| 147 |
+
outstr = outstr.replace('#','') # Remove trailing spaces
|
| 148 |
+
return outstr
|
| 149 |
+
|
| 150 |
# Function to handle speech recognition
|
| 151 |
def recognize_speech(audio_file):
|
| 152 |
audio, sr = librosa.load(audio_file, sr=16000)
|
|
|
|
| 156 |
result = asr_model(denoised_audio)
|
| 157 |
text_value = result['text']
|
| 158 |
cleaned_text = text_value.replace("<s>", "")
|
| 159 |
+
cleaned_text=convert2num(cleaned_text,lex)
|
| 160 |
# converted_to_list = convert_to_list(cleaned_text, text_to_list())
|
| 161 |
# processed_doubles = process_doubles(converted_to_list)
|
| 162 |
# replaced_words = replace_words(processed_doubles)
|