question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetch issue running pre-trained USE model in Node.js

See original GitHub issue

Had an issue with fetch when loading a pre-trained TensorFlow.js model in a Node.js app.

System information

Issue

I’m using the pre-trained USE model in a simple Express server endpoint—it basically just returns the text embedding for an input string.

The relevant source (which lives in a separate file called by the endpoint):

// use tensorflow js node backend
require('@tensorflow/tfjs-node');
const universalSentenceEncoder = require('@tensorflow-models/universal-sentence-encoder');

/**
 * Encode a variable length string as a sentence embedding via the Universal Sentence Encoder.
 *
 * @param {*} text - the input string
 */
exports.embed = async function embed(text) {
  const result = universalSentenceEncoder.load().then(model => model.embed(text));
  return result;
};

This appears to be related to #489, but my issue remained.

Calling the universalSentenceEncoder.load() function causes a ReferenceError: fetch is not defined error.

This appears to be the offending line: https://github.com/tensorflow/tfjs-models/blob/master/universal-sentence-encoder/src/index.ts#L49

It seems odd to me that tfjs-node requires the node-fetch package and yet still required a separate package to fix this.

The workaround I am using is overriding the global fetch with a line like this at the top of the file:

global.fetch = require('node-fetch');

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
annxingyuancommented, Mar 20, 2019

@alexellis yes good point - thanks for pointing this out! We’ll patch this soon. Meanwhile you can override global fetch as a bandaid, but hopefully not for too much longer 😃

2reactions
alexelliscommented, Mar 20, 2019

It’d be ideal if all examples worked on Node as well as in the browser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to load a converted pre-trained keras model to ...
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not...
Read more >
How To Deploy a Pre-Trained Question and Answer ...
In this tutorial, you have built and deployed a web application that uses a TensorFlow. js pre-trained model. Your Question and Answer web...
Read more >
Save and load models | TensorFlow.js
This tutorial will focus on saving and loading TensorFlow.js models (identifiable by JSON files). We can also import TensorFlow Python ...
Read more >
An introduction to AI in Node.js - IBM Developer
Reuse a model that has been pre-trained. For Node.js specifically, a model can be written in Python to use the distributed training ...
Read more >
Pre-Trained Models - codecraft.tv
The easiest place to get started is to use one of the pre-trained models found ... Node.js, how to install packages and run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found