How to use a Tensorflow model pre-trained in Python in a browser extension / add-on using Tensorflow.js

Viewed 186

I've already set up and trained a model to classify text in TensorFlow using Python which is working. I've then saved this as a keras model.

tfjs.converters.save_keras_model(model, "tfjsmodel")

Then I uploaded the model file with weighting shards to a server.

Now I'm trying to use that model on the text within a webpage using a firefox add-on to classify it using my model. However it yields no output.

import * as tf from '@tensorflow/tfjs';

const model = await tf.loadLayersModel("https://myserver.com/tfjsmodel/model.json");

var articles = document.getElementsByTagName('article');

var articleAsString = "";
for (i = 0; i < articles.length ; i++){
    articleAsString += articles[i].outerHTML;
}

var strippedArticles = articleAsString.replace(/(<([^>]+)>)/gi, "");

const prediction = model.predict(strippedArticles);

window.alert(prediction);

Here is the code from the add-on manifest.json in case my issue is there.

{
    "manifest_version": 2,
    "name": "PolicyDetector",
    "version": "1.0",
  
    "description": "Detects political policies in articles and asks user to explain how they work",

    "content_security_policy": "script-src 'self' https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest 'unsafe-eval'; object-src 'self'",
  
    "icons": {
      "48": "icons/border-48.png"
    },
  
    "content_scripts": [
      {
        "matches": ["*://*.bbc.co.uk/news/*"],
        "js": ["policyDetector.js"],
        "run_at": "document_end"
      }
    ]  
}

Where am I going wrong that I am getting no output at all from my extension?

0 Answers
Related