huggingface / transformers.js

State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!
https://huggingface.co/docs/transformers.js
Apache License 2.0
11.56k stars 715 forks source link

Error with Using require for ES Modules in @xenova/transformers Package #927

Open qamarali205 opened 1 month ago

qamarali205 commented 1 month ago

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" }

xenova commented 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.

qamarali205 commented 1 month ago

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); });

xenova commented 1 month ago

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: