Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,179 +31,179 @@ from scipy.signal import butter, lfilter, wiener
|
|
| 31 |
|
| 32 |
asr_model_malayalam = pipeline("automatic-speech-recognition", model="cdactvm/w2v-bert-malayalam")
|
| 33 |
|
| 34 |
-
def createlex(filename):
|
| 35 |
-
#filename = "num_map.txt"
|
| 36 |
|
| 37 |
-
# Initialize an empty dictionary
|
| 38 |
-
|
| 39 |
|
| 40 |
-
# Open the file and read it line by line
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
tellex=createlex("num_words_tel.txt")
|
| 50 |
-
kanlex=createlex("num_words_kn.txt")
|
| 51 |
-
def addnum(inlist):
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
from rapidfuzz import process
|
| 59 |
-
def get_val(word, lexicon):
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
def convert2numtel(input, lex):
|
| 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 |
-
def convert2numkn(input, lex):
|
| 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 |
# Function to apply a high-pass filter
|
| 209 |
def high_pass_filter(audio, sr, cutoff=300):
|
|
@@ -240,16 +240,16 @@ def recognize_speech_malayalam_model1(audio_file):
|
|
| 240 |
return text_value +" -----------------> " + final_text
|
| 241 |
|
| 242 |
## Function to handle speech recognition
|
| 243 |
-
def recognize_speech_malayalam2(audio_file):
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
|
| 254 |
def sel_lng(lng, mic=None, file=None):
|
| 255 |
if mic is not None:
|
|
@@ -261,8 +261,8 @@ def sel_lng(lng, mic=None, file=None):
|
|
| 261 |
|
| 262 |
if lng == "malayalam_model1":
|
| 263 |
return recognize_speech_malayalam_model1(audio)
|
| 264 |
-
elif lng == "malayalam_model2":
|
| 265 |
-
|
| 266 |
|
| 267 |
|
| 268 |
demo=gr.Interface(
|
|
@@ -270,7 +270,7 @@ demo=gr.Interface(
|
|
| 270 |
|
| 271 |
inputs=[
|
| 272 |
gr.Dropdown([
|
| 273 |
-
"malayalam_model1"
|
| 274 |
gr.Audio(sources=["microphone","upload"], type="filepath"),
|
| 275 |
],
|
| 276 |
outputs=[
|
|
|
|
| 31 |
|
| 32 |
asr_model_malayalam = pipeline("automatic-speech-recognition", model="cdactvm/w2v-bert-malayalam")
|
| 33 |
|
| 34 |
+
# def createlex(filename):
|
| 35 |
+
# #filename = "num_map.txt"
|
| 36 |
|
| 37 |
+
# # Initialize an empty dictionary
|
| 38 |
+
# data_dict = {}
|
| 39 |
|
| 40 |
+
# # Open the file and read it line by line
|
| 41 |
+
# with open(filename, "r", encoding="utf-8") as f:
|
| 42 |
+
# for line in f:
|
| 43 |
+
# # Strip newline characters and split by tab
|
| 44 |
+
# key, value = line.strip().split("\t")
|
| 45 |
+
# # Add to dictionary
|
| 46 |
+
# data_dict[key] = value
|
| 47 |
+
# return data_dict
|
| 48 |
|
| 49 |
+
# tellex=createlex("num_words_tel.txt")
|
| 50 |
+
# kanlex=createlex("num_words_kn.txt")
|
| 51 |
+
# def addnum(inlist):
|
| 52 |
+
# sum=0
|
| 53 |
+
# for num in inlist:
|
| 54 |
+
# sum+=int(num)
|
| 55 |
|
| 56 |
+
# return sum
|
| 57 |
|
| 58 |
+
# from rapidfuzz import process
|
| 59 |
+
# def get_val(word, lexicon):
|
| 60 |
+
# threshold = 80 # Minimum similarity score
|
| 61 |
+
# length_difference = 4
|
| 62 |
+
# #length_range = (4, 6) # Acceptable character length range (min, max)
|
| 63 |
|
| 64 |
+
# # Find the best match above the similarity threshold
|
| 65 |
+
# result = process.extractOne(word, lexicon.keys(), score_cutoff=threshold)
|
| 66 |
+
# print (result)
|
| 67 |
+
# if result:
|
| 68 |
+
# match, score, _ = result
|
| 69 |
+
# #print(lexicon[match])
|
| 70 |
+
# #return lexicon[match]
|
| 71 |
+
# if abs(len(match) - len(word)) <= length_difference:
|
| 72 |
+
# #if length_range[0] <= len(match) <= length_range[1]:
|
| 73 |
+
# return lexicon[match]
|
| 74 |
+
# else:
|
| 75 |
+
# return None
|
| 76 |
+
# else:
|
| 77 |
+
# return None
|
| 78 |
+
# def convert2numtel(input, lex):
|
| 79 |
+
# input += " #" # Add a period for termination
|
| 80 |
+
# words = input.split()
|
| 81 |
+
# i = 0
|
| 82 |
+
# num = 0
|
| 83 |
+
# outstr = ""
|
| 84 |
+
# digit_end = True
|
| 85 |
+
# numlist = []
|
| 86 |
+
# addflag = False
|
| 87 |
+
# prevword=""
|
| 88 |
+
# single_list=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,17,18,19]
|
| 89 |
+
# # Process the words
|
| 90 |
+
# while i < len(words):
|
| 91 |
+
# #checkwordlist = handleSpecialnum(words[i])
|
| 92 |
|
| 93 |
+
# # Handle special numbers
|
| 94 |
+
# #if len(checkwordlist) == 2:
|
| 95 |
+
# # words[i] = checkwordlist[0]
|
| 96 |
+
# # words.insert(i + 1, checkwordlist[1]) # Collect new word for later processing
|
| 97 |
|
| 98 |
+
# # Get numerical value of the word
|
| 99 |
+
# numval = get_val(words[i], lex)
|
| 100 |
+
# if numval is not None:
|
| 101 |
+
# if prevword not in single_list:
|
| 102 |
+
# addflag = True
|
| 103 |
+
# numlist.append(numval)
|
| 104 |
+
# else:
|
| 105 |
+
# if addflag:
|
| 106 |
+
# numlist.append(numval)
|
| 107 |
+
# num = addnum(numlist)
|
| 108 |
+
# outstr += str(num) + " "
|
| 109 |
+
# addflag = False
|
| 110 |
+
# numlist = []
|
| 111 |
+
# else:
|
| 112 |
+
# outstr += " " + str(numval) + " "
|
| 113 |
+
# digit_end = False
|
| 114 |
+
# prevword=numval
|
| 115 |
+
# else:
|
| 116 |
+
# prevword=""
|
| 117 |
+
# if addflag:
|
| 118 |
+
# num = addnum(numlist)
|
| 119 |
+
# outstr += str(num) + " " + words[i] + " "
|
| 120 |
+
# addflag = False
|
| 121 |
+
# numlist = []
|
| 122 |
+
# else:
|
| 123 |
+
# outstr += words[i] + " "
|
| 124 |
+
# if not digit_end:
|
| 125 |
+
# digit_end = True
|
| 126 |
|
| 127 |
+
# # Move to the next word
|
| 128 |
+
# i += 1
|
| 129 |
|
| 130 |
+
# # Final processing
|
| 131 |
+
# outstr = outstr.replace('#','') # Remove trailing spaces
|
| 132 |
+
# return outstr
|
| 133 |
|
| 134 |
+
# def convert2numkn(input, lex):
|
| 135 |
+
# input += " ######" # Add a period for termination
|
| 136 |
+
# words = input.split()
|
| 137 |
+
# i = 0
|
| 138 |
+
# num = 0
|
| 139 |
+
# outstr = ""
|
| 140 |
+
# digit_end = True
|
| 141 |
+
# numlist = []
|
| 142 |
+
# addflag = False
|
| 143 |
|
| 144 |
+
# prevword = []
|
| 145 |
|
| 146 |
+
# # Process the words
|
| 147 |
+
# while i < len(words):
|
| 148 |
|
| 149 |
+
# # Get numerical value of the word
|
| 150 |
+
# numval = get_val(words[i], lex)
|
| 151 |
+
# if len(prevword)>=3:
|
| 152 |
+
# prevword.pop(0)
|
| 153 |
+
# prevword.append(words[i])
|
| 154 |
+
# else:
|
| 155 |
+
# prevword.append(words[i])
|
| 156 |
+
# if numval is not None:
|
| 157 |
|
| 158 |
+
# addflag = True
|
| 159 |
|
| 160 |
+
# numlist.append(numval)
|
| 161 |
|
| 162 |
+
# else:
|
| 163 |
|
| 164 |
|
| 165 |
+
# #print("word--->"+words[i])
|
| 166 |
+
# #print("addflagword--->"+str(addflag))
|
| 167 |
+
# prevwords=" ".join(prevword)
|
| 168 |
+
# #print("prev word--->"+prevwords)
|
| 169 |
+
# numval=get_val(prevwords,lex)
|
| 170 |
+
# if numval is not None:
|
| 171 |
+
# #addflag=True
|
| 172 |
+
# #print("numval " +numval)
|
| 173 |
+
# numlist=[]
|
| 174 |
+
# #print("First outstr--->"+outstr)
|
| 175 |
|
| 176 |
|
| 177 |
+
# outwords = outstr.split()
|
| 178 |
+
# outstr=" ".join(outwords[:-1])
|
| 179 |
+
# #print("outstr--->"+outstr)
|
| 180 |
|
| 181 |
+
# outstr += " " + str(numval) + " "
|
| 182 |
+
# #print(" aoutstr--->"+outstr)
|
| 183 |
+
# numval=0
|
| 184 |
+
# addflag=False
|
| 185 |
|
| 186 |
+
# else:
|
| 187 |
+
# if addflag:
|
| 188 |
+
# num = addnum(numlist)
|
| 189 |
+
# outstr += str(num) + " " + words[i] + " "
|
| 190 |
+
# #print("penlast outstr--->"+outstr)
|
| 191 |
+
# addflag = False
|
| 192 |
+
# numlist = []
|
| 193 |
+
# else:
|
| 194 |
+
# outstr += words[i] + " "
|
| 195 |
+
# #print("last outstr--->"+outstr)
|
| 196 |
+
# if not digit_end:
|
| 197 |
+
# digit_end = True
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
+
# # Move to the next word
|
| 202 |
+
# i += 1
|
| 203 |
|
| 204 |
+
# # Final processing
|
| 205 |
+
# outstr = outstr.replace('#','') # Remove trailing spaces
|
| 206 |
+
# return outstr
|
| 207 |
|
| 208 |
# Function to apply a high-pass filter
|
| 209 |
def high_pass_filter(audio, sr, cutoff=300):
|
|
|
|
| 240 |
return text_value +" -----------------> " + final_text
|
| 241 |
|
| 242 |
## Function to handle speech recognition
|
| 243 |
+
# def recognize_speech_malayalam2(audio_file):
|
| 244 |
+
# audio, sr = librosa.load(audio_file, sr=16000)
|
| 245 |
+
# audio = high_pass_filter(audio, sr)
|
| 246 |
+
# audio = apply_wiener_filter(audio)
|
| 247 |
+
# denoised_audio = wavelet_denoise(audio)
|
| 248 |
+
# result = asr_model_malayalam(denoised_audio)
|
| 249 |
+
# text_value = result['text']
|
| 250 |
+
# cleaned_text = text_value.replace("[UNK]", "")
|
| 251 |
+
# converted_text=convert2numkn(cleaned_text,kanlex)
|
| 252 |
+
# return cleaned_text +" -----------------> " + converted_text
|
| 253 |
|
| 254 |
def sel_lng(lng, mic=None, file=None):
|
| 255 |
if mic is not None:
|
|
|
|
| 261 |
|
| 262 |
if lng == "malayalam_model1":
|
| 263 |
return recognize_speech_malayalam_model1(audio)
|
| 264 |
+
# elif lng == "malayalam_model2":
|
| 265 |
+
# return recognize_speech_malayalam_model2(audio)
|
| 266 |
|
| 267 |
|
| 268 |
demo=gr.Interface(
|
|
|
|
| 270 |
|
| 271 |
inputs=[
|
| 272 |
gr.Dropdown([
|
| 273 |
+
"malayalam_model1"],label="Select Model"),
|
| 274 |
gr.Audio(sources=["microphone","upload"], type="filepath"),
|
| 275 |
],
|
| 276 |
outputs=[
|