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

Created on 20 Mar 2019  路  7Comments  路  Source: tensorflow/tfjs

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鈥攊t 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');

Most helpful comment

@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 :)

All 7 comments

I don't seem to be able to get the toxicity model working either. I commented here with the error: https://github.com/tensorflow/tfjs/issues/1162#issuecomment-474525325

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

@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 :)

I don't know if this is related, or a new issue鈥攚ith the same system configuration as above (fresh copies of the packages, but same versions), I'm now seeing the following error:

TypeError: Cannot read property 'id' of undefined
    at .../node_modules/@tensorflow/tfjs-converter/dist/src/executor/graph_executor.js:295:99
    at Array.map (<anonymous>)
    at GraphExecutor.<anonymous> (.../node_modules/@tensorflow/tfjs-converter/dist/src/executor/graph_executor.js:295:58)
    at step (.../node_modules/@tensorflow/tfjs-converter/dist/src/executor/graph_executor.js:56:23)
    at Object.next (.../node_modules/@tensorflow/tfjs-converter/dist/src/executor/graph_executor.js:37:53)
    at fulfilled (.../node_modules/@tensorflow/tfjs-converter/dist/src/executor/graph_executor.js:28:58)
    at tryCatcher (.../node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (.../node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (.../node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (.../node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (.../node_modules/bluebird/js/release/promise.js:694:18)
    at _drainQueueStep (.../node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (.../node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (.../node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (.../node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:705:18)

related to #1449

et {PythonShell} = require('python-shell')

var myPythonScriptPath = 'tacotron-master/eval.py';

var options = {
mode: 'text',
args: [' --checkpoint ~/tacotron/logs-tacotron/model.ckpt', '-185000']
};

PythonShell.run('tacotron-master/eval.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
i wrote this code to run this command "python3 eval.py --checkpoint ~/tacotron/logs-tacotron/model.ckpt-185000"
and gives me error
No module named 'tensorflow'
wich is in another script.py that eval call import tensor flow
what should i do to run this pre-trained model

This has been resolve here , @Mariaa98 please open a new issue using template , thank you

Was this page helpful?
0 / 5 - 0 ratings