Views
No views yet
1import torch
2from transformers import AutoTokenizer
3from transformers.models.blenderbot import BlenderbotTokenizer, BlenderbotForConditionalGeneration
4
5def _norm(x):
6 return ' '.join(x.strip().split())
7
8tokenizer = BlenderbotTokenizer.from_pretrained('thu-coai/blenderbot-400M-esconv')
9model = BlenderbotForConditionalGeneration.from_pretrained('thu-coai/blenderbot-400M-esconv')
10model.eval()
11
12utterances = [
13 "I am having a lot of anxiety about quitting my current job. It is too stressful but pays well",
14 "What makes your job stressful for you?",
15 "I have to deal with many people in hard financial situations and it is upsetting",
16 "Do you help your clients to make it to a better financial situation?",
17 "I do, but often they are not going to get back to what they want. Many people are going to lose their home when safeguards are lifted",
18]
19input_sequence = ' '.join([' ' + e for e in utterances]) + tokenizer.eos_token # add space prefix and separate utterances with two spaces
20input_ids = tokenizer.convert_tokens_to_ids(tokenizer.tokenize(input_sequence))[-128:]
21input_ids = torch.LongTensor([input_ids])
22
23model_output = model.generate(input_ids, num_beams=1, do_sample=True, top_p=0.9, num_return_sequences=5, return_dict=False)
24generation = tokenizer.batch_decode(model_output, skip_special_tokens=True)
25generation = [_norm(e) for e in generation]
26print(generation)
27
28utterances.append(generation[0]) # for future loop1@inproceedings{liu-etal-2021-towards,
2 title={Towards Emotional Support Dialog Systems},
3 author={Liu, Siyang and
4 Zheng, Chujie and
5 Demasi, Orianna and
6 Sabour, Sahand and
7 Li, Yu and
8 Yu, Zhou and
9 Jiang, Yong and
10 Huang, Minlie},
11 booktitle={Proceedings of the 59th annual meeting of the Association for Computational Linguistics},
12 year={2021}
13}