猿问

在哪里添加导入命令

我正在尝试在我的 Laravel 项目中实现一个毒性 js 我正在使用这个库 https://www.npmjs.com/package/@tensorflow-models/toxicity


但是当我运行它时,我会在控制台中收到此错误


Uncaught ReferenceError: toxicity is not defined

我的代码看起来像这个 atm


看法


<div id="result"></div>

myjs.js


//import * as toxicity from '../../node_modules/@tensorflow-models/toxicity'; //doesnt work

//import * as toxicity from '@tensorflow-models/toxicity'; //doesnt work

$(document).ready(function() {

    const threshold = 0.9;


// Load the model. Users optionally pass in a threshold and an array of

// labels to include.

    toxicity.load(threshold).then(model => {

        const sentences = ['you suck'];


        model.classify(sentences).then(predictions => {

            // `predictions` is an array of objects, one for each prediction head,

            // that contains the raw probabilities for each input along with the

            // final prediction in `match` (either `true` or `false`).

            // If neither prediction exceeds the threshold, `match` is `null`.


            console.log(predictions);

            /*

            prints:

            {

              "label": "identity_attack",

              "results": [{

                "probabilities": [0.9659664034843445, 0.03403361141681671],

                "match": false

              }]

            },

            {

              "label": "insult",

              "results": [{

                "probabilities": [0.08124706149101257, 0.9187529683113098],

                "match": true

              }]

            },

            ...

             */

        });

    });

});

我认为问题是我不知道我应该在 Laravel 中的何处包含这一行


import * as toxicity from '@tensorflow-models/toxicity';

如果我将它包含在 js 文件中,它会给出一个错误:


Uncaught SyntaxError: Unexpected token *


白衣非少年
浏览 158回答 3
3回答

阿波罗的战车

好的,所以我设法通过进入 bootstrap.js 文件并添加这个来修复这个错误window.toxicity&nbsp;=&nbsp;require('@tensorflow-models/toxicity');然后运行npm&nbsp;run&nbsp;dev

郎朗坤

尝试将其添加到资源/js 文件夹中的 app.js 文件然后在您的终端中运行命令npm&nbsp;run&nbsp;dev然后再次尝试使用它。希望这可以帮助
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答