当前位置:   article > 正文

使用模型Helsinki-NLP/opus-mt-en-zh实现英译中

opus-mt-en-zh

google codlab 运行需要5秒

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

HfFolder.save_token('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')

text ="Memphis is a series of models which advance human-data models, offering good performance without relying on proprietary model outputs"
mname ='Helsinki-NLP/opus-mt-en-zh'
tokenizer = AutoTokenizer.from_pretrained(mname)
model = MarianMTModel.from_pretrained(mname)
input_ids = tokenizer.encode(text, return_tensors="pt")
outputs = model.generate(input_ids)
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(decoded)#Nice to meet you
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

也可以使用pipeline

from transformers import AutoModel , AutoTokenizer,MarianMTModel
from transformers import pipeline

text ="Memphis is a series of models which advance human-data models, offering good performance without relying on proprietary model outputs"
mname ='Helsinki-NLP/opus-mt-en-zh'
translator = pipeline(model=mname)
print(translator(text)[0].get("generated_text"))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号