-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseragree.js
More file actions
122 lines (98 loc) · 2.72 KB
/
useragree.js
File metadata and controls
122 lines (98 loc) · 2.72 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
/*
This uses a ml5 charRNN model trained on a corpus of privacy policies
For more about ml5 see https://ml5js.org/
*/
let charRNN;
let textInput;
let tempSlider;
let modelIsReady = false
let runningInference = false;
let autoGenerating = false;
let currentText = '';
function setup() {
noCanvas();
// Create the charRNN generator passing it the model directory
charRNN = ml5.charRNN('useragree/', modelReady);
// Grab the DOM elements
textInput = select('#textInput');
tempSlider = select('#tempSlider');
// DOM element events
select('#reset').mousePressed(onResetButton);
select('#generate').mousePressed(onGenerateButton);
tempSlider.input(updateSliders);
}
// Update the slider values
function updateSliders() {
select('#temperature').html(tempSlider.value());
}
function modelReady() {
select('#status').html('Loaded ' + new Date().toLocaleString());
modelIsReady = true;
}
// Read and seed with full text from input box
function generateWithFullInputText() {
currentText = textInput.value();
generate(currentText, false);
}
// Seed with last character of current text, preserving state (stateful LSTM)
function generateWithSingleChar() {
generate(currentText.slice(-1), true);
}
// Update UI with current text
function updateTextUI() {
select('#result').html(currentText);
}
// Clear current text, stop auto-generating
function onResetButton() {
currentText = '';
updateTextUI();
autoGenerating = false;
}
// Start auto-generating
function onGenerateButton() {
if(currentText == '') generateWithFullInputText();
else generateWithSingleChar();
autoGenerating = true;
}
// Stop auto-generating
function onStopButton() {
autoGenerating = false;
}
// Generate new text
function generate(seed, stateful) {
// prevent starting inference if we've already started another instance
if(!runningInference) {
runningInference = true;
// Update the status log
select('#status').html('Generating...');
let data = {
seed: seed,
temperature: tempSlider.value(),
length: 1,
stateful: stateful,
};
// Generate text
charRNN.generate(data, gotData);
// When it's finished
function gotData(err, result) {
if(result) {
// If the result is not a period, add output sample to current text
var str = result.sample;
var check = str.startsWith(".");
if (check) {
// console.log("a period!");
autoGenerating = false;
}
currentText += str;
updateTextUI();
}
// Update the status log
status = 'Ready! '
select('#status').html(status);
runningInference = false;
}
}
}
function draw() {
if(autoGenerating && modelIsReady) generateWithSingleChar();
}