Skip to content

Commit f7c8479

Browse files
authored
Cleanup Sentiment demo. (tensorflow#29)
1 parent d63a4b5 commit f7c8479

File tree

5 files changed

+419
-133
lines changed

5 files changed

+419
-133
lines changed

sentiment/index.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<h1>
3232
TensorFlow.js Layers: Sentiment Analysis Demo
3333
</h1>
34+
<hr>
3435
<div>
3536
<div>
3637
<span>Model type: </span>
@@ -45,6 +46,7 @@ <h1>
4546
<span id="maxLen"></span>
4647
</div>
4748
</div>
49+
<hr>
4850
<div>
4951
<select id="test-example-select" class="form-control">
5052
<option value="positive">Positive example</option>
@@ -54,8 +56,8 @@ <h1>
5456
<div>
5557
<textarea id="review-text"></textarea>
5658
</div>
59+
<hr>
5760
<div>
58-
<button id="run-inference">Run inference</button>
5961
<span id="status">Standing by.</span>
6062
</div>
6163

sentiment/index.js

+47-94
Original file line numberDiff line numberDiff line change
@@ -16,118 +16,71 @@
1616
*/
1717

1818
import * as tf from '@tensorflow/tfjs';
19+
import * as loader from './loader';
20+
import * as ui from './ui';
1921

20-
let model;
22+
const HOSTED_MODEL_JSON_URL =
23+
'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/model.json';
24+
const HOSTED_METADATA_JSON_URL =
25+
'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/metadata.json';
2126

22-
function status(statusText) {
23-
document.getElementById('status').textContent = statusText;
24-
}
25-
26-
/**
27-
* Load pretrained model stored at a remote URL.
28-
*
29-
* @return An instance of `tf.Model` with model topology and weights loaded.
30-
*/
31-
async function loadHostedPretrainedModel() {
32-
const HOSTED_MODEL_JSON_URL =
33-
'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/model.json';
34-
status('Loading pretrained model from ' + HOSTED_MODEL_JSON_URL);
35-
try {
36-
model = await tf.loadModel(HOSTED_MODEL_JSON_URL);
37-
status('Done loading pretrained model.');
38-
} catch (err) {
39-
console.log(err);
40-
status('Loading pretrained model failed.');
41-
}
42-
}
43-
44-
/**
45-
* Load metadata file stored at a remote URL.
46-
*
47-
* @return An object containing metadata as key-value pairs.
48-
*/
49-
async function loadHostedMetadata() {
50-
const HOSTED_METADATA_JSON_URL =
51-
'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/metadata.json';
52-
status('Loading metadata from ' + HOSTED_METADATA_JSON_URL);
53-
try {
54-
const metadataJson = await fetch(HOSTED_METADATA_JSON_URL);
55-
const metadata = await metadataJson.json();
56-
status('Done loading metadata.');
57-
return metadata;
58-
} catch (err) {
59-
console.log(err);
60-
status('Loading metadata failed.');
27+
class SentimentPredictor {
28+
/**
29+
* Initializes the Sentiment demo.
30+
*/
31+
async init() {
32+
this.model = await loader.loadHostedPretrainedModel(HOSTED_MODEL_JSON_URL);
33+
await this.loadMetadata();
34+
return this;
6135
}
62-
}
6336

64-
/**
65-
* The main function of the Sentiment demo.
66-
*
67-
* Loads the pretrained model and metadata, and registers a listener to run
68-
* inference on the contents of the text box when a button is clicked.
69-
*/
70-
async function sentiment() {
71-
const sentimentMetadataJSON = await loadHostedMetadata();
72-
await loadHostedPretrainedModel();
73-
74-
document.getElementById('modelType').textContent =
75-
sentimentMetadataJSON['model_type'];
76-
document.getElementById('vocabularySize').textContent =
77-
sentimentMetadataJSON['vocabulary_size'];
78-
document.getElementById('maxLen').textContent =
79-
sentimentMetadataJSON['max_len'];
80-
81-
const exampleReviews = {
82-
'positive':
83-
'die hard mario fan and i loved this game br br this game starts slightly boring but trust me it\'s worth it as soon as you start your hooked the levels are fun and exiting they will hook you OOV your mind turns to mush i\'m not kidding this game is also orchestrated and is beautifully done br br to keep this spoiler free i have to keep my mouth shut about details but please try this game it\'ll be worth it br br story 9 9 action 10 1 it\'s that good OOV 10 attention OOV 10 average 10',
84-
'negative':
85-
'the mother in this movie is reckless with her children to the point of neglect i wish i wasn\'t so angry about her and her actions because i would have otherwise enjoyed the flick what a number she was take my advise and fast forward through everything you see her do until the end also is anyone else getting sick of watching movies that are filmed so dark anymore one can hardly see what is being filmed as an audience we are impossibly involved with the actions on the screen so then why the hell can\'t we have night vision'
86-
};
87-
const testExampleSelect = document.getElementById('test-example-select');
88-
const reviewText = document.getElementById('review-text');
89-
const runInference = document.getElementById('run-inference');
90-
testExampleSelect.addEventListener('change', () => {
91-
reviewText.value = exampleReviews[testExampleSelect.value];
92-
});
93-
reviewText.value = exampleReviews['positive'];
94-
95-
const indexFrom = sentimentMetadataJSON['index_from'];
96-
const maxLen = sentimentMetadataJSON['max_len'];
97-
console.log('indexFrom = ' + indexFrom);
98-
console.log('maxLen = ' + maxLen);
37+
async loadMetadata() {
38+
const sentimentMetadata =
39+
await loader.loadHostedMetadata(HOSTED_METADATA_JSON_URL);
40+
ui.showMetadata(sentimentMetadata);
41+
this.indexFrom = sentimentMetadata['index_from'];
42+
this.maxLen = sentimentMetadata['max_len'];
43+
console.log('indexFrom = ' + this.indexFrom);
44+
console.log('maxLen = ' + this.maxLen);
9945

100-
const wordIndex = sentimentMetadataJSON['word_index']
46+
this.wordIndex = sentimentMetadata['word_index']
47+
}
10148

102-
runInference.addEventListener('click', async () => {
49+
predict(text) {
10350
// Convert to lower case and remove all punctuations.
104-
const inputText = reviewText.value.trim()
105-
.toLowerCase()
106-
.replace(/(\.|\,|\!)/g, '')
107-
.split(' ');
108-
status(inputText);
51+
const inputText =
52+
text.trim().toLowerCase().replace(/(\.|\,|\!)/g, '').split(' ');
53+
ui.status(inputText);
10954
// Look up word indices.
110-
const inputBuffer = tf.buffer([1, maxLen], 'float32');
55+
const inputBuffer = tf.buffer([1, this.maxLen], 'float32');
11156
for (let i = 0; i < inputText.length; ++i) {
11257
// TODO(cais): Deal with OOV words.
11358
const word = inputText[i];
114-
status(word);
115-
inputBuffer.set(wordIndex[word] + indexFrom, 0, i);
59+
ui.status(word);
60+
inputBuffer.set(this.wordIndex[word] + this.indexFrom, 0, i);
11661
}
11762
const input = inputBuffer.toTensor();
118-
console.log('inputBuffer.values:', inputBuffer.values);
11963

120-
status('Running inference');
64+
ui.status('Running inference');
12165
const beginMs = performance.now();
122-
const predictOut = model.predict(input);
66+
const predictOut = this.model.predict(input);
12367
const score = predictOut.dataSync()[0];
12468
predictOut.dispose();
12569
const endMs = performance.now();
12670

127-
status(
128-
'Inference result (0 - negative; 1 - positive): ' + score +
129-
' (elapsed: ' + (endMs - beginMs) + ' ms)');
130-
});
71+
return {score: score, elapsed: (endMs - beginMs)};
72+
}
73+
};
74+
75+
76+
/**
77+
* Loads the pretrained model and metadata, and registers the predict
78+
* function with the UI.
79+
*/
80+
async function setupSentiment() {
81+
const predictor = await new SentimentPredictor().init();
82+
ui.setPredictFunction(x => predictor.predict(x));
83+
ui.prepUI(x => predictor.predict(x));
13184
}
13285

133-
sentiment();
86+
setupSentiment();

sentiment/loader.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google LLC. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
import * as tf from '@tensorflow/tfjs';
19+
import * as ui from './ui';
20+
21+
/**
22+
* Load pretrained model stored at a remote URL.
23+
*
24+
* @return An instance of `tf.Model` with model topology and weights loaded.
25+
*/
26+
export async function loadHostedPretrainedModel(url) {
27+
ui.status('Loading pretrained model from ' + url);
28+
try {
29+
const model = await tf.loadModel(url);
30+
ui.status('Done loading pretrained model.');
31+
return model;
32+
} catch (err) {
33+
console.log(err);
34+
ui.status('Loading pretrained model failed.');
35+
}
36+
}
37+
38+
/**
39+
* Load metadata file stored at a remote URL.
40+
*
41+
* @return An object containing metadata as key-value pairs.
42+
*/
43+
export async function loadHostedMetadata(url) {
44+
ui.status('Loading metadata from ' + url);
45+
try {
46+
const metadataJson = await fetch(url);
47+
const metadata = await metadataJson.json();
48+
ui.status('Done loading metadata.');
49+
return metadata;
50+
} catch (err) {
51+
console.log(err);
52+
ui.status('Loading metadata failed.');
53+
}
54+
}

sentiment/ui.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google LLC. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
const exampleReviews = {
19+
'positive':
20+
'die hard mario fan and i loved this game br br this game starts slightly boring but trust me it\'s worth it as soon as you start your hooked the levels are fun and exiting they will hook you OOV your mind turns to mush i\'m not kidding this game is also orchestrated and is beautifully done br br to keep this spoiler free i have to keep my mouth shut about details but please try this game it\'ll be worth it br br story 9 9 action 10 1 it\'s that good OOV 10 attention OOV 10 average 10',
21+
'negative':
22+
'the mother in this movie is reckless with her children to the point of neglect i wish i wasn\'t so angry about her and her actions because i would have otherwise enjoyed the flick what a number she was take my advise and fast forward through everything you see her do until the end also is anyone else getting sick of watching movies that are filmed so dark anymore one can hardly see what is being filmed as an audience we are impossibly involved with the actions on the screen so then why the hell can\'t we have night vision'
23+
};
24+
25+
export function status(statusText) {
26+
document.getElementById('status').textContent = statusText;
27+
}
28+
29+
export function showMetadata(sentimentMetadataJSON) {
30+
document.getElementById('modelType').textContent =
31+
sentimentMetadataJSON['model_type'];
32+
document.getElementById('vocabularySize').textContent =
33+
sentimentMetadataJSON['vocabulary_size'];
34+
document.getElementById('maxLen').textContent =
35+
sentimentMetadataJSON['max_len'];
36+
}
37+
38+
export function prepUI(predict) {
39+
const testExampleSelect = document.getElementById('test-example-select');
40+
testExampleSelect.addEventListener('change', () => {
41+
setReviewText(exampleReviews[testExampleSelect.value], predict);
42+
});
43+
setReviewText(exampleReviews['positive'], predict);
44+
}
45+
46+
export function getReviewText() {
47+
const reviewText = document.getElementById('review-text');
48+
return reviewText.value;
49+
}
50+
51+
function doPredict(predict) {
52+
const reviewText = document.getElementById('review-text');
53+
const result = predict(reviewText.value);
54+
status(
55+
'Inference result (0 - negative; 1 - positive): ' + result.score +
56+
' (elapsed: ' + result.elapsed + ' ms)');
57+
}
58+
59+
function setReviewText(text, predict) {
60+
const reviewText = document.getElementById('review-text');
61+
reviewText.value = text;
62+
doPredict(predict);
63+
}
64+
65+
export function setPredictFunction(predict) {
66+
const reviewText = document.getElementById('review-text');
67+
reviewText.addEventListener('input', () => doPredict(predict));
68+
}

0 commit comments

Comments
 (0)