File size: 2,477 Bytes
66ca64a
 
 
f4c0d36
66ca64a
bbb9123
66ca64a
 
 
 
 
 
 
 
 
 
 
3a32016
11ba6b5
 
 
 
 
 
66ca64a
 
 
bbb9123
66ca64a
e493298
bbb9123
 
e493298
 
f4c0d36
bbb9123
f4c0d36
bbb9123
f4c0d36
 
 
bbb9123
11ba6b5
bbb9123
 
 
 
 
11ba6b5
bbb9123
 
 
 
 
66ca64a
bbb9123
 
 
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
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"}}