-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__main__.py
47 lines (34 loc) · 1.33 KB
/
__main__.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
import sys
import joblib
from joblib import load as load_prep
from keras.models import load_model
from tools import StemmedTfidfVectorizer, tensorflow_shutup
sys.modules['sklearn.externals.joblib'] = joblib # needed for saved models
class MBType(object):
def __init__(self):
self.model = load_model('trained/model.h5')
self.vectorizer = load_prep('trained/vectorizer.pkl')
self.type_encoder = load_prep('trained/type_encoder.pkl')
def predict(self, X):
"""
From an unprocessed string predict the class.
"""
return self.type_encoder.inverse_transform(
self.model.predict(
self.vectorizer.transform([X]).toarray()
)
)[0][0]
if __name__ == '__main__':
def test_from_data():
from pandas import read_csv
predictor = MBType()
df = read_csv('data/mbti-myers-briggs-personality-types.csv')
sample = df['posts'].iloc[-1].split('|||')[-1]
print('Target is', df['type'].iloc[-1])
print('Predicted is', predictor.predict(sample))
print('Sample:\n', sample)
print("Loading the model...", end="")
predictor = MBType()
print("done!")
sample = input("Write something about yourself: ")
print("Predicted MBType is:", predictor.predict(sample))