Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4
5tokenizer = AutoTokenizer.from_pretrained("keonju/chat_bot")
6model = AutoModelForCausalLM.from_pretrained("keonju/chat_bot")
7
8# Let's chat for 5 lines
9for step in range(5):
10 message = input("MESSAGE: ")
11
12 if message in ["", "q"]: # if the user doesn't wanna talk
13 break
14
15 # encode the new user input, add the eos_token and return a tensor in Pytorch
16 new_user_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
17
18 # append the new user input tokens to the chat history
19 bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids
20
21
22 # generated a response while limiting the total chat history to 1000 tokens,
23 if (trained):
24 chat_history_ids = model.generate(
25 bot_input_ids,
26 max_length=1000,
27 pad_token_id=tokenizer.eos_token_id,
28 no_repeat_ngram_size=3,
29 do_sample=True,
30 top_k=100,
31 top_p=0.7,
32 temperature = 0.8,
33 )
34 else:
35 chat_history_ids = model.generate(
36 bot_input_ids,
37 max_length=1000,
38 pad_token_id=tokenizer.eos_token_id,
39 no_repeat_ngram_size=3
40 )
41
42 # pretty print last ouput tokens from bot
43 print("DialoGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))