-
Notifications
You must be signed in to change notification settings - Fork 3
/
plato_mini.py
55 lines (43 loc) · 1.36 KB
/
plato_mini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging
from typing import List, Dict, Any
from paddlenlp.transformers import (UnifiedTransformerLMHeadModel,
UnifiedTransformerTokenizer)
logger = logging.getLogger(__name__)
tokenizer = UnifiedTransformerTokenizer.from_pretrained('plato-mini')
model = UnifiedTransformerLMHeadModel.from_pretrained('plato-mini')
logger.info('Model fully loaded')
def dialog_predict(
history: List[str],
**kwargs: Dict[str, Any]
) -> str:
"""
Predict the next dialog utterance.
Args:
history: A list of dialog utterances.
**kwargs: Additional keyword arguments.
Returns:
The predicted dialog utterance.
"""
inputs = tokenizer.dialogue_encode(
history,
add_start_token_as_response=True,
return_tensors=True,
is_split_into_words=False
)
output_ids, score = model.generate(
**inputs,
**kwargs,
)
token_ids = output_ids.numpy()[0]
eos_pos = len(token_ids)
for i, tok_id in enumerate(token_ids):
if tok_id == tokenizer.sep_token_id:
eos_pos = i
break
token_ids = token_ids[:eos_pos]
tokens = tokenizer.convert_ids_to_tokens(token_ids)
tokens = tokenizer.merge_subword(tokens)
return ''.join(tokens)
if __name__ == '__main__':
history = ['你好!']
print(dialog_predict(history))