/
scaffold.py
195 lines (154 loc) · 5.43 KB
/
scaffold.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
import argparse
import os
from typing import List, Text
import rasa.train
from rasa.cli.shell import shell
from rasa.cli.utils import create_output_path, print_success
from rasa.constants import (
DEFAULT_CONFIG_PATH,
DEFAULT_DATA_PATH,
DEFAULT_DOMAIN_PATH,
DOCS_BASE_URL,
)
# noinspection PyProtectedMember
def add_subparser(
subparsers: argparse._SubParsersAction, parents: List[argparse.ArgumentParser]
):
scaffold_parser = subparsers.add_parser(
"init",
parents=parents,
help="Creates a new project, with example training data, actions, and config files.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
scaffold_parser.add_argument(
"--no-prompt",
action="store_true",
help="Automatically choose default options for prompts and suppress warnings.",
)
scaffold_parser.set_defaults(func=run)
def print_train_or_instructions(args: argparse.Namespace, path: Text) -> None:
import questionary
print_success("Finished creating project structure.")
should_train = questionary.confirm(
"Do you want to train an initial model? 💪🏽"
).skip_if(args.no_prompt, default=True)
if should_train:
print_success("Training an initial model...")
config = os.path.join(path, DEFAULT_CONFIG_PATH)
training_files = os.path.join(path, DEFAULT_DATA_PATH)
domain = os.path.join(path, DEFAULT_DOMAIN_PATH)
output = os.path.join(path, create_output_path())
args.model = rasa.train(domain, config, training_files, output)
print_run_or_instructions(args, path)
else:
print_success(
"No problem 👍🏼. You can also train a model later by going "
"to the project directory and running 'rasa train'."
"".format(path)
)
def print_run_or_instructions(args: argparse.Namespace, path: Text) -> None:
from rasa.core import constants
import questionary
should_run = (
questionary.confirm(
"Do you want to speak to the trained assistant on the command line? 🤖"
)
.skip_if(args.no_prompt, default=False)
.ask()
)
if should_run:
# provide defaults for command line arguments
attributes = [
"endpoints",
"credentials",
"cors",
"auth_token",
"jwt_secret",
"jwt_method",
"enable_api",
"remote_storage",
]
for a in attributes:
setattr(args, a, None)
args.port = constants.DEFAULT_SERVER_PORT
shell(args)
else:
if args.no_prompt:
print(
"If you want to speak to the assistant, "
"run 'rasa shell' at any time inside "
"the project directory."
"".format(path)
)
else:
print_success(
"Ok 👍🏼. "
"If you want to speak to the assistant, "
"run 'rasa shell' at any time inside "
"the project directory."
"".format(path)
)
def init_project(args: argparse.Namespace, path: Text) -> None:
create_initial_project(path)
print("Created project directory at '{}'.".format(os.path.abspath(path)))
print_train_or_instructions(args, path)
def create_initial_project(path: Text) -> None:
from distutils.dir_util import copy_tree
copy_tree(scaffold_path(), path)
def scaffold_path() -> Text:
import pkg_resources
return pkg_resources.resource_filename(__name__, "initial_project")
def print_cancel() -> None:
print_success("Ok. You can continue setting up by running 'rasa init' 🙋🏽♀️")
exit(0)
def _ask_create_path(path: Text) -> None:
import questionary
should_create = questionary.confirm(
f"Path '{path}' does not exist 🧐. Create path?"
).ask()
if should_create:
os.makedirs(path)
else:
print_success("Ok. You can continue setting up by running " "'rasa init' 🙋🏽♀️")
exit(0)
def _ask_overwrite(path: Text) -> None:
import questionary
overwrite = questionary.confirm(
"Directory '{}' is not empty. Continue?".format(os.path.abspath(path))
).ask()
if not overwrite:
print_cancel()
def run(args: argparse.Namespace) -> None:
import questionary
print_success("Welcome to Rasa! 🤖\n")
if args.no_prompt:
print(
"To get started quickly, an "
"initial project will be created.\n"
"If you need some help, check out "
"the documentation at {}.\n".format(DOCS_BASE_URL)
)
else:
print(
"To get started quickly, an "
"initial project will be created.\n"
"If you need some help, check out "
"the documentation at {}.\n"
"Now let's start! 👇🏽\n".format(DOCS_BASE_URL)
)
path = (
questionary.text(
"Please enter a path where the project will be "
"created [default: current directory]",
default=".",
)
.skip_if(args.no_prompt, default=".")
.ask()
)
if path and not os.path.isdir(path):
_ask_create_path(path)
if path is None or not os.path.isdir(path):
print_cancel()
if not args.no_prompt and len(os.listdir(path)) > 0:
_ask_overwrite(path)
init_project(args, path)