-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
31 lines (26 loc) · 920 Bytes
/
server.py
File metadata and controls
31 lines (26 loc) · 920 Bytes
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
from flask import jsonify, request, Flask
from utils import config
from utils.logger import logger
from utils.server_utils import infer
import io
import soundfile as sf
import resampy
import librosa
app = Flask(__name__)
def get_audio_from_request(request):
audio_file = request.files['audio']
data = audio_file.read()
audio, sampling_rate = sf.read(io.BytesIO(data), always_2d=True, dtype='float32')
if sampling_rate != 16000:
audio = resampy.resample(audio, sampling_rate, 16000, axis=-1)
return librosa.effects.trim(audio.reshape(-1))[0]
@app.route('/audio', methods=['POST'])
def audio_post():
audio = get_audio_from_request(request)
result = infer(audio)
logger.info(f"Received utterance from {request.remote_addr}. Transcript was: {result}")
return jsonify({'text': result})
def main():
app.run(config.host_ip, config.port)
if __name__ == "__main__":
main()