Variable65536/immersive_translate_en-zh
Viewer • Updated • 18.2k • 78
How to use Variable65536/minicpm5-1b-immersive-translate with Transformers:
# Use a pipeline as a high-level helper
# Warning: Pipeline type "translation" is no longer supported in transformers v5.
# You must load the model directly (see below) or downgrade to v4.x with:
# 'pip install "transformers<5.0.0'
from transformers import pipeline
pipe = pipeline("translation", model="Variable65536/minicpm5-1b-immersive-translate") # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("Variable65536/minicpm5-1b-immersive-translate")
model = AutoModelForCausalLM.from_pretrained("Variable65536/minicpm5-1b-immersive-translate", device_map="auto")英译中专用翻译模型,基于 MiniCPM5-1B-Base 微调,面向 沉浸式翻译 插件场景优化。已合并 LoRA 权重,可直接使用 Transformers 加载。
| 项目 | 说明 |
|---|---|
| 基座模型 | openbmb/MiniCPM5-1B-Base |
| 微调数据 | Variable65536/immersive_translate_en-zh(18,228 条 SFT 样本) |
| 微调方法 | LoRA(r=16, alpha=32, target=all) |
| 训练轮数 | 2 epoch |
| 最终 eval_loss | 1.1135 |
| 硬件 | Tesla P100 16GB |
| 权重格式 | safetensors(LoRA 已合并) |
| 量化版本 | 见 Variable65536/minicpm5-1b-immersive-translate-gguf |
pip install transformers torch accelerate
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_path = "Variable65536/minicpm5-immersive-translate"
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float16,
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(model_path)
模型训练时使用与沉浸式翻译插件完全对齐的 system prompt 和 user prompt 格式,推理时需保持一致。
SYSTEM_PROMPT = """You are a professional Chinese native translator who needs to fluently translate text into Chinese.
## Translation Rules
1. Output only the translated content, without explanations or additional content (such as "Here's the translation:" or "Translation as follows:")
2. The returned translation must maintain exactly the same number of paragraphs and format as the original text
3. If the text contains HTML tags, consider where the tags should be placed in the translation while maintaining fluency
4. For content that should not be translated (such as proper nouns, code, etc.), keep the original text.
5. If input contains %%, use %% in your output, if input has no %%, don't use %% in your output
## OUTPUT FORMAT:
- **Single paragraph input** → Output translation directly (no separators, no extra text)
- **Multi-paragraph input** → Use %% as paragraph separator between translations
"""
# 单段输入
user_input = "Translate to Chinese (output translation only):\n\nThe committee approved the proposal after extensive deliberation."
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_input},
]
input_ids = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt",
return_dict=False,
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=512,
temperature=0.2,
top_p=0.9,
do_sample=True,
)
result = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
print(result)
# 输出:委员会经过充分讨论后批准了该提案。
user_input = """Translate to Chinese:
## Installation
Run `pip install minicpm` to install the package.
%%
## Usage
See the <a href="https://github.com/OpenBMB/MiniCPM">repo</a> for examples.
"""
模型会输出对应中文译文,并保留 %% 分隔符、代码块和 HTML 标签。
You are a professional Chinese native translator who needs to fluently translate text into Chinese.
## Translation Rules
1. Output only the translated content, without explanations or additional content (such as "Here's the translation:" or "Translation as follows:")
2. The returned translation must maintain exactly the same number of paragraphs and format as the original text
3. If the text contains HTML tags, consider where the tags should be placed in the translation while maintaining fluency
4. For content that should not be translated (such as proper nouns, code, etc.), keep the original text.
5. If input contains %%, use %% in your output, if input has no %%, don't use %% in your output
## OUTPUT FORMAT:
- **Single paragraph input** → Output translation directly (no separators, no extra text)
- **Multi-paragraph input** → Use %% as paragraph separator between translations
Translate to Chinese (output translation only):
{英文原文}
Translate to Chinese:
{英文段落 1}
%%
{英文段落 2}
| 参数 | 值 |
|---|---|
| temperature | 0.2 |
| top_p | 0.9 |
| max_new_tokens | 512 |
| repetition_penalty | 1.0 |
温度建议设在 0.1~0.3 之间,翻译任务不需要高随机性。
训练数据覆盖以下场景,模型在这些任务上表现良好:
```)、行内代码(`code`)、HTML 标签、URL、Markdown 标题%% 分隔段落,输入输出段落数严格一致推荐通过 vLLM 部署为 OpenAI 兼容 API:
pip install vllm
python -m vllm.entrypoints.openai.api_server \
--model Variable65536/minicpm5-immersive-translate \
--served-model-name minicpm5-immersive \
--port 8000 \
--dtype float16 \
--max-model-len 4096
然后在沉浸式翻译插件中配置:
| 字段 | 值 |
|---|---|
| API URL | http://localhost:8000/v1/chat/completions |
| API Key | 任意值(本地服务不校验) |
| 模型 | minicpm5-immersive |
插件会自动发送其内置的 system prompt,与本模型训练时使用的格式一致。
%% 数量不一致。实际使用中插件通常会剥离代码块,影响较小。Parent、Founded、Industry 等字段的翻译可能不准确,训练数据未覆盖此类结构化字段。after、before 等时间状语从句的语序可能出错。如果使用本模型,请同时引用原始数据源及 MiniCPM5:
@misc{minicpm5,
title={MiniCPM5},
author={OpenBMB},
year={2025},
howpublished={\url{https://huggingface.co/openbmb/MiniCPM5-1B}}
}
Base model
openbmb/MiniCPM5-1B