-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelPredict.py
63 lines (50 loc) · 1.75 KB
/
modelPredict.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
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense, Flatten, LSTM, Conv1D, MaxPooling1D, Dropout, Activation
from keras.layers.embeddings import Embedding
import pickle
import pandas as pd
import tensorflow as tf
def classify(title,body):
# with open ('data1row.txt', 'r') as file:
# strdata = file.read().replace('\n', '')
data = makewordembeddings(title,body)
pred = makePredictions(data)
result = []
result.append('Fake')
result.append(pred[0][0])
if result[1] > 0.5:
return result
else:
result[0] = 'Real'
return result
def classify_single(body):
data = makewordembeddings('title', body)
pred = makePredictions(data)
result = []
result.append('Fake')
result.append(pred[0][0])
if result[1] > 0.5:
response = {'category': result[0], 'status': 'ok', 'score': str(result[1]) }
else:
result[0] = 'Real'
response = {'category': result[0], 'status': 'ok', 'score': str(result[1]) }
# anything printed to the STDOUT will be stored in heroku's logs
# print "TEXT: '{0}' :: RESPONSE : '{1}'" .format ( body.replace("\n", " ").replace("\r", " "), result)
return response
def makewordembeddings(title,body):
vocabulary_size = 2000
with open('tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
testDF = pd.DataFrame([body],columns=['text'])
#title_sequences = tokenizer.texts_to_sequences(title)
body_sequences = tokenizer.texts_to_sequences(testDF['text'])
data = pad_sequences(body_sequences, maxlen=50)
return data
def makePredictions(testdata):
model_glove = tf.keras.models.load_model('fakenewsClassifier')
pred = model_glove.predict(testdata)
return pred
if __name__ == '__main__':
classify(title,body)