Hi everyone,
I'm super new to all of this (tfjs, nodejs, building servers, posting on forums, etc.), so please let me know if I'm doing anything wrong or if you have any comments on my newbie-forum-posting-habits!
I'm currently trying to modify the
lstm tfjs example to run as a node.js server. Essentially, I'd like it to load a previously created and trained model, and then accept http requests with seed text and return generated text using the loaded model.
I had it running in the browser so that it would load a pre-trained model from Amazon s3 and output generated text in the console and it worked fine. Now I'm trying to take that code and run it via node on a remote server.
When I send an http request, I get the following error:
"Error: browserHTTPRequest is not supported outside the web browser without a fetch polyfill.
at new BrowserHTTPRequest (/work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:46:19)
at Object.browserHTTPRequest (/work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:247:12)
at Object.<anonymous> (/work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-layers/dist/models.js:98:50)
at step (/work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-layers/dist/models.js:42:23)
at Object.next (/work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-layers/dist/models.js:23:53)
at /work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-layers/dist/models.js:17:71
at new Promise (<anonymous>)
at __awaiter (/work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-layers/dist/models.js:13:12)
at Object.loadModelInternal (/work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-layers/dist/models.js:92:12)
at Object.loadModel (/work/lstm_model_loader/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-layers/dist/exports.js:17:21)"
Like it says in the post, I tried adding the following code around my tf.loadModel(modelUrl), but I still get the fetch polyfill error.
const prevIsBrowser = tf.ENV.getFeatures().IS_BROWSER;
tf.ENV.features.IS_BROWSER = true;
const model = await tf.loadModel(modelUrl);
tf.ENV.features.IS_BROWSER = prevIsBrowser;
They also talk about replacing "fetch" with "const fetch = require('node-fetch')" in core and depending on "node-fetch", but I don't know where in core I should put "const fetch = require('node-fetch')". (I tried putting it in my own entry-point file, but that definitely didn't work!)
Could someone explain to me where I might put "const fetch = require('node-fetch')" to get around this error message? Or if you have any other ideas about how I could implement this code with node.js, please let me know!
Thanks!