|
16 | 16 | */
|
17 | 17 |
|
18 | 18 | import * as tf from '@tensorflow/tfjs';
|
| 19 | +import * as loader from './loader'; |
| 20 | +import * as ui from './ui'; |
19 | 21 |
|
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'; |
21 | 26 |
|
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; |
61 | 35 | }
|
62 |
| -} |
63 | 36 |
|
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); |
99 | 45 |
|
100 |
| - const wordIndex = sentimentMetadataJSON['word_index'] |
| 46 | + this.wordIndex = sentimentMetadata['word_index'] |
| 47 | + } |
101 | 48 |
|
102 |
| - runInference.addEventListener('click', async () => { |
| 49 | + predict(text) { |
103 | 50 | // 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); |
109 | 54 | // Look up word indices.
|
110 |
| - const inputBuffer = tf.buffer([1, maxLen], 'float32'); |
| 55 | + const inputBuffer = tf.buffer([1, this.maxLen], 'float32'); |
111 | 56 | for (let i = 0; i < inputText.length; ++i) {
|
112 | 57 | // TODO(cais): Deal with OOV words.
|
113 | 58 | 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); |
116 | 61 | }
|
117 | 62 | const input = inputBuffer.toTensor();
|
118 |
| - console.log('inputBuffer.values:', inputBuffer.values); |
119 | 63 |
|
120 |
| - status('Running inference'); |
| 64 | + ui.status('Running inference'); |
121 | 65 | const beginMs = performance.now();
|
122 |
| - const predictOut = model.predict(input); |
| 66 | + const predictOut = this.model.predict(input); |
123 | 67 | const score = predictOut.dataSync()[0];
|
124 | 68 | predictOut.dispose();
|
125 | 69 | const endMs = performance.now();
|
126 | 70 |
|
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)); |
131 | 84 | }
|
132 | 85 |
|
133 |
| -sentiment(); |
| 86 | +setupSentiment(); |
0 commit comments