Next.js Discord

Discord Forum

How to load tensorflow.js model inside an API Route

Unanswered
Black-capped Petrel posted this in #help-forum
Open in Discord
Black-capped PetrelOP
hi all,
I am trying to load a tensorflow.js model inside an API route like this:
import * as tf from "@tensorflow/tfjs-node";
...
// then inside my handler function
const model = await tfn.loadLayersModel("what path to use here, Ive tried absolute and relative paths?")

here is the error I get when using an absolute file path

 ⨯ ./node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>

Import trace for requested module:
./node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html
./node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$
./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
./node_modules/@tensorflow/tfjs-node/dist/index.js
./src/app/api/ai/route.ts

this works in my node only project I used to build the model
await tf.loadLayersModel("file://src/export/model.json")


Note: I have been able to load the model on the client using a useEffect and having the model public but I am wanting to keep it private in an API route

Given I can load it perfectly in a project just using node. Maybe it has something to do with the execution context?
Anyone who could shed some light? Thank you!

1 Reply

Black-capped PetrelOP
After further experimenting, I am finding that a few libraries seem to be incompatible when using them in app routers API folder. There is some tokenizing I am trying to do and getting similar issues where dependencies can't be found. I think I will go with a separate node express api