使用模型bigscience/mt0-large实现中文到英文的翻译

cpu版本

from transformers import AutoTokenizer, AutoModel
from huggingface_hub.hf_api import HfFolder

HfFolder.save_token('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')
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer,MT5ForConditionalGeneration
checkpoint = "bigscience/mt0-large"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
inputs = tokenizer.encode("Translate to English: 泰卢固语具有丰富的音系,包括元音、辅音和复合音", return_tensors="pt")
outputs = model.generate(inputs,max_new_tokens=200)
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(decoded)



输出:

Turkish has a wide variety of sounds, including sounds of the alphabet, sounds of the alphabet, and sounds of the combination of sounds

也可以使用pipeline:

from transformers import AutoTokenizer, AutoModel,pipeline
from huggingface_hub.hf_api import HfFolder

HfFolder.save_token('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')
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer,MT5ForConditionalGeneration
checkpoint = "bigscience/mt0-large"
classifier = pipeline(model=checkpoint,max_new_tokens=2000)
print(classifier("Translate to English: 泰卢固语具有丰富的音系,包括元音、辅音和复合音"))
print(classifier("一个传奇的开端,一个不灭的神话,这不仅仅是一部电影,而是作为一个走进新时代的标签,永远彪炳史册。你认为这句话的立场是赞扬、中立还是批评"))
print(classifier("写一首有关草莓的故事,不少于100字"))


  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值