Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="textarea-container">
    <textarea placeholder="Enter text to classify"></textarea>
    <div class="classification-result"></div>
  </div>
  <div class="buttons">
    <div class="model tfjs-webgl">
      <div class="btn disabled">TFJS Toxicity (webgl)</div>
      <div class="result"></div>
    </div>
    <div class="model tflite">
      <div class="btn disabled">TFLite Movie Review</div>
      <div class="result"></div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                body {
  font-family: Arial, Helvetica, sans-serif;
  padding: 20px;
}

.container {
  display: flex;
}

textarea {
  border: 1px solid #999;
  border-radius: 4px;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 18px;
  height: 60px;
  padding: 8px;
  width: 500px;
}

.container {
  display: flex;
}

.buttons {
  margin-left: 20px;
  margin-top: -10px;
}

.model {
  display: flex;
  align-items: center;
}

.result {
  margin-left: 20px;
}

.btn {
  border: 1px solid #888;
  cursor: pointer;
  width: 200px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 10px 0;
}

.btn:hover {
  background-color: #eee;
}

.classification-result {
  margin-top: 6px;
  color: #888;
}

.classification-result .label {
  display: inline-block;
  width: 140px;
  color: black;
}

.classification-result .sentiment.true {
  color: #00AA00;
}
              
            
!

JS

              
                const textarea = document.querySelector("textarea");
const classificationResult = document.querySelector(".classification-result");
let lastModel;
let lastClassName;
let warmedUp = false;

setupButton(
  "tfjs-webgl",
  async () =>
    await tfTask.SentimentDetection.Toxicity.TFJS.load({
      backend: "webgl",
    }),
  true
);

setupButton(
  "tflite",
  async () => await tfTask.SentimentDetection.MovieReview.TFLite.load()
);

async function setupButton(className, modelCreateFn, needWarmup) {
  document
    .querySelector(`.model.${className} .btn`)
    .classList.remove("disabled");
  const resultEle = document.querySelector(`.model.${className} .result`);
  document
    .querySelector(`.model.${className} .btn`)
    .addEventListener("click", async () => {
      let model;
      // Create the model when user clicks on a button.
      if (lastClassName !== className) {
        // Clean up the previous model if existed.
        if (lastModel) {
          lastModel.cleanUp();
        }
        // Create the new model and save it.
        resultEle.textContent = "Loading...";
        model = await modelCreateFn();
        lastModel = model;
        lastClassName = className;
      }
      // Reuse the model if user clicks on the same button.
      else {
        model = lastModel;
      }

      // Warm up if needed.
      if (needWarmup && !warmedUp) {
        await model.predict(textarea.value);
        warmedUp = true;
      }

      // Run inference and update result.
      const start = Date.now();
      const result = await model.predict(textarea.value);
      const latency = Date.now() - start;
      renderResult(result);
      resultEle.textContent = `Latency: ${latency}ms`;
    });
}

function renderResult(result) {
  console.log(result);
  let strResult = "";
  const labels = Object.keys(result.sentimentLabels).sort();
  for (const label of labels) {
    const classificationResult = result.sentimentLabels[label].result;
    const probabilities = result.sentimentLabels[label].probabilities;
    strResult += `<span class="label">${label}</span>`;
    strResult += `<span class="sentiment ${classificationResult}">${classificationResult}</span><br>`;
  }
  classificationResult.innerHTML = strResult;
}

              
            
!
999px

Console