import wget import random import string import ffmpeg from .. import helpers import speech_recognition as sr def recognizeVoice(request): try: if request.method == 'POST': url = request.form['url'] else: url = request.args['url'] if url.strip() in ['', None]: raise Exception() except: return {"status": "error", "details": { "error_code": 101, "error_details": "No link provided" }} try: if request.method == 'POST': signature = request.form['signature'] else: signature = request.args['signature'] except: return {"status": "error", "details": { "error_code": 103, "error_details": "No signature" }} if not helpers.checkSignature(signature): return {"status": "error", "details": { "error_code": 105, "error_details": "Invalid signature" }} try: if request.method == 'POST': lang = request.form['lang'] else: lang = request.args['lang'] if lang.lower() in ['en','en-us']: lang = 'en-US' elif lang.lower() in ['ru','ru-ru']: lang = 'ru-RU' except: lang = "en-US" fileId = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16)) fileExt = url[url.rfind('.'):url.rfind('.')+4] if fileExt in [".wav",".mp3",".ogg",'.aac']: pass else: return {"status": "error", "details": { "error_code": 111, "error_details": "Wrong file format (only ogg, wav, mp3)" }} r = sr.Recognizer() config = helpers.configFile() wget.download(url, f"{config['temp-path']}/{fileId}{fileExt}") if fileExt != ".wav": audio_input = ffmpeg.input(f"{config['temp-path']}/{fileId}{fileExt}") oldFE = fileExt fileExt = ".wav" audio_output = ffmpeg.output(audio_input.audio, f"{config['temp-path']}/{fileId}{fileExt}") ffmpeg.run(audio_output) helpers.deleteAudio(f"temp/{fileId}.{oldFE}") rawSource = sr.AudioFile(f"{config['temp-path']}/{fileId}{fileExt}") with rawSource as source: r.adjust_for_ambient_noise(source) audio = r.record(source) try: googleText = r.recognize_google(audio, language=lang) except: googleText = "" # at now here's no keys :( #try: houndifyText = r.recognize_houndify(audio) #except: houndifyText = "" helpers.deleteAudio(f"temp/{fileId}{fileExt}") return {"status": "pass", "result": {"google": googleText, "houndify": "NOT-WORKING"}}