jarvis-server-v2/jarvis/api.py

112 lines
3.0 KiB
Python
Raw Normal View History

2022-11-28 21:56:08 +01:00
import json
import logging
import sys
2022-11-28 21:56:08 +01:00
import tempfile
2022-12-02 15:45:17 +01:00
from threading import Lock
2022-11-28 21:56:08 +01:00
import openai
from flask import Flask, request
2022-12-02 15:45:17 +01:00
from flask_socketio import SocketIO, emit, join_room, leave_room, \
rooms
2022-11-28 21:56:08 +01:00
2023-05-31 17:16:09 +02:00
from jarvis.utils import chat_utils, whisper_utils, chatgpt_utils, faster_whisper_utils
2022-12-02 15:45:17 +01:00
# Set this variable to "threading", "eventlet" or "gevent" to test the
# different async modes, or leave it set to None for the application to choose
# the best option based on installed packages.
async_mode = None
2022-11-28 21:56:08 +01:00
app = Flask(__name__)
2022-12-02 15:45:17 +01:00
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
openai.api_key = sys.argv[1]
2022-12-02 15:45:17 +01:00
@app.route('/')
def index():
return "Welcome to Jarvis Server API !"
@socketio.event
def process_message(message):
message = json.loads(message)
logging.info("New PROCESS request from room " + message['uuid'])
logging.info("Message : " + message['data'])
2022-12-02 15:45:17 +01:00
if message['uuid'] not in rooms():
logging.warning("Room not found, creating it")
join_room(message['uuid'])
# TODO: maybe implement grammar check and correction ?
2022-12-02 15:45:17 +01:00
# intent_manager.recognise(message['data'], message['uuid'])
if message['data'] != "":
2023-03-26 13:26:43 +02:00
response = chatgpt_utils.chatgpt_recognise(message['data'], message['uuid'])
text_response = chatgpt_utils.get_answer_from_response(response)
# response = "Tokens are expensive ya know?"
chat_utils.send_jarvis_message_to_room(text_response, message['uuid'])
2022-12-02 15:45:17 +01:00
@socketio.event
def join(message):
message = json.loads(message)
logging.info("New client joined room " + message['uuid'])
2022-12-02 15:45:17 +01:00
join_room(message['uuid'])
@socketio.event
def leave(message):
leave_room(message['uuid'])
@socketio.event
def connect():
global thread
emit('my_response', {'data': 'Connected', 'count': 0})
@socketio.event
def clear_chat(message):
message = json.loads(message)
emit('clear_chat', {}, to=message['uuid'])
chatgpt_utils.clear_chat(message['uuid'])
2022-11-28 21:56:08 +01:00
# .WAV (i.e.) FILE REQUEST
2022-12-02 15:45:17 +01:00
@app.route("/get_text_from_audio", methods=['POST'])
def get_text_from_audio():
logging.info("New STT request from " + request.remote_addr)
2022-11-28 21:56:08 +01:00
audio_temp_file = tempfile.NamedTemporaryFile(prefix='jarvis-audio_', suffix='_client')
audio_temp_file.write(request.data)
2023-05-31 17:16:09 +02:00
# text = whisper_utils.whisper_cpp_stt(audio_temp_file.name)
text = faster_whisper_utils.faster_whisper_stt(audio_temp_file.name)
logging.info("STT result for " + request.remote_addr + " : " + text)
return {"data": text}
2022-12-02 15:45:17 +01:00
"""
@app.route("/process_text", methods=['POST'])
def process_text():
print("[" + request.remote_addr + "] - New TXT request")
text = request.values['text']
answer = intent_manager.recognise(text, request.headers.get('Client-Ip'), request.headers.get('Client-Port'))
2022-12-02 15:45:17 +01:00
return {"transcription": text, "answer": answer}"""
2022-11-28 21:56:08 +01:00
def start_api():
logging.info("Starting Jarvis Server API...")
socketio.run(app, host='0.0.0.0', port=6000)