Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -31,7 +31,105 @@ from scipy.signal import butter, lfilter, wiener
|
|
31 |
|
32 |
asr_model = pipeline("automatic-speech-recognition", model="cdactvm/telugu_w2v-bert_model")
|
33 |
|
|
|
|
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
# Function to apply a high-pass filter
|
36 |
def high_pass_filter(audio, sr, cutoff=300):
|
37 |
nyquist = 0.5 * sr
|
@@ -62,12 +160,13 @@ def recognize_speech(audio_file):
|
|
62 |
result = asr_model(denoised_audio)
|
63 |
text_value = result['text']
|
64 |
cleaned_text = text_value.replace("<s>", "")
|
|
|
65 |
# cleaned_text=convert2num(cleaned_text,lex)
|
66 |
# converted_to_list = convert_to_list(cleaned_text, text_to_list())
|
67 |
# processed_doubles = process_doubles(converted_to_list)
|
68 |
# replaced_words = replace_words(processed_doubles)
|
69 |
# converted_text = text_to_int(replaced_words)
|
70 |
-
return cleaned_text
|
71 |
|
72 |
def sel_lng(lng, mic=None, file=None):
|
73 |
if mic is not None:
|
|
|
31 |
|
32 |
asr_model = pipeline("automatic-speech-recognition", model="cdactvm/telugu_w2v-bert_model")
|
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 |
+
lex=createlex("num_words_tel.txt")
|
50 |
+
|
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 convert2num(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 |
# Function to apply a high-pass filter
|
134 |
def high_pass_filter(audio, sr, cutoff=300):
|
135 |
nyquist = 0.5 * sr
|
|
|
160 |
result = asr_model(denoised_audio)
|
161 |
text_value = result['text']
|
162 |
cleaned_text = text_value.replace("<s>", "")
|
163 |
+
converted_text=convert2num(cleaned_text,lex)
|
164 |
# cleaned_text=convert2num(cleaned_text,lex)
|
165 |
# converted_to_list = convert_to_list(cleaned_text, text_to_list())
|
166 |
# processed_doubles = process_doubles(converted_to_list)
|
167 |
# replaced_words = replace_words(processed_doubles)
|
168 |
# converted_text = text_to_int(replaced_words)
|
169 |
+
return cleaned_text +" -----------------> " + converted_text
|
170 |
|
171 |
def sel_lng(lng, mic=None, file=None):
|
172 |
if mic is not None:
|