Writo commited on
Commit
3c60981
·
1 Parent(s): a72a47e

Create gpt2_sentence

Browse files
Files changed (1) hide show
  1. gpt2_sentence +24 -0
gpt2_sentence ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a pipeline as a high-level helper
2
+ from transformers import pipeline
3
+
4
+ pipe = pipeline("text-generation", model="gpt2")
5
+
6
+ # Load model directly
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained("gpt2")
10
+ model = AutoModelForCausalLM.from_pretrained("gpt2")
11
+
12
+ #get the text
13
+ text_input = "One upon a time there was a tree"
14
+ max_length = 100
15
+ temperature = 0.8
16
+ top_k = 100
17
+
18
+ input_ids = tokenizer.encode(text_input,return_tensors='pt')
19
+
20
+ output = model.generate(input_ids, max_length=max_length, temperature=temperature, top_k=top_k, do_sample = True)
21
+
22
+
23
+ response = tokenizer.decode(output[0], skip_special_token=True)
24
+ print(response)