-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_lense.py
213 lines (177 loc) · 8.14 KB
/
fake_lense.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM, Trainer, TrainingArguments
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
import pandas as pd
from datasets import Dataset
import os
import string
import re
# 0. GPU or CPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using", device)
# 1. Load Dataset
def load_data(train_path, test_path):
train_data = pd.read_csv(train_path, encoding='utf-8')
test_data = pd.read_csv(test_path, encoding='utf-8')
train_texts = train_data['text'].tolist()
train_labels = train_data['target'].tolist()
test_texts = test_data['text'].tolist()
test_labels = test_data['target'].tolist()
return train_texts, test_texts, train_labels, test_labels
def tokenize_data(texts, tokenizer, max_length=512):
if isinstance(texts, list):
texts = [str(text) if text is not None else "" for text in texts]
else:
texts = str(texts) if texts is not None else ""
return tokenizer(texts, padding='max_length', truncation=True, return_tensors="pt", max_length=max_length)
# 2. Text Preprocessing
def text_preprocessing(text):
# Check if the input is a string; if not, convert it to an empty string
if not isinstance(text, str):
text = ''
text = text.lower()
text = re.sub(r'\[.*?\]', '', text)
text = re.sub(r'https?://\S+|www\.\S+', '', text)
text = re.sub(r'<.*?>+', '', text)
text = re.sub(r'[%s]' % re.escape(string.punctuation + "–—−±×÷"), '', text)
text = re.sub(r'\n', '', text)
text = re.sub(r'\w*\d\w*', '', text)
text = re.sub(r'reuters', '', text)
text = re.sub(r' +', ' ', text).strip()
return text
# 3. Load Model and Tokenizer
def load_model_and_tokenizer(model_dir, model_class):
model = model_class.from_pretrained(model_dir).to(device)
tokenizer = AutoTokenizer.from_pretrained(model_dir)
return model, tokenizer
# 4. Train BERT-based model
def train_bert(llm_name, train_texts, train_labels, test_texts, test_labels, epochs, fine_tune=False, output_dir='./model/bert_lense'):
if fine_tune and os.path.exists(output_dir):
model, tokenizer = load_model_and_tokenizer(output_dir, AutoModelForSequenceClassification)
print("BERTLense is fine-tuned on BERTLense again")
else:
if llm_name is None:
llm_name = 'roberta-base'
print("BERTLense is fine-tuned on", llm_name)
tokenizer = AutoTokenizer.from_pretrained(llm_name)
model = AutoModelForSequenceClassification.from_pretrained(llm_name, num_labels=2).to(device)
train_encodings = tokenize_data(train_texts, tokenizer)
test_encodings = tokenize_data(test_texts, tokenizer)
train_dataset = Dataset.from_dict({
'input_ids': train_encodings['input_ids'],
'attention_mask': train_encodings['attention_mask'],
'labels': torch.tensor(train_labels)
})
test_dataset = Dataset.from_dict({
'input_ids': test_encodings['input_ids'],
'attention_mask': test_encodings['attention_mask'],
'labels': torch.tensor(test_labels)
})
training_args = TrainingArguments(
output_dir=output_dir,
num_train_epochs=epochs,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
evaluation_strategy="epoch",
save_strategy="epoch",
learning_rate=2e-5,
logging_dir='./bert_logs',
load_best_model_at_end=True,
metric_for_best_model="accuracy",
fp16=True
)
def compute_metrics(pred):
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='weighted')
acc = accuracy_score(labels, preds)
return {
'accuracy': acc,
'f1': f1,
'precision': precision,
'recall': recall
}
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=test_dataset,
compute_metrics=compute_metrics,
)
trainer.train()
# Save model and tokenizer
trainer.save_model(output_dir)
tokenizer.save_pretrained(output_dir)
return trainer, model, tokenizer
# 5. Train GPT-based model
def train_gpt(llm_name, train_texts, test_texts, epochs, fine_tune=False, output_dir='./model/gpt_lense'):
if fine_tune and os.path.exists(output_dir):
model, tokenizer = load_model_and_tokenizer(output_dir, AutoModelForCausalLM)
print("GPTLense is fine-tuned on GPTLense again")
else:
if llm_name is None:
llm_name = 'gpt2'
print("GPTLense is fine-tuned on", llm_name)
tokenizer = AutoTokenizer.from_pretrained(llm_name)
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(llm_name).to(device)
train_encodings = tokenize_data(train_texts, tokenizer)
test_encodings = tokenize_data(test_texts, tokenizer)
train_dataset = Dataset.from_dict({
'input_ids': train_encodings['input_ids'],
'attention_mask': train_encodings['attention_mask'],
'labels': train_encodings['input_ids']
})
test_dataset = Dataset.from_dict({
'input_ids': test_encodings['input_ids'],
'attention_mask': test_encodings['attention_mask'],
'labels': test_encodings['input_ids']
})
training_args = TrainingArguments(
output_dir=output_dir,
num_train_epochs=epochs,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
evaluation_strategy="epoch",
save_strategy="epoch",
learning_rate=5e-5,
logging_dir='./gpt_logs',
load_best_model_at_end=True,
fp16=True
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=test_dataset,
)
trainer.train()
# Save model and tokenizer
trainer.save_model(output_dir)
tokenizer.save_pretrained(output_dir)
return trainer, model, tokenizer
# 6. Fake News Detection Model
def FakeLense(text, bert_model, bert_tokenizer, gpt_model, gpt_tokenizer, similarity_threshold=0.8):
# Text preprocessing
text = text_preprocessing(text)
# BERT prediction
bert_inputs = bert_tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512).to(device)
bert_outputs = bert_model(input_ids=bert_inputs['input_ids'], attention_mask=bert_inputs['attention_mask'], output_hidden_states=True)
bert_prediction = torch.argmax(bert_outputs.logits, dim=1).item()
# GPT text generation
#gpt_inputs = gpt_tokenizer.encode(text, return_tensors='pt', max_length=512, truncation=True).to(device)
#gpt_outputs = gpt_model.generate(gpt_inputs, max_length=100)
gpt_inputs = gpt_tokenizer.encode(text, return_tensors='pt', max_length=512, truncation=True).to(device)
gpt_outputs = gpt_model.generate(gpt_inputs, max_length=100, pad_token_id=gpt_tokenizer.eos_token_id)
generated_text = gpt_tokenizer.decode(gpt_outputs[0], skip_special_tokens=True)
# BERT prediction on GPT-generated text
generated_bert_inputs = bert_tokenizer(generated_text, return_tensors='pt', truncation=True, padding=True, max_length=512).to(device)
generated_bert_outputs = bert_model(input_ids=generated_bert_inputs['input_ids'], attention_mask=generated_bert_inputs['attention_mask'], output_hidden_states=True)
# Cosine similarity between original and generated text embeddings
bert_embedding = bert_outputs.hidden_states[-1][:,0,:] # [CLS] token embedding
generated_bert_embedding = generated_bert_outputs.hidden_states[-1][:,0,:]
similarity = torch.nn.functional.cosine_similarity(bert_embedding, generated_bert_embedding, dim=1).item()
if bert_prediction == 1 or similarity < similarity_threshold:
return "Fake News Detected."
else:
return "Real News Detected."