-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer_republican_llm.py
69 lines (49 loc) · 2.16 KB
/
trainer_republican_llm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from datasets import load_dataset
import torch
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
from transformers import AutoModelForCausalLM, AutoTokenizer
dataset = load_dataset("m-newhauser/senator-tweets", split="train")
democratic_dataset = dataset.filter(lambda x: x["party"] == "Democrat")
republican_dataset = dataset.filter(lambda x: x["party"] == "Republican")
print("Num democratic tweets: ", len(democratic_dataset))
print("Num republican tweets: ", len(republican_dataset))
model = AutoModelForCausalLM.from_pretrained("Writer/palmyra-small")
tokenizer = AutoTokenizer.from_pretrained("Writer/palmyra-small")
user_input = "What side of the abortion argument do you take"
input_ids = tokenizer.encode(user_input, return_tensors="pt")
output = model.generate(input_ids, max_length=100, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95)
# Decode and print the generated text
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print("\nGenerated text before finetunning:")
print(generated_text, "\n")
training_args = TrainingArguments(
output_dir="republican-trainer-fine-tuned",
per_device_train_batch_size=4,
optim="adamw_torch",
logging_steps=80,
learning_rate=2e-4,
warmup_ratio=0.1,
lr_scheduler_type="linear",
num_train_epochs=1,
save_strategy="epoch"
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=republican_dataset,
)
trainer.train()
## Evaluate on abortion input text again
model.eval()
user_input = "Abortion is"
input_ids = tokenizer.encode(user_input, return_tensors="pt")
# Adjust max_length and other parameters as needed
model.to('cuda')
input_ids.to('cuda')
output = model.generate(input_ids=input_ids.cuda(), min_length=200, max_length=1000, num_beams=5, temperature=0.7, attention_mask=input_ids.cuda().ne(tokenizer.pad_token_id),
pad_token_id=tokenizer.eos_token_id)
# Decode and print the generated text
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print("\nGenerated text after finetunning:")
print(generated_text, "\n")