2022-11-28 21:56:08 +01:00
|
|
|
import json
|
|
|
|
import tempfile
|
2022-12-02 15:45:17 +01:00
|
|
|
from threading import Lock
|
2022-11-28 21:56:08 +01:00
|
|
|
|
|
|
|
import requests
|
2023-03-14 21:42:14 +01:00
|
|
|
from flask import Flask, request, jsonify
|
2022-12-02 15:45:17 +01:00
|
|
|
from flask_socketio import SocketIO, emit, join_room, leave_room, \
|
2022-12-04 15:27:45 +01:00
|
|
|
rooms
|
2023-03-14 21:42:14 +01:00
|
|
|
from pywhispercpp.model import Model
|
2022-11-28 21:56:08 +01:00
|
|
|
|
2022-12-04 15:27:45 +01:00
|
|
|
from jarvis.skills.intent_services import intent_manager
|
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-12-01 16:06:23 +01:00
|
|
|
|
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()
|
2023-03-14 21:42:14 +01:00
|
|
|
model = Model('small')
|
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)
|
2022-12-04 15:27:45 +01:00
|
|
|
print("New PROCESS request from room " + message['uuid'])
|
2022-12-02 15:45:17 +01:00
|
|
|
|
2022-12-04 15:27:45 +01:00
|
|
|
print("Message : " + message['data'])
|
|
|
|
# TODO: maybe implement grammar check ?
|
2022-12-02 15:45:17 +01:00
|
|
|
|
2022-12-04 15:27:45 +01:00
|
|
|
intent_manager.recognise(message['data'], message['uuid'])
|
2022-12-02 15:45:17 +01:00
|
|
|
|
2022-12-04 15:27:45 +01:00
|
|
|
send_jarvis_message_to_room(message['data'], message['uuid'])
|
2022-12-02 15:45:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.event
|
|
|
|
def join(message):
|
|
|
|
message = json.loads(message)
|
2022-12-04 15:27:45 +01:00
|
|
|
print("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'])
|
2022-12-04 15:27:45 +01:00
|
|
|
emit('my_response', 'In rooms: ' + ', '.join(rooms()))
|
2022-12-02 15:45:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.event
|
|
|
|
def connect():
|
|
|
|
global thread
|
|
|
|
emit('my_response', {'data': 'Connected', 'count': 0})
|
|
|
|
|
|
|
|
|
|
|
|
def send_user_message_to_room(text, room_id):
|
|
|
|
socketio.emit('message_from_user', {'data': text, "uuid": room_id}, to=room_id)
|
|
|
|
|
|
|
|
|
|
|
|
def send_jarvis_message_to_room(text, room_id):
|
|
|
|
socketio.emit('message_from_jarvis', {'data': text, "uuid": room_id}, to=room_id)
|
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():
|
2022-11-28 21:56:08 +01:00
|
|
|
print("[" + request.remote_addr + "] - New STT request")
|
|
|
|
|
|
|
|
audio_temp_file = tempfile.NamedTemporaryFile(prefix='jarvis-audio_', suffix='_client')
|
|
|
|
audio_temp_file.write(request.data)
|
|
|
|
|
2023-03-14 21:42:14 +01:00
|
|
|
# text = whisper_stt(audio_temp_file.name)
|
|
|
|
text = whisper_cpp_stt(audio_temp_file.name)
|
2022-12-04 15:27:45 +01:00
|
|
|
print(text)
|
2022-12-01 16:06:23 +01:00
|
|
|
|
2022-12-02 15:45:17 +01:00
|
|
|
return {"data": text, "uuid": "null"}
|
2022-12-01 16:06:23 +01:00
|
|
|
|
2022-12-04 15:27:45 +01:00
|
|
|
|
2022-12-02 15:45:17 +01:00
|
|
|
"""
|
2022-12-01 16:06:23 +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
|
|
|
|
|
|
|
|
|
|
|
# send request to whisper-asr server (docker)
|
2022-12-02 15:45:17 +01:00
|
|
|
def whisper_stt(audio_file):
|
2022-11-28 21:56:08 +01:00
|
|
|
headers = {
|
|
|
|
'accept': 'application/json',
|
|
|
|
# 'Content-Type': 'multipart/form-data',
|
|
|
|
}
|
|
|
|
|
|
|
|
params = {
|
|
|
|
'task': 'transcribe',
|
|
|
|
# TODO: add to config
|
|
|
|
'language': 'fr',
|
|
|
|
'output': 'json',
|
|
|
|
}
|
|
|
|
|
|
|
|
files = {
|
|
|
|
'audio_file': open(audio_file, 'rb'),
|
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: add to config
|
2022-11-28 22:15:09 +01:00
|
|
|
response = requests.post('https://whisper.broillet.ch/asr', params=params, headers=headers, files=files)
|
2022-11-28 21:56:08 +01:00
|
|
|
return json.loads(response.text)['text']
|
|
|
|
|
|
|
|
|
2023-03-14 21:42:14 +01:00
|
|
|
def whisper_cpp_stt(audio_file):
|
|
|
|
segments = model.transcribe(audio_file, speed_up=False, translate=False)
|
|
|
|
|
|
|
|
# combines all segments in one string
|
|
|
|
text = ''
|
|
|
|
for segment in segments:
|
|
|
|
text += segment.text + ' '
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2022-11-28 21:56:08 +01:00
|
|
|
def start_server():
|
2022-12-02 15:45:17 +01:00
|
|
|
socketio.run(app, host='0.0.0.0', port=6000, allow_unsafe_werkzeug=True)
|