1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-09-20 06:17:20 +03:00

feat: map Python bridge and TCP server binaries to runtime code

This commit is contained in:
louistiti 2022-10-07 11:53:43 +08:00
parent b59f6a7264
commit 1a3d4f2204
7 changed files with 49 additions and 21 deletions

View File

@ -5,6 +5,15 @@ import { command } from 'execa'
import archiver from 'archiver'
import prettyBytes from 'pretty-bytes'
import {
PYTHON_BRIDGE_SRC_PATH,
TCP_SERVER_SRC_PATH,
BINARIES_FOLDER_NAME,
PYTHON_BRIDGE_DIST_PATH,
TCP_SERVER_DIST_PATH,
PYTHON_BRIDGE_BIN_NAME,
TCP_SERVER_BIN_NAME
} from '@/constants'
import { LogHelper } from '@/helpers/log-helper'
import { LoaderHelper } from '@/helpers/loader-helper'
import { OSHelper, OSTypes } from '@/helpers/os-helper'
@ -18,24 +27,20 @@ import { OSHelper, OSTypes } from '@/helpers/os-helper'
*/
const BUILD_TARGETS = new Map()
const PYTHON_BRIDGE_SRC_PATH = 'bridges/python/src'
const TCP_SERVER_SRC_PATH = 'tcp_server/src'
const BINARIES_FOLDER_NAME = OSHelper.getBinariesFolderName()
BUILD_TARGETS.set('python-bridge', {
name: 'Python bridge',
pipfilePath: path.join(PYTHON_BRIDGE_SRC_PATH, 'Pipfile'),
setupFilePath: path.join(PYTHON_BRIDGE_SRC_PATH, 'setup.py'),
distPath: 'bridges/python/dist',
archiveName: `leon-python-bridge-${BINARIES_FOLDER_NAME}.zip`
distPath: PYTHON_BRIDGE_DIST_PATH,
archiveName: `${PYTHON_BRIDGE_BIN_NAME}-${BINARIES_FOLDER_NAME}.zip`
})
BUILD_TARGETS.set('tcp-server', {
name: 'TCP server',
pipfilePath: path.join(TCP_SERVER_SRC_PATH, 'Pipfile'),
setupFilePath: path.join(TCP_SERVER_SRC_PATH, 'setup.py'),
distPath: 'tcp_server/dist',
archiveName: `leon-tcp-server-${BINARIES_FOLDER_NAME}.zip`
distPath: TCP_SERVER_DIST_PATH,
archiveName: `${TCP_SERVER_BIN_NAME}-${BINARIES_FOLDER_NAME}.zip`
})
;(async () => {
LoaderHelper.start()

View File

@ -7,6 +7,7 @@ import semver from 'semver'
import { version } from '@@/package.json'
import { LogHelper } from '@/helpers/log-helper'
import { PYTHON_BRIDGE_BIN_PATH } from '@/constants'
dotenv.config()
@ -171,7 +172,7 @@ export default () =>
try {
LogHelper.time('Skill execution time')
const p = await command(
'./bridges/python/dist/python-bridge/leon-python-bridge scripts/assets/intent-object.json',
`${PYTHON_BRIDGE_BIN_PATH} scripts/assets/intent-object.json`,
{ shell: true }
)
LogHelper.timeEnd('Skill execution time')

View File

@ -3,6 +3,7 @@ import path from 'node:path'
import { command } from 'execa'
import { PYTHON_BRIDGE_SRC_PATH, TCP_SERVER_SRC_PATH } from '@/constants'
import { LogHelper } from '@/helpers/log-helper'
import { LoaderHelper } from '@/helpers/loader-helper'
@ -18,8 +19,6 @@ import { LoaderHelper } from '@/helpers/loader-helper'
const SETUP_TARGETS = new Map()
// Find new spaCy models: https://github.com/explosion/spacy-models/releases
const SPACY_MODELS = ['en_core_web_trf-3.4.0', 'fr_core_news_md-3.4.0']
const PYTHON_BRIDGE_SRC_PATH = 'bridges/python/src'
const TCP_SERVER_SRC_PATH = 'tcp_server/src'
SETUP_TARGETS.set('python-bridge', {
name: 'Python bridge',

View File

@ -1,6 +1,9 @@
import path from 'node:path'
import dotenv from 'dotenv'
import type { LongLanguageCode } from '@/helpers/lang-helper'
import { OSHelper } from '@/helpers/os-helper'
dotenv.config()
@ -8,6 +11,16 @@ const PRODUCTION_ENV = 'production'
const DEVELOPMENT_ENV = 'development'
const TESTING_ENV = 'testing'
export const BINARIES_FOLDER_NAME = OSHelper.getBinariesFolderName()
export const PYTHON_BRIDGE_DIST_PATH = path.join('bridges', 'python', 'dist')
export const TCP_SERVER_DIST_PATH = path.join('tcp_server', 'dist')
export const PYTHON_BRIDGE_SRC_PATH = path.join('bridges', 'python', 'src')
export const TCP_SERVER_SRC_PATH = path.join('tcp_server', 'src')
export const PYTHON_BRIDGE_BIN_NAME = 'leon-python-bridge'
export const TCP_SERVER_BIN_NAME = 'leon-tcp-server'
export const IS_PRODUCTION_ENV = process.env['LEON_NODE_ENV'] === PRODUCTION_ENV
export const IS_DEVELOPMENT_ENV =
process.env['LEON_NODE_ENV'] === DEVELOPMENT_ENV
@ -35,3 +48,14 @@ export const HAS_LOGGER = process.env['LEON_LOGGER'] === 'true'
export const TCP_SERVER_HOST = process.env['LEON_PY_TCP_SERVER_HOST']
export const TCP_SERVER_PORT = process.env['LEON_PY_TCP_SERVER_PORT']
export const TCP_SERVER_BIN_PATH = path.join(
TCP_SERVER_DIST_PATH,
BINARIES_FOLDER_NAME,
TCP_SERVER_BIN_NAME
)
export const PYTHON_BRIDGE_BIN_PATH = path.join(
PYTHON_BRIDGE_DIST_PATH,
BINARIES_FOLDER_NAME,
PYTHON_BRIDGE_BIN_NAME
)

View File

@ -3,7 +3,7 @@ import path from 'node:path'
import { spawn } from 'node:child_process'
import { langs } from '@@/core/langs.json'
import { HAS_TTS } from '@/constants'
import { HAS_TTS, PYTHON_BRIDGE_BIN_PATH } from '@/constants'
import { LangHelper } from '@/helpers/lang-helper'
import { LogHelper } from '@/helpers/log-helper'
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
@ -225,7 +225,7 @@ class Brain {
try {
fs.writeFileSync(intentObjectPath, JSON.stringify(intentObj))
this.process = spawn(
`./bridges/python/dist/python-bridge/leon-python-bridge ${intentObjectPath}`,
`${PYTHON_BRIDGE_BIN_PATH} ${intentObjectPath}`,
{ shell: true }
)
} catch (e) {

View File

@ -14,6 +14,7 @@ import { version } from '@@/package.json'
import {
HAS_LOGGER,
IS_TESTING_ENV,
TCP_SERVER_BIN_PATH,
TCP_SERVER_HOST,
TCP_SERVER_PORT
} from '@/constants'
@ -223,10 +224,9 @@ class Nlu {
// Recreate a new TCP server process and reconnect the TCP client
kill(global.tcpServerProcess.pid, () => {
global.tcpServerProcess = spawn(
`./bridges/python/dist/tcp-server/leon-tcp-server ${locale}`,
{ shell: true }
)
global.tcpServerProcess = spawn(`${TCP_SERVER_BIN_PATH} ${locale}`, {
shell: true
})
global.tcpClient = new TcpClient(TCP_SERVER_HOST, TCP_SERVER_PORT)

View File

@ -4,7 +4,8 @@ import {
TCP_SERVER_HOST,
TCP_SERVER_PORT,
IS_DEVELOPMENT_ENV,
LANG as LEON_LANG
LANG as LEON_LANG,
TCP_SERVER_BIN_PATH
} from '@/constants'
import { LangHelper } from '@/helpers/lang-helper'
import TcpClient from '@/core/tcp-client'
@ -13,9 +14,7 @@ import server from '@/core/http-server/server'
process.title = 'leon'
global.tcpServerProcess = spawn(
`./bridges/python/dist/tcp-server/leon-tcp-server ${LangHelper.getShortCode(
LEON_LANG
)}`,
`${TCP_SERVER_BIN_PATH} ${LangHelper.getShortCode(LEON_LANG)}`,
{
shell: true,
detached: IS_DEVELOPMENT_ENV