Retrieving the "forecasts" from the Tensorflow.js model for detecting text toxicity

Currently, I am working on integrating a comment section into the website I designed for my university project.
My main objective is to determine whether a comment is toxic or not and then either display an alert or publish the comment.
To achieve this, I am utilizing a pretrained model from TensorFlow.js.
I am facing challenges in retrieving the 'predictions' array, which would allow me to verify if the model has classified the comment as toxic.

// Load Model
toxicity.load(threshold).then(model => {
    // Input Comment as Model Input
    var sentences = comment;
    var modelOutput = new Object();

    // Prediction
    model.classify(sentences).then(predictions => {
        modelOutput = predictions;
        return modelOutput;
    });
    // Pass Prediction
    console.log(modelOutput);
});

Thank you in anticipation.

Answer №1

To get the prediction result, make sure to use the await keyword.

predictedOutput = await model.classify(sentences)

console.log(predictedOutput);

Remember that the await keyword should be within an asynchronous function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring search operations in a circularly sorted matrix and its computational complexity

The function is designed to work with an NxN matrix that always has dimensions that are powers of 2. Given a number and such a matrix, it will return true if the number is found. Here is an example for a 4x4 size matrix: https://i.sstatic.net/nlr02.png Th ...

Order the JavaScript object by its value if the keys are numerical

I'm currently working on a project using InertiaJS. I have a PHP array that I pass as a prop to Vue and display it as a select box. Here's what the object looks like in JavaScript: { 1: "Vexapa", 5: "Linga & Satro", ...

Erasing information and then moving to the identical webpage

The HTML Content : <td> <a (click)="onDelete(item.id)"> <i class="material-icons" style="font-weight:bold; font-style:inherit ;color: rgba(247, 37, 37, 0.884);"> delete_outline </i> </a> </td> The correspondin ...

Certain scripts fail to load properly when using Internet Explorer

The code snippet provided only seems to be functional in Firefox, where it displays all alerts and successfully executes the displayUserLanding function. However, in Internet Explorer, the browser appears to only execute the following alert: alert("Helper ...

The jsx file is not being parsed by Webpack

In my current project, I am working with a JSX file that contains React code. import React from 'react'; import {render} from 'react-dom'; class App extends React.Component { render () { return <p> Hello React!</p>; ...

Issue with implementing TensorFlow in an R code demonstration

I am new to the world of Tensorflow and have heard great things about using Tensorflow with R. I am currently facing a challenge while trying to execute the example provided in this link: The issue seems to be arising from the following code snippet: whi ...

Determine whether a webpage includes a specific text string

Can someone assist me with this issue? I struggle with explaining things and my grammar may not be the best, so please bear with me. I have a client who needs a website that can automatically search for a manufacturing part number from a specific website: ...

Enable Row Editing with a Click in Material Table

Utilizing the material-table library, I am implementing a feature to enable table rows to be editable upon double-click. The goal is for clicking on a row to trigger the same action as clicking the edit button located in the actions column on the leftmost ...

Two Ajax Requests Simultaneously

I am currently faced with the challenge of handling two requests simultaneously. The first request involves executing a lengthy PHP script that takes 10 minutes to complete (I cannot modify it using JavaScript, so that's not an option). The second ...

What is the procedure for altering the location of a mesh within the animate() function in three.js?

In my implementation of a three.js mesh (found in three.js-master\examples\webgl_loader_collada_keyframe.html), I have a basic setup: function init() { ... ... var sphereGeometry = new THREE.SphereGeometry( 50, 32, 16 ); var sphereMater ...

Storing JSON information within a variable

I'm currently working on an autocomplete form that automatically populates the location field based on the user's zipcode. Below is the code snippet I've written to retrieve a JSON object containing location information using the provided zi ...

Having an issue with my code in angular 12 where I am unable to successfully call an API to retrieve a token, and then pass that token to another API for further processing

Here is the code snippet containing two methods: getToken and validateuser. I am fetching the token from getToken and passing it as a parameter to validateuser. However, before retrieving the token, my second API call is being executed. ...

The presence of <tr> tags nested within <td> elements on tables

Is it feasible to create a table structure like the one mentioned below in basic HTML? <table> <td> <tr>col 1 row 1</tr> <tr>col 1 row 2</tr> <tr>col 1 row 3</tr> </td> ...

The process of showcasing an enum value in RestAPIs

There is a defined enum class called Size: enum class Size { SMALL("1-50"), MEDIUM("51-1000"), LARGE("1001-5000"), EXTRA_LARGE("5000+"); } How should this be presented to users of the API? If we were ...

The problem with JQuery ajax arises when attempting to send a file input value

I am facing an issue when trying to send a file value to PHP using the ajax code below. The file gets uploaded successfully and stored in the database, but the problem arises when I get redirected. // form data submission $('#myForm').submit(fun ...

Execute JavaScript script once when route changes

I'm currently working on a system where I want to ensure that my animation-based JavaScript code only runs once after the route changes. The issue I'm facing is that when switching between pages and returning to the about page, the scripts are ca ...

Establishing a connection between JavaScript and MySQL within an HTML document

Recently, I started using node.js's node-mysql driver for MySQL and decided to write this piece of code: var mysql = require("mysql"); var connection = mysql.createConnection({ host : "localhost", user : "root", ...

Retrieve ObjectID from MongoDB Go driver after inserting a document

When I use the InsertOne method to create a new document, the result is returning an array of numbers instead of an ObjectID. However, in the database, the ID is being generated correctly. type User struct { ID string Email string Username ...

I have a large array with multiple elements that I need to search through a file. I want to only print out the array elements that are not already present in the file, ensuring that there are

Seeking assistance for the following code: I am working with an array containing 155 elements and a file that contains some elements of the array. I need to retrieve all values of the array elements that are present in the file. In cases where the array e ...

Angular setPristine function is not functioning properly

I am looking to achieve a simple task - cleaning the $scope.user fields without encountering errors. if ($scope.contactForm.$valid) { $scope.user = {}; $scope.contactForm.$setPristine(); } However, I'm still experiencing v ...