-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model.py
More file actions
46 lines (34 loc) · 1.31 KB
/
test_model.py
File metadata and controls
46 lines (34 loc) · 1.31 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
import json
import numpy as np
from keras.preprocessing.text import tokenizer_from_json
from pathlib import Path
import torch
from utils import *
MAX_LEN = 220
MAX_FEATURES = 327009
NUM_AUX = 6
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Paths
colab_path = "/content/"
kaggle_path = "/kaggle/working/"
prepend_path = "./models/"
if Path(colab_path).exists():
prepend_path = colab_path
elif Path(kaggle_path).exists():
prepend_path = kaggle_path
model_path = prepend_path + "model.pth"
embedding_matrix_path = prepend_path + "embeddings.npy"
tokenizer_path = prepend_path + "tokenizer.json"
# Load model and params
embedding_matrix = np.load(embedding_matrix_path)
with open(tokenizer_path) as tknzr:
tokenizer_json = json.load(tknzr)
tokenizer = tokenizer_from_json(tokenizer_json)
model = NeuralNet(embedding_matrix, num_aux_targets=NUM_AUX, max_features=MAX_FEATURES)
model.load_state_dict(torch.load(model_path, map_location=device)['model_state_dict'])
test_text = "This is a test text."
prediction = predict(test_text, model, tokenizer, device)
print(f'Prediction: {"Toxic" if prediction[0][0] > 0.5 else "Not Toxic"}')
test_text = "Shit! This is bad."
prediction = predict(test_text, model, tokenizer, device)
print(f'Prediction: {"Toxic" if prediction[0][0] > 0.5 else "Not Toxic"}')