-
Notifications
You must be signed in to change notification settings - Fork 2
/
interact_with_model.py
30 lines (27 loc) · 1.08 KB
/
interact_with_model.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
#!/usr/bin/env python3
""" Provides functions to interact with models, launch it in
interactive mode and provide it a model and a tokenizer
WARNING: It is recommended that the model has been trained
with the same tokenizer to obtain correct results """
import pickle
import argparse
from preprocessing import preprocess_tweet
def predict(text):
""" Feeds the text into the model and output it's prediction """
# Turn the text into an input vector
vector = tokenizer.transform([text])
vector = vector.toarray()
# Feed it into the model
sentiment = model.predict(vector)
# Register the sentiment
print("Stay:{0:10.4f}, Leave:{1:10.4f}".format(1 - sentiment[0][0], sentiment[0][0]))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=argparse.FileType("rb"))
parser.add_argument("--tokenizer", \
type=argparse.FileType("rb"))
args = parser.parse_args()
tokenizer = pickle.load(args.tokenizer)
model = pickle.load(args.model)
print("\nUse predict(text) to explore what your model \
is thinking")