Integration with the Client Speech to Text working :) (also moved flask related methods to flask_utils.py)

This commit is contained in:
Mathieu B 2021-07-31 12:57:23 +02:00
parent f92628f263
commit 411ddec237
3 changed files with 45 additions and 81 deletions

View File

@ -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()

View File

@ -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")

View File

@ -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)