Transformers.js 文件

Node.js 中的伺服器端推理

您正在檢視的是需要從原始碼安裝。如果您想進行常規的 npm install,請檢視最新的穩定版本 (v3.0.0)。
Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

Node.js 中的伺服器端推理

儘管 Transformers.js 最初設計用於瀏覽器,但它也能夠在伺服器上執行推理。在本教程中,我們將設計一個簡單的 Node.js API,使用 Transformers.js 進行情感分析。

我們還將向您展示如何在 CommonJS 和 ECMAScript 模組中使用該庫,以便您可以選擇最適合您專案的模組系統。

  • ECMAScript 模組 (ESM) - 用於封裝可重用 JavaScript 程式碼的官方標準格式。它是現代瀏覽器中的預設模組系統,模組使用 import 匯入,使用 export 匯出。幸運的是,從 13.2.0 版本開始,Node.js 對 ES 模組有了穩定的支援。
  • CommonJS - Node.js 中的預設模組系統。在此係統中,模組使用 require() 匯入,使用 module.exports 匯出。

儘管您總是可以使用Python 庫進行伺服器端推理,但使用 Transformers.js 意味著您可以用 JavaScript 編寫所有程式碼(而無需設定和與獨立的 Python 程序通訊)。

實用連結

先決條件

入門

我們先建立一個新的 Node.js 專案,並透過 NPM 安裝 Transformers.js。

npm init -y
npm i @huggingface/transformers

接下來,建立一個名為 app.js 的新檔案,它將是我們應用程式的入口點。根據您使用的是 ECMAScript 模組還是 CommonJS,您需要做一些不同的事情(見下文)。

我們還將建立一個名為 MyClassificationPipeline 的輔助類來控制管道的載入。它使用單例模式,在首次呼叫 getInstance 時延遲建立一個唯一的管道例項,並在所有後續呼叫中使用此管道。

ECMAScript 模組 (ESM)

要表明您的專案使用 ECMAScript 模組,您需要在 package.json 中新增 "type": "module"

{
  ...
  "type": "module",
  ...
}

接下來,您需要在 app.js 的頂部新增以下匯入語句:

import http from 'http';
import querystring from 'querystring';
import url from 'url';

然後,讓我們匯入 Transformers.js 並定義 MyClassificationPipeline 類。

import { pipeline, env } from '@huggingface/transformers';

class MyClassificationPipeline {
  static task = 'text-classification';
  static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english';
  static instance = null;

  static async getInstance(progress_callback = null) {
    if (this.instance === null) {
      // NOTE: Uncomment this to change the cache directory
      // env.cacheDir = './.cache';

      this.instance = pipeline(this.task, this.model, { progress_callback });
    }

    return this.instance;
  }
}

CommonJS

首先在 app.js 的頂部新增以下匯入語句:

const http = require('http');
const querystring = require('querystring');
const url = require('url');

然後,讓我們匯入 Transformers.js 並定義 MyClassificationPipeline 類。由於 Transformers.js 是一個 ESM 模組,我們需要使用 import() 函式動態匯入該庫。

class MyClassificationPipeline {
  static task = 'text-classification';
  static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english';
  static instance = null;

  static async getInstance(progress_callback = null) {
    if (this.instance === null) {
      // Dynamically import the Transformers.js library
      let { pipeline, env } = await import('@huggingface/transformers');

      // NOTE: Uncomment this to change the cache directory
      // env.cacheDir = './.cache';

      this.instance = pipeline(this.task, this.model, { progress_callback });
    }

    return this.instance;
  }
}

建立一個基本的 HTTP 伺服器

接下來,讓我們使用內建的 HTTP 模組建立一個基本的伺服器。我們將監聽傳送到伺服器的請求(使用 /classify 端點),提取 text 查詢引數,並透過管道執行它。

// Define the HTTP server
const server = http.createServer();
const hostname = '127.0.0.1';
const port = 3000;

// Listen for requests made to the server
server.on('request', async (req, res) => {
  // Parse the request URL
  const parsedUrl = url.parse(req.url);

  // Extract the query parameters
  const { text } = querystring.parse(parsedUrl.query);

  // Set the response headers
  res.setHeader('Content-Type', 'application/json');

  let response;
  if (parsedUrl.pathname === '/classify' && text) {
    const classifier = await MyClassificationPipeline.getInstance();
    response = await classifier(text);
    res.statusCode = 200;
  } else {
    response = { 'error': 'Bad request' }
    res.statusCode = 400;
  }

  // Send the JSON response
  res.end(JSON.stringify(response));
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

由於我們使用延遲載入,第一次向伺服器發出的請求也將負責載入管道。如果您希望在伺服器啟動後立即開始載入管道,可以在定義 MyClassificationPipeline 後新增以下程式碼行:

MyClassificationPipeline.getInstance();

要啟動伺服器,請執行以下命令:

node app.js

伺服器應該在 http://127.0.0.1:3000/ 上線,您可以在您的 Web 瀏覽器中訪問它。您應該會看到以下訊息:

{"error":"Bad request"}

這是因為我們沒有使用有效的 text 查詢引數來訪問 /classify 端點。讓我們再試一次,這次使用一個有效的請求。例如,您可以訪問 http://127.0.0.1:3000/classify?text=I%20love%20Transformers.js,您應該會看到:

[{"label":"POSITIVE","score":0.9996721148490906}]

太棒了!我們已經成功建立了一個使用 Transformers.js 對文字進行分類的基本 HTTP 伺服器。

(可選)自定義

模型快取

預設情況下,當您第一次執行應用程式時,它會下載模型檔案並將其快取在您的檔案系統上(在 ./node_modules/@huggingface/transformers/.cache/ 中)。所有後續請求都將使用此模型。您可以透過設定 env.cacheDir 來更改快取的位置。例如,要將模型快取在當前工作目錄的 .cache 目錄中,您可以新增:

env.cacheDir = './.cache';

使用本地模型

如果您想使用本地模型檔案,可以按如下方式設定 env.localModelPath

// Specify a custom location for models (defaults to '/models/').
env.localModelPath = '/path/to/models/';

您還可以透過將 env.allowRemoteModels 設定為 false 來停用遠端模型的載入:

// Disable the loading of remote models from the Hugging Face Hub:
env.allowRemoteModels = false;
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.