-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
149 lines (122 loc) · 3.98 KB
/
Copy pathtrain.py
File metadata and controls
149 lines (122 loc) · 3.98 KB
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
"""
Training module for AI text detection
Author: Keratin
"""
# pylint: disable=import-error
import pandas as pd
from tensorflow.keras.layers import Input, Embedding, Conv1D, MaxPooling1D, Flatten, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
def load_data():
"""Load dataset from CSV file and split into training and testing sets."""
dataf = pd.read_csv(
'./datasets/labeled_data.csv'
)
trains_text, tests_text, trains_labels, tests_labels = train_test_split(
dataf['message'],
dataf['class'],
test_size=0.2
)
return trains_text, tests_text, trains_labels, tests_labels
def tokenize_data(trained_text, tested_text):
"""Tokenize text data using Tokenizer from Keras and pad sequences to a fixed length."""
tokenizer_data = Tokenizer(
num_words=5000
)
tokenizer_data.fit_on_texts(
trained_text
)
trained_sequences = pad_sequences(
tokenizer_data.texts_to_sequences(trained_text),
maxlen=100
)
tested_sequences = pad_sequences(
tokenizer_data.texts_to_sequences(tested_text),
maxlen=100
)
return tokenizer_data, trained_sequences, tested_sequences
def define_model():
"""Define a convolutional neural network model using Keras."""
input_layer = Input(
shape=(100,)
)
embedding_layer = Embedding(
input_dim=5000,
output_dim=50
)(input_layer)
conv_layer = Conv1D(
filters=128,
kernel_size=5,
activation='relu'
)(embedding_layer)
pooling_layer = MaxPooling1D(pool_size=5)(conv_layer)
flatten_layer = Flatten()(pooling_layer)
output_layer = Dense(
units=1,
activation='sigmoid'
)(flatten_layer)
model_defined = Model(
inputs=input_layer,
outputs=output_layer
)
model_defined.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
return model_defined
def train_model(model_training,sequences_training,labels_training,sequences_testing,labels_testing):
"""Train the convolutional neural network model."""
model_training.fit(
sequences_training,
labels_training,
epochs=1,
batch_size=1, # Works better, dont be a puss
validation_data=(sequences_testing, labels_testing)
)
def save_model(saved_model):
"""Save the trained convolutional neural network model to a file."""
saved_model.save(
'Models/ai_detection_model.h5'
)
def classify_input(model_classify, tokenizer_classify):
"""Classify a text string input as AI or human using the trained model and tokenizer."""
while True:
user_input = input(
'Enter a text string to classify (or "exit" to quit): '
)
if user_input.lower() == 'exit':
exit()
if user_input.lower() == '':
continue
sequence = pad_sequences(
tokenizer_classify.texts_to_sequences([user_input]),
maxlen=100
)
prediction = model_classify.predict(sequence)[0][0]
if prediction > 0.5:
print("Message classified as AI.")
else:
print("Message is classified as human.")
if __name__ == '__main__':
train_text, test_text, train_labels, test_labels = load_data()
tokenizer, train_sequences, test_sequences = tokenize_data(
train_text,
test_text
)
model = define_model()
train_model(model,
train_sequences,
train_labels,
test_sequences,
test_labels
)
save_model(
model
)
classify_input(
model,
tokenizer
)