Text2Text Generation
Transformers
PyTorch
t5
codet5
text-generation-inference
nielsr HF staff commited on
Commit
5194b26
1 Parent(s): e1c8132

Update code example

Browse files
Files changed (1) hide show
  1. README.md +4 -48
README.md CHANGED
@@ -43,57 +43,13 @@ from transformers import RobertaTokenizer, T5ForConditionalGeneration
43
  tokenizer = RobertaTokenizer.from_pretrained('Salesforce/codet5-small')
44
  model = T5ForConditionalGeneration.from_pretrained('Salesforce/codet5-small')
45
 
46
- text = "def greet(user): print(f'hello <extra_id_0>!') </s>"
47
- inputs = tokenizer(text, return_tensors="pt").input_ids
48
 
49
  # simply generate a single sequence
50
- generated_ids = model.generate(input_ids, max_length=8)
51
  print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
52
- # this prints {user.name}
53
-
54
- # or, generating 20 sequences with maximum length set to 10
55
- outputs = model.generate(input_ids=input_ids,
56
- num_beams=200, num_return_sequences=20,
57
- max_length=10)
58
-
59
- _0_index = text.index('<extra_id_0>')
60
- _result_prefix = text[:_0_index]
61
- _result_suffix = text[_0_index+12:] # 12 is the length of <extra_id_0>
62
-
63
- def _filter(output, end_token='<extra_id_1>'):
64
- # The first token is <pad> (indexed at 0), the second token is <s> (indexed at 1)
65
- # and the third token is <extra_id_0> (indexed at 32099)
66
- # So we only decode from the fourth generated id
67
- _txt = tokenizer.decode(output[3:], skip_special_tokens=False, clean_up_tokenization_spaces=False)
68
- if end_token in _txt:
69
- _end_token_index = _txt.index(end_token)
70
- return _result_prefix + _txt[:_end_token_index] + _result_suffix
71
- else:
72
- return _result_prefix + _txt + _result_suffix
73
-
74
- results = list(map(_filter, outputs))
75
- print(results)
76
- # this prints:
77
- #["def greet(user): print(f'hello {user.name} {user!') </s>",
78
- # "def greet(user): print(f'hello {user.username} {user!') </s>",
79
- # "def greet(user): print(f'hello {user.name}: {user!') </s>",
80
- # "def greet(user): print(f'hello {user}') print(f!') </s>",
81
- # "def greet(user): print(f'hello {user.name} �!') </s>",
82
- # "def greet(user): print(f'hello {user}') print ( f!') </s>",
83
- # "def greet(user): print(f'hello {user.username}: {user!') </s>",
84
- # "def greet(user): print(f'hello {user}' ) print(f!') </s>",
85
- # "def greet(user): print(f'hello {user.username} �!') </s>",
86
- # "def greet(user): print(f'hello {user.name}, {user!') </s>",
87
- # "def greet(user): print(f'hello {user.login} {user!') </s>",
88
- # "def greet(user): print(f'hello {user} →!') </s>",
89
- # "def greet(user): print(f'hello {user}!') print(!') </s>",
90
- # "def greet(user): print(f'hello {user.name} ({user!') </s>",
91
- # "def greet(user): print(f'hello {user.email} {user!') </s>",
92
- # "def greet(user): print(f'hello {user}!') print (!') </s>",
93
- # "def greet(user): print(f'hello {user.username}, {user!') </s>",
94
- # "def greet(user): print(f'hello {user}' ) print ( f!') </s>",
95
- # "def greet(user): print(f'hello {user.nickname} {!') </s>",
96
- # "def greet(user): print(f'hello {user} {user.name!') </s>"]
97
  ```
98
 
99
  ## Training data
 
43
  tokenizer = RobertaTokenizer.from_pretrained('Salesforce/codet5-small')
44
  model = T5ForConditionalGeneration.from_pretrained('Salesforce/codet5-small')
45
 
46
+ text = "def greet(user): print(f'hello <extra_id_0>!')"
47
+ input_ids = tokenizer(text, return_tensors="pt").input_ids
48
 
49
  # simply generate a single sequence
50
+ generated_ids = model.generate(input_ids, max_length=10)
51
  print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
52
+ # this prints "user: {user.name}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  ```
54
 
55
  ## Training data