开源翻译大模型

开源翻译大模型

1 简介

在开发过程中,会遇到定制化翻译工具的需要,开源的翻译模型可以解决相应的问题。其中英语转中文的比较好的开源项目有:

序号 组织 模型 地址 备注
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)

2 下载模型

下载模型的网站

# 国内代理huggingface的网站
https://hf-mirror.com/

# 下载大模型的网站
https://aliendao.cn

下载命令

# 安装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

3 项目实例

3.1 模型nllb-200-3.3

源代码

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)

在这里插入图片描述

3.2 模型mbart-large-50-one-to-many-mmt

安装依赖

# 安装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)

结果
在这里插入图片描述

3.3 模型opus-mt-en-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)

结果
在这里插入图片描述

3.4 模型SeamlessM4T

由于SeamlessM4T模型太大,此处借助HuggingFace上的模型运行。
在这里插入图片描述

相关推荐

  1. 开源模型源代码

    2024-03-31 19:22:02       5 阅读
  2. 开源语言模型简记

    2024-03-31 19:22:02       31 阅读
  3. 开源模型 Llama 3

    2024-03-31 19:22:02       19 阅读
  4. 盘点热门开源模型

    2024-03-31 19:22:02       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-31 19:22:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-31 19:22:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-31 19:22:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-31 19:22:02       20 阅读

热门阅读

  1. cs0449 c

    cs0449 c

    2024-03-31 19:22:02      19 阅读
  2. 每日一题 --- 替换数字[卡码][Go]

    2024-03-31 19:22:02       18 阅读
  3. js 扩展运算符(...)的用法

    2024-03-31 19:22:02       20 阅读
  4. 力扣爆刷第108天之CodeTop100五连刷26-30

    2024-03-31 19:22:02       16 阅读
  5. mysql用户与权限

    2024-03-31 19:22:02       15 阅读
  6. 汇总17个工作必备的Python自动化代码

    2024-03-31 19:22:02       16 阅读
  7. vue路由重定向

    2024-03-31 19:22:02       14 阅读
  8. CSS 滚动条样式修改

    2024-03-31 19:22:02       18 阅读
  9. 30. 串联所有单词的子串 —— LeetCode (python)

    2024-03-31 19:22:02       16 阅读