Mxode commited on
Commit
244f223
1 Parent(s): bd3b5dd

upload model

Browse files
README.md CHANGED
@@ -1,3 +1,71 @@
1
  ---
2
  license: gpl-3.0
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: gpl-3.0
3
  ---
4
+ # NanoTranslator-365M-immersive_translate-v1
5
+
6
+ ## Introduction
7
+
8
+ NanoTranslator-365M-immersive_translate-v1 is a translation model specifically designed for **Chinese-English bilingual** translation, trained with 200M data from the [wmt-19](https://huggingface.co/datasets/wmt/wmt19) dataset, based on [NanoLM-365M-Base](https://huggingface.co/Mxode/NanoLM-365M-Base).
9
+
10
+ This model is trained following the Immersive Translate prompt format and can be deployed as an OpenAI format interface using tools like vllm and lmdeploy for utilization.
11
+
12
+ ## How to use
13
+
14
+ Below is a method to call the model using transformers. The prompt follows the immersive translation format to ensure optimal results.
15
+
16
+ ```python
17
+ import torch
18
+ from typing import Literal
19
+ from transformers import AutoModelForCausalLM, AutoTokenizer
20
+
21
+ model_path = 'Mxode/NanoTranslator-365M-immersive_translate-v1'
22
+
23
+ model = AutoModelForCausalLM.from_pretrained(model_path).to('cuda:0', torch.bfloat16)
24
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
25
+
26
+ def translate(
27
+ text: str,
28
+ to: Literal["chinese", "english"] = "chinese",
29
+ **kwargs
30
+ ):
31
+ generation_args = dict(
32
+ max_new_tokens = kwargs.pop("max_new_tokens", 512),
33
+ do_sample = kwargs.pop("do_sample", True),
34
+ temperature = kwargs.pop("temperature", 0.35),
35
+ top_p = kwargs.pop("top_p", 0.8),
36
+ top_k = kwargs.pop("top_k", 40),
37
+ **kwargs
38
+ )
39
+
40
+ prompt = """Translate the following source text to {to}. Output translation directly without any additional text.
41
+ Source Text: {text}
42
+
43
+ Translated Text:"""
44
+
45
+ messages = [
46
+ {"role": "system", "content": "You are a professional, authentic machine translation engine."},
47
+ {"role": "user", "content": prompt.format(to=to, text=text)}
48
+ ]
49
+ inputs = tokenizer.apply_chat_template(
50
+ messages,
51
+ tokenize=False,
52
+ add_generation_prompt=True
53
+ )
54
+ model_inputs = tokenizer([inputs], return_tensors="pt").to(model.device)
55
+
56
+ generated_ids = model.generate(model_inputs.input_ids, **generation_args)
57
+ generated_ids = [
58
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
59
+ ]
60
+
61
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
62
+ return response
63
+
64
+ 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."
65
+ response = translate(text=text, to='chinese')
66
+ print(f'Translation: {response}')
67
+
68
+ """
69
+ Translation: 工作了一天,我喜欢吃一顿美味的晚餐,看我最喜欢的电视剧,这样做有助于我放松,补充能量。
70
+ """
71
+ ```
README_zh-CN.md ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # NanoTranslator-365M-immersive_translate-v1
2
+
3
+ ## Introduction
4
+
5
+ NanoTranslator-365M-immersive_translate-v1 是由 [NanoLM-365M-Base](https://huggingface.co/Mxode/NanoLM-365M-Base) 在 [wmt-19](https://huggingface.co/datasets/wmt/wmt19) 数据集上训练了 200M 数据得来的专门用于**中英双语**的翻译模型。
6
+
7
+ 此模型遵循沉浸式翻译(Immersive Translate)的 prompt 格式进行训练,可以通过 vllm、lmdeploy 等方式部署为 OpenAI 格式接口,从而完成调用。
8
+
9
+ ## How to use
10
+
11
+ 下面是一个用 transformers 调用的方式,prompt 遵循沉浸式翻译以保持最佳效果。
12
+
13
+ ```python
14
+ import torch
15
+ from typing import Literal
16
+ from transformers import AutoModelForCausalLM, AutoTokenizer
17
+
18
+ model_path = 'Mxode/NanoTranslator-365M-immersive_translate-v1'
19
+
20
+ model = AutoModelForCausalLM.from_pretrained(model_path).to('cuda:0', torch.bfloat16)
21
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
22
+
23
+ def translate(
24
+ text: str,
25
+ to: Literal["chinese", "english"] = "chinese",
26
+ **kwargs
27
+ ):
28
+ generation_args = dict(
29
+ max_new_tokens = kwargs.pop("max_new_tokens", 512),
30
+ do_sample = kwargs.pop("do_sample", True),
31
+ temperature = kwargs.pop("temperature", 0.35),
32
+ top_p = kwargs.pop("top_p", 0.8),
33
+ top_k = kwargs.pop("top_k", 40),
34
+ **kwargs
35
+ )
36
+
37
+ prompt = """Translate the following source text to {to}. Output translation directly without any additional text.
38
+ Source Text: {text}
39
+
40
+ Translated Text:"""
41
+
42
+ messages = [
43
+ {"role": "system", "content": "You are a professional, authentic machine translation engine."},
44
+ {"role": "user", "content": prompt.format(to=to, text=text)}
45
+ ]
46
+ inputs = tokenizer.apply_chat_template(
47
+ messages,
48
+ tokenize=False,
49
+ add_generation_prompt=True
50
+ )
51
+ model_inputs = tokenizer([inputs], return_tensors="pt").to(model.device)
52
+
53
+ generated_ids = model.generate(model_inputs.input_ids, **generation_args)
54
+ generated_ids = [
55
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
56
+ ]
57
+
58
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
59
+ return response
60
+
61
+ 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."
62
+ response = translate(text=text, to='chinese')
63
+ print(f'Translation: {response}')
64
+
65
+ """
66
+ Translation: 工作了一天,我喜欢吃一顿美味的晚餐,看我最喜欢的电视剧,这样做有助于我放松,补充能量。
67
+ """
68
+ ```
config.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "Mxode/NanoTranslator-365M-immersive_translate-v1",
3
+ "architectures": [
4
+ "Qwen2ForCausalLM"
5
+ ],
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 151643,
8
+ "eos_token_id": 151643,
9
+ "hidden_act": "silu",
10
+ "hidden_size": 896,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 4864,
13
+ "max_position_embeddings": 131072,
14
+ "max_window_layers": 24,
15
+ "model_type": "qwen2",
16
+ "num_attention_heads": 14,
17
+ "num_hidden_layers": 24,
18
+ "num_key_value_heads": 2,
19
+ "rms_norm_eps": 1e-06,
20
+ "rope_theta": 1000000.0,
21
+ "sliding_window": 131072,
22
+ "tie_word_embeddings": true,
23
+ "torch_dtype": "bfloat16",
24
+ "transformers_version": "4.42.0",
25
+ "use_cache": false,
26
+ "use_sliding_window": false,
27
+ "vocab_size": 8000
28
+ }
generation_config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 151643,
3
+ "eos_token_id": 2,
4
+ "max_new_tokens": 2048,
5
+ "pad_token_id": 0,
6
+ "transformers_version": "4.42.0"
7
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a6c1c82e6983f99f0540a667bd98c8336a2fb4e2284fcde51a5b055dea8dc97d
3
+ size 730164456
special_tokens_map.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<|endoftext|>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "<|im_end|>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "<|endoftext|>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ }
23
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<|endoftext|>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<|im_start|>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "<|im_end|>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ }
27
+ },
28
+ "bos_token": "<|endoftext|>",
29
+ "chat_template": "{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n' }}{% endif %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}",
30
+ "clean_up_tokenization_spaces": false,
31
+ "eos_token": "<|im_end|>",
32
+ "errors": "replace",
33
+ "model_max_length": 4096,
34
+ "pad_token": "<|endoftext|>",
35
+ "split_special_tokens": false,
36
+ "tokenizer_class": "PreTrainedTokenizerFast"
37
+ }