-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
55 lines (47 loc) · 1.84 KB
/
predict.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
from keras.layers import *
from keras.models import Sequential
from tensorflow.keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import pandas as pd
import numpy as np
import json
import pickle
import keras
import tensorflow as tf
from process_data import DataProcessor
import sqlite3
from utils import processData
import csv
def getPreds(df, out=None):
# GPU won't work without the next three lines
physical_devices = tf.config.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], enable=True)
dp = DataProcessor()
test_articles = processData(df, ['body']).to_numpy()
test_articles = list(map(lambda x: x[0], test_articles))
test_articles_raw = df.to_numpy()
test_articles_raw = list(map(lambda x: x[0], test_articles_raw))
with open('./onion_tokenizer.pyc', 'rb') as pickleHand:
tokenizer = pickle.load(pickleHand)
assert isinstance(tokenizer, Tokenizer)
seqs = test_articles
max_len = dp.getMaxWords()
seqs = tokenizer.texts_to_sequences(seqs)
seqs = pad_sequences(seqs, max_len)
model = keras.models.load_model('static/onion_connoisseur.h5')
assert isinstance(model, keras.models.Model)
print(test_articles)
predVals = model.predict(seqs)
preds = list(map(lambda x: "Real" if x < 0.75 else "Fake", predVals))
print(preds)
if out:
with open('predictions.csv', 'w', encoding='utf-8') as outHand:
out = csv.writer(outHand)
for i in range(0, len(preds)):
out.writerow([test_articles_raw[i], preds[i], predVals[i]])
return [preds, predVals]
if __name__ == '__main__':
conn = sqlite3.Connection('static/onion_barn.db')
df = pd.read_sql_query("SELECT body FROM The_Onion", conn)
getPreds(df)