Been trying to load this model in my expressjs app for sentiment analysis but have been seeing a few issues with it but have been stuck on this one with no progress. I’m fairly curtain my path is correct as well but just in case here’s my full path to model.json
C:/dev/AI-GroupProject/ExpressApiProject/model.json
This is my code for creating the model and saving it in case it helps
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import os
import json
# Load the dataset
data = pd.read_csv(os.getcwd() + "/datasets/sentiment labelled sentences/amazon_cells_labelled.txt", delimiter="t", header=None)
sentences = data[0].tolist()
labels = np.array(data[1].tolist())
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(sentences, labels, test_size=0.2, random_state=42)
# Tokenize the data
tokenizer = Tokenizer(num_words=10000, oov_token="<OOV>")
tokenizer.fit_on_texts(X_train)
# Save the tokenizer object as a JSON file
tokenizer_json = tokenizer.to_json()
with open('tokenizer.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(tokenizer_json, ensure_ascii=False))
# Convert the text to sequences
train_sequences = tokenizer.texts_to_sequences(X_train)
test_sequences = tokenizer.texts_to_sequences(X_test)
# Pad the sequences to the same length
train_padded = pad_sequences(train_sequences, maxlen=100, padding='post', truncating='post')
test_padded = pad_sequences(test_sequences, maxlen=100, padding='post', truncating='post')
# Define the model
model = Sequential([
Embedding(input_dim=10000, output_dim=16, input_length=100),
GlobalAveragePooling1D(),
Dense(1, activation='sigmoid')
])
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(train_padded, y_train, epochs=10, batch_size=32, validation_data=(test_padded, y_test))
# Save the model
model_json = model.to_json()
with open('sentiment_model.json', 'w') as f:
f.write(model_json)
# Load the model
with open('sentiment_model.json', 'r') as f:
model_json = f.read()
loaded_model = tf.keras.models.model_from_json(model_json)
# Load the tokenizer object from the JSON file
with open('tokenizer.json', 'r', encoding='utf-8') as f:
tokenizer_json = json.load(f)
tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(tokenizer_json)
# Example usage of the loaded model
example_text = ["This movie was terrible"]
example_sequence = tokenizer.texts_to_sequences(example_text)
example_padded = pad_sequences(example_sequence, maxlen=100, padding='post', truncating='post')
prediction = loaded_model.predict(example_padded)
print(prediction)
And this is the code in the express app and it fails when loading the model
const loadedModel = await tf.loadLayersModel(MODEL_PATH);
const express = require('express');
const bodyParser = require('body-parser');
const tf = require('@tensorflow/tfjs');
const fs = require('fs');
require('@tensorflow/tfjs-node');
async function loadModel() {
// Load the saved model
const MODEL_PATH = 'file:///dev/AI-GroupProject/ExpressApiProject/model.json';
console.log("Loading Model")
const loadedModel = await tf.loadLayersModel(MODEL_PATH);
console.log("Model loaded");
return loadedModel;
}
// Initialize the Express app
const app = express();
// Use the body-parser middleware to parse request bodies as JSON
app.use(bodyParser.json());
// Define a route to handle sentiment analysis requests
app.post('/', async (req, res) => {
try {
// Load the model
const loadedModel = await loadModel();
// Get the text to analyze from the request body
const { sentence } = req.body;
const text = sentence.text;
// Tokenize and pad the text
const tokenizerJson = fs.readFileSync(__dirname + '/tokenizer.json', 'utf-8');
const tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(tokenizerJson);
const textSequences = tokenizer.textsToSequences([text]);
const paddedSequences = tf.keras.preprocessing.sequence.padSequences(
[textSequences],
{ maxlen: 100, padding: 'post', truncating: 'post' }
);
// Make a prediction with the loaded model
const prediction = loadedModel.predict(paddedSequences);
// Extract the prediction score
const score = prediction.dataSync()[0];
// Return the sentiment prediction as a JSON response
res.json({
sentiment: score >= 0.5 ? 'positive' : 'negative',
score
});
} catch (error) {
// Handle errors by returning a 500 Internal Server Error response
console.error(error);
res.status(500).json({ error: error.message });
}
});
// Start the server
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Debugger attached.
2023-04-09 05:53:24.918929: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Server listening on port 3000
Loading Model
TypeError: Cannot read property 'model_config' of undefined
at C:devAI-GroupProjectExpressApiProjectnode_modules@tensorflowtfjs-layersdisttf-layers.node.js:25405:38
at step (C:devAI-GroupProjectExpressApiProjectnode_modules@tensorflowtfjs-layersdisttf-layers.node.js:162:27)
at Object.next (C:devAI-GroupProjectExpressApiProjectnode_modules@tensorflowtfjs-layersdisttf-layers.node.js:111:53)
at fulfilled (C:devAI-GroupProjectExpressApiProjectnode_modules@tensorflowtfjs-layersdisttf-layers.node.js:92:28)
Iven been working to try and find a solution for a while and I’m stumped, though I’m kind of new to working with tensorflow and js so if you notice anything else I’ve done wrong please let me know.