-
Notifications
You must be signed in to change notification settings - Fork 902
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #790 from ml5js/univeral-sentence-encoder
[Univeral sentence encoder] Ports tfjs Univeral sentence encoder model
- Loading branch information
Showing
10 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
examples/p5js/UniversalSentenceEncoder/UniversalSentenceEncoder_Basic/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" > | ||
<title>Universal Sentence Encoder with Tokenizer</title> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.min.js"></script> | ||
<script src="http://localhost:8080/ml5.js" type="text/javascript"></script> | ||
</head> | ||
|
||
<body> | ||
<h1>Universal Sentence Encoder with Tokenizer</h1> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
42 changes: 42 additions & 0 deletions
42
examples/p5js/UniversalSentenceEncoder/UniversalSentenceEncoder_Basic/sketch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
let sentenceEncoder; | ||
const sentences = [ | ||
'I love rainbows', | ||
'I love rainbows too', | ||
'I love cupcakes', | ||
'I love bagels more' | ||
] | ||
|
||
function setup(){ | ||
createCanvas(512, 512); | ||
// background(220); | ||
colorMode(HSB, 360, 100, 100); | ||
sentenceEncoder = ml5.universalSentenceEncoder(modelLoaded) | ||
} | ||
|
||
function modelLoaded(){ | ||
|
||
predict(); | ||
} | ||
|
||
function predict(){ | ||
|
||
sentenceEncoder.predict(sentences, gotResults); | ||
} | ||
|
||
function gotResults(err, result){ | ||
if(err){ | ||
return err; | ||
} | ||
console.log(result); | ||
|
||
result.forEach( (item, y) => { | ||
// console.log(item); | ||
item.forEach( (val, x) => { | ||
const l = map(val, -1, 1, 0, 100); | ||
noStroke(); | ||
fill(360, 100, l); | ||
rect(x, y * (height/result.length) , 1, (height/result.length)); | ||
}) | ||
}) | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
examples/p5js/UniversalSentenceEncoder/UniversalSentenceEncoder_WithTokenizer/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" > | ||
<title>Universal Sentence Encoder</title> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.min.js"></script> | ||
<script src="http://localhost:8080/ml5.js" type="text/javascript"></script> | ||
</head> | ||
|
||
<body> | ||
<h1>Universal Sentence Encoder</h1> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
37 changes: 37 additions & 0 deletions
37
examples/p5js/UniversalSentenceEncoder/UniversalSentenceEncoder_WithTokenizer/sketch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
let sentenceEncoder; | ||
const sentence = 'Monday, Tuesday, Wednesday, Thursday, and Friday are days of the Week'; | ||
|
||
|
||
function setup(){ | ||
createCanvas(512, 512); | ||
// background(220); | ||
colorMode(HSB, 360, 100, 100); | ||
rectMode(CENTER); | ||
textAlign(CENTER); | ||
sentenceEncoder = ml5.universalSentenceEncoder({withTokenizer:true}, modelLoaded) | ||
} | ||
|
||
function modelLoaded(){ | ||
console.log('model ready') | ||
predict(); | ||
} | ||
|
||
function predict(){ | ||
console.log('predicting') | ||
sentenceEncoder.encode(sentence, gotResults); | ||
} | ||
|
||
function gotResults(err, result){ | ||
if(err){ | ||
return err; | ||
} | ||
console.log(result); | ||
translate(40, 0); | ||
result.forEach( (item, idx) => { | ||
const rectHeight = map(item, 0, 7999, 0, 100); | ||
fill(180, 100, 100); | ||
rect(idx * 20, height/2 , 20, rectHeight); | ||
}) | ||
|
||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// import * as tf from '@tensorflow/tfjs'; | ||
import * as USE from '@tensorflow-models/universal-sentence-encoder'; | ||
import callCallback from '../utils/callcallback'; | ||
|
||
const DEFAULTS = { | ||
withTokenizer: false, | ||
} | ||
|
||
class UniversalSentenceEncoder { | ||
constructor(options, callback){ | ||
this.model = null; | ||
this.tokenizer = null; | ||
this.config = { | ||
withTokenizer: options.withTokenizer || DEFAULTS.withTokenizer | ||
}; | ||
|
||
callCallback(this.loadModel(), callback); | ||
} | ||
|
||
/** | ||
* load model | ||
*/ | ||
async loadModel(){ | ||
if(this.config.withTokenizer === true){ | ||
this.tokenizer = await USE.loadTokenizer(); | ||
} | ||
this.model = await USE.load(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Encodes a string or array based on the USE | ||
* @param {*} textString | ||
* @param {*} callback | ||
*/ | ||
predict(textArray, callback){ | ||
return callCallback(this.predictInternal(textArray), callback); | ||
} | ||
|
||
async predictInternal(textArray){ | ||
try{ | ||
const embeddings = await this.model.embed(textArray); | ||
const results = await embeddings.array(); | ||
embeddings.dispose(); | ||
return results; | ||
} catch(err){ | ||
console.error(err); | ||
return err; | ||
} | ||
} | ||
|
||
/** | ||
* Encodes a string based on the loaded tokenizer if the withTokenizer:true | ||
* @param {*} textString | ||
* @param {*} callback | ||
*/ | ||
encode(textString, callback){ | ||
return callCallback(this.encodeInternal(textString), callback); | ||
} | ||
|
||
async encodeInternal(textString){ | ||
if(this.config.withTokenizer === true){ | ||
return this.tokenizer.encode(textString); | ||
} | ||
console.error('withTokenizer must be set to true - please pass "withTokenizer:true" as an option in the constructor'); | ||
return false; | ||
} | ||
|
||
} | ||
|
||
const universalSentenceEncoder = (optionsOr, cb) => { | ||
const options = (typeof optionsOr === 'object') ? optionsOr : {}; | ||
const callback = (typeof optionsOr === 'function') ? optionsOr : cb; | ||
|
||
return new UniversalSentenceEncoder(options, callback); | ||
}; | ||
|
||
export default universalSentenceEncoder; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters