Variable65536 commited on
Commit
dd1d1cb
·
verified ·
1 Parent(s): 973646a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +236 -1
README.md CHANGED
@@ -1,3 +1,238 @@
 
1
  ---
2
- license: apache-2.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
  ---
3
+ language:
4
+ - en
5
+ - zh
6
+ base_model: openbmb/MiniCPM5-1B
7
+ library_name: transformers
8
+ pipeline_tag: translation
9
+ tags:
10
+ - minicpm
11
+ - translation
12
+ - english-to-chinese
13
+ - immersive-translate
14
+ - lora
15
+ - fine-tuned
16
+ - text-generation
17
+ license: other
18
+ datasets:
19
+ - Variable65536/immersive_translate_en-zh
20
  ---
21
+
22
+ # MiniCPM5-1B Immersive Translate
23
+
24
+ 英译中专用翻译模型,基于 [`MiniCPM5-1B-Base`](https://huggingface.co/openbmb/MiniCPM5-1B) 微调,面向 [沉浸式翻译](https://immersivetranslate.com/) 插件场景优化。已合并 LoRA 权重,可直接使用 Transformers 加载。
25
+
26
+ ## 模型概览
27
+
28
+ | 项目 | 说明 |
29
+ |---|---|
30
+ | 基座模型 | `openbmb/MiniCPM5-1B-Base` |
31
+ | 微调数据 | [`Variable65536/immersive_translate_en-zh`](https://huggingface.co/datasets/Variable65536/immersive_translate_en-zh)(18,228 条 SFT 样本) |
32
+ | 微调方法 | LoRA(r=16, alpha=32, target=all) |
33
+ | 训练轮数 | 2 epoch |
34
+ | 最终 eval_loss | 1.1135 |
35
+ | 硬件 | Tesla P100 16GB |
36
+ | 权重格式 | safetensors(LoRA 已合并) |
37
+ | 量化版本 | 见 [`Variable65536/minicpm5-1b-immersive-translate-gguf`](https://huggingface.co/Variable65536/minicpm5-1b-immersive-translate-gguf) |
38
+
39
+ ## 快速开始
40
+
41
+ ### 安装依赖
42
+
43
+ ```bash
44
+ pip install transformers torch accelerate
45
+ ```
46
+
47
+ ### 加载模型
48
+
49
+ ```python
50
+ import torch
51
+ from transformers import AutoModelForCausalLM, AutoTokenizer
52
+
53
+ model_path = "Variable65536/minicpm5-immersive-translate"
54
+
55
+ model = AutoModelForCausalLM.from_pretrained(
56
+ model_path,
57
+ torch_dtype=torch.float16,
58
+ device_map="auto",
59
+ )
60
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
61
+ ```
62
+
63
+ ### 推理示例
64
+
65
+ 模型训练时使用与沉浸式翻译插件完全对齐的 system prompt 和 user prompt 格式,推理时需保持一致。
66
+
67
+ ```python
68
+ SYSTEM_PROMPT = """You are a professional Chinese native translator who needs to fluently translate text into Chinese.
69
+
70
+ ## Translation Rules
71
+ 1. Output only the translated content, without explanations or additional content (such as "Here's the translation:" or "Translation as follows:")
72
+ 2. The returned translation must maintain exactly the same number of paragraphs and format as the original text
73
+ 3. If the text contains HTML tags, consider where the tags should be placed in the translation while maintaining fluency
74
+ 4. For content that should not be translated (such as proper nouns, code, etc.), keep the original text.
75
+ 5. If input contains %%, use %% in your output, if input has no %%, don't use %% in your output
76
+
77
+ ## OUTPUT FORMAT:
78
+ - **Single paragraph input** → Output translation directly (no separators, no extra text)
79
+ - **Multi-paragraph input** → Use %% as paragraph separator between translations
80
+ """
81
+
82
+ # 单段输入
83
+ user_input = "Translate to Chinese (output translation only):\n\nThe committee approved the proposal after extensive deliberation."
84
+
85
+ messages = [
86
+ {"role": "system", "content": SYSTEM_PROMPT},
87
+ {"role": "user", "content": user_input},
88
+ ]
89
+
90
+ input_ids = tokenizer.apply_chat_template(
91
+ messages,
92
+ tokenize=True,
93
+ add_generation_prompt=True,
94
+ return_tensors="pt",
95
+ return_dict=False,
96
+ ).to(model.device)
97
+
98
+ outputs = model.generate(
99
+ input_ids,
100
+ max_new_tokens=512,
101
+ temperature=0.2,
102
+ top_p=0.9,
103
+ do_sample=True,
104
+ )
105
+
106
+ result = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
107
+ print(result)
108
+ # 输出:委员会经过充分讨论后批准了该提案。
109
+ ```
110
+
111
+ ### 多段输入示例
112
+
113
+ ```python
114
+ user_input = """Translate to Chinese:
115
+
116
+ ## Installation
117
+
118
+ Run `pip install minicpm` to install the package.
119
+
120
+ %%
121
+
122
+ ## Usage
123
+
124
+ See the <a href="https://github.com/OpenBMB/MiniCPM">repo</a> for examples.
125
+ """
126
+ ```
127
+
128
+ 模型会输出对应中文译文,并保留 `%%` 分隔符、代码块和 HTML 标签。
129
+
130
+ ## 提示词格式
131
+
132
+ ### System Prompt
133
+
134
+ ```
135
+ You are a professional Chinese native translator who needs to fluently translate text into Chinese.
136
+
137
+ ## Translation Rules
138
+ 1. Output only the translated content, without explanations or additional content (such as "Here's the translation:" or "Translation as follows:")
139
+ 2. The returned translation must maintain exactly the same number of paragraphs and format as the original text
140
+ 3. If the text contains HTML tags, consider where the tags should be placed in the translation while maintaining fluency
141
+ 4. For content that should not be translated (such as proper nouns, code, etc.), keep the original text.
142
+ 5. If input contains %%, use %% in your output, if input has no %%, don't use %% in your output
143
+
144
+ ## OUTPUT FORMAT:
145
+ - **Single paragraph input** → Output translation directly (no separators, no extra text)
146
+ - **Multi-paragraph input** → Use %% as paragraph separator between translations
147
+ ```
148
+
149
+ ### 单段 User Prompt
150
+
151
+ ```
152
+ Translate to Chinese (output translation only):
153
+
154
+ {英文原文}
155
+ ```
156
+
157
+ ### 多段 User Prompt
158
+
159
+ ```
160
+ Translate to Chinese:
161
+
162
+ {英文段落 1}
163
+
164
+ %%
165
+
166
+ {英文段落 2}
167
+ ```
168
+
169
+ ### 推荐推理参数
170
+
171
+ | 参数 | 值 |
172
+ |---|---|
173
+ | temperature | 0.2 |
174
+ | top_p | 0.9 |
175
+ | max_new_tokens | 512 |
176
+ | repetition_penalty | 1.0 |
177
+
178
+ 温度建议设在 **0.1~0.3** 之间,翻译任务不需要高随机性。
179
+
180
+ ## 模型能力
181
+
182
+ 训练数据覆盖以下场景,模型在这些任务上表现良好:
183
+
184
+ - **技术文档**:GitHub README、Hugging Face 模型卡片、软件文档
185
+ - **学术摘要**:arXiv 论文摘要英译中
186
+ - **格式保留**:代码块(` ``` `)、行内代码(`` `code` ``)、HTML 标签、URL、Markdown 标题
187
+ - **多段翻译**:使用 `%%` 分隔段落,输入输出段落数严格一致
188
+ - **专有名词**:GitHub、Git、Microsoft 等保留原文不译
189
+
190
+ ## 与沉浸式翻译插件配合使用
191
+
192
+ 推荐通过 **vLLM** 部署为 OpenAI 兼容 API:
193
+
194
+ ```bash
195
+ pip install vllm
196
+ python -m vllm.entrypoints.openai.api_server \
197
+ --model Variable65536/minicpm5-immersive-translate \
198
+ --served-model-name minicpm5-immersive \
199
+ --port 8000 \
200
+ --dtype float16 \
201
+ --max-model-len 4096
202
+ ```
203
+
204
+ 然后在沉浸式翻译插件中配置:
205
+
206
+ | 字段 | 值 |
207
+ |---|---|
208
+ | API URL | `http://localhost:8000/v1/chat/completions` |
209
+ | API Key | 任意值(本地服务不校验) |
210
+ | 模型 | `minicpm5-immersive` |
211
+
212
+ 插件会自动发送其内置的 system prompt,与本模型训练时使用的格式一致。
213
+
214
+ ## 已知限制
215
+
216
+ - **纯代码块段落**:当多段输入中存在仅含代码块的段落时,模型可能将其与相邻段落合并,导致 `%%` 数量不一致。实际使用中插件通常会剥离代码块,影响较小。
217
+ - **维基百科信息框字段**:如 `Parent`、`Founded`、`Industry` 等字段的翻译可能不准确,训练数据未覆盖此类结构化字段。
218
+ - **复杂从句语序**:个别 `after`、`before` 等时间状语从句的语序可能出错。
219
+ - **合成数据风险**:训练数据中 BiST 部分的中文译文为 LLM 合成,可能继承源模型的翻译偏好。
220
+
221
+ ## 引用
222
+
223
+ 如果使用本模型,请同时引用原始数据源及 MiniCPM5:
224
+
225
+ ```bibtex
226
+ @misc{minicpm5,
227
+ title={MiniCPM5},
228
+ author={OpenBMB},
229
+ year={2025},
230
+ howpublished={\url{https://huggingface.co/openbmb/MiniCPM5-1B}}
231
+ }
232
+ ```
233
+
234
+ ## 致谢
235
+
236
+ - [OpenBMB](https://github.com/OpenBMB) 提供 MiniCPM5-1B 基座模型
237
+ - [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory) 提供微调框架
238
+ - 各源数据集作者与维护者