* Use `textGenerator` to generate random text, show the characters on the * screen as they are generated one by one.
()
| 174 | * screen as they are generated one by one. |
| 175 | */ |
| 176 | async function generateText() { |
| 177 | try { |
| 178 | disableModelButtons(); |
| 179 | |
| 180 | if (textGenerator == null) { |
| 181 | logStatus('ERROR: Please load text data set first.'); |
| 182 | return; |
| 183 | } |
| 184 | const generateLength = parseInt(generateLengthInput.value); |
| 185 | const temperature = parseFloat(temperatureInput.value); |
| 186 | if (!(generateLength > 0)) { |
| 187 | logStatus( |
| 188 | `ERROR: Invalid generation length: ${generateLength}. ` + |
| 189 | `Generation length must be a positive number.`); |
| 190 | enableModelButtons(); |
| 191 | return; |
| 192 | } |
| 193 | if (!(temperature > 0 && temperature <= 1)) { |
| 194 | logStatus( |
| 195 | `ERROR: Invalid temperature: ${temperature}. ` + |
| 196 | `Temperature must be a positive number.`); |
| 197 | enableModelButtons(); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | let seedSentence; |
| 202 | let seedSentenceIndices; |
| 203 | if (seedTextInput.value.length === 0) { |
| 204 | // Seed sentence is not specified yet. Get it from the data. |
| 205 | [seedSentence, seedSentenceIndices] = textData.getRandomSlice(); |
| 206 | seedTextInput.value = seedSentence; |
| 207 | } else { |
| 208 | seedSentence = seedTextInput.value; |
| 209 | if (seedSentence.length < textData.sampleLen()) { |
| 210 | logStatus( |
| 211 | `ERROR: Seed text must have a length of at least ` + |
| 212 | `${textData.sampleLen()}, but has a length of ` + |
| 213 | `${seedSentence.length}.`); |
| 214 | enableModelButtons(); |
| 215 | return; |
| 216 | } |
| 217 | seedSentence = seedSentence.slice( |
| 218 | seedSentence.length - textData.sampleLen(), seedSentence.length); |
| 219 | seedSentenceIndices = textData.textToIndices(seedSentence); |
| 220 | } |
| 221 | |
| 222 | const sentence = await textGenerator.generateText( |
| 223 | seedSentenceIndices, generateLength, temperature); |
| 224 | generatedTextInput.value = sentence; |
| 225 | const status = 'Done generating text.'; |
| 226 | logStatus(status); |
| 227 | textGenerationStatus.value = status; |
| 228 | |
| 229 | enableModelButtons(); |
| 230 | |
| 231 | return sentence; |
| 232 | } catch (err) { |
| 233 | logStatus(`ERROR: Failed to generate text: ${err.message}, ${err.stack}`); |
no test coverage detected