-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_upload_record_en.py
74 lines (63 loc) · 2.75 KB
/
flask_upload_record_en.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
71
72
73
74
from flask import Flask, Response, render_template, request, session
from flask_socketio import SocketIO, send, emit, join_room, leave_room
import os
import requests
import base64
import time
from scipy.io.wavfile import write as write_wav
from infer import restore_model, load_audio
# from g2pNp2g_simple.p2gFuntion import p2g_simple as p2g
app = Flask(__name__)
app.config["SECRET_KEY"] = "dangvansam"
socketio = SocketIO(app)
config = 'pretrained_quartznet15x5/quartznet15x5.yaml'
encoder_checkpoint = 'pretrained_quartznet15x5/JasperEncoder-STEP-247400.pt'
decoder_checkpoint = 'pretrained_quartznet15x5/JasperDecoderForCTC-STEP-247400.pt'
neural_factory = restore_model(config, encoder_checkpoint, decoder_checkpoint, lm=False)
print('restore model checkpoint done!')
@app.route("/")
def index():
return render_template("socketio_template/index.html", audio_path=None, async_mode=socketio.async_mode)
@socketio.on('connect')
def connected():
print("CONNECTED: " + request.sid)
emit('to_client', {'text':request.sid})
@socketio.on('to_server')
def response_to_client(data):
print(data["text"])
emit('to_client',{'text':len(data["text"].split())})
@socketio.on('audio_to_server')
def get_audio(data):
#print(data)
filename = time.strftime("%Y%m%d_%H%M%S")
filepath = "static/record/" + filename + ".wav"
audio_file = open(filepath, "wb")
decode_string = base64.b64decode(data["audio_base64"].split(",")[1])
audio_file.write(decode_string)
#asr
print("asr processing...")
sig = load_audio(filepath)
greedy_hypotheses = neural_factory.infer_signal(sig)
print('greedy predict:{}'.format(greedy_hypotheses))
print("asr completed")
emit('audio_to_client', {'text_beamlm': "not use LM", 'text_greedy':greedy_hypotheses, 'filepath':filepath})
@socketio.on('image-upload')
def imageUpload(image):
emit('send-image', image, broadcast = True)
@app.route('/upload', methods=['POST'])
def predic_upload():
print('upload file')
if request.method == 'POST':
_file = request.files['file']
if _file.filename == '':
return index()
print('\n\nfile uploaded:',_file.filename)
_file.save('static/upload/' + _file.filename)
print('Write file success!')
sig = load_audio('static/upload/'+ _file.filename)
greedy_hypotheses = neural_factory.infer_signal(sig)
print('greedy predict:{}'.format(greedy_hypotheses))
# print('beamLM predict:{}'.format(beam_hypotheses))
return render_template('socketio_template/index.html', greedy_predict=greedy_hypotheses, beam_predict="not use LM", audio_path='static/upload/' + _file.filename)
if __name__ == '__main__':
socketio.run(app, host="192.168.2.26", port=9011, ssl_context="adhoc", debug=True)