jarvis-server-v2/jarvis/api.py

108 lines
3.3 KiB
Python

import json
import logging
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 jarvis.utils import chat_utils, whisper_utils
# 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
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
openai.api_key = sys.argv[1]
@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'])
# TODO: maybe implement grammar check and correction ?
# intent_manager.recognise(message['data'], message['uuid'])
if message['data'] != "":
# response = chatgpt_recognise(message['data'])
response = {'action': 'answer',
'answer': "Hello! As an AI, I don't have emotions, but I'm always here to help you with your smart home needs. How can I assist you today?"}
if response['action'] == 'clarify':
chat_utils.send_jarvis_message_to_room(response['question'], message['uuid'])
elif response['action'] == 'command':
chat_utils.send_jarvis_message_to_room(response['comment'], message['uuid'])
elif response['action'] == 'query':
chat_utils.send_jarvis_message_to_room(response['device_description'], message['uuid'])
elif response['action'] == 'answer':
chat_utils.send_jarvis_message_to_room(response['answer'], message['uuid'])
else:
chat_utils.send_jarvis_message_to_room("I don't know how to respond to that...", message['uuid'])
@socketio.event
def join(message):
message = json.loads(message)
logging.info("New client joined room " + message['uuid'])
join_room(message['uuid'])
@socketio.event
def leave(message):
leave_room(message['uuid'])
emit('my_response', 'In rooms: ' + ', '.join(rooms()))
@socketio.event
def connect():
global thread
emit('my_response', {'data': 'Connected', 'count': 0})
# .WAV (i.e.) FILE REQUEST
@app.route("/get_text_from_audio", methods=['POST'])
def get_text_from_audio():
logging.info("New STT request from " + request.remote_addr)
audio_temp_file = tempfile.NamedTemporaryFile(prefix='jarvis-audio_', suffix='_client')
audio_temp_file.write(request.data)
text = whisper_utils.whisper_cpp_stt(audio_temp_file.name)
logging.info("STT result for " + request.remote_addr + " : " + text)
return {"data": text}
"""
@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'))
return {"transcription": text, "answer": answer}"""
def start_api():
logging.info("Starting Jarvis Server API...")
socketio.run(app, host='0.0.0.0', port=6000)