Skip to content

Commit aab9e53

Browse files
authored
Prepare universal sentence encoder for npm (#144)
- Rename the global variable to `use` - Add `.npmignore` file to reduce npm pkg size - Update the docs to reflect both npm and script tag
1 parent e10b44a commit aab9e53

File tree

4 files changed

+86
-470
lines changed

4 files changed

+86
-470
lines changed

universal-sentence-encoder/.npmignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.vscode/
2+
.rpt2_cache/
3+
demo/
4+
src/
5+
coverage/
6+
package/
7+
node_modules/
8+
karma.conf.js
9+
*.tgz
10+
.travis.yml
11+
.npmignore
12+
tslint.json
13+
yarn.lock

universal-sentence-encoder/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,33 @@ This module is a TensorFlow.js [`FrozenModel`](https://js.tensorflow.org/api/lat
66

77
## Usage
88

9-
```js
9+
To import in npm:
1010

11+
```js
1112
import * as use from '@tensorflow-models/universal-sentence-encoder';
13+
```
1214

13-
// Load the model.
14-
const model = await use.load();
15-
16-
// Embed an array of sentences.
17-
const sentences = [
18-
'Hello.',
19-
'How are you?'
20-
];
15+
or as a standalone script tag:
2116

22-
const embeddings = await model.embed(sentences);
17+
```js
18+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
19+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder"></script>
20+
```
2321

24-
// `embeddings` is a 2D tensor consisting of the 512-dimensional embeddings for each sentence.
25-
// So in this example `embeddings` has the shape [2, 512].
26-
const verbose = true;
27-
embeddings.print(verbose);
22+
Then:
2823

24+
```js
25+
// Load the model.
26+
use.load().then(model => {
27+
// Embed an array of sentences.
28+
const sentences = [
29+
'Hello.',
30+
'How are you?'
31+
];
32+
model.embed(sentences).then(embeddings => {
33+
// `embeddings` is a 2D tensor consisting of the 512-dimensional embeddings for each sentence.
34+
// So in this example `embeddings` has the shape [2, 512].
35+
embeddings.print(true /* verbose */);
36+
});
37+
});
2938
```

universal-sentence-encoder/rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ function config({plugins = [], output = {}}) {
4040

4141
export default [
4242
config(
43-
{output: {format: 'umd', name: 'universal-sentence-encoder', file: 'dist/universal-sentence-encoder.js'}}),
43+
{output: {format: 'umd', name: 'use', file: 'dist/universal-sentence-encoder.js'}}),
4444
config({
4545
plugins: [minify()],
46-
output: {format: 'umd', name: 'universal-sentence-encoder', file: 'dist/universal-sentence-encoder.min.js'}
46+
output: {format: 'umd', name: 'use', file: 'dist/universal-sentence-encoder.min.js'}
4747
}),
4848
config({
4949
plugins: [minify()],

0 commit comments

Comments
 (0)