-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·52 lines (40 loc) · 1.44 KB
/
app.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
#!/usr/bin/env python3
import json
import os
from io import BytesIO
from flask import Flask, request, json as json_flask
from vosk import Model, KaldiRecognizer
MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'model')
if not os.path.exists(MODEL_PATH):
print('Model folder not found ({}), bye.'.format(MODEL_PATH))
exit(1)
kaldi_model = Model(MODEL_PATH)
def stt(fp, buffer_size=8192) -> str:
kaldi = KaldiRecognizer(kaldi_model, 16000)
im_ok = False
while chunk := fp.read(buffer_size):
kaldi.AcceptWaveform(chunk)
im_ok = True
return json.loads(kaldi.FinalResult())['text'] if im_ok else ''
app = Flask(__name__, static_url_path='')
@app.route('/stt', methods=['GET', 'POST'])
def say():
if request.method == 'POST':
target = None
if request.headers.get('Transfer-Encoding') == 'chunked':
target = request.stream
elif request.data:
target = BytesIO(request.data)
if target is None:
code, text = 1, 'No data'
else:
try:
code, text = 0, stt(target)
except Exception as e:
code, text = 3, 'Internal error'
print('{}: {}'.format(text, e))
else:
code, text = 2, 'What do you want? I accept only POST!'
return json_flask.jsonify(text=text, code=code)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8086, threaded=False)