mirror of
https://github.com/wassname/talk.git
synced 2026-09-09 11:38:08 +08:00
talk-plugin-toxic-comments - ADD MODEL VARIABLE TO TOXIC COMMENTS (#2178)
* updated readme * added config var TALK_PERSPECTIVE_MODEL * fixed duplication on readme * fixed link on readme * fix: applied some changes
This commit is contained in:
@@ -9,6 +9,7 @@ const config = {
|
||||
API_TIMEOUT: ms(process.env.TALK_PERSPECTIVE_TIMEOUT || '300ms'),
|
||||
DO_NOT_STORE: process.env.TALK_PERSPECTIVE_DO_NOT_STORE || true,
|
||||
SEND_FEEDBACK: process.env.TALK_PERSPECTIVE_SEND_FEEDBACK === 'TRUE',
|
||||
API_MODEL: process.env.TALK_PERSPECTIVE_MODEL || 'SEVERE_TOXICITY',
|
||||
};
|
||||
|
||||
if (process.env.NODE_ENV !== 'test' && !config.API_KEY) {
|
||||
|
||||
@@ -5,8 +5,10 @@ const {
|
||||
THRESHOLD,
|
||||
API_TIMEOUT,
|
||||
DO_NOT_STORE,
|
||||
API_MODEL,
|
||||
} = require('./config');
|
||||
const debug = require('debug')('talk:plugin:toxic-comments');
|
||||
const get = require('lodash/get');
|
||||
|
||||
// Load the global Talk configuration, we want to grab some variables..
|
||||
const { ROOT_URL } = require('config');
|
||||
@@ -55,7 +57,7 @@ async function getScores(text) {
|
||||
doNotStore: DO_NOT_STORE,
|
||||
requestedAttributes: {
|
||||
TOXICITY: {},
|
||||
SEVERE_TOXICITY: {},
|
||||
[API_MODEL]: {},
|
||||
},
|
||||
});
|
||||
if (!data || data.error) {
|
||||
@@ -64,7 +66,7 @@ async function getScores(text) {
|
||||
TOXICITY: {
|
||||
summaryScore: null,
|
||||
},
|
||||
SEVERE_TOXICITY: {
|
||||
[API_MODEL]: {
|
||||
summaryScore: null,
|
||||
},
|
||||
};
|
||||
@@ -74,20 +76,21 @@ async function getScores(text) {
|
||||
TOXICITY: {
|
||||
summaryScore: data.attributeScores.TOXICITY.summaryScore.value,
|
||||
},
|
||||
SEVERE_TOXICITY: {
|
||||
summaryScore: data.attributeScores.SEVERE_TOXICITY.summaryScore.value,
|
||||
[API_MODEL]: {
|
||||
summaryScore: data.attributeScores[API_MODEL].summaryScore.value,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get toxicity probability
|
||||
* Get toxicity probability from the scores, trying first the selection model,
|
||||
* but falling back to the `TOXICITY` model when the selected model isn't found.
|
||||
*
|
||||
* @param {object} scores scores as returned by `getScores`
|
||||
* @return {number} toxicity probability from 0 - 1.0
|
||||
*/
|
||||
function getProbability(scores) {
|
||||
return scores.SEVERE_TOXICITY.summaryScore;
|
||||
return get(scores, API_MODEL, scores.TOXICITY).summaryScore;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
const get = require('lodash/get');
|
||||
const { API_MODEL } = require('./config');
|
||||
|
||||
module.exports = {
|
||||
Comment: {
|
||||
toxicity: comment =>
|
||||
get(comment, 'metadata.perspective.SEVERE_TOXICITY.summaryScore'),
|
||||
// Try to get the score from the custom model first, but fall back to the
|
||||
// TOXICITY score if it isn't provided.
|
||||
get(
|
||||
comment,
|
||||
`metadata.perspective.${API_MODEL}.summaryScore`,
|
||||
get(comment, 'metadata.perspective.TOXICITY.summaryScore')
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user