-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.py
70 lines (54 loc) · 1.54 KB
/
tasks.py
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
import os
from celery import Celery
from vosk import Model, KaldiRecognizer, SetLogLevel
import wave
try:
REDIS_BASE_URL = os.environ['REDIS_BASE_URL']
except KeyError:
REDIS_BASE_URL = 'redis://localhost:6379'
celery = Celery(
'asr-tasks',
broker=f"{REDIS_BASE_URL}/0",
backend=f"{REDIS_BASE_URL}/1",
task_time_limit=30 * 60,
task_soft_time_limit=30 * 60
)
class Config:
enable_utc = True
timezone = 'Europe/London'
result_expires = 60 * 60 * 12 # 12 hours
result_extended = True
celery.config_from_object(Config)
# set vosk log level
SetLogLevel(0)
# make sure to only load Model once
model = Model("model") if __name__ == '__main__' else None
@celery.task(name='asr', default_retry_delay=300, max_retry=5)
def recognize(data):
try:
file = wave.open(data['filename'], "rb")
if (file.getnchannels() != 1 or
file.getsampwidth() != 2 or
file.getcomptype() != "NONE"
):
return "Invalid wav file", 400
except:
return "Invalid audio file", 400
rec = KaldiRecognizer(model, file.getframerate())
rec.SetWords(True)
rec.SetMaxAlternatives(0)
while True:
data = file.readframes(4000)
if len(data) == 0:
break
rec.AcceptWaveform(data)
return rec.FinalResult()
if __name__ == '__main__':
celery.start(argv=[
'worker',
# '--without-mingle',
# '--without-gossip',
# '--without-heartbeat'
# '--loglevel=DEBUG',
'--concurrency=2'
])