当前位置:   article > 正文

中文译英文 模型_helsinki-nlp 中英文互译

helsinki-nlp 中英文互译

Helsinki-NLP/opus-mt-zh-en · Hugging FaceWe’re on a journey to advance and democratize artificial intelligence through open source and open science.https://huggingface.co/Helsinki-NLP/opus-mt-zh-en?text=%E6%88%91%E5%8F%AB%E6%B2%83%E5%B0%94%E5%A4%AB%E5%86%88%EF%BC%8C%E6%88%91%E4%BD%8F%E5%9C%A8%E6%9F%8F%E6%9E%97%E3%80%82NLP(四十一)使用HuggingFace翻译模型的一次尝试_huggingface 翻译_山阴少年的博客-CSDN博客  本文将如何如何使用HuggingFace中的翻译模型。  HuggingFace是NLP领域中响当当的团体,它在预训练模型方面作出了很多接触的工作,并开源了许多预训练模型和已经针对具体某个NLP人物训练好的直接可以使用的模型。本文将使用HuggingFace提供的可直接使用的翻译模型。  HuggingFace的翻译模型可参考网址:https://huggingface.co/models?pipeline_tag=translation ,该部分模型中的绝大部分是由Helsinki-NLP(Lanhttps://blog.csdn.net/jclian91/article/details/114647084

  1. # -*- coding: utf-8 -*-
  2. import sys
  3. sys.path.append("/home/sniss/local_disk/stable_diffusion_api/")
  4. from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
  5. tokenizer = AutoTokenizer.from_pretrained("/home/sniss/local_disk/stable_diffusion_api/models/opus-mt-zh-en")
  6. model = AutoModelForSeq2SeqLM.from_pretrained("/home/sniss/local_disk/stable_diffusion_api/models/opus-mt-zh-en")
  7. def translation_zh_en(text):
  8. # Tokenize the text
  9. batch = tokenizer.prepare_seq2seq_batch(src_texts=[text], return_tensors='pt', max_length=512)
  10. # batch = tokenizer.prepare_seq2seq_batch(src_texts=[text])
  11. # Make sure that the tokenized text does not exceed the maximum
  12. # allowed size of 512
  13. # import pdb;pdb.set_trace()
  14. # batch["input_ids"] = batch["input_ids"][:, :512]
  15. # batch["attention_mask"] = batch["attention_mask"][:, :512]
  16. # Perform the translation and decode the output
  17. translation = model.generate(**batch)
  18. result = tokenizer.batch_decode(translation, skip_special_tokens=True)
  19. return result
  20. if __name__ == "__main__":
  21. text = "从时间上看,中国空间站的建造比国际空间站晚20多年。"
  22. result = translation_zh_en(text)
  23. print(result)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/362129
推荐阅读
相关标签
  

闽ICP备14008679号