dennis-fast commited on
Commit
78cc221
1 Parent(s): 0640343

Stateful model -> memory

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -7,18 +7,24 @@ from transformers import GPT2LMHeadModel, GPT2Tokenizer
7
  tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium')
8
  model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
9
 
10
- def chat(message, token_response):
11
- token_message = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
12
- token_response = model.generate(token_message, max_length=1000, pad_token_id=tokenizer.eos_token_id)
13
- response = tokenizer.decode(token_response[:, token_message.shape[-1]:][0], skip_special_tokens=True)
14
- return response, token_response
15
 
16
- input = gr.inputs.Textbox(lines=2, label='User:')
17
- output = gr.outputs.Textbox(label='Bot:')
18
 
19
- gr.Interface(fn=chat,
 
 
 
 
 
 
 
 
20
  title="DialoGPT-medium",
21
- inputs=[input, "state"],
22
- outputs=[output, "state"],
23
  allow_screenshot=False,
24
  allow_flagging='never').launch()
 
7
  tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium')
8
  model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
9
 
10
+ def predict(input, history=[]):
11
+ # tokenize the new input sentence
12
+ new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
 
 
13
 
14
+ # append the new user input tokens to the chat history
15
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
16
 
17
+ # generate a response
18
+ history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
19
+
20
+ # convert the tokens to text, and then split the responses into lines
21
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
22
+ response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
23
+ return response, history
24
+
25
+ gr.Interface(fn=predict,
26
  title="DialoGPT-medium",
27
+ inputs=["text", "state"],
28
+ outputs=["text", "state"],
29
  allow_screenshot=False,
30
  allow_flagging='never').launch()