Spaces:
Sleeping
Sleeping
File size: 13,450 Bytes
5baf1ba 4da13bc a13d2fb 4da13bc 5baf1ba eaa3add 6dd1216 5baf1ba 6dd1216 5baf1ba eaa3add 5baf1ba cb9e139 5baf1ba cb9e139 eaa3add cb9e139 eaa3add cb9e139 5baf1ba b54b57f 5baf1ba eaa3add 9fc1cf9 cb9e139 9fc1cf9 cb9e139 9fc1cf9 71ca02a cb9e139 9fc1cf9 9eb3cf7 089cd84 9fc1cf9 089cd84 9fc1cf9 089cd84 9eb3cf7 089cd84 cb9e139 089cd84 cb9e139 089cd84 cb9e139 089cd84 cc2ec1d 9fc1cf9 cb9e139 9fc1cf9 cb9e139 6dd1216 cb9e139 f1f9820 cb9e139 9fc1cf9 cb9e139 9fc1cf9 5baf1ba 6dd1216 5baf1ba 6dd1216 eaa3add b4d2c26 6dd1216 5baf1ba ea5222d 5baf1ba |
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 |
import gradio as gr
from transformers import pipeline
import os
import re
os.system('git clone https://github.com/irshadbhat/indic-trans.git')
os.system('pip install ./indic-trans/.')
p1= pipeline(task="automatic-speech-recognition", model="cdactvm/w2v-bert-2.0-odia_v1")
p2=pipeline(task="automatic-speech-recognition", model="cdactvm/w2v-bert-2.0-hindi_v1")
def transcribe_odiya(speech):
#print (p1(speech))
text = p1(speech)["text"]
#text=cleanhtml(text)
return text
def transcribe_hindi(speech):
#print (p1(speech))
text = p2(speech)["text"]
#text=cleanhtml(text)
return text
def transcribe_odiya_eng(speech):
from indictrans import Transliterator
trn = Transliterator(source='ori', target='eng', build_lookup=True)
text = p1(speech)["text"]
#text=trn.transform(text)
#text = master_function(text)
sentence = trn.transform(text)
replaced_words = replace_words(sentence)
processed_sentence = process_doubles(replaced_words)
input_sentence_1 = processed_sentence
# Create empty mappings
word_to_code_map = {}
code_to_word_map = {}
# Convert sentence to transcript
transcript_1 = sentence_to_transcript(input_sentence_1, word_to_code_map)
# Convert transcript to numerical representation
numbers = text2int(transcript_1)
# Create reverse mapping
code_to_word_map = {v: k for k, v in word_to_code_map.items()}
# Convert transcript back to sentence
text = transcript_to_sentence(numbers, code_to_word_map)
return text
def transcribe_hin_eng(speech):
from indictrans import Transliterator
trn = Transliterator(source='hin', target='eng', build_lookup=True)
text = p2(speech)["text"]
#text=trn.transform(text)
#text = master_function(text)
sentence = trn.transform(text)
replaced_words = replace_words(sentence)
processed_sentence = process_doubles(replaced_words)
input_sentence_1 = processed_sentence
# Create empty mappings
word_to_code_map = {}
code_to_word_map = {}
# Convert sentence to transcript
transcript_1 = sentence_to_transcript(input_sentence_1, word_to_code_map)
# Convert transcript to numerical representation
numbers = text2int(transcript_1)
# Create reverse mapping
code_to_word_map = {v: k for k, v in word_to_code_map.items()}
# Convert transcript back to sentence
text = transcript_to_sentence(numbers, code_to_word_map)
return 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=="Odiya"):
return transcribe_odiya(audio)
elif (lng=="Odiya-trans"):
return transcribe_odiya_eng(audio)
elif (lng=="Hindi-trans"):
return transcribe_hin_eng(audio)
elif (lng=="Hindi"):
return transcribe_hindi(audio)
#####################################################
def replace_words(sentence):
replacements = [
(r'\bjiro\b', 'zero'), (r'\bjero\b', 'zero'), (r'\bnn\b', 'one'),
(r'\bn\b', 'one'), (r'\bna\b', 'one'), (r'\btu\b', 'two'),
(r'\btoo\b', 'two'), (r'\bthiri\b', 'three'), (r'\bfor\b', 'four'),
(r'\bfore\b', 'four'), (r'\bfib\b', 'five'), (r'\bdublseven\b', 'double seven'),
(r'\bdubalathri\b', 'double three'), (r'\bnineeit\b', 'nine eight'),
(r'\bfipeit\b', 'five eight'), (r'\bdubal\b', 'double'), (r'\bsevenatu\b', 'seven two'),
]
for pattern, replacement in replacements:
sentence = re.sub(pattern, replacement, sentence)
return sentence
# Function to process "double" followed by a number
def process_doubles(sentence):
tokens = sentence.split()
result = []
i = 0
while i < len(tokens):
if tokens[i] in ("double", "dubal"):
if i + 1 < len(tokens):
result.append(tokens[i + 1])
result.append(tokens[i + 1])
i += 2
else:
result.append(tokens[i])
i += 1
else:
result.append(tokens[i])
i += 1
return ' '.join(result)
# Function to generate Soundex code for a word
def soundex(word):
word = word.upper()
word = ''.join(filter(str.isalpha, word))
if not word:
return None
soundex_mapping = {
'B': '1', 'F': '1', 'P': '1', 'V': '1',
'C': '2', 'G': '2', 'J': '2', 'K': '2', 'Q': '2', 'S': '2', 'X': '2', 'Z': '2',
'D': '3', 'T': '3', 'L': '4', 'M': '5', 'N': '5', 'R': '6'
}
soundex_code = word[0]
for char in word[1:]:
if char not in ('H', 'W'):
soundex_code += soundex_mapping.get(char, '0')
soundex_code = soundex_code[0] + ''.join(c for i, c in enumerate(soundex_code[1:]) if c != soundex_code[i])
soundex_code = soundex_code.replace('0', '') + '000'
return soundex_code[:4]
def is_number(x):
if type(x) == str:
x = x.replace(',', '')
try:
float(x)
except:
return False
return True
# Function to convert text to numerical representation
def text2int(textnum, numwords={}):
units = ['Z600', 'O500','T000','T600','F600','F100','S220','S150','E300','N500',
'T500', 'E415', 'T410', 'T635', 'F635', 'F135', 'S235', 'S153', 'E235','N535']
tens = ['', '', 'T537', 'T637', 'F637', 'F137', 'S230', 'S153', 'E230', 'N530']
scales = ['H536', 'T253', 'M450', 'C600']
ordinal_words = {'oh': 'Z600', 'first': 'O500', 'second': 'T000', 'third': 'T600', 'fourth': 'F600', 'fifth': 'F100',
'sixth': 'S200','seventh': 'S150','eighth': 'E230', 'ninth': 'N500', 'twelfth': 'T410'}
ordinal_endings = [('ieth', 'y'), ('th', '')]
if not numwords:
numwords['and'] = (1, 0)
for idx, word in enumerate(units): numwords[word] = (1, idx)
for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
textnum = textnum.replace('-', ' ')
current = result = 0
curstring = ''
onnumber = False
lastunit = False
lastscale = False
def is_numword(x):
if is_number(x):
return True
if word in numwords:
return True
return False
def from_numword(x):
if is_number(x):
scale = 0
increment = int(x.replace(',', ''))
return scale, increment
return numwords[x]
for word in textnum.split():
if word in ordinal_words:
scale, increment = (1, ordinal_words[word])
current = current * scale + increment
if scale > 100:
result += current
current = 0
onnumber = True
lastunit = False
lastscale = False
else:
for ending, replacement in ordinal_endings:
if word.endswith(ending):
word = "%s%s" % (word[:-len(ending)], replacement)
if (not is_numword(word)) or (word == 'and' and not lastscale):
if onnumber:
# Flush the current number we are building
curstring += repr(result + current) + " "
curstring += word + " "
result = current = 0
onnumber = False
lastunit = False
lastscale = False
else:
scale, increment = from_numword(word)
onnumber = True
if lastunit and (word not in scales):
# Assume this is part of a string of individual numbers to
# be flushed, such as a zipcode "one two three four five"
curstring += repr(result + current)
result = current = 0
if scale > 1:
current = max(1, current)
current = current * scale + increment
if scale > 100:
result += current
current = 0
lastscale = False
lastunit = False
if word in scales:
lastscale = True
elif word in units:
lastunit = True
if onnumber:
curstring += repr(result + current)
return curstring
# Convert sentence to transcript using Soundex
def sentence_to_transcript(sentence, word_to_code_map):
words = sentence.split()
transcript_codes = []
for word in words:
if word not in word_to_code_map:
word_to_code_map[word] = soundex(word)
transcript_codes.append(word_to_code_map[word])
transcript = ' '.join(transcript_codes)
return transcript
# Convert transcript back to sentence using mapping
def transcript_to_sentence(transcript, code_to_word_map):
codes = transcript.split()
sentence_words = []
for code in codes:
sentence_words.append(code_to_word_map.get(code, code))
sentence = ' '.join(sentence_words)
return sentence
######################################################
demo=gr.Interface(
fn=sel_lng,
inputs=[
gr.Dropdown(["Hindi","Hindi-trans","Odiya","Odiya-trans"],value="Hindi",label="Select Language"),
gr.Audio(sources=["microphone","upload"], type="filepath"),
#gr.Audio(sources="upload", type="filepath"),
#"state"
],
outputs=[
"textbox"
# #"state"
],
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()
|