Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
tokenizer = AutoTokenizer.from_pretrained("Ahmade/rick-and-morty-v2")
|
| 5 |
+
model = AutoModelForCausalLM.from_pretrained("Ahmade/rick-and-morty-v2")
|
| 6 |
+
|
| 7 |
+
def chat(model, tokenizer):
|
| 8 |
+
print("type \"q\" to quit. Automatically quits after 5 messages")
|
| 9 |
+
|
| 10 |
+
for step in range(5):
|
| 11 |
+
message = input("MESSAGE: ")
|
| 12 |
+
|
| 13 |
+
if message in ["", "q"]: # if the user doesn't wanna talk
|
| 14 |
+
break
|
| 15 |
+
|
| 16 |
+
# encode the new user input, add the eos_token and return a tensor in Pytorch
|
| 17 |
+
new_user_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
|
| 18 |
+
|
| 19 |
+
# append the new user input tokens to the chat history
|
| 20 |
+
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids
|
| 21 |
+
|
| 22 |
+
# generated a response while limiting the total chat history to 1000 tokens,
|
| 23 |
+
|
| 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 |
+
|
| 35 |
+
# pretty print last ouput tokens from bot
|
| 36 |
+
print("DialoGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))
|
| 37 |
+
|
| 38 |
+
chat(model, tokenizer)
|