-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
214 lines (165 loc) · 6.32 KB
/
test.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import asyncio
import logging
import tempfile
from collections import defaultdict
from typing import Text, Dict, Optional, List, Any
import os
from rasa.core.interpreter import RegexInterpreter
from rasa.constants import DEFAULT_RESULTS_PATH, RESULTS_FILE
from rasa.model import get_model, get_model_subdirectories, unpack_model
from rasa.cli.utils import minimal_kwargs, print_error, print_warning
logger = logging.getLogger(__name__)
def test_compare_core(models: List[Text], stories: Text, output: Text):
from rasa.core.test import compare, plot_core_results
import rasa.utils.io
model_directory = copy_models_to_compare(models)
loop = asyncio.get_event_loop()
loop.run_until_complete(compare(model_directory, stories, output))
story_n_path = os.path.join(model_directory, "num_stories.json")
number_of_stories = rasa.utils.io.read_json_file(story_n_path)
plot_core_results(output, number_of_stories)
def test(
model: Text,
stories: Text,
nlu_data: Text,
endpoints: Optional[Text] = None,
output: Text = DEFAULT_RESULTS_PATH,
kwargs: Optional[Dict] = None,
):
if kwargs is None:
kwargs = {}
test_core(model, stories, endpoints, output, **kwargs)
test_nlu(model, nlu_data, kwargs)
def test_core(
model: Optional[Text] = None,
stories: Optional[Text] = None,
endpoints: Optional[Text] = None,
output: Text = DEFAULT_RESULTS_PATH,
kwargs: Optional[Dict] = None,
):
import rasa.core.test
import rasa.core.utils as core_utils
from rasa.nlu import utils as nlu_utils
from rasa.model import get_model
from rasa.core.interpreter import NaturalLanguageInterpreter
from rasa.core.agent import Agent
_endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints)
if kwargs is None:
kwargs = {}
if output:
nlu_utils.create_dir(output)
unpacked_model = get_model(model)
if unpacked_model is None:
print_error(
"Unable to test: could not find a model. Use 'rasa train' to train a "
"Rasa model."
)
return
core_path, nlu_path = get_model_subdirectories(unpacked_model)
if not os.path.exists(core_path):
print_error(
"Unable to test: could not find a Core model. Use 'rasa train' to "
"train a model."
)
use_e2e = kwargs["e2e"] if "e2e" in kwargs else False
_interpreter = RegexInterpreter()
if use_e2e:
if os.path.exists(nlu_path):
_interpreter = NaturalLanguageInterpreter.create(nlu_path, _endpoints.nlu)
else:
print_warning(
"No NLU model found. Using default 'RegexInterpreter' for end-to-end "
"evaluation."
)
_agent = Agent.load(unpacked_model, interpreter=_interpreter)
kwargs = minimal_kwargs(kwargs, rasa.core.test, ["stories", "agent"])
loop = asyncio.get_event_loop()
loop.run_until_complete(
rasa.core.test(stories, _agent, out_directory=output, **kwargs)
)
def test_nlu(model: Optional[Text], nlu_data: Optional[Text], kwargs: Optional[Dict]):
from rasa.nlu.test import run_evaluation
unpacked_model = get_model(model)
if unpacked_model is None:
print_error(
"Could not find any model. Use 'rasa train nlu' to train an NLU model."
)
return
nlu_model = os.path.join(unpacked_model, "nlu")
if os.path.exists(nlu_model):
kwargs = minimal_kwargs(kwargs, run_evaluation, ["data_path", "model"])
run_evaluation(nlu_data, nlu_model, **kwargs)
else:
print_error(
"Could not find any model. Use 'rasa train nlu' to train an NLU model."
)
def compare_nlu_models(
configs: List[Text],
nlu: Text,
output: Text,
runs: int,
exclusion_percentages: List[int],
):
"""Trains multiple models, compares them and saves the results."""
from rasa.nlu.test import drop_intents_below_freq
from rasa.nlu.training_data import load_data
from rasa.nlu.utils import write_json_to_file
from rasa.utils.io import create_path
from rasa.nlu.test import compare_nlu
from rasa.core.test import plot_nlu_results
data = load_data(nlu)
data = drop_intents_below_freq(data, cutoff=5)
create_path(output)
bases = [os.path.basename(nlu_config) for nlu_config in configs]
model_names = [os.path.splitext(base)[0] for base in bases]
f1_score_results = {
model_name: [[] for _ in range(runs)] for model_name in model_names
}
training_examples_per_run = compare_nlu(
configs,
data,
exclusion_percentages,
f1_score_results,
model_names,
output,
runs,
)
f1_path = os.path.join(output, RESULTS_FILE)
write_json_to_file(f1_path, f1_score_results)
plot_nlu_results(output, training_examples_per_run)
def perform_nlu_cross_validation(
config: Text, nlu: Text, kwargs: Optional[Dict[Text, Any]]
):
import rasa.nlu.config
from rasa.nlu.test import (
drop_intents_below_freq,
cross_validate,
return_results,
return_entity_results,
)
kwargs = kwargs or {}
folds = int(kwargs.get("folds", 3))
nlu_config = rasa.nlu.config.load(config)
data = rasa.nlu.training_data.load_data(nlu)
data = drop_intents_below_freq(data, cutoff=folds)
kwargs = minimal_kwargs(kwargs, cross_validate)
results, entity_results = cross_validate(data, folds, nlu_config, **kwargs)
logger.info("CV evaluation (n={})".format(folds))
if any(results):
logger.info("Intent evaluation results")
return_results(results.train, "train")
return_results(results.test, "test")
if any(entity_results):
logger.info("Entity evaluation results")
return_entity_results(entity_results.train, "train")
return_entity_results(entity_results.test, "test")
def copy_models_to_compare(models: List[str]) -> Text:
models_dir = tempfile.mkdtemp()
for i, model in enumerate(models):
if os.path.exists(model) and os.path.isfile(model):
path = os.path.join(models_dir, "model_" + str(i))
unpack_model(model, path)
else:
logger.warning("Ignore '{}' as it is not a valid model file.".format(model))
logger.debug("Unpacked models to compare to '{}'".format(models_dir))
return models_dir