赞
踩
在开发过程中,会遇到定制化翻译工具的需要,开源的翻译模型可以解决相应的问题。其中英语转中文的比较好的开源项目有:
序号 | 组织 | 模型 | 地址 | 备注 |
---|---|---|---|---|
1 | 赫尔辛基大学语言技术研究小组(Language Technology Research Group at the University of Helsinki) | opus-mt-en-zh | 英文翻译为中文: https://hf-mirror.com/Helsinki-NLP/opus-mt-en-zh | 支持英文转中文;其他翻译模型在该项目下查找即可。 |
2 | facebook(Meta) | nllb-200 | https://hf-mirror.com/facebook/nllb-200-3.3B | nllb-200可以在200种语言之间进行单句翻译。它有多个参数的模型,推荐使用3.3B,其中600M翻译时会出现“预载载载载载载载载载载”的错误 |
3 | facebook(Meta) | mbart-large-50 | 英语翻译为其他语言: https://hf-mirror.com/facebook/mbart-large-50-one-to-many-mmt 多语言翻译为多语言: https://hf-mirror.com/facebook/mbart-large-50-many-to-many-mmt | mbart-large-50支持将50种语言翻译为其他多语言。 |
4 | facebook(Meta) | SeamlessM4T | 在线使用: https://hf-mirror.com/spaces/facebook/seamless-m4t-v2-large 仓库地址: https://hf-mirror.com/facebook/seamless-m4t-v2-large | Seamless M4T是一个一体化大规模多语言和多模式机器j基座翻译模型,提供近100种语言的语音和文本高质量翻译。可支持: 1.语音到语音翻译(S2ST) 2.语音到文本翻译(S2TT) 3.文本到语音翻译(T2ST) 4.文本到文本翻译(T2TT) 5.自动语音识别(ASR) |
下载模型的网站
# 国内代理huggingface的网站
https://hf-mirror.com/
# 下载大模型的网站
https://aifasthub.com/
下载命令
# 安装huggingface_hub,会在相应的环境中生成huggingface-cli
pip install -U huggingface_hub -i https://pypi.tuna.tsinghua.edu.cn/simple
# 进入到相应的目录下后,下载模型
huggingface-cli download --resume-download facebook/mbart-large-50-one-to-many-mmt --local-dir mbart-large-50-one-to-many-mmt --local-dir-use-symlinks False
源代码
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline # 加载模型 model = AutoModelForSeq2SeqLM.from_pretrained("D:/model/nllb-200-3.3B") tokenizer = AutoTokenizer.from_pretrained("D:/model/nllb-200-3.3B") # pipelines使用方法: https://hf-mirror.com/docs/transformers/main_classes/pipelines # 翻译语言的地址:https://hf-mirror.com/facebook/nllb-200-3.3B/blob/main/README.md # task:任务类型,translation表示翻译 # src_lang: 输入文本的语言,eng_Latn表示英文 # tgt_lang: 输出文本的语言,zho_Hans表示中文 # max_length: 输入文本最大长度; translator = pipeline( task='translation', model=model, tokenizer=tokenizer, src_lang='eng_Latn', tgt_lang='zho_Hans', max_length=512 ) # 文本 text_en = "Heart disease is a serious threat to human health. " text_zh = translator(text_en) print(text_zh)
安装依赖
# 安装sentencepiece
pip install sentencepiece -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装protobuf
pip install protobuf -i https://pypi.tuna.tsinghua.edu.cn/simple
源代码
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast # 加载模型 model = MBartForConditionalGeneration.from_pretrained( pretrained_model_name_or_path="D:/model/mbart-large-50-one-to-many-mmt" ) tokenizer = MBart50TokenizerFast.from_pretrained( pretrained_model_name_or_path="D:/model/mbart-large-50-one-to-many-mmt", src_lang="en_XX" ) # 序列化 text_en = "Heart disease is a serious threat to human health. " model_inputs = tokenizer(text_en, return_tensors="pt") # 将英语翻译成中文 generated_tokens = model.generate( **model_inputs, forced_bos_token_id=tokenizer.lang_code_to_id["zh_CN"] ) text_zh = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) print(text_zh)
结果
源代码
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from transformers import pipeline
# 加载模型
model = AutoModelForSeq2SeqLM.from_pretrained("D:/model/opus-mt-en-zh")
tokenizer = AutoTokenizer.from_pretrained("D:/model/opus-mt-en-zh")
# 创建 pipeline
translator = pipeline(task="translation", model=model, tokenizer=tokenizer)
text_en = "Heart disease is a serious threat to human health. "
text_zh = translator(text_en)
print(text_zh)
结果
由于SeamlessM4T模型太大,此处借助HuggingFace上的模型运行。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。