Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How do I use this library with ts-node? #158

Open
moonman239 opened this issue Jun 21, 2023 · 3 comments
Open

[Question] How do I use this library with ts-node? #158

moonman239 opened this issue Jun 21, 2023 · 3 comments
Labels
question Further information is requested

Comments

@moonman239
Copy link

I have a non-Web/browser-based project that uses TypeScript with ts-node.

The "pipeline" function attempts to use the JavaScript Fetch API, which is not included with NodeJS, and the code therefore fails with an error: "fetch is not defined."

The "node-fetch" package doesn't seem to provide a compatible API.

@moonman239 moonman239 added the question Further information is requested label Jun 21, 2023
@xenova
Copy link
Collaborator

xenova commented Jun 21, 2023

One workaround to this is to pre-download your model files and then set env.allowRemoteModels=false; and env.localModelPath = '/path/to/local/models/'; to avoid the call to fetch. See here for more information about setting environment variables.

One issue you may run into is that Response will not be defined. However, since the response will be of type FileResponse (and not Response), you can maybe find a workaround by just setting globalThis.Response={} (or something similar):

https://github.com/xenova/transformers.js/blob/15854f9cd6ec0bc08c67c4dec742d92d84b46092/src/utils/hub.js#L453

If this is indeed the case, it might be beneficial to adapt the code (that line specifically) to account for the case where fetch and/or Request is not defined (PRs welcome!)

@Emsu might be able to help with this too.

@xenova
Copy link
Collaborator

xenova commented Jun 22, 2023

Another option is the use a polyfill for fetch. Here are some options:

@jedLahrim
Copy link

@moonman239 :

export class SentimentAnalysis {
  constructor() {}

  async _sentimentAnalysis(text: string) {
    let TransformersApi = Function(Constant.XENOVA_IMPORT)();
    const { pipeline, env } = await TransformersApi;
    // Disable remote models.
    env.allowRemoteModels = false;
    // Set local model path.
    env.localModelPath = Constant.XENOVA_PATH;
    let classifier = await pipeline(
      "sentiment-analysis",
      Constant.XENOVA_SENTIMENT_ANALYST_MODEL,
    );
    let result = await classifier(text);
    console.log(result);
    return result;
    // [{'label': 'POSITIVE', 'score': 0.999817686}]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants