Views
No views yet
1import torch
2from transformers.models.bert import BertTokenizer, BertForSequenceClassification
3
4tokenizer = BertTokenizer.from_pretrained('thu-coai/roberta-base-cdconv')
5model = BertForSequenceClassification.from_pretrained('thu-coai/roberta-base-cdconv')
6model.eval()
7
8turn1 = [
9 "嗯嗯,你喜欢钓鱼吗?", # user
10 "喜欢啊,钓鱼很好玩的", # bot
11]
12turn2 = [
13 "你喜欢钓鱼吗?", # user
14 "不喜欢,我喜欢看别人钓鱼", # bot, we want to identify whether this utterance makes a contradiction
15] # turn1 and turn2 are not required to be two consecutive turns
16text1 = "[SEP]".join(turn1 + turn2[:1])
17text2 = turn2[1]
18
19model_input = tokenizer(text1, text2, return_tensors='pt', return_token_type_ids=True, return_attention_mask=True)
20model_output = model(**model_input, return_dict=False)
21prediction = torch.argmax(model_output[0].cpu(), dim=-1)[0].item()
22print(prediction) # output 1. 0 for non-contradiction, 1 for contradiction1@inproceedings{zheng-etal-2022-cdconv,
2 title={Towards Emotional Support Dialog Systems},
3 author={Zheng, Chujie and
4 Zhou, Jinfeng and
5 Zheng, Yinhe and
6 Peng, Libiao and
7 Guo, Zhen and
8 Wu, Wenquan and
9 Niu, Zhengyu and
10 Wu, Hua and
11 Huang, Minlie},
12 booktitle={Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing},
13 year={2022}
14}