Skip to content

Commit

Permalink
feat(server): introduce main NLP model and resolvers NLP model
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Jun 30, 2022
1 parent 8996761 commit e37526d
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ packages/**/config/config.json
skills/**/src/config.json
packages/**/data/db/*.json
skills/**/memory/*.json
core/data/leon-model.nlp
core/data/models/*.nlp
package.json.backup
.python-version
Empty file added core/data/models/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"build:server": "npm run delete-dist:server && npm run train && npm run generate:skills-endpoints && babel ./server/src -d ./server/dist --copy-files && shx mkdir -p server/dist/tmp",
"start:tcp-server": "cross-env PIPENV_PIPFILE=bridges/python/Pipfile pipenv run python bridges/python/tcp_server/main.py",
"start": "cross-env LEON_NODE_ENV=production node ./server/dist/index.js",
"train": "babel-node scripts/run-train.js",
"train": "babel-node scripts/train/run-train.js",
"prepare-release": "babel-node scripts/release/prepare-release.js",
"check": "babel-node scripts/run-check.js",
"docker:build": "docker build -t leonai/leon .",
Expand Down
24 changes: 19 additions & 5 deletions scripts/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default () => new Promise(async (resolve, reject) => {
const googleCloudPath = 'core/config/voice/google-cloud.json'
const watsonSttPath = 'core/config/voice/watson-stt.json'
const watsonTtsPath = 'core/config/voice/watson-tts.json'
const nlpModelPath = 'core/data/leon-model.nlp'
const resolversNlpModelPath = 'core/data/models/leon-resolvers-model.nlp'
const mainNlpModelPath = 'core/data/models/leon-main-model.nlp'
const report = {
can_run: { title: 'Run', type: 'error', v: true },
can_run_module: { title: 'Run modules', type: 'error', v: true },
Expand Down Expand Up @@ -87,13 +88,26 @@ export default () => new Promise(async (resolve, reject) => {
log.error(`${e}\n`)
}

// NLP model checking
// Resolvers NLP model checking

log.info('NLP model state')
if (!fs.existsSync(nlpModelPath) || !Object.keys(fs.readFileSync(nlpModelPath)).length) {
log.info('Resolvers NLP model state')
if (!fs.existsSync(resolversNlpModelPath)
|| !Object.keys(fs.readFileSync(resolversNlpModelPath)).length) {
report.can_text.v = false
Object.keys(report).forEach((item) => { if (item.indexOf('stt') !== -1 || item.indexOf('tts') !== -1) report[item].v = false })
log.error('NLP model not found or broken. Try to generate a new one: "npm run train"\n')
log.error('Resolvers NLP model not found or broken. Try to generate a new one: "npm run train"\n')
} else {
log.success('Found and valid\n')
}

// Main NLP model checking

log.info('Main NLP model state')
if (!fs.existsSync(mainNlpModelPath)
|| !Object.keys(fs.readFileSync(mainNlpModelPath)).length) {
report.can_text.v = false
Object.keys(report).forEach((item) => { if (item.indexOf('stt') !== -1 || item.indexOf('tts') !== -1) report[item].v = false })
log.error('Main NLP model not found or broken. Try to generate a new one: "npm run train"\n')
} else {
log.success('Found and valid\n')
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import loader from '@/helpers/loader'
import log from '@/helpers/log'

import train from '../train'
import train from '../train/train'
import generateHttpApiKey from '../generate/generate-http-api-key'
import setupDotenv from './setup-dotenv'
import setupCore from './setup-core'
Expand Down
213 changes: 0 additions & 213 deletions scripts/train.js

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/run-train.js → scripts/train/run-train.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import log from '@/helpers/log'
import train from './train'

/**
* Execute the training script
* Execute the training scripts
*/
(async () => {
try {
Expand Down
40 changes: 40 additions & 0 deletions scripts/train/train-main-model/train-global-entities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import path from 'path'
import fs from 'fs'

import log from '@/helpers/log'

/**
* Train global entities
*/
export default (lang, nlp) => new Promise((resolve) => {
log.title('Global entities training')

const globalEntitiesPath = path.join(process.cwd(), 'core/data', lang, 'global-entities')
const globalEntityFiles = fs.readdirSync(globalEntitiesPath)
const newEntitiesObj = { }

// Add global entities annotations (@...)
for (let i = 0; i < globalEntityFiles.length; i += 1) {
const globalEntityFileName = globalEntityFiles[i]
const [entityName] = globalEntityFileName.split('.')
const globalEntityPath = path.join(globalEntitiesPath, globalEntityFileName)
const { options } = JSON.parse(fs.readFileSync(globalEntityPath, 'utf8'))
const optionKeys = Object.keys(options)
const optionsObj = { }

log.info(`[${lang}] Adding "${entityName}" global entity...`)

optionKeys.forEach((optionKey) => {
const { synonyms } = options[optionKey]

optionsObj[optionKey] = synonyms
})

newEntitiesObj[entityName] = { options: optionsObj }
log.success(`[${lang}] "${entityName}" global entity added`)
}

nlp.addEntities(newEntitiesObj, lang)

resolve()
})
Loading

0 comments on commit e37526d

Please sign in to comment.