This repository was archived by the owner on Jan 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerator.js
More file actions
193 lines (171 loc) · 4.86 KB
/
generator.js
File metadata and controls
193 lines (171 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const rimraf = require('rimraf')
const mkdirp = require('mkdirp')
const {spawn} = require('child_process')
const fs = require('fs')
const path = require('path')
const Ajv = require('ajv');
const {setModelTrainingStopped} = require("./db");
const util = require("util")
const {
UPLOADS_PATH,
TRAIN_FILENAME,
TRAIN_PID_FILENAME,
GENERATOR_PATH,
MODEL_DIR
} = require("./constants")
const trainOptionsSchema = require("../generator/train_arguments_schema")
const sampleOptionsSchema = require("../generator/sample_arguments_schema")
const ajv = new Ajv({allErrors: true, coerceTypes: true, removeAdditional: true});
const trainValidator = ajv.compile(trainOptionsSchema)
const sampleValidator = ajv.compile(sampleOptionsSchema)
function validate(validator, params) {
if (validator(params)) {
return null
}
let errors = {}
validator.errors.forEach((err) => {
let keyWithoutTrailingDot = err.dataPath.replace(/^\./, "");
errors[keyWithoutTrailingDot] = err.message
})
return errors
}
function chackTrainParams(params) {
return validate(trainValidator, params)
}
function checkSampleParams(params) {
return validate(sampleValidator, params)
}
/**
* Prepare model dir, create log file and try to train on _maybe_ existing train file
* @param submissionId {String}
* @param [params] {Object}
* @return {Promise}
*/
function trainModel(submissionId, params) {
let args = {
num_seqs: 32,
num_steps: 50,
lstm_size: 128,
num_layers: 2,
use_embedding: false,
embedding_size: 128,
learning_rate: 0.001,
train_keep_prob: 0.5,
max_steps: 1000,
save_every_n: 1000,
log_every_n: 100,
max_vocab: 3500
}
return new Promise(function (resolve, reject) {
if (!submissionId) reject("submissionId required");
if (typeof params === "object") {
let errors = chackTrainParams(params)
if (errors) {
return reject(errors)
} else {
Object.assign(args, params)
}
}
const folderPath = path.join(UPLOADS_PATH, submissionId)
const trainFilePath = path.join(folderPath, TRAIN_FILENAME)
if (!fs.existsSync(trainFilePath))
return reject("missing training data file")
const trainPidPath = path.join(folderPath, TRAIN_PID_FILENAME)
const modelDir = path.join(GENERATOR_PATH, MODEL_DIR, submissionId)
rimraf.sync(modelDir)
mkdirp.sync(modelDir)
/*
python train.py \
--input_file data/shakespeare.txt \
--name shakespeare \
--num_steps 50 \
--num_seqs 32 \
--learning_rate 0.01 \
--max_steps 20000
*/
let spawnArgs = [
"-u",
path.join(GENERATOR_PATH, 'train.py'),
"--input_file", trainFilePath, // utf8 encoded text file
"--name", submissionId // name of the model
// TODO add whitelist file
]
Object.keys(args).forEach((k) => {
if (k != null && args[k] != null) {
spawnArgs.push(`--${k}`)
spawnArgs.push(args[k])
}
})
const subprocess = spawn('python', spawnArgs, {
stdio: ['ignore', "pipe", "pipe"]
});
fs.writeFileSync(trainPidPath, subprocess.pid)
subprocess.on("error", () => {
rimraf.sync(trainPidPath)
setModelTrainingStopped(submissionId)
})
subprocess.on("exit", () => {
rimraf.sync(trainPidPath)
setModelTrainingStopped(submissionId)
})
resolve(subprocess);
})
}
/**
* @param submissionId {String}
* @param params {Object}
* @return {Promise}
*/
function sampleModel(submissionId, params) {
return new Promise(function (resolve, reject) {
if (!submissionId) return reject("submissionId required");
const modelDir = path.join(GENERATOR_PATH, MODEL_DIR, submissionId)
if (!fs.existsSync(modelDir)) return reject("missing the model")
let args = {
lstm_size: 128,
num_layers: 2,
use_embedding: false,
embedding_size: 128,
start_string: '',
max_length: 30
}
if (typeof params === "object") {
let errors = checkSampleParams(params)
if (errors) {
return reject(errors)
} else {
Object.assign(args, params)
}
}
/*
python sample.py \
--converter_path model/shakespeare/converter.pkl \
--checkpoint_path model/shakespeare/ \
--max_length 1000
*/
let spawnArgs = [
"-u",
path.join(GENERATOR_PATH, 'sample.py'),
"-W", "ignore",
"--converter_path", path.join(modelDir, "converter.pkl"),
"--checkpoint_path", modelDir,
]
Object.keys(args).forEach((k) => {
if (k != null && args[k] != null) {
spawnArgs.push(`--${k}`)
spawnArgs.push(args[k])
}
})
console.log("Sampling", util.inspect(spawnArgs))
const subprocess = spawn('python', spawnArgs, {
stdio: ["ignore", "pipe", "pipe"]
});
resolve(subprocess)
})
}
module.exports = {
chackTrainParams,
checkSampleParams,
trainModel,
sampleModel
}