Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2from transformers.trainer_utils import set_seed
3import torch
4
5tokenizer = AutoTokenizer.from_pretrained("TheRensselaerIDEA/gpt2-large-covid-tweet-response")
6model = AutoModelForCausalLM.from_pretrained("TheRensselaerIDEA/gpt2-large-covid-tweet-response")
7device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8model.to(device)
9set_seed(33)
10
11message = "Is your child worried about #COVID19? Learn the facts so you can answer your children’s questions."
12author = "CDCgov"
13num_responses = 2
14
15author_token, message_token, response_token = tokenizer.additional_special_tokens
16input_str = f"{message_token}{message}{author_token}{author}{response_token}"
17inputs = tokenizer(input_str, return_tensors="pt").to(device)
18
19responses_ids = model.generate(**inputs,
20 max_new_tokens=100,
21 pad_token_id=tokenizer.pad_token_id,
22 do_sample=True,
23 top_p=0.95,
24 temperature=1.5,
25 num_beams=3,
26 early_stopping=True,
27 num_return_sequences=num_responses)
28
29responses = [tokenizer.decode(r[inputs.input_ids.shape[-1]:], skip_special_tokens=True) for r in responses_ids]
30for i, resp in enumerate(responses):
31 print(f"Response {i}: {resp}\n")Response 0: @CDCgov I'm not worried. I don't know who needs to hear this, but I have a feeling I know who will be listening.
It is not the virus. It is the media. I know you and CDC have been lying for months now, but the media will keep pushing this lie.
Response 1: #WashYourHands to help #StopTheSpread of #COVID19 and other diseases. Learn more about hand washing: #HandWashing