2021-07-26 18:23:32 +02:00
|
|
|
import json
|
|
|
|
|
2021-07-31 12:57:23 +02:00
|
|
|
import flask
|
|
|
|
import speech_recognition as sr
|
2021-07-31 20:38:00 +02:00
|
|
|
from flask import Response, request, jsonify, Flask
|
2021-07-26 18:23:32 +02:00
|
|
|
|
2021-07-31 12:57:23 +02:00
|
|
|
from jarvis.skills import intent_manager
|
|
|
|
from jarvis.utils import config_utils, languages_utils
|
|
|
|
|
2021-07-31 20:38:00 +02:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2021-07-31 12:57:23 +02:00
|
|
|
|
2021-08-01 18:25:52 +02:00
|
|
|
@app.route("/process_text_request", methods=['POST'])
|
|
|
|
def process_text_request():
|
2021-07-31 12:57:23 +02:00
|
|
|
data = get_data_in_request(request)
|
|
|
|
|
|
|
|
if 'sentence' not in data or not data['sentence']:
|
|
|
|
flask.abort(Response('You must provide a \'sentence\' parameter (not empty aswell)!'))
|
|
|
|
|
2021-08-01 18:25:52 +02:00
|
|
|
return jsonify(intent_manager.recognise(data['sentence'], request.headers.get('Client-Ip'),
|
|
|
|
request.headers.get('Client-Port')))
|
2021-07-31 12:57:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/process_audio_request", methods=['POST'])
|
|
|
|
def process_audio_request():
|
|
|
|
frame_data = request.data
|
|
|
|
sample_rate = 44100
|
|
|
|
sample_width = 2
|
|
|
|
|
|
|
|
r = sr.Recognizer()
|
|
|
|
audio = sr.AudioData(frame_data, sample_rate, sample_width)
|
|
|
|
|
|
|
|
result_stt = r.recognize_google(audio, language=languages_utils.get_language_only_country())
|
|
|
|
|
2021-08-01 11:47:51 +02:00
|
|
|
return jsonify(
|
|
|
|
intent_manager.recognise(result_stt, request.headers.get('Client-Ip'), request.headers.get('Client-Port')))
|
2021-07-31 12:57:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_data_in_request(flask_request):
|
|
|
|
data_str = str(flask_request.data.decode('utf8')).replace('"', '\"').replace("\'", "'")
|
2021-07-26 18:23:32 +02:00
|
|
|
|
|
|
|
# if no data return an empty json to avoid error with json.loads below
|
|
|
|
if not data_str:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
data_json = json.loads(data_str)
|
|
|
|
|
|
|
|
if not isinstance(data_json, dict):
|
|
|
|
data_json = json.loads(data_json)
|
|
|
|
|
|
|
|
return data_json
|
2021-07-31 12:57:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def start_server():
|
|
|
|
app.config['JSON_AS_ASCII'] = False
|
|
|
|
app.run(port=config_utils.get_in_config("PORT"), debug=False, host='0.0.0.0', threaded=True)
|