• Transformers.js v2.6 现已发布


    a18e484fe398d09a3eff649508484019.jpeg

    🤯 新增了 14 种架构

    在这次发布中,我们添加了大量的新架构:BLOOM、MPT、BeiT、CamemBERT、CodeLlama、GPT NeoX、GPT-J、HerBERT、mBART、mBART-50、OPT、ResNet、WavLM 和 XLM。这将支持架构的总数提升到了 46 个!以下是一些示例代码以帮助你入门:

    文本生成(MPT 模型):

    1. import { pipeline } from '@xenova/transformers';
    2. const generator = await pipeline('text-generation''Xenova/ipt-350m', {
    3.     quantized: false// 使用非量化以确保与 Python 版本匹配
    4. });
    5. const output = await generator('La nostra azienda');
    6. // { generated_text: "La nostra azienda è specializzata nella vendita di prodotti per l'igiene orale e per la salute." }

    其他文本生成模型:BLOOM、GPT-NeoX、CodeLlama、GPT-J、OPT。

    CamemBERT 用于遮罩语言模型、文本分类、令牌分类、问题回答和特征提取(模型)。例如:

    1. import { pipeline } from '@xenova/transformers';
    2. let pipe = await pipeline('token-classification''Xenova/camembert-ner-with-dates');
    3. let output = await pipe("Je m'appelle jean-baptiste et j'habite à montréal depuis fevr 2012");
    4. // [
    5. //   { entity: 'I-PER', score: 0.9258053302764893, index: 5, word: 'jean' },
    6. //   { entity: 'I-PER', score: 0.9048717617988586, index: 6, word: '-' },
    7. //   { entity: 'I-PER', score: 0.9227054119110107, index: 7, word: 'ba' },
    8. //   { entity: 'I-PER', score: 0.9385354518890381, index: 8, word: 'pt' },
    9. //   { entity: 'I-PER', score: 0.9139659404754639, index: 9, word: 'iste' },
    10. //   { entity: 'I-LOC', score: 0.9877734780311584, index: 15, word: 'montré' },
    11. //   { entity: 'I-LOC', score: 0.9891639351844788, index: 16, word: 'al' },
    12. //   { entity: 'I-DATE', score: 0.9858269691467285, index: 18, word: 'fe' },
    13. //   { entity: 'I-DATE', score: 0.9780661463737488, index: 19, word: 'vr' },
    14. //   { entity: 'I-DATE', score: 0.980688214302063, index: 20, word: '2012' }
    15. // ]

    WavLM 用于特征提取(模型)。例如:

    1. import { AutoProcessor, AutoModel, read_audio } from '@xenova/transformers';
    2. // Read and preprocess audio
    3. const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base');
    4. const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'16000);
    5. const inputs = await processor(audio);
    6. // Run model with inputs
    7. const model = await AutoModel.from_pretrained('Xenova/wavlm-base');
    8. const output = await model(inputs);
    9. // {
    10. //   last_hidden_state: Tensor {
    11. //     dims: [ 1, 549, 768 ],
    12. //     type: 'float32',
    13. //     data: Float32Array(421632) [-0.349443256855011, -0.39341306686401367,  0.022836603224277496, ...],
    14. //     size: 421632
    15. //   }
    16. // }

    mBART 和 mBart50 用于多语言翻译(模型)。例如:

    1. import { pipeline } from '@xenova/transformers';
    2. let translator = await pipeline('translation''Xenova/mbart-large-50-many-to-many-mmt');
    3. let output = await translator('संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है', {
    4.   src_lang: 'hi_IN'// 印地语
    5.   tgt_lang: 'fr_XX'// 法语
    6. });
    7. // [{ translation_text: 'Le chef des Nations affirme qu 'il n 'y a military solution in Syria.' }]

    BeiT 用于图像分类(模型):

    1. import { pipeline } from '@xenova/transformers';
    2. let url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
    3. let pipe = await pipeline('image-classification''Xenova/beit-base-patch16-224');
    4. let output = await pipe(url);
    5. // [{ label: 'tiger, Panthera tigris', score: 0.7168469429016113 }]

    ResNet 用于图像分类(模型):

    1. import { pipeline } from '@xenova/transformers';
    2. let url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
    3. let pipe = await pipeline('image-classification''Xenova/resnet-50');
    4. let output = await pipe(url);
    5. // [{ label: 'tiger, Panthera tigris', score: 0.7576608061790466 }]

    😍 超过 150 个新转换的模型

    为了开始使用这些新架构(以及扩展其他模型的覆盖范围),我们在 Hugging Face Hub 上发布了超过 150 个新模型!点击这里查看完整列表。https://huggingface.co/models?library=transformers.js

    🏋️ 模型大小大幅度减少(最多 -40%)

    借助 🤗 Optimum 的最新更新,我们能够在各种模型中去除重复的权重。在某些情况下,比如 whisper-tiny 的解码器,这导致了大小减少 40%!以下是我们看到的一些改进:

    • Whisper-tiny 解码器:50MB → 30MB(-40%)

    • NLLB 解码器:732MB → 476MB(-35%)

    • bloom:819MB → 562MB(-31%)

    • T5 解码器:59MB → 42MB(-28%)

    • distilbert-base:91MB → 68MB(-25%)

    • bart-base 解码器:207MB → 155MB(-25%)

    • roberta-base:165MB → 126MB(-24%)

    • gpt2:167MB → 127MB(-24%)

    • bert-base:134MB → 111MB(-17%)

    • 还有更多!

    在这里尝试一些较小的 whisper 模型(用于自动语音识别)。

    5601dc509415ea5c02b4f10f16185477.gif

    其他更新

    文档更新,加入了 Transformers.js 与 LangChain JS 的集成。

    文档页面:https://js.langchain.com/docs/modules/data_connection/text_embedding/integrations/transformers

    1. import { HuggingFaceTransformersEmbeddings } from "langchain/embeddings/hf_transformers";
    2. const model = new HuggingFaceTransformersEmbeddings({
    3.   modelName: "Xenova/all-MiniLM-L6-v2",
    4. });
    5. /* Embed queries */
    6. const res = await model.embedQuery(
    7.   "What would be a good company name for a company that makes colorful socks?"
    8. );
    9. console.log({ res });
    10. /* Embed documents */
    11. const documentRes = await model.embedDocuments(["Hello world""Bye bye"]);
    12. console.log({ documentRes });

    重构了 PreTrainedModel 以在添加新模型时显著减少代码量。

  • 相关阅读:
    判断用户的当前的浏览器
    SpringBoot 日志文件
    ubuntu配置yolov5环境
    小柏实战学习Liunx(图文教程二十二)
    第七十天 c++perimer70~115string类型总结
    js基础笔记学习28-逻辑与和或
    MFCC--学习笔记
    机器学习之特征提取
    【同花顺】同花顺解题
    什么是app测试,什么是web测试,app测试和web测试有什么区别
  • 原文地址:https://blog.csdn.net/HuggingFace/article/details/133287633