docs: usage
Browse files
README.md
CHANGED
@@ -3,4 +3,32 @@ datasets:
|
|
3 |
- liweili/c4_200m
|
4 |
language:
|
5 |
- en
|
6 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
- liweili/c4_200m
|
4 |
language:
|
5 |
- en
|
6 |
+
---
|
7 |
+
|
8 |
+
```python
|
9 |
+
# Load model directly
|
10 |
+
import torch
|
11 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
12 |
+
|
13 |
+
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
14 |
+
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained("thenHung/english-grammar-error-correction-t5-seq2seq")
|
16 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("thenHung/english-grammar-error-correction-t5-seq2seq").to(torch_device)
|
17 |
+
|
18 |
+
|
19 |
+
def correct_grammar(input_text,num_return_sequences):
|
20 |
+
batch = tokenizer([input_text],truncation=True,padding='max_length',max_length=64, return_tensors="pt").to(torch_device)
|
21 |
+
translated = model.generate(**batch,max_length=64,num_beams=4, num_return_sequences=num_return_sequences, temperature=1.5)
|
22 |
+
tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
|
23 |
+
return tgt_text
|
24 |
+
|
25 |
+
input_text = """
|
26 |
+
He are an teachers.
|
27 |
+
"""
|
28 |
+
num_return_sequences = 3
|
29 |
+
corrected_texts = correct_grammar(input_text, num_return_sequences)
|
30 |
+
print(corrected_texts)
|
31 |
+
|
32 |
+
# output:
|
33 |
+
# ['He is a teacher.', 'He is an educator.', 'He is one of the teachers.']
|
34 |
+
```
|