From ea477269c950615dfb767268df9d158ad5857797 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Wed, 31 May 2023 18:53:47 +0200 Subject: [PATCH] refactor --- jarvis/api.py | 29 +++++++++++++++++++---------- start.py | 3 +++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/jarvis/api.py b/jarvis/api.py index c5a8205..219fdb7 100644 --- a/jarvis/api.py +++ b/jarvis/api.py @@ -1,13 +1,12 @@ import json import logging +import openai import sys import tempfile -from threading import Lock - -import openai from flask import Flask, request from flask_socketio import SocketIO, emit, join_room, leave_room, \ rooms +from threading import Lock from jarvis.utils import chat_utils, whisper_utils, chatgpt_utils, faster_whisper_utils @@ -45,10 +44,9 @@ def process_message(message): # intent_manager.recognise(message['data'], message['uuid']) if message['data'] != "": response = chatgpt_utils.chatgpt_recognise(message['data'], message['uuid']) - text_response = chatgpt_utils.get_answer_from_response(response) - # response = "Tokens are expensive ya know?" + # text_response = "Tokens are expensive ya know?" - chat_utils.send_jarvis_message_to_room(text_response, message['uuid']) + chat_utils.send_jarvis_message_to_room(response['response'], message['uuid']) @socketio.event @@ -71,16 +69,27 @@ def connect(): @socketio.event -def clear_chat(message): - message = json.loads(message) +def clear_chat(uuid): + """ + Clear chat history for a specific room. + :param uuid: uuid + :return: + """ + # uuid = json.loads(uuid) - emit('clear_chat', {}, to=message['uuid']) - chatgpt_utils.clear_chat(message['uuid']) + emit('clear_chat', {}, to=uuid) + chatgpt_utils.clear_chat(uuid) # .WAV (i.e.) FILE REQUEST @app.route("/get_text_from_audio", methods=['POST']) def get_text_from_audio(): + """ + Transcribe audio file using whisper. + + :return: transcription text + """ + logging.info("New STT request from " + request.remote_addr) audio_temp_file = tempfile.NamedTemporaryFile(prefix='jarvis-audio_', suffix='_client') diff --git a/start.py b/start.py index a201c52..d934083 100644 --- a/start.py +++ b/start.py @@ -3,11 +3,14 @@ import logging import lingua_franca import jarvis.api +from jarvis.db import db_utils from jarvis.utils import faster_whisper_utils if __name__ == '__main__': logging.getLogger().setLevel(logging.INFO) + db_utils.create_database() + # Load lingua franca in the memory lingua_franca.load_language(lang="fr")