Spaces:
Running
Running
import wget | |
import random | |
import string | |
from .. import helpers | |
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': extendInfo = request.form['extendInfo'] | |
else: extendInfo = request.args['extendInfo'] | |
if extendInfo in ['true','1',1,'yes','y']: extendInfo = True | |
else: extendInfo = False | |
except: extendInfo = False | |
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 == ".wav": fileHeader = {'Content-Type': 'audio/wav'} | |
elif fileExt == ".mp3": fileHeader = {'Content-Type': 'audio/mpeg3'} | |
elif fileExt == ".ogg": fileHeader = {'Content-Type': 'audio/ogg'} | |
else: return {"status": "error", "details": { "error_code": 111, "error_details": "Wrong file format (only ogg, wav, mp3)" }} | |
wget.download(url, f"/home/ubuntu/api/static/temp/{fileId}{fileExt}") | |
noEx = True | |
with open(f"/home/ubuntu/api/static/temp/{fileId}{fileExt}", 'rb') as f: | |
try: resp = helpers.dictation("ANR3UUMGUHZJFF366EY3YM3FS54QDJPF", f, {'Content-Type': 'audio/mpeg3'}) | |
except Exception as e: | |
noEx = False | |
resp = e | |
helpers.deleteAudio(f"temp/{fileId}{fileExt}") | |
try: | |
if resp['text'] == "": return {"status": "ok", "result": "", "details": ""} | |
except: pass | |
if noEx: | |
if extendInfo: return {"status": "ok", "result": helpers.delivering(resp), "details": str(helpers.devRaw(resp))} | |
else: return {"status": "ok", "result": helpers.delivering(resp)} | |
else: return {"status": "error", "details": { "error_code": 123, "error_details": resp }} |