Skip to content

Commit

Permalink
fix: nlu cleanup of old models
Browse files Browse the repository at this point in the history
  • Loading branch information
slvnperron committed Feb 7, 2019
1 parent 6023927 commit 273d4a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions modules/nlu/src/backend/pipelines/intents/ft_classifier.ts
Expand Up @@ -33,6 +33,12 @@ export default class FastTextClassifier implements IntentClassifier {
return Promise.fromCallback(cb => fileStream.end(cb))
}

private teardownModels() {
if (this._modelsByContext) {
_.values(this._modelsByContext).forEach(x => x.cleanup())
}
}

private _hasSufficientData(intents: sdk.NLU.IntentDefinition[]) {
const datasetSize = _.flatMap(intents, intent => intent.utterances).length
return intents.length > 0 && datasetSize > 0
Expand Down Expand Up @@ -86,6 +92,7 @@ export default class FastTextClassifier implements IntentClassifier {
}
}

this.teardownModels()
this._modelsByContext = modelsByContext

return models
Expand All @@ -111,6 +118,7 @@ export default class FastTextClassifier implements IntentClassifier {
m[model.name] = ft
}

this.teardownModels()
this._modelsByContext = m
}

Expand Down
6 changes: 5 additions & 1 deletion src/bp/ml/fasttext.ts
@@ -1,4 +1,3 @@
import { reject, resolve } from 'bluebird'
import * as sdk from 'botpress/sdk'
import { VError } from 'verror'

Expand Down Expand Up @@ -34,6 +33,11 @@ export class FastTextModel implements sdk.MLToolkit.FastText.Model {

constructor(private lazy: boolean = true, private keepInMemory = false) {}

cleanup() {
this._modelPromise = undefined
this._queryPromise = undefined
}

async trainToFile(
method: sdk.MLToolkit.FastText.TrainCommand,
modelPath: string,
Expand Down
1 change: 1 addition & 0 deletions src/bp/sdk/botpress.d.ts
Expand Up @@ -190,6 +190,7 @@ declare module 'botpress/sdk' {
}

export interface Model {
cleanup: () => void
trainToFile: (method: TrainCommand, modelPath: string, args: Partial<TrainArgs>) => Promise<void>
loadFromFile: (modelPath: string) => Promise<void>
predict: (str: string, nbLabels: number) => Promise<PredictResult[]>
Expand Down

0 comments on commit 273d4a9

Please sign in to comment.