mirror of
https://github.com/leon-ai/leon.git
synced 2024-11-24 04:31:31 +03:00
feat(server): fully implement low-level networking for IPC
This commit is contained in:
parent
8476d5fc3c
commit
8acb82da9b
@ -1,4 +1,5 @@
|
||||
import socket
|
||||
import select
|
||||
import os
|
||||
import json
|
||||
from os.path import join, dirname
|
||||
@ -40,13 +41,14 @@ def extract_spacy_entities(utterance):
|
||||
|
||||
while True:
|
||||
print('Waiting for connection...')
|
||||
s, addr = tcp_socket.accept()
|
||||
|
||||
conn, addr = tcp_socket.accept()
|
||||
|
||||
try:
|
||||
print(f'Client connected: {addr}')
|
||||
|
||||
while True:
|
||||
data = s.recv(1024)
|
||||
data = conn.recv(1024)
|
||||
|
||||
if not data:
|
||||
break
|
||||
@ -64,6 +66,7 @@ while True:
|
||||
}
|
||||
}
|
||||
|
||||
s.sendall(json.dumps(res).encode('utf-8'))
|
||||
conn.sendall(json.dumps(res).encode('utf-8'))
|
||||
finally:
|
||||
s.close()
|
||||
print(f'Client disconnected: {addr}')
|
||||
conn.close()
|
||||
|
@ -171,6 +171,15 @@ server.handleOnConnection = (socket) => {
|
||||
log.success(`STT ${sttState}`)
|
||||
log.success(`TTS ${ttsState}`)
|
||||
|
||||
tcpClient.ee.removeAllListeners()
|
||||
tcpClient.ee.on('spacy-entities-received', async ({ utterance, spacyEntities }) => {
|
||||
try {
|
||||
nlu.ner.spacyEntities = spacyEntities
|
||||
|
||||
await nlu.process(utterance)
|
||||
} catch (e) { /* */ }
|
||||
})
|
||||
|
||||
// Listen for new utterance
|
||||
socket.on('utterance', async (data) => {
|
||||
log.title('Socket')
|
||||
@ -180,24 +189,9 @@ server.handleOnConnection = (socket) => {
|
||||
|
||||
const utterance = data.value
|
||||
|
||||
/* tcpClient.on('spacy-entities-received', async (entities) => {
|
||||
try {
|
||||
await nlu.process(utterance)
|
||||
} catch (e) { /!* *!/ }
|
||||
}) */
|
||||
|
||||
tcpClient.emit('get-spacy-entities', utterance)
|
||||
})
|
||||
|
||||
tcpClient.ee.on('spacy-entities-received', async ({ utterance, spacyEntities }) => {
|
||||
console.log('utterance', utterance)
|
||||
console.log('spacyEntities', spacyEntities)
|
||||
|
||||
try {
|
||||
await nlu.process(utterance)
|
||||
} catch (e) { /* */ }
|
||||
})
|
||||
|
||||
// Handle automatic speech recognition
|
||||
socket.on('recognize', async (data) => {
|
||||
try {
|
||||
@ -250,20 +244,27 @@ server.bootstrap = async () => {
|
||||
instance.post('/api/query', async (request, reply) => {
|
||||
const { utterance } = request.body
|
||||
|
||||
try {
|
||||
tcpClient.ee.removeAllListeners()
|
||||
tcpClient.ee.on('spacy-entities-received', async ({ utterance, spacyEntities }) => {
|
||||
nlu.ner.spacyEntities = spacyEntities
|
||||
|
||||
const data = await nlu.process(utterance, { mute: true })
|
||||
|
||||
reply.send({
|
||||
...data,
|
||||
success: true
|
||||
})
|
||||
} catch (e) {
|
||||
reply.statusCode = 500
|
||||
reply.send({
|
||||
message: e.message,
|
||||
success: false
|
||||
})
|
||||
}
|
||||
try {
|
||||
reply.send({
|
||||
...data,
|
||||
success: true
|
||||
})
|
||||
} catch (e) {
|
||||
reply.statusCode = 500
|
||||
reply.send({
|
||||
message: e.message,
|
||||
success: false
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
tcpClient.emit('get-spacy-entities', utterance)
|
||||
})
|
||||
|
||||
server.generateSkillsRoutes(instance)
|
||||
|
@ -11,11 +11,16 @@ import string from '@/helpers/string'
|
||||
class Ner {
|
||||
constructor (ner) {
|
||||
this.ner = ner
|
||||
this._spacyEntities = []
|
||||
|
||||
log.title('NER')
|
||||
log.success('New instance')
|
||||
}
|
||||
|
||||
set spacyEntities (newSpacyEntities) {
|
||||
this._spacyEntities = newSpacyEntities
|
||||
}
|
||||
|
||||
static logExtraction (entities) {
|
||||
entities.forEach((ent) => log.success(`{ value: ${ent.sourceText}, entity: ${ent.entity} }`))
|
||||
}
|
||||
@ -51,7 +56,7 @@ class Ner {
|
||||
|
||||
await Promise.all(promises)
|
||||
|
||||
const { entities } = await this.ner.process({ locale: lang, text: utterance })
|
||||
let { entities } = await this.ner.process({ locale: lang, text: utterance })
|
||||
|
||||
// Trim whitespace at the beginning and the end of the entity value
|
||||
entities.map((e) => {
|
||||
@ -61,6 +66,10 @@ class Ner {
|
||||
return e
|
||||
})
|
||||
|
||||
// Merge with spaCy entities
|
||||
entities = entities.concat(this._spacyEntities)
|
||||
this._spacyEntities = []
|
||||
|
||||
if (entities.length > 0) {
|
||||
Ner.logExtraction(entities)
|
||||
return entities
|
||||
|
@ -19,9 +19,9 @@ export default class TcpClient {
|
||||
|
||||
this.tcpSocket.on('data', (chunk) => {
|
||||
log.title('TCP Client')
|
||||
log.info(`Received data: ${chunk.toString()}`)
|
||||
|
||||
const data = JSON.parse(chunk)
|
||||
|
||||
this._ee.emit(data.topic, data.data)
|
||||
})
|
||||
|
||||
@ -32,6 +32,7 @@ export default class TcpClient {
|
||||
|
||||
this.tcpSocket.on('end', () => {
|
||||
log.title('TCP Client')
|
||||
|
||||
log.success('Disconnected from TCP server')
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user