Spaces:
Runtime error
Runtime error
File size: 8,542 Bytes
056c529 01ff6b3 056c529 62f49fe 4a684d4 b98b502 4a684d4 b98b502 9050bc9 4a684d4 eb5fb7a 5d8c2f0 a18ccbb eb5fb7a a18ccbb 2661fcb 4a684d4 2661fcb eb5fb7a 4a684d4 62f49fe fd4e0e8 056c529 01ff6b3 2f716a3 01ff6b3 fd4e0e8 01ff6b3 fd4e0e8 2f716a3 19a72ca 01ff6b3 19a72ca 01ff6b3 fd4e0e8 4a684d4 01ff6b3 c2e2361 056c529 c2e2361 01ff6b3 056c529 01ff6b3 d3e1b2e 01ff6b3 056c529 01ff6b3 056c529 c69ccab 056c529 01ff6b3 62f49fe 01ff6b3 056c529 71dc112 |
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 |
import gradio as gr
from transformers import pipeline
from pydub import AudioSegment
import os
import speech_recognition as sr
html_seeker='''<style>
html, body {
margin: 0;
padding: 0;
min-width: 900px;
}
#header {
/*position: fixed;*/
top: 0;
left: 0;
height: 50px;
min-width: 900px;
line-height: 50px;
width: 100%;
background-color: #999;
box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.5);
font-family: Helvetica, sans-serif;
}
#header, #header a {
color: white;
}
.home {
margin: 0;
font-weight: bold;
text-transform: lowercase;
width: 100px;
}
h4.home {
margin: 0;
background: #666;
padding-left: 25px;
padding-right: 30px;
margin-right: 20px;
float: left;
text-decoration: none;
}
.home:hover a {
background: #555;
}
#audio {
margin-left: 10px;
width: 500px;
display: inline-block;
}
#transcript {
margin: 0 15px;
margin-bottom: 5em;
white-space: pre-wrap;
line-height: 2em;
max-width: 600px;
color: #999;
clear: both;
margin-top: 75px;
/*direction: rtl;*/
}
.success {
color: black;
}
.success:hover {
text-decoration: underline;
}
.active {
color: magenta;
background-color: yellow;
}
#preloader {
visibility: hidden;
}
</style><div id="header">
<audio id="audio" src="17.mp3" controls="true" ></audio>
</div>
</div>
<div id="transcript" dir="auto"></div>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onload="
var oldScript = document.querySelector('script#huihiuh6');
var newScript = document.createElement('script');
Array.from(oldScript.attributes)
.forEach( attr => newScript.setAttribute(attr.name, attr.value) );
newScript.appendChild(document.createTextNode(oldScript.innerHTML));
oldScript.parentNode.replaceChild(newScript, oldScript);
">
<script id="huihiuh6">
function myFunction543rr(){
console.log('loaded00000000000000002');
}
var $a = document.getElementById("audio");
$a.src=document.querySelector('audio').src;
console.log($a);
window.onkeydown = function(ev) {
if(ev.keyCode == 32) {
ev.preventDefault();
$a.pause();
}
}
var $trans = document.getElementById("transcript");
var wds = [];
var cur_wd;
function highlight_word() {
var t = $a.currentTime;
// XXX: O(N); use binary search
var hits = wds.filter(function(x) {
return (t - x['timestamp']['0']) > 0.01 && (x['timestamp']['1'] - t) > 0.01;
}, wds);
var next_wd = hits[hits.length - 1];
if(cur_wd != next_wd) {
var active = document.querySelectorAll('.active');
for(var i = 0; i < active.length; i++) {
active[i].classList.remove('active');
}
if(next_wd && next_wd.$div) {
next_wd.$div.classList.add('active');
//render_phones(next_wd);
}
}
cur_wd = next_wd;
//highlight_phone(t);
window.requestAnimationFrame(highlight_word);
}
window.requestAnimationFrame(highlight_word);
$trans.innerHTML = "Loading...";
function render(ret) {
wds = ret['chunks'] || [];
transcript = ret['text'];
$trans.innerHTML = '';
var currentOffset = 0;
wds.forEach(function(wd) {
var $wd = document.createElement('span');
var txt = wd['text'];
var $wdText = document.createTextNode(txt);
$wd.appendChild($wdText);
wd.$div = $wd;
$wd.className = 'success';
$wd.onclick = function() {
console.log(wd['timestamp']['0']);
$a.currentTime = wd['timestamp']['0'];
$a.play();
};
$trans.appendChild($wd);
$trans.appendChild(document.createTextNode(' '));
});
}
function update() {
if(INLINE_JSON) {
// We want this to work from file:/// domains, so we provide a
// mechanism for inlining the alignment data.
render(INLINE_JSON);
}
}
var INLINE_JSON='''
html_seeker2=''';
update();
</script>'''
model_name = "voidful/wav2vec2-xlsr-multilingual-56"
model0 = pipeline(task="automatic-speech-recognition",
model=model_name)
model_name = "SLPL/Sharif-wav2vec2"
model2 = pipeline(task="automatic-speech-recognition",
model=model_name)
model_name = "ghofrani/common8"
model1 = pipeline(task="automatic-speech-recognition",
model=model_name)
import json
def predict_fa(speech,model):
if model== "SLPL/Sharif-wav2vec2":
text = model2(speech,return_timestamps="word" )
elif model== "ghofrani/common8":
text = model1(speech,return_timestamps="word" )
elif model== "voidful/wav2vec2-xlsr-multilingual-56":
text = model0(speech,return_timestamps="word" )
return [text['text'],json.dumps(text),html_seeker+json.dumps(text)+html_seeker2]
def convert_to_wav(filename):
filenameObj=os.path.splitext(filename)
audio = AudioSegment.from_file(filename,format=filenameObj[1].replace(".",""))
new_filename = filenameObj[0] + ".wav"
while os.path.exists(new_filename):
new_filename = os.path.splitext(new_filename)[0]+"(1)"+ ".wav"
audio.export(new_filename, format="wav")
print(f"Converting {filename} to {new_filename}...")
return new_filename
def g_rec(audio_File ,language):
r = sr.Recognizer()
print(audio_File)
#if not os.path.splitext(audio_File)[1]==".wav":
# audio_File=convert_to_wav(audio_File)
hellow=sr.AudioFile(audio_File)
with hellow as source:
audio = r.record(source)
try:
s = r.recognize_google(audio,language =language)
res= "Text: "+s
except Exception as e:
res= "Exception: "+str(e)
return res
# Export file as .wav
#predict(load_file_to_data('audio file path',sampling_rate=16_000)) # beware of the audio file sampling rate
#predict_lang_specific(load_file_to_data('audio file path',sampling_rate=16_000),'en') # beware of the audio file sampling rate
with gr.Blocks() as demo:
gr.Markdown("multilingual Speech Recognition")
with gr.Tab("Persian models"):
inputs_speech_fa =gr.Audio(source="upload", type="filepath", optional=True,label="Upload your audio:")
inputs_model_fa =gr.inputs.Radio(label="Language", choices=["ghofrani/common8","SLPL/Sharif-wav2vec2","voidful/wav2vec2-xlsr-multilingual-56"])
output_transcribe1_fa = gr.Textbox(label="Transcribed text:")
output_transcribe1_fa1 = gr.Textbox(label="Transcribed text with timestamps:")
output_transcribe1_fa2 =gr.HTML(label="")
transcribe_audio1_fa= gr.Button("Submit")
with gr.Tab("google"):
gr.Markdown("set your speech language")
inputs_speech1 =[
gr.Audio(source="upload", type="filepath"),
gr.Dropdown(choices=["af-ZA","am-ET","ar-AE","ar-BH","ar-DZ","ar-EG","ar-IL","ar-IQ","ar-JO","ar-KW","ar-LB","ar-MA","ar-MR","ar-OM","ar-PS","ar-QA","ar-SA","ar-TN","ar-YE","az-AZ","bg-BG","bn-BD","bn-IN","bs-BA","ca-ES","cs-CZ","da-DK","de-AT","de-CH","de-DE","el-GR","en-AU","en-CA","en-GB","en-GH","en-HK","en-IE","en-IN","en-KE","en-NG","en-NZ","en-PH","en-PK","en-SG","en-TZ","en-US","en-ZA","es-AR","es-BO","es-CL","es-CO","es-CR","es-DO","es-EC","es-ES","es-GT","es-HN","es-MX","es-NI","es-PA","es-PE","es-PR","es-PY","es-SV","es-US","es-UY","es-VE","et-EE","eu-ES","fa-IR","fi-FI","fil-PH","fr-BE","fr-CA","fr-CH","fr-FR","gl-ES","gu-IN","hi-IN","hr-HR","hu-HU","hy-AM","id-ID","is-IS","it-CH","it-IT","iw-IL","ja-JP","jv-ID","ka-GE","kk-KZ","km-KH","kn-IN","ko-KR","lo-LA","lt-LT","lv-LV","mk-MK","ml-IN","mn-MN","mr-IN","ms-MY","my-MM","ne-NP","nl-BE","nl-NL","no-NO","pa-Guru-IN","pl-PL","pt-BR","pt-PT","ro-RO","ru-RU","si-LK","sk-SK","sl-SI","sq-AL","sr-RS","su-ID","sv-SE","sw-KE","sw-TZ","ta-IN","ta-LK","ta-MY","ta-SG","te-IN","th-TH","tr-TR","uk-UA","ur-IN","ur-PK","uz-UZ","vi-VN","yue-Hant-HK","zh (cmn-Hans-CN)","zh-TW (cmn-Hant-TW)","zu-ZA"]
,value="fa-IR",label="language code")
]
output_transcribe1 = gr.Textbox(label="output")
transcribe_audio1_go= gr.Button("Submit")
transcribe_audio1_fa.click(fn=predict_fa,
inputs=[inputs_speech_fa ,inputs_model_fa ],
outputs=[output_transcribe1_fa ,output_transcribe1_fa1,output_transcribe1_fa2 ] )
transcribe_audio1_go.click(fn=g_rec,
inputs=inputs_speech1 ,
outputs=output_transcribe1 )
if __name__ == "__main__":
demo.launch()
|