-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
77 lines (65 loc) · 2 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
# -*- 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="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(
devices=1 if torch.cuda.is_available() else 0,
deterministic=True,
logger=False
)
trainer.test(model, dataloaders=data.test_dataloader())
if __name__ == "__main__":
cli()