From 411ddec23784de1154b5a87d4b00ea3d38c1f510 Mon Sep 17 00:00:00 2001 From: Mathieu B Date: Sat, 31 Jul 2021 12:57:23 +0200 Subject: [PATCH] Integration with the Client Speech to Text working :) (also moved flask related methods to flask_utils.py) --- jarvis/main.py | 24 ++++---------- jarvis/utils/client_utils.py | 61 ------------------------------------ jarvis/utils/flask_utils.py | 41 ++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 81 deletions(-) diff --git a/jarvis/main.py b/jarvis/main.py index 2738f71..2df4cb6 100644 --- a/jarvis/main.py +++ b/jarvis/main.py @@ -1,27 +1,14 @@ -import flask import lingua_franca -from flask import Flask, request, jsonify, Response +from flask import Flask from jarvis.skills import intent_manager from jarvis.skills.entertainement.jokes import JokesSkill from jarvis.skills.entertainement.spotify import SpotifySkill from jarvis.skills.research.wikipedia import WikipediaSkill -from jarvis.utils import languages_utils -from utils import config_utils, flask_utils +from jarvis.utils import languages_utils, flask_utils app = Flask(__name__) - -@app.route("/process", methods=['POST']) -def process_request(): - data = flask_utils.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)!')) - - return jsonify(intent_manager.recognise(sentence=data['sentence'])) - - if __name__ == '__main__': # Load lingua franca in the memory # Supported : English French German Hungarian Italian Portuguese Swedish @@ -32,14 +19,15 @@ if __name__ == '__main__': JokesSkill().register() SpotifySkill().register() + # Load all skills intent_manager.load_all_skills() + # Bunch of tests # intent_manager.recognise("cherche Elon Musk sur wikipédia") # WORKING # intent_manager.recognise("raconte moi une blague") # WORKING # intent_manager.recognise("joue le morceau crazy crazy nights de KISS sur spotify") # WORKING # intent_manager.recognise("coupe la musique") # WORKING # intent_manager.recognise("c'est quoi le nom de cette chanson ?") # WORKING - # start the flask 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) + # Start the flask server + flask_utils.start_server() diff --git a/jarvis/utils/client_utils.py b/jarvis/utils/client_utils.py index f9a906a..e69de29 100644 --- a/jarvis/utils/client_utils.py +++ b/jarvis/utils/client_utils.py @@ -1,61 +0,0 @@ -import json - -import requests -from requests.structures import CaseInsensitiveDict - -from jarvis.utils import config_utils - -client_url = config_utils.get_in_config("CLIENT_URL") - - -def ask_for_microphone_output(record_for_seconds, speech_before_input): - data = { - 'record_for_seconds': record_for_seconds, - 'speech_before_input': speech_before_input - } - - call_client_api("POST", "/record", json.dumps(data)) - - -def ask_for_input(listen_for_seconds, speech_before_input): - data = { - 'listen_for_seconds': listen_for_seconds, - 'speech_before_input': speech_before_input - } - - call_client_api("POST", "/input", json.dumps(data)) - - -def speak(speech): - data = { - 'speech': speech - } - call_client_api("POST", "/speak", data) - - -def sound(sound_name): - data = { - 'sound_name': sound_name - } - call_client_api("POST", "/sound", data) - - -def call_client_api(method, url, json_data=None): - if json_data is None: - json_data = {} - - try: - url_service = client_url + url - - headers = CaseInsensitiveDict() - headers["Authorization"] = config_utils.get_in_config("API_KEY") - headers["Content-Type"] = "application/json; charset=utf8" - - if method == 'GET': - return json.loads(requests.get(url_service, headers=headers).content.decode("utf-8")) - elif method == 'POST': - json_data = json.dumps(json_data) - return json.loads( - requests.post(url_service, headers=headers, data=json_data.encode("utf8")).content.decode("utf-8")) - except: - print("Error when calling the client API") diff --git a/jarvis/utils/flask_utils.py b/jarvis/utils/flask_utils.py index a5e8c8f..e8388d9 100644 --- a/jarvis/utils/flask_utils.py +++ b/jarvis/utils/flask_utils.py @@ -1,8 +1,40 @@ import json +import flask +import speech_recognition as sr +from flask import Response, request, jsonify -def get_data_in_request(request): - data_str = str(request.data.decode('utf8')).replace('"', '\"').replace("\'", "'") +from jarvis.main import app +from jarvis.skills import intent_manager +from jarvis.utils import config_utils, languages_utils + + +@app.route("/process", methods=['POST']) +def process_request(): + 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)!')) + + return jsonify(intent_manager.recognise(sentence=data['sentence'])) + + +@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()) + + return jsonify(intent_manager.recognise(sentence=result_stt)) + + +def get_data_in_request(flask_request): + data_str = str(flask_request.data.decode('utf8')).replace('"', '\"').replace("\'", "'") # if no data return an empty json to avoid error with json.loads below if not data_str: @@ -14,3 +46,8 @@ def get_data_in_request(request): data_json = json.loads(data_json) return data_json + + +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)