|
|
--- |
|
|
license: mit |
|
|
--- |
|
|
## Usage (HuggingFace Transformers) |
|
|
Without [ChineseErrorCorrector](https://github.com/TW-NLP/ChineseErrorCorrector), you can use the model like this: |
|
|
|
|
|
First, you pass your input through the transformer model, then you get the generated sentence. |
|
|
|
|
|
Install package: |
|
|
``` |
|
|
pip install transformers |
|
|
``` |
|
|
|
|
|
```python |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
model_name = "twnlp/ChineseErrorCorrector2-7B" |
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
|
model_name, |
|
|
torch_dtype="auto", |
|
|
device_map="auto" |
|
|
) |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
prompt = "你是一个文本纠错专家,纠正输入句子中的语法错误,并输出正确的句子,输入句子为:" |
|
|
text_input = "对待每一项工作都要一丝不够。" |
|
|
messages = [ |
|
|
{"role": "user", "content": prompt + text_input} |
|
|
] |
|
|
text = tokenizer.apply_chat_template( |
|
|
messages, |
|
|
tokenize=False, |
|
|
add_generation_prompt=True |
|
|
) |
|
|
model_inputs = tokenizer([text], return_tensors="pt").to(model.device) |
|
|
|
|
|
generated_ids = model.generate( |
|
|
**model_inputs, |
|
|
max_new_tokens=512 |
|
|
) |
|
|
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] |
|
|
print(response) |
|
|
|
|
|
``` |
|
|
|
|
|
output: |
|
|
```shell |
|
|
对待每一项工作都要一丝不苟。 |
|
|
``` |