nllb-200-distilled-600M模型部署

本地加载

环境依赖:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple transformer sentencepiece
pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 --index-url https://download.pytorch.org/whl/cu124

模型下载:
https://hf-mirror.com/facebook/nllb-200-distilled-600M
支持的语言:
https://hf-mirror.com/facebook/nllb-200-distilled-600M/blob/main/README.md

from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("/data/models/facebook/nllb-200-distilled-600M")
model = AutoModelForSeq2SeqLM.from_pretrained("/data/models/facebook/nllb-200-distilled-600M")

content = """
A database of Chinese surnames and Chinese given names (1930-2008). This database contains nationwide frequency statistics of 1,806 Chinese surnames and 2,614 Chinese characters used in given names, covering about 1.2 billion Han Chinese population (96.8% of the Han Chinese household-registered population born from 1930 to 2008 and still alive in 2008). This package also contains a function for computing multiple features of Chinese surnames and Chinese given names for scientific research (e.g., name uniqueness, name gender, name valence, and name warmth/competence).
"""

inputs = tokenizer(content, return_tensors="pt")
translated_tokens = model.generate(
    **inputs, forced_bos_token_id=tokenizer.convert_tokens_to_ids("zho_Hans"), num_beams=4,
)
for translated in tokenizer.batch_decode(translated_tokens, skip_special_tokens=True):
    print(translated)

拆分长文本

sudo apt-get install -y g++
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple stopes[mono] botok khmer-nltk laonlp
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from stopes.pipelines.monolingual.utils.sentence_split import get_split_algo

tokenizer = AutoTokenizer.from_pretrained("/data/models/facebook/nllb-200-distilled-600M")
model = AutoModelForSeq2SeqLM.from_pretrained("/data/models/facebook/nllb-200-distilled-600M")

content = """
A database of Chinese surnames and Chinese given names (1930-2008). This database contains nationwide frequency statistics of 1,806 Chinese surnames and 2,614 Chinese characters used in given names, covering about 1.2 billion Han Chinese population (96.8% of the Han Chinese household-registered population born from 1930 to 2008 and still alive in 2008). This package also contains a function for computing multiple features of Chinese surnames and Chinese given names for scientific research (e.g., name uniqueness, name gender, name valence, and name warmth/competence).
"""


# now split the content into individual sentences, just as NLLB was supposed to work!
splitter = get_split_algo("eng", "default")
input_sentences = list(splitter(content))
print(len(input_sentences))  # 3

inputs = tokenizer(input_sentences, return_tensors="pt", padding=True)
translated_tokens = model.generate(
    **inputs, forced_bos_token_id=tokenizer.convert_tokens_to_ids("zho_Hans"), num_beams=4,
)
for translated in tokenizer.batch_decode(translated_tokens, skip_special_tokens=True):
    print(translated)

cuda加载

import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

# 检查CUDA是否可用
if not torch.cuda.is_available():
    raise RuntimeError("CUDA is not available on this machine.")

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

tokenizer = AutoTokenizer.from_pretrained("/data/models/facebook/nllb-200-distilled-600M")
model = AutoModelForSeq2SeqLM.from_pretrained("/data/models/facebook/nllb-200-distilled-600M")

# 将模型移动到GPU
model.to(device)

content = """
A database of Chinese surnames and Chinese given names (1930-2008). This database contains nationwide frequency statistics of 1,806 Chinese surnames and 2,614 Chinese characters used in given names, covering about 1.2 billion Han Chinese population (96.8% of the Han Chinese household-registered population born from 1930 to 2008 and still alive in 2008). This package also contains a function for computing multiple features of Chinese surnames and Chinese given names for scientific research (e.g., name uniqueness, name gender, name valence, and name warmth/competence).
"""

# 对输入进行编码,并将结果张量移动到GPU
inputs = tokenizer(content, return_tensors="pt").to(device)

translated_tokens = model.generate(
    **inputs, forced_bos_token_id=tokenizer.convert_tokens_to_ids("zho_Hans"), num_beams=4,
)

# 将生成的token解码为文本
for translated in tokenizer.batch_decode(translated_tokens, skip_special_tokens=True):
    print(translated)

del inputs
torch.cuda.empty_cache()

pipeline

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline

tokenizer = AutoTokenizer.from_pretrained("/data/models/facebook/nllb-200-distilled-600M")
model = AutoModelForSeq2SeqLM.from_pretrained("/data/models/facebook/nllb-200-distilled-600M")

translator = pipeline(
    'translation',
    model=model,
    tokenizer=tokenizer,
    src_lang='zho_Hans',
    tgt_lang='eng_Latn',
    max_length=512
)
print(translator(["你好 世界", "青春", ]))
### 部署NLLB翻译模型的方法 对于希望部署NLLB(No Language Left Behind)翻译模型的开发者而言,了解其具体操作流程至关重要。Facebook AI Research开发了这个多语言机器翻译模型来支持超过200种低资源语言之间的互译[^1]。 #### 准备工作环境 为了顺利部署NLLB模型,需先安装必要的依赖库并下载预训练好的权重文件。通常情况下会建议使用Python虚拟环境隔离项目所需的包版本: ```bash conda create -n nllb python=3.8 conda activate nllb pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu pip install transformers sentencepiece sacrebleu ``` #### 加载预训练模型 通过Hugging Face提供的`transformers`库可以方便地加载官方发布的多个不同规模大小版本之一作为基础架构: ```python from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M") model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M") ``` #### 实现简单的推理接口 创建一个函数用于接收待处理文本输入,并返回经过转换后的目标语言输出字符串形式的结果: ```python def translate(text, target_lang="eng_Latn"): inputs = tokenizer([text], return_tensors="pt", truncation=True, max_length=512) outputs = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[target_lang]) translated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] return translated_text ``` 上述代码片段展示了如何利用已有的API快速搭建起能够执行跨语系即时在线服务的基础框架[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝净云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值