allows you to run your machine learning model on your website in for common use cases on the web. You can add the power of ML to your website in just a few lines of code! There is even a pre-trained model to help you moderate written content, which is what we're looking at today. The text toxicity classifier ML model
There is an existing pretrained model that works well for content moderation: the to see the classifier in action. I admit that I had a bit of fun testing out what sort of content would be flagged as harmful. For example:
| cd text-moderation |
This project requires you to have the plan for Firebase. If you choose to follow this demo including the server-side component, your project might need to be upgraded from Spark to Blaze. If you have a billing account set on your project through Google Cloud, you are already upgraded and good to go! Most importantly, if you're not ready to upgrade your project, then do not deploy the Cloud Functions portion of the sample. You can still use the client-side moderation without Cloud Functions.
To implement client-side moderation in the sample, I added some code to the index.html and main.js files in the Firebase text moderation example. There are three main steps to implement when using a TensorFlow.js model: installing the required components, loading the model, and then running the prediction. Let's add the code for each of these steps.
Install the scripts
Add the required TensorFlow.js dependencies. I added the dependencies as script tags in the HTML, but you can with some phrases that could come up on your website to determine how the model handles them.
toxicity.load loads the model, passing the threshold. Once loaded, it sets toxicity_model to the model value.
Run the prediction
Add a checkContent function that runs the model predictions on messages upon clicking "Add message":
| // main.js Guestbook.checkContent = function(message) { if (!toxicity_model) { console.log('no model found'); return false; } const messages = [message]; return toxicity_model.classify(messages).then(predictions => { for (let item of predictions) { for (let i in item.results) { console.log(item.results[i].match) if (item.results[i].match === true) { console.log('toxicity found'); return true; } } } console.log('no toxicity found'); return false; }); } |
This function does the following:
- Verifies that the model load has completed. If
toxicity_modelhas a value, then theload()function has finished loading the model. - Puts the message into an array called
messages, as an array is the object type that theclassifyfunction accepts. - Calls
classifyon themessagesarray. - Iterates through the prediction results.
predictionsis an array of objects each representing a different language label. You may want to know about only specific labels rather than iterating through them all. For example, if your use case is a website for hosting the transcripts of rap battles, you probably don't want to detect and remove insults. - Checks if the content is a match for that label. if the
matchvalue istrue, then the model has detected the given type of unwanted language. If the unwanted language is detected, the function returns true. There's no need to keep checking the rest of the results, since the content has already been deemed inappropriate. - If the function iterates through all the results and no label match is set to
true, then the function returnsfalse– meaning no undesirable language was found. The match label can also benull. In that case, its value isn'ttrue, so it's considered acceptable language. I will talk more about thenulloption in a future post.
Add a call to the checkContent in the saveMessage function:
| // main.js // Saves a new message on the Firebase DB. Guestbook.prototype.saveMessage = function(e) { e.preventDefault(); if (!this.messageInput.value || !this.nameInput.value) { return; } Guestbook.checkContent(this.messageInput.value).then((toxic) => { if (toxic === true) { // display a message to the user to be kind Guestbook.displaySnackbar(); // clear the message field Guestbook.resetMaterialTextfield(this.messageInput); return; } //… |
After a couple quick checks for input values, the contents of the message box is passed to the checkContent function.
If the content passes this check, the message is written to the Realtime Database. If not, a snack bar displays reminding the message author to be kind. The snack bar isn't anything special, so I'm not going to include the code here. You can see it in the full example code, or .
| An acceptable message gets published to the guestbook |
Next steps
If you enjoyed this article and would like to learn more about TensorFlow.js, here are some things you can do:
- Check out the .
- Play around with .
Community-Analysen & Experten-Meinungen 0
Verwandte Story-Cluster & Quellen (Vektor-KI)
Ähnliche Beiträge
Auch interessante Nachrichten Content moderation using machine learning: a dual approach
Thematisch verwandte Begriffe: Content, moderation, using, machine · 6 Treffer
Tengu, a Mirai-style Linux and IoT botnet
Creator Panel – One Creator, Full Production: Der neue Creator Workflow
Creator Panel – One Creator, Full Production: Der neue Creator Workflow
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
SOCIAL SHARE CARD GENERATOR