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

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +80 -71
README.md CHANGED
@@ -1,71 +1,80 @@
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
- ```
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: gpl-3.0
3
+ datasets:
4
+ - wmt/wmt19
5
+ language:
6
+ - en
7
+ - zh
8
+ base_model: Mxode/NanoLM-365M-Base
9
+ pipeline_tag: translation
10
+ tags:
11
+ - text-generation-inference
12
+ ---
13
+ # NanoTranslator-365M-immersive_translate-v1
14
+
15
+ ## Introduction
16
+
17
+ 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).
18
+
19
+ 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.
20
+
21
+ ## How to use
22
+
23
+ Below is a method to call the model using transformers. The prompt follows the immersive translation format to ensure optimal results.
24
+
25
+ ```python
26
+ import torch
27
+ from typing import Literal
28
+ from transformers import AutoModelForCausalLM, AutoTokenizer
29
+
30
+ model_path = 'Mxode/NanoTranslator-365M-immersive_translate-v1'
31
+
32
+ model = AutoModelForCausalLM.from_pretrained(model_path).to('cuda:0', torch.bfloat16)
33
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
34
+
35
+ def translate(
36
+ text: str,
37
+ to: Literal["chinese", "english"] = "chinese",
38
+ **kwargs
39
+ ):
40
+ generation_args = dict(
41
+ max_new_tokens = kwargs.pop("max_new_tokens", 512),
42
+ do_sample = kwargs.pop("do_sample", True),
43
+ temperature = kwargs.pop("temperature", 0.35),
44
+ top_p = kwargs.pop("top_p", 0.8),
45
+ top_k = kwargs.pop("top_k", 40),
46
+ **kwargs
47
+ )
48
+
49
+ prompt = """Translate the following source text to {to}. Output translation directly without any additional text.
50
+ Source Text: {text}
51
+
52
+ Translated Text:"""
53
+
54
+ messages = [
55
+ {"role": "system", "content": "You are a professional, authentic machine translation engine."},
56
+ {"role": "user", "content": prompt.format(to=to, text=text)}
57
+ ]
58
+ inputs = tokenizer.apply_chat_template(
59
+ messages,
60
+ tokenize=False,
61
+ add_generation_prompt=True
62
+ )
63
+ model_inputs = tokenizer([inputs], return_tensors="pt").to(model.device)
64
+
65
+ generated_ids = model.generate(model_inputs.input_ids, **generation_args)
66
+ generated_ids = [
67
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
68
+ ]
69
+
70
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
71
+ return response
72
+
73
+ 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."
74
+ response = translate(text=text, to='chinese')
75
+ print(f'Translation: {response}')
76
+
77
+ """
78
+ Translation: 工作了一天,我喜欢吃一顿美味的晚餐,看我最喜欢的电视剧,这样做有助于我放松,补充能量。
79
+ """
80
+ ```