forked from vzhou842/profanity-check
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sync.py
More file actions
39 lines (33 loc) · 939 Bytes
/
test.sync.py
File metadata and controls
39 lines (33 loc) · 939 Bytes
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
# %%
import os
from sentence_transformers import SentenceTransformer
import pandas as pd
from sklearn.calibration import CalibratedClassifierCV
from sklearn.svm import LinearSVC
import joblib
import numpy as np
# %%
# Read in data
data = pd.read_csv('data/train_data.csv')
texts = data['text'].astype(str)
y = data['is_offensive']
# %%
# Vectorize the text
path_vec = "vectorized.npy"
vectorizer = SentenceTransformer(
'sentence-transformers/all-MiniLM-L6-v2')
if not os.path.exists(path_vec):
X = vectorizer.encode(list(texts), batch_size=256,
show_progress_bar=True)
np.save(path_vec, X)
else:
X = np.load(path_vec)
# %%
# Train the model
model = LinearSVC(class_weight="balanced", dual=False,
tol=1e-2, max_iter=int(1e6))
cclf = CalibratedClassifierCV(base_estimator=model)
cclf.fit(X, y)
# %%
# Save the model
joblib.dump(cclf, 'profanity_protector/data/model.joblib')