File size: 5,879 Bytes
62ff4c4
 
 
 
 
 
 
a618928
 
 
bd850cf
a618928
62ff4c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a618928
 
4ecc6af
62ff4c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a618928
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62ff4c4
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
thumbnail: https://huggingface.co/front/thumbnails/dialogpt.png
tags:
- conversational
license: mit
---

## dialogGPT-homer-simpson

This model has been fine tuned with the entire scripts of Homer Simpson from the T.V. show The Simpsons
It will give some nice answers seemingly from Homers brain in the Simpsons Universe during single turn conversation, letting you chat to Homer Simpson

## A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)

DialoGPT is a SOTA large-scale pretrained dialogue response generation model for multiturn conversations. 
The [human evaluation results](https://github.com/dreasysnail/Dialogpt_dev#human-evaluation) indicate that the response generated from DialoGPT is comparable to human response quality under a single-turn conversation Turing test.
The model is trained on 147M multi-turn dialogue from Reddit discussion thread. 

* Multi-turn generation examples from an interactive environment:

|Role | Response |
|---------|--------|
|User | Who are you? |
| HomerBot |  Homer Simpson .|
|User | What is your favorite Restaurant ? |
| HomerBot | Moes Tavern. |
|User | Have you ever been in a band?! |
| HomerBot | no. |

Please find the information about preprocessing, training and full details of the DialoGPT in the [original DialoGPT repository](https://github.com/microsoft/DialoGPT)

ArXiv paper: [https://arxiv.org/abs/1911.00536](https://arxiv.org/abs/1911.00536)

### How to use Multi-Turn

#### NOTE: Multi-Turn seems to be broken, after a few exchanges the output will mostly be exclamation marks.

Now we are ready to try out how the model works as a chatting partner!

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch


tokenizer = AutoTokenizer.from_pretrained("shalpin87/dialoGPT-homer-simpson")
model = AutoModelForCausalLM.from_pretrained("shalpin87/dialoGPT-homer-simpson")

# Let's chat for 5 lines
for step in range(5):
	# encode the new user input, add the eos_token and return a tensor in Pytorch
	new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt')

	# append the new user input tokens to the chat history
	bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids

	# generated a response while limiting the total chat history to 1000 tokens, 
	chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)

	# pretty print last ouput tokens from bot
	print("DialoG-PT-HomerBot: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))
```

### How to use Single Turn

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch


tokenizer = AutoTokenizer.from_pretrained("shalpin87/dialoGPT-homer-simpson")
model = AutoModelForCausalLM.from_pretrained("shalpin87/dialoGPT-homer-simpson")

questions = [
    "What is your name?",
    "Who are you?",
    "Where do you work?",
    "Who really killed Mr Burns?",
    "Have you ever stolen from the Kwik-E-Mart?",
    "Did you kill Frank Grimes?",
    "Who was the worst member of the Be Sharps?",
    "Hey where did Barney go?",
    "What is your favorite bar to have a beer?",
    "What is the best beer in Springfield?",
    "Is Bart working for the Mob?",
    "I think there was an incident in sector 7 G",
    "Is Ned Flanders house okay?",
    "Oh my god it's Sideshow Bob",
    "What is a Flaming Moe?",
    "What is happening to Apu?",
    "Who quit the band?"
]

# Let's chat
for step in range(len(questions)):
    print("***************************************")
    print("Q. {}".format(questions[step]))
    new_user_input_ids = tokenizer.encode(questions[step] + tokenizer.eos_token, return_tensors='pt')
    # new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt')

    bot_input_ids = new_user_input_ids

    num_return_seqs=1

    chat_history_ids = model.generate(
        bot_input_ids, 
        max_length=200,
        pad_token_id=tokenizer.eos_token_id,  
        no_repeat_ngram_size=3,       
        do_sample=True, 
        top_k=200, 
        top_p=0.55,
        temperature = 0.85,
        num_return_sequences=num_return_seqs
    )
    
    botname = "HomerBot"
    for i in range(0,num_return_seqs):
      print("{}:{}: {}".format(i, botname, tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][i], skip_special_tokens=True)))

    chat_history_ids = []
```

Sample Output
```
***************************************
Q. What is your name?
0:HomerBot: Homer Simpson.
***************************************
Q. Who are you?
0:HomerBot: Homer Simpson.
***************************************
Q. Where do you work?
0:HomerBot: I work at a nuclear power plant.
***************************************
Q. Who really killed Mr Burns?
0:HomerBot: I dunno. Some guy named Ot-to.
***************************************
Q. Have you ever stolen from the Kwik-E-Mart?
0:HomerBot: No.
***************************************
Q. Did you kill Frank Grimes?
0:HomerBot: No.
***************************************
Q. Who was the worst member of the Be Sharps?
0:HomerBot: Homer Simpson, sir.
***************************************
Q. Hey where did Barney go?
0:HomerBot: He's been eating that thing for a week.
***************************************
Q. What is your favorite bar to have a beer?
0:HomerBot: The Flanders' Club.
***************************************
Q. What is the best beer in Springfield?
0:HomerBot: Pepi!
***************************************
Q. Is Bart working for the Mob?
0:HomerBot: I think he's trying to tell us something.
***************************************
Q. I think there was an incident in sector 7 G
0:HomerBot: Uh, let's not jump to any conclusions. The dog didn't run away.
```