🚀 NanoTranslator-immersive_translate-365M
NanoTranslator-immersive_translate-365M 是一款专门为中英双语翻译设计的模型。它基于 NanoLM-365M-Base,使用来自 wmt-19 数据集的 600 万条数据进行训练,能有效实现中英双语间的准确翻译。
🚀 快速开始
NanoTranslator-immersive_translate-365M 专为中英双语翻译量身打造。它基于 NanoLM-365M-Base 模型,使用 wmt-19 数据集的 600 万条数据进行训练。该模型按照 Immersive Translate 的提示格式进行训练,可借助 vllm 和 lmdeploy 等工具以 OpenAI 格式接口进行部署和使用。
✨ 主要特性
- 双语翻译:专注于中英双语翻译,满足特定语言需求。
- 数据丰富:使用 600 万条来自 wmt-19 数据集的数据进行训练,保证翻译质量。
- 灵活部署:可按照 Immersive Translate 提示格式训练,借助 vllm 和 lmdeploy 等工具以 OpenAI 格式接口部署。
💻 使用示例
基础用法
以下是使用 transformers 调用该模型的方法,提示遵循沉浸式翻译格式以确保最佳效果。
import torch
from typing import Literal
from transformers import AutoModelForCausalLM, AutoTokenizer
model_path = 'Mxode/NanoTranslator-immersive_translate-365M'
model = AutoModelForCausalLM.from_pretrained(model_path).to('cuda:0', torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained(model_path)
def translate(
text: str,
to: Literal["chinese", "english"] = "chinese",
**kwargs
):
generation_args = dict(
max_new_tokens = kwargs.pop("max_new_tokens", 512),
do_sample = kwargs.pop("do_sample", True),
temperature = kwargs.pop("temperature", 0.35),
top_p = kwargs.pop("top_p", 0.8),
top_k = kwargs.pop("top_k", 40),
**kwargs
)
prompt = """Translate the following source text to {to}. Output translation directly without any additional text.
Source Text: {text}
Translated Text:"""
messages = [
{"role": "system", "content": "You are a professional, authentic machine translation engine."},
{"role": "user", "content": prompt.format(to=to, text=text)}
]
inputs = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([inputs], return_tensors="pt").to(model.device)
generated_ids = model.generate(model_inputs.input_ids, **generation_args)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
return response
text = "After a long day at work, I love to unwind by cooking a nice dinner and watching my favorite TV series. It really helps me relax and recharge for the next day."
response = translate(text=text, to='chinese')
print(f'Translation: {response}')
"""
Translation: 工作了一天,我喜欢吃一顿美味的晚餐,看我最喜欢的电视剧,这样做有助于我放松,补充能量。
"""
📄 许可证
本项目采用 GPL-3.0 许可证。