// Functions // IMPORTANT: the function that loads and calls the model must be an async function! async function runModel(input){ // 1. Provide the PATH to the model.json after the 'file://' const modelPath = 'file://assets/model.json'; // 2. Load the model and await it const model = await tf.loadLayersModel(modelPath); // 2.1 When manipulating tensors and calling .predict on a model, wrap it up in a tf.tidy() function const predictions = tf.tidy(() => { // 3. Data Preprocessing: // Transform the data into tensors and cast the data into the correct expected data type // (in this case, the model expects a float tensor) const data = tf.tensor1d([input]).cast('float32'); // 4. Call the predict function. It is, by default, an async function. Do not forget // to add the '.dataSync()' method to retrieve the output tensor from the model return model.predict(data).dataSync(); }); // 5. Do additional operations with the output tensor // (In this case, we want to return the predict value for the given input. The tensor shape is [1,1] so we retrieve the value at the first position) return predictions[0]; }