Skip to content

Commit a7ad135

Browse files
committed
LanguageModel: Change the callback to be on-completion rather than on-token
As suggested by @shiffman. This makes the most basic example easier to understand.
1 parent e3a3e8d commit a7ad135

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

examples/LanguageModel/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<script src="../../dist/ml5.js"></script>
99
</head>
1010
<body>
11-
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate" disabled></input>
11+
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate" disabled></input><br><br>
1212
<script src="sketch.js"></script>
1313
</body>
1414
</html>

examples/LanguageModel/sketch.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
let lm;
2+
let words = [];
23
let curWord = 0;
3-
let angle = 0;
4-
let radius = 60;
4+
let curX = 0;
5+
let curY = 20;
56

67
function setup() {
78
createCanvas(400, 400);
9+
frameRate(15);
810
background(0);
911

1012
lm = ml5.languageModel('TinyStories-15M', onModelLoaded);
1113
}
1214

1315
function draw() {
14-
fill(0, 5);
16+
fill(0, 2);
1517
rect(0, 0, width, height);
18+
19+
if (curWord < words.length) {
20+
let wordWidth = textWidth(words[curWord]);
21+
if (curX+wordWidth > width) {
22+
curX = 0;
23+
curY += textAscent() + textDescent();
24+
}
25+
fill(255);
26+
text(words[curWord] + ' ', curX, curY);
27+
curX += wordWidth;
28+
curWord++;
29+
}
1630
}
1731

1832
function onModelLoaded() {
@@ -23,39 +37,17 @@ function onModelLoaded() {
2337

2438
function generateText() {
2539
let prompt = select('#prompt').value();
26-
console.log('Prompt: ' + prompt);
40+
console.log('Prompt is "' + prompt + '"');
2741

2842
let options = {
2943
temperature: 0.9
3044
};
31-
lm.generate(prompt, options, onToken);
32-
33-
curWord = 0;
45+
lm.generate(prompt, options, gotText);
3446
}
3547

36-
function onToken(lm) {
37-
console.log(lm.tokens[lm.tokens.length-1]);
38-
39-
while (curWord < lm.words.length) {
40-
push();
41-
translate(width/2, height/2);
42-
rotate(radians(angle));
43-
translate(radius, 0);
44-
rotate(radians(-angle));
45-
angle = angle + 5;
46-
radius += 0.5;
47-
if (radius > width/2 || radius > height/2) {
48-
radius = 60;
49-
}
50-
fill(255);
51-
textAlign(CENTER);
52-
textSize(10);
53-
text(lm.words[curWord], 0, 0);
54-
pop();
55-
curWord++;
56-
}
57-
58-
if (lm.finished) {
59-
console.log('Generation finished');
60-
}
48+
function gotText(out, lm) {
49+
console.log('Model returned "' + out + '"');
50+
// lm.words contains the output broken up in words
51+
words = lm.words;
52+
curWord = 0;
6153
}

src/LanguageModel/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ class LanguageModel extends EventEmitter {
108108
this.out += tokenStr;
109109
}
110110

111-
// on-token callback/event
112-
if (this.callback) {
113-
this.callback(this);
114-
}
111+
// on-token event
115112
this.emit('token', this);
116113

117114
// redo word tokenization
@@ -128,13 +125,16 @@ class LanguageModel extends EventEmitter {
128125
this.emit('word', this.words[i], this);
129126
}
130127

131-
// on-finished promise/event
128+
// on-finished promise/event/callback
132129
if (this.finished) {
133130
// fulfill the promise returned by generate()
134131
if (this.promiseResolve) {
135132
this.promiseResolve(this.out);
136133
}
137134
this.emit('finsh', this);
135+
if (this.callback) {
136+
this.callback(this.out, this);
137+
}
138138
}
139139
}, 'viifi');
140140

0 commit comments

Comments
 (0)