-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictor.py
52 lines (42 loc) · 1.52 KB
/
predictor.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
from emoji import emojize
import numpy as np
import pandas as pd
from tensorflow.keras.models import model_from_json
print("-- Loading Model File")
model_json_file = 'model.json'
# with open(model_json_file, "r") as file:
# model = model_from_json(file.read())
with open("model.json", "r") as file:
model = model_from_json(file.read())
model.load_weights("model.h5")
print("-- Loading Embeddings File")
embeddings = {}
with open('glove.6B.50d.txt',encoding='utf-8') as f:
for line in f:
values = line.split()
word = values[0]
coeffs = np.asarray(values[1:],dtype='float32')
embeddings[word] = coeffs
def getOutputEmbeddings(X):
embedding_matrix_output = np.zeros((X.shape[0],10,50))
for ix in range(X.shape[0]):
X[ix] = X[ix].split()
for jx in range(len(X[ix])):
embedding_matrix_output[ix][jx] = embeddings[X[ix][jx].lower()]
return embedding_matrix_output
def predict(input_str):
emoji_dictionary = {
"0": "\u2764\uFE0F", # :heart: prints a black instead of red heart depending on the font
"1": ":baseball:",
"2": ":beaming_face_with_smiling_eyes:",
"3": ":downcast_face_with_sweat:",
"4": ":fork_and_knife:",
}
X = pd.Series([input_str])
emb_X = getOutputEmbeddings(X)
p = model.predict_classes(emb_X)
return emojize(emoji_dictionary[str(p[0])])
# if __name__ == "__main__":
# test_str = " i am playing "
# output = predict(test_str)
# print(emojize(output))