-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
67 lines (53 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, jsonify, request
from tasks import recognize
from os import remove
import json
import tempfile
api = Flask(__name__)
@api.route('/asr/recognize', methods=['POST'])
def transcribe():
if request.content_type != 'audio/x-wav':
return jsonify({
'msg': "415 Unsupported Media Type ;)"
}), 415
data = None
if request.headers.get('Transfer-Encoding') == 'chunked':
data = request.stream
elif request.data:
data = request.data
if data is None:
return jsonify({
'msg': "400 No data found ;)"
}), 400
fp = tempfile.NamedTemporaryFile(delete=False)
fp.write(data)
task = recognize.apply_async(args=[{
'filename': fp.name
}])
return jsonify({
'id': task.id
})
@api.route('/asr/recognize/status/<task_id>', methods=['GET'])
def result(task_id):
task = recognize.AsyncResult(task_id)
if task.state == 'PENDING':
response = {
'state': task.state,
}
elif task.state == 'SUCCESS':
try:
remove(task.args[0]['filename'])
except FileNotFoundError:
pass
response = {
'state': task.state,
'data': json.loads(task.info)
}
else:
response = {
'state': task.state,
'status': str(task.info)
}
return jsonify(response)
if __name__ == '__main__':
api.run()