Open qamarali205 opened 1 month ago
Hi there :wave: your usage for the dynamic import is slightly incorrect, it should be:
const { pipeline } = await import('@xenova/transformers');
const pipe = await pipeline('text-classification', 'Xenova/bert-base-uncased');
...
If you'd like to use require
, you can install our alpha version with npm i @huggingface/transformers
and then use it with the CommonJS syntax.
If you'd like to use require, you can install our alpha version with npm i @huggingface/transformers and then use it with the CommonJS syntax.
after install this how to use give me full correct code and if any require based packages so please provide me
like this const { Pipeline } =require('@xenova/transformers');
async function checkToxicity(text) { const pipeline = new Pipeline('text-classification', 'xenova/bert-base-uncased'); const result = await pipeline(text); return result; }
checkToxicity('Your message here').then(result => { console.log(result); });
Here's the fixed code:
const { pipeline } = require('@huggingface/transformers');
const classifierPromise = pipeline('text-classification', 'Xenova/toxic-bert');
async function checkToxicity(text) {
const classifier = await classifierPromise;
const result = await classifier(text);
return result;
}
async function main() {
const test1 = await checkToxicity('Hello world');
console.log(test1); // [{ label: 'toxic', score: 0.0009625141974538565 }]
const test2 = await checkToxicity('I hate you!');
console.log(test2); // [{ label: 'toxic', score: 0.9555305242538452 }]
}
main();
Some notes:
pipeline(...)
, we allocate memory for a new pipeline. For this reason, if you want to re-use the pipeline, be sure to define it globally.dtype
as follows:
const classifierPromise = pipeline('text-classification', 'Xenova/toxic-bert', {
dtype: 'q8',
});
Question
trying to use require to import the Pipeline class from the @xenova/transformers package, but encounter the following error:
const { Pipeline } = require('@xenova/transformers'); ^
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Z-charity\dating_app_backend\node_modules@xenova\transformers\src\transformers.js from D:\Z-charity\dating_app_backend\controllers\authController.js not supported. Instead change the require of transformers.js in D:\Z-charity\dating_app_backend\controllers\authController.js to a dynamic import() which is available in all CommonJS modules. at Object. (D:\Z-charity\dating_app_backend\controllers\authController.js:10:22) { code: 'ERR_REQUIRE_ESM'
Issue with Dynamic Import
const getPipeline = async () => { const { Pipeline } = await import('@xenova/transformers'); return new Pipeline('text-classification', 'xenova/bert-base-uncased'); };
{ "message": "Server error", "error": "Must implement _call method in subclass" }
Reproduction trying to use require to import the Pipeline class from the @xenova/transformers package, but encounter the following error:
const { Pipeline } = require('@xenova/transformers'); ^
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Z-charity\dating_app_backend\node_modules@xenova\transformers\src\transformers.js from D:\Z-charity\dating_app_backend\controllers\authController.js not supported. Instead change the require of transformers.js in D:\Z-charity\dating_app_backend\controllers\authController.js to a dynamic import() which is available in all CommonJS modules. at Object. (D:\Z-charity\dating_app_backend\controllers\authController.js:10:22) { code: 'ERR_REQUIRE_ESM'
Issue with Dynamic Import
const getPipeline = async () => { const { Pipeline } = await import('@xenova/transformers'); return new Pipeline('text-classification', 'xenova/bert-base-uncased'); };
{ "message": "Server error", "error": "Must implement _call method in subclass" }