-
Notifications
You must be signed in to change notification settings - Fork 39
/
interact.py
58 lines (50 loc) · 1.66 KB
/
interact.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
"""
Runs a script to interact with a model using the shell.
"""
import os
from argparse import ArgumentParser, Namespace
import yaml
from classifier import Classifier
def load_model_from_experiment(experiment_folder: str):
"""Function that loads the model from an experiment folder.
:param experiment_folder: Path to the experiment folder.
Return:
- Pretrained model.
"""
hparams_file = experiment_folder + "/hparams.yaml"
hparams = yaml.load(open(hparams_file).read(), Loader=yaml.FullLoader)
checkpoints = [
file
for file in os.listdir(experiment_folder + "/checkpoints/")
if file.endswith(".ckpt")
]
checkpoint_path = experiment_folder + "/checkpoints/" + checkpoints[-1]
model = Classifier.load_from_checkpoint(
checkpoint_path, hparams=Namespace(**hparams)
)
# Make sure model is in prediction mode
model.eval()
model.freeze()
return model
if __name__ == "__main__":
parser = ArgumentParser(
description="Minimalist Transformer Classifier", add_help=True
)
parser.add_argument(
"--experiment",
required=True,
type=str,
help="Path to the experiment folder.",
)
hparams = parser.parse_args()
print("Loading model...")
model = load_model_from_experiment(hparams.experiment)
print(model)
while 1:
print("Please write a sentence or quit to exit the interactive shell:")
# Get input sentence
input_sentence = input("> ")
if input_sentence == "q" or input_sentence == "quit":
break
prediction = model.predict(sample={"text": input_sentence})
print(prediction)