forked from hcmlab/vadnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
42 lines (35 loc) · 1.25 KB
/
app.py
File metadata and controls
42 lines (35 loc) · 1.25 KB
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
import pickle
import numpy as np
from flask import Flask, request
from vadnet.predict_audio import Predictor
import json
import time
predictor = Predictor()
app = Flask(__name__)
@app.route("/predict", methods=["POST"])
def api_message():
if request.headers["Content-Type"] == "application/octet-stream":
now = time.time()
data = request.data
request_read_time = time.time() - now
now = time.time()
audio_array = pickle.loads(data)
if isinstance(audio_array, (list, tuple)):
audio_array, granularity = audio_array
else:
audio_array, granularity = audio_array, None
buffer_read_time = time.time() - now
now = time.time()
result = predictor.run(audio_array, granularity=granularity)
prediction_time = time.time() - now
return json.dumps({
"result":[i.tolist() for i in result],
"prediction_time": prediction_time,
"buffer_read_time": buffer_read_time,
"request_read_time": request_read_time,
"granularity": 1 if granularity is None else granularity
})
else:
return "415 Unsupported Media Type"
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5001)