Skip to content

Commit

Permalink
Cleanup Sentiment demo. (tensorflow#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsoergel authored Mar 23, 2018
1 parent d63a4b5 commit f7c8479
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 133 deletions.
4 changes: 3 additions & 1 deletion sentiment/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<h1>
TensorFlow.js Layers: Sentiment Analysis Demo
</h1>
<hr>
<div>
<div>
<span>Model type: </span>
Expand All @@ -45,6 +46,7 @@ <h1>
<span id="maxLen"></span>
</div>
</div>
<hr>
<div>
<select id="test-example-select" class="form-control">
<option value="positive">Positive example</option>
Expand All @@ -54,8 +56,8 @@ <h1>
<div>
<textarea id="review-text"></textarea>
</div>
<hr>
<div>
<button id="run-inference">Run inference</button>
<span id="status">Standing by.</span>
</div>

Expand Down
141 changes: 47 additions & 94 deletions sentiment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,118 +16,71 @@
*/

import * as tf from '@tensorflow/tfjs';
import * as loader from './loader';
import * as ui from './ui';

let model;
const HOSTED_MODEL_JSON_URL =
'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/model.json';
const HOSTED_METADATA_JSON_URL =
'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/metadata.json';

function status(statusText) {
document.getElementById('status').textContent = statusText;
}

/**
* Load pretrained model stored at a remote URL.
*
* @return An instance of `tf.Model` with model topology and weights loaded.
*/
async function loadHostedPretrainedModel() {
const HOSTED_MODEL_JSON_URL =
'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/model.json';
status('Loading pretrained model from ' + HOSTED_MODEL_JSON_URL);
try {
model = await tf.loadModel(HOSTED_MODEL_JSON_URL);
status('Done loading pretrained model.');
} catch (err) {
console.log(err);
status('Loading pretrained model failed.');
}
}

/**
* Load metadata file stored at a remote URL.
*
* @return An object containing metadata as key-value pairs.
*/
async function loadHostedMetadata() {
const HOSTED_METADATA_JSON_URL =
'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/metadata.json';
status('Loading metadata from ' + HOSTED_METADATA_JSON_URL);
try {
const metadataJson = await fetch(HOSTED_METADATA_JSON_URL);
const metadata = await metadataJson.json();
status('Done loading metadata.');
return metadata;
} catch (err) {
console.log(err);
status('Loading metadata failed.');
class SentimentPredictor {
/**
* Initializes the Sentiment demo.
*/
async init() {
this.model = await loader.loadHostedPretrainedModel(HOSTED_MODEL_JSON_URL);
await this.loadMetadata();
return this;
}
}

/**
* The main function of the Sentiment demo.
*
* Loads the pretrained model and metadata, and registers a listener to run
* inference on the contents of the text box when a button is clicked.
*/
async function sentiment() {
const sentimentMetadataJSON = await loadHostedMetadata();
await loadHostedPretrainedModel();

document.getElementById('modelType').textContent =
sentimentMetadataJSON['model_type'];
document.getElementById('vocabularySize').textContent =
sentimentMetadataJSON['vocabulary_size'];
document.getElementById('maxLen').textContent =
sentimentMetadataJSON['max_len'];

const exampleReviews = {
'positive':
'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',
'negative':
'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'
};
const testExampleSelect = document.getElementById('test-example-select');
const reviewText = document.getElementById('review-text');
const runInference = document.getElementById('run-inference');
testExampleSelect.addEventListener('change', () => {
reviewText.value = exampleReviews[testExampleSelect.value];
});
reviewText.value = exampleReviews['positive'];

const indexFrom = sentimentMetadataJSON['index_from'];
const maxLen = sentimentMetadataJSON['max_len'];
console.log('indexFrom = ' + indexFrom);
console.log('maxLen = ' + maxLen);
async loadMetadata() {
const sentimentMetadata =
await loader.loadHostedMetadata(HOSTED_METADATA_JSON_URL);
ui.showMetadata(sentimentMetadata);
this.indexFrom = sentimentMetadata['index_from'];
this.maxLen = sentimentMetadata['max_len'];
console.log('indexFrom = ' + this.indexFrom);
console.log('maxLen = ' + this.maxLen);

const wordIndex = sentimentMetadataJSON['word_index']
this.wordIndex = sentimentMetadata['word_index']
}

runInference.addEventListener('click', async () => {
predict(text) {
// Convert to lower case and remove all punctuations.
const inputText = reviewText.value.trim()
.toLowerCase()
.replace(/(\.|\,|\!)/g, '')
.split(' ');
status(inputText);
const inputText =
text.trim().toLowerCase().replace(/(\.|\,|\!)/g, '').split(' ');
ui.status(inputText);
// Look up word indices.
const inputBuffer = tf.buffer([1, maxLen], 'float32');
const inputBuffer = tf.buffer([1, this.maxLen], 'float32');
for (let i = 0; i < inputText.length; ++i) {
// TODO(cais): Deal with OOV words.
const word = inputText[i];
status(word);
inputBuffer.set(wordIndex[word] + indexFrom, 0, i);
ui.status(word);
inputBuffer.set(this.wordIndex[word] + this.indexFrom, 0, i);
}
const input = inputBuffer.toTensor();
console.log('inputBuffer.values:', inputBuffer.values);

status('Running inference');
ui.status('Running inference');
const beginMs = performance.now();
const predictOut = model.predict(input);
const predictOut = this.model.predict(input);
const score = predictOut.dataSync()[0];
predictOut.dispose();
const endMs = performance.now();

status(
'Inference result (0 - negative; 1 - positive): ' + score +
' (elapsed: ' + (endMs - beginMs) + ' ms)');
});
return {score: score, elapsed: (endMs - beginMs)};
}
};


/**
* Loads the pretrained model and metadata, and registers the predict
* function with the UI.
*/
async function setupSentiment() {
const predictor = await new SentimentPredictor().init();
ui.setPredictFunction(x => predictor.predict(x));
ui.prepUI(x => predictor.predict(x));
}

sentiment();
setupSentiment();
54 changes: 54 additions & 0 deletions sentiment/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import * as tf from '@tensorflow/tfjs';
import * as ui from './ui';

/**
* Load pretrained model stored at a remote URL.
*
* @return An instance of `tf.Model` with model topology and weights loaded.
*/
export async function loadHostedPretrainedModel(url) {
ui.status('Loading pretrained model from ' + url);
try {
const model = await tf.loadModel(url);
ui.status('Done loading pretrained model.');
return model;
} catch (err) {
console.log(err);
ui.status('Loading pretrained model failed.');
}
}

/**
* Load metadata file stored at a remote URL.
*
* @return An object containing metadata as key-value pairs.
*/
export async function loadHostedMetadata(url) {
ui.status('Loading metadata from ' + url);
try {
const metadataJson = await fetch(url);
const metadata = await metadataJson.json();
ui.status('Done loading metadata.');
return metadata;
} catch (err) {
console.log(err);
ui.status('Loading metadata failed.');
}
}
68 changes: 68 additions & 0 deletions sentiment/ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

const exampleReviews = {
'positive':
'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',
'negative':
'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'
};

export function status(statusText) {
document.getElementById('status').textContent = statusText;
}

export function showMetadata(sentimentMetadataJSON) {
document.getElementById('modelType').textContent =
sentimentMetadataJSON['model_type'];
document.getElementById('vocabularySize').textContent =
sentimentMetadataJSON['vocabulary_size'];
document.getElementById('maxLen').textContent =
sentimentMetadataJSON['max_len'];
}

export function prepUI(predict) {
const testExampleSelect = document.getElementById('test-example-select');
testExampleSelect.addEventListener('change', () => {
setReviewText(exampleReviews[testExampleSelect.value], predict);
});
setReviewText(exampleReviews['positive'], predict);
}

export function getReviewText() {
const reviewText = document.getElementById('review-text');
return reviewText.value;
}

function doPredict(predict) {
const reviewText = document.getElementById('review-text');
const result = predict(reviewText.value);
status(
'Inference result (0 - negative; 1 - positive): ' + result.score +
' (elapsed: ' + result.elapsed + ' ms)');
}

function setReviewText(text, predict) {
const reviewText = document.getElementById('review-text');
reviewText.value = text;
doPredict(predict);
}

export function setPredictFunction(predict) {
const reviewText = document.getElementById('review-text');
reviewText.addEventListener('input', () => doPredict(predict));
}
Loading

0 comments on commit f7c8479

Please sign in to comment.