shibing624 commited on
Commit
2f66290
1 Parent(s): a384336

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +40 -1
README.md CHANGED
@@ -5,7 +5,7 @@ tags:
5
  - t5
6
  - pytorch
7
  - zh
8
- - text-generation
9
  license: "apache-2.0"
10
  widget:
11
  - text: "对联:丹枫江冷人初去"
@@ -33,6 +33,11 @@ T5的网络结构(原生T5):
33
 
34
  本项目开源在文本生成项目:[textgen](https://github.com/shibing624/textgen),可支持T5模型,通过如下命令调用:
35
 
 
 
 
 
 
36
  ```shell
37
  >>> from textgen import T5Model
38
  >>> model = T5Model("t5", "shibing624/t5-chinese-couplet")
@@ -40,6 +45,40 @@ T5的网络结构(原生T5):
40
  ['白石矶寒客不归']
41
  ```
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  模型文件组成:
44
  ```
45
  t5-chinese-couplet
 
5
  - t5
6
  - pytorch
7
  - zh
8
+ - Text2Text-Generation
9
  license: "apache-2.0"
10
  widget:
11
  - text: "对联:丹枫江冷人初去"
 
33
 
34
  本项目开源在文本生成项目:[textgen](https://github.com/shibing624/textgen),可支持T5模型,通过如下命令调用:
35
 
36
+ Install package:
37
+ ```shell
38
+ pip install -U textgen
39
+ ```
40
+
41
  ```shell
42
  >>> from textgen import T5Model
43
  >>> model = T5Model("t5", "shibing624/t5-chinese-couplet")
 
45
  ['白石矶寒客不归']
46
  ```
47
 
48
+ ## Usage (HuggingFace Transformers)
49
+ Without [textgen](https://github.com/shibing624/textgen), you can use the model like this:
50
+
51
+ First, you pass your input through the transformer model, then you get the generated sentence.
52
+
53
+ Install package:
54
+ ```
55
+ pip install transformers
56
+ ```
57
+
58
+ ```python
59
+ from transformers import T5ForConditionalGeneration, T5Tokenizer
60
+
61
+ tokenizer = T5Tokenizer.from_pretrained("shibing624/t5-chinese-couplet")
62
+ model = T5ForConditionalGeneration.from_pretrained("shibing624/t5-chinese-couplet")
63
+
64
+
65
+ def batch_generate(input_texts, max_length=64):
66
+ features = tokenizer(input_texts, return_tensors='pt')
67
+ outputs = model.generate(input_ids=features['input_ids'],
68
+ attention_mask=features['attention_mask'],
69
+ max_length=max_length)
70
+ return tokenizer.batch_decode(outputs, skip_special_tokens=True)
71
+
72
+
73
+ r = batch_generate(["对联:丹枫江冷人初去"])
74
+ print(r)
75
+ ```
76
+
77
+ output:
78
+ ```shell
79
+ ['白石矶寒客不归']
80
+ ```
81
+
82
  模型文件组成:
83
  ```
84
  t5-chinese-couplet