Text Generation
Transformers
PyTorch
Chinese
English
gpt2
text-generation-inference
Inference Endpoints
Hollway commited on
Commit
0e34de3
1 Parent(s): 47f5f45

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +33 -0
README.md CHANGED
@@ -1,3 +1,36 @@
1
  ---
2
  license: mit
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
  ---
4
+
5
+ ## Usage
6
+
7
+ ### Initalization
8
+ ```
9
+ !pip install transformers[torch]
10
+
11
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel
12
+ import torch
13
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+
15
+ tokenizer = GPT2Tokenizer.from_pretrained('Hollway/gpt2_finetne')
16
+ model = GPT2LMHeadModel.from_pretrained('Hollway/gpt2_finetne').to(device)
17
+ ```
18
+
19
+ ### Inference
20
+ ```
21
+ def generate(text):
22
+ inputs = tokenizer(text, return_tensors="pt").to(model.device)
23
+ with torch.no_grad():
24
+ tokens = model.generate(
25
+ **inputs,
26
+ max_new_tokens=256,
27
+ do_sample=True,
28
+ temperature=0.7,
29
+ top_p=0.9,
30
+ repetition_penalty=1.05,
31
+ pad_token_id=tokenizer.pad_token_id,
32
+ )
33
+ return tokenizer.decode(tokens[0], skip_special_tokens=True)
34
+
35
+ generate("只因你")
36
+ ```