-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
85 lines (70 loc) · 2.91 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
import os
import warnings
from flask import Flask, render_template, jsonify, request
from flask_socketio import SocketIO
from pydub import AudioSegment
from silence_tensorflow import silence_tensorflow
from app_config import DEBUG, BOT_NAME, CACHE_DIR, INPUT_FILENAME, APP_MODE
from core.core import get_bot_response_as_text, load_core_model
from keys import FLASK_SECRET_KEY
from language_model.language_model import correct_user_utterance, load_language_model
from speech_recognizer.config import SAMPLING_RATE
from speech_recognizer.speech_recognizer import wav_to_text, load_speech_recognizer
from speech_synthesizer.speech_synthesizer import render_bot_response_as_audio, load_speech_synthesizer
silence_tensorflow()
warnings.filterwarnings("ignore", category=FutureWarning)
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.config['SECRET_KEY'] = FLASK_SECRET_KEY
socket_io = SocketIO(app)
@app.route('/')
def sessions():
if APP_MODE == "VOICE_CHAT_LITE_MODE":
return render_template('voice_chat_lite_session.html')
if APP_MODE == "VOICE_CHAT_MODE":
return render_template('voice_chat_session.html')
return render_template('text_chat_session.html')
@app.route('/initialize')
def initialize():
if APP_MODE == "VOICE_CHAT_MODE":
load_speech_recognizer()
load_language_model()
load_speech_synthesizer()
if APP_MODE == "VOICE_CHAT_LITE_MODE":
load_speech_synthesizer()
load_core_model()
return jsonify("Models loaded successfully")
@app.route('/send_wav', methods=['POST'])
def handle_user_wav():
audio_data = request.files['audio_data']
audio_data_path = os.path.join(CACHE_DIR, "audio_data")
audio_data.save(audio_data_path)
sound = AudioSegment.from_file(audio_data_path)
sound = sound.set_frame_rate(SAMPLING_RATE)
wav_file_path = os.path.join(CACHE_DIR, INPUT_FILENAME)
sound.export(wav_file_path, format="wav")
user_utterance = wav_to_text(wav_file_path)
logger.info(user_utterance)
corrected_user_utterance = correct_user_utterance(user_utterance)
logger.info(corrected_user_utterance)
bot_response = get_bot_response_as_text(corrected_user_utterance)
logger.info(bot_response)
render_bot_response_as_audio(bot_response)
return jsonify(bot_response)
@app.route('/send_text', methods=['POST'])
def handle_user_text():
user_utterance = request.data.decode("utf-8")
logger.info(user_utterance)
bot_response = get_bot_response_as_text(user_utterance)
logger.info(bot_response)
render_bot_response_as_audio(bot_response)
return jsonify(bot_response)
@socket_io.on('chat_send')
def handle_user_msg(payload):
user_utterance = payload['message']
bot_response = get_bot_response_as_text(user_utterance)
response = {'name': BOT_NAME, 'reply': bot_response}
socket_io.emit('chat_response', response)
if __name__ == '__main__':
socket_io.run(app, debug=DEBUG)