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