Skip to content

Commit

Permalink
LanguageModel: Support p5's preload()
Browse files Browse the repository at this point in the history
Unsure if calling _{inc,de}crementPreload() manually is the best way to accomplish this. I first tried the p5PreloadHelper from the old repo, but this never made window._preloadCount go above zero for me. (Maybe @ziyuan-linn has some idea?)
  • Loading branch information
gohai committed Aug 4, 2023
1 parent d68a688 commit d962efe
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/LanguageModel/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<script src="../../dist/ml5.js"></script>
</head>
<body>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate" disabled></input><br><br>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate"></input><br><br>
<script src="sketch.js"></script>
</body>
</html>
13 changes: 5 additions & 8 deletions examples/LanguageModel/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ let curWord = 0;
let curX = 0;
let curY = 20;

function preload() {
lm = ml5.languageModel('TinyStories-15M');
}

function setup() {
createCanvas(400, 400);
frameRate(15);
background(0);

lm = ml5.languageModel('TinyStories-15M', onModelLoaded);
select('#generate').mouseClicked(generateText);
}

function draw() {
Expand All @@ -29,12 +32,6 @@ function draw() {
}
}

function onModelLoaded() {
console.log('Model loaded');
select('#generate').removeAttribute('disabled');
select('#generate').mouseClicked(generateText);
}

function generateText() {
let prompt = select('#prompt').value();
console.log('Prompt is "' + prompt + '"');
Expand Down
2 changes: 1 addition & 1 deletion examples/LanguageModelEvents/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<script src="../../dist/ml5.js"></script>
</head>
<body>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate" disabled></input><br><br>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate"></input><br><br>
<script src="sketch.js"></script>
</body>
</html>
9 changes: 5 additions & 4 deletions examples/LanguageModelEvents/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ let curWord = 0;
let angle = 0;
let radius = 60;

function preload() {
lm = ml5.languageModel('TinyStories-15M', onModelLoaded);
}

function setup() {
createCanvas(400, 400);
background(0);

lm = ml5.languageModel('TinyStories-15M', onModelLoaded);
select('#generate').mouseClicked(generateText);
}

function draw() {
Expand All @@ -17,8 +20,6 @@ function draw() {

function onModelLoaded() {
console.log('Model loaded');
select('#generate').removeAttribute('disabled');
select('#generate').mouseClicked(generateText);
}

function generateText() {
Expand Down
2 changes: 1 addition & 1 deletion examples/LanguageModelManual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<script src="../../dist/ml5.js"></script>
</head>
<body>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate" disabled></input>
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate"></input>
<script src="sketch.js"></script>
</body>
</html>
11 changes: 6 additions & 5 deletions examples/LanguageModelManual/sketch.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
let lm;
let numOptions = 40;

async function setup() {
noCanvas();

function preload() {
lm = await ml5.languageModel('TinyStories-15M', onModelLoaded);
}

function setup() {
noCanvas();
select('#generate').mouseClicked(generateText);
}

function draw() {
}

function onModelLoaded() {
console.log('Model loaded');
select('#generate').removeAttribute('disabled');
select('#generate').mouseClicked(generateText);
}

function generateText() {
Expand Down
10 changes: 10 additions & 0 deletions src/LanguageModel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class LanguageModel extends EventEmitter {
this.words = [];
this.finished = true;

// for compatibility with p5's preload()
if (typeof window === 'object' && typeof window._incrementPreload === 'function') {
window._incrementPreload();
}

this.ready = callCallback(this.loadModel(), callback);
}

Expand Down Expand Up @@ -141,6 +146,11 @@ class LanguageModel extends EventEmitter {
await this.llama2.ccall('register_callback', null, [ 'number' ], [ onTokenCallback ]);

//console.log('loadModel done');

// for compatibility with p5's preload()
if (typeof window === 'object' && typeof window._decrementPreload === 'function') {
window._decrementPreload();
}
}

async generate(prompt, optionsOrCb, cb) {
Expand Down

0 comments on commit d962efe

Please sign in to comment.