xenova / 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
10.1k stars 591 forks source link

Module parse failed: Unexpected character '�' (1:0) #440

Open gaohongfeng1977 opened 7 months ago

gaohongfeng1977 commented 7 months ago

System Info

browser:chrome/edge os:ubuntu node:18.19 package management tools:pnpm

Environment/Platform

Description

./node_modules/.pnpm/registry.npmmirror.com+onnxruntime-node@1.14.0/node_modules/onnxruntime-node/bin/napi-v3/darwin/arm64/onnxruntime_binding.node

Module parse failed: Unexpected character '�' (1:0)

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders (Source code omitted for this binary file)

Reproduction

code include two files:

1.app/embedding/page.tsx webpage:

import {EmbeddingResponse} from '@/lib/embedding'

export default function EmbeddingPage() {
console.log("=====EembeddingPage====")

const rets = EmbeddingResponse()

  return (
    <div>
      {rets}
    </div>

  );
}

2.lib/embedding.ts

import { env } from '@xenova/transformers';  
import { HuggingFaceTransformersEmbeddings } from "langchain/embeddings/hf_transformers";

export async  function EmbeddingResponse() {

  env.localModelPath = '/data2/embedding/';
  env.allowRemoteModels = false;
  env.backends.onnx.wasm.wasmPaths = '/data2/embedding/cdn@xenovatransformers/';
  env.backends.onnx.numThreads = 1

  console.log("====env.backends.onnx====")
  console.log(env.backends.onnx);

  const currentMessageContent ="12345"

  const embs = new HuggingFaceTransformersEmbeddings({modelName:"bge-large-zh"})

  const embeddingArray = await embs.embedQuery(currentMessageContent)

  return embeddingArray 

}
xenova commented 7 months ago

Hi there 👋 This is because webpack is trying to bundle the *.node files, but this is not necessary. Others have had this issue in the past; see previous issues (which includes ways to fix):

Assuming you are running in Next.js, please follow this tutorial which shows how to do this. In the tutorial, we cover both client-side and server-side inference. We also showcase demo applications for each. The main thing you need to take note of is the next.config.js in each case (client-side, server-side).

gaohongfeng1977 commented 6 months ago

Thank for your reply !

But , I saw these issues above , can not find method to fix my error. and the tutorial link of huggingface is not visited for me in my area.

I tried to fix with modified next.config.js :

  config.plugins.push(
            // Ignore node-specific modules when bundling for the browser
            new webpack.IgnorePlugin({
                resourceRegExp: /^onnxruntime-node$|^node:|^sharp/,
            })
        );

but i got this error :

⨯ Error: Cannot find module 'onnxruntime-node' at webpack_require (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at webpack_require (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at webpack_require (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at webpack_require (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at webpack_require (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at webpack_require (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at eval (./lib/embedding.ts:5:78) at (rsc)/./lib/embedding.ts (/data2/langchain/langchain-nextjs-template/.next/server/app/embedding/page.js:299:1) at webpack_require (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at eval (./app/embedding/page.tsx:7:72) at (rsc)/./app/embedding/page.tsx (/data2/langchain/langchain-nextjs-template/.next/server/app/embedding/page.js:250:1) at Function.webpack_require (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at async Promise.all (index 0) at async Promise.all (index 0)

... ...

Error: Cannot find module 'onnxruntime-node' at webpackMissingModule (webpack-internal:///(rsc)/./node_modules/.pnpm/@xenova+transformers@2.9.0/node_modules/@xenova/transformers/src/backends/onnx.js:7:50) at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@xenova+transformers@2.9.0/node_modules/@xenova/transformers/src/backends/onnx.js:7:141) at (rsc)/./node_modules/.pnpm/@xenova+transformers@2.9.0/node_modules/@xenova/transformers/src/backends/onnx.js (/data2/langchain/langchain-nextjs-template/.next/server/vendor-chunks/@xenova+transformers@2.9.0.js:20:1) at __webpack_require__ (/data2/langchain/langchain-nextjs-template/.next/server/webpack-runtime.js:33:42) at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@xenova+transformers@2.9.0/node_modules/@xenova/transformers/src/env.js:8:75) at (rsc)/./node_modules/.pnpm/@xenova+transformers@2.9.0/node_modules/@xenova/transformers/src/env.js (/data2/langchain/langchain-nextjs-template/.next/server/vendor-chunks/@xenova+transformers@2.9.0.js:40:1)

those information maybe to say : because config ignored the module onnexruntime-node ,so the file "node_modules/.pnpm/@xenova+transformers@2.9.0/node_modules/@xenova/transformers/src/backends/onnx.js" cannot find module 'onnxruntime-node' .

But @xenova , you replied me with “but this is not necessary.” , I was confused by it .

pogzyb commented 5 months ago

@gaohongfeng1977 This is the snippet from the tutorial that should fix your issue.

next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
    // (Optional) Export as a static site
    // See https://nextjs.org/docs/pages/building-your-application/deploying/static-exports#configuration
    output: 'export', // Feel free to modify/remove this option

    // Override the default webpack configuration
    webpack: (config) => {
        // See https://webpack.js.org/configuration/resolve/#resolvealias
        config.resolve.alias = {
            ...config.resolve.alias,
            "sharp$": false,
            "onnxruntime-node$": false,
        }
        return config;
    },
}

module.exports = nextConfig