-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.py
99 lines (85 loc) · 2.83 KB
/
cli.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
r"""
Command Line Interface
=======================
Commands:
- train: for Training a new model.
- interact: Model interactive mode where we can "talk" with a trained model.
- test: Tests the model ability to rank candidate answers and generate text.
"""
import json
import click
import pytorch_lightning as pl
import torch
import yaml
from pytorch_lightning import seed_everything
from model.data_module import DataModule
from model.emotion_transformer import EmotionTransformer
from trainer import TrainerConfig, build_trainer
@click.group()
def cli():
pass
@cli.command(name="train")
@click.option(
"--config",
"-f",
type=click.Path(exists=True),
required=True,
help="Path to the configure YAML file",
)
def train(config: str) -> None:
yaml_file = yaml.load(open(config).read(), Loader=yaml.FullLoader)
# Build Trainer
train_configs = TrainerConfig(yaml_file)
seed_everything(train_configs.seed)
trainer = build_trainer(train_configs.namespace())
# Build Model
model_config = EmotionTransformer.ModelConfig(yaml_file)
model = EmotionTransformer(model_config.namespace())
data = DataModule(model.config, model.tokenizer)
trainer.fit(model, data)
@cli.command(name="interact")
@click.option(
"--experiment",
type=click.Path(exists=True),
required=True,
help="Path to the experiment folder containing the checkpoint we want to interact with.",
)
def interact(experiment: str) -> None:
"""Interactive mode command where we can have a conversation with a trained model
that impersonates a Vegan that likes cooking and radical activities such as sky-diving.
"""
model = EmotionTransformer.from_experiment(experiment)
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(samples=[input_sentence])
print(json.dumps(prediction[0], indent=3))
@cli.command(name="test")
@click.option(
"--experiment",
type=click.Path(exists=True),
required=True,
help="Path to the experiment folder containing the checkpoint we want to interact with.",
)
def test(
experiment: str,
) -> None:
"""Testing function where a trained model is tested in its ability to rank candidate
answers and produce replies.
"""
model = EmotionTransformer.from_experiment(experiment)
data = DataModule(model.config, model.tokenizer)
data.prepare_data()
# Build a very simple trainer
trainer = pl.Trainer(
gpus=1 if torch.cuda.is_available() else 0,
deterministic=True,
logger=False
)
trainer.test(model, test_dataloaders=data.test_dataloader())
if __name__ == "__main__":
cli()