Skip to content

Commit c1c1441

Browse files
authored
feat: CLI and Python argument to install model requirements before interaction (#1603)
1 parent bb06448 commit c1c1441

File tree

7 files changed

+62
-57
lines changed

7 files changed

+62
-57
lines changed

README.md

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,26 @@ evaluate and infer it:
9393

9494
#### GPU requirements
9595

96-
To run supported DeepPavlov models on GPU you should have [CUDA](https://developer.nvidia.com/cuda-toolkit) compatible
97-
with used GPU and [library PyTorch version](deeppavlov/requirements/pytorch.txt).
96+
By default, DeepPavlov installs models requirements from PyPI. PyTorch from PyPI could not support your device CUDA
97+
capability. To run supported DeepPavlov models on GPU you should have [CUDA](https://developer.nvidia.com/cuda-toolkit)
98+
compatible with used GPU and [PyTorch version](deeppavlov/requirements/pytorch.txt) required by DeepPavlov models.
99+
See [docs](https://docs.deeppavlov.ai/en/master/intro/quick_start.html#using-gpu) for details.
98100

99101
### Command line interface (CLI)
100102

101103
To get predictions from a model interactively through CLI, run
102104

103105
```bash
104-
python -m deeppavlov interact <config_path> [-d]
106+
python -m deeppavlov interact <config_path> [-d] [-i]
105107
```
106108

107-
* `-d` downloads required data -- pretrained model files and embeddings
108-
(optional).
109+
* `-d` downloads required data - pretrained model files and embeddings (optional).
110+
* `-i` installs model requirements (optional).
109111

110112
You can train it in the same simple way:
111113

112114
```bash
113-
python -m deeppavlov train <config_path> [-d]
115+
python -m deeppavlov train <config_path> [-d] [-i]
114116
```
115117

116118
Dataset will be downloaded regardless of whether there was `-d` flag or not.
@@ -122,10 +124,11 @@ The data format is specified in the corresponding model doc page.
122124
There are even more actions you can perform with configs:
123125

124126
```bash
125-
python -m deeppavlov <action> <config_path> [-d]
127+
python -m deeppavlov <action> <config_path> [-d] [-i]
126128
```
127129

128130
* `<action>` can be
131+
* `install` to install model requirements (same as `-i`),
129132
* `download` to download model's data (same as `-d`),
130133
* `train` to train the model on the data specified in the config file,
131134
* `evaluate` to calculate metrics on the same dataset,
@@ -136,6 +139,7 @@ python -m deeppavlov <action> <config_path> [-d]
136139
*<file_path>* if `-f <file_path>` is specified.
137140
* `<config_path>` specifies path (or name) of model's config file
138141
* `-d` downloads required data
142+
* `-i` installs model requirements
139143

140144

141145
### Python
@@ -145,33 +149,26 @@ To get predictions from a model interactively through Python, run
145149
```python
146150
from deeppavlov import build_model
147151

148-
model = build_model(<config_path>, download=True)
152+
model = build_model(<config_path>, install=True, download=True)
149153

150154
# get predictions for 'input_text1', 'input_text2'
151155
model(['input_text1', 'input_text2'])
152156
```
153-
154-
* where `download=True` downloads required data from web -- pretrained model
155-
files and embeddings (optional),
156-
* `<config_path>` is path to the chosen model's config file (e.g.
157-
`"deeppavlov/configs/ner/ner_ontonotes_bert_mult.json"`) or
158-
`deeppavlov.configs` attribute (e.g.
157+
where
158+
* `install=True` installs model requirements (optional),
159+
* `download=True` downloads required data from web - pretrained model files and embeddings (optional),
160+
* `<config_path>` is model name (e.g. `'ner_ontonotes_bert_mult'`), path to the chosen model's config file (e.g.
161+
`"deeppavlov/configs/ner/ner_ontonotes_bert_mult.json"`), or `deeppavlov.configs` attribute (e.g.
159162
`deeppavlov.configs.ner.ner_ontonotes_bert_mult` without quotation marks).
160163

161164
You can train it in the same simple way:
162165

163166
```python
164167
from deeppavlov import train_model
165168

166-
model = train_model(<config_path>, download=True)
169+
model = train_model(<config_path>, install=True, download=True)
167170
```
168171

169-
* `download=True` downloads pretrained model, therefore the pretrained
170-
model will be, first, loaded and then train (optional).
171-
172-
Dataset will be downloaded regardless of whether there was ``-d`` flag or
173-
not.
174-
175172
To train on your own data you need to modify dataset reader path in the
176173
[train config doc](http://docs.deeppavlov.ai/en/master/intro/config_description.html#train-config).
177174
The data format is specified in the corresponding model doc page.
@@ -181,12 +178,9 @@ You can also calculate metrics on the dataset specified in your config file:
181178
```python
182179
from deeppavlov import evaluate_model
183180

184-
model = evaluate_model(<config_path>, download=True)
181+
model = evaluate_model(<config_path>, install=True, download=True)
185182
```
186183

187-
188184
## License
189185

190186
DeepPavlov is Apache 2.0 - licensed.
191-
192-
##

deeppavlov/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626

2727

2828
# TODO: make better
29-
def train_model(config: [str, Path, dict], download: bool = False, recursive: bool = False) -> Chainer:
30-
train_evaluate_model_from_config(config, download=download, recursive=recursive)
29+
def train_model(config: [str, Path, dict], install: bool = False,
30+
download: bool = False, recursive: bool = False) -> Chainer:
31+
train_evaluate_model_from_config(config, install=install, download=download, recursive=recursive)
3132
return build_model(config, load_trained=True)
3233

3334

34-
def evaluate_model(config: [str, Path, dict], download: bool = False, recursive: bool = False) -> dict:
35-
return train_evaluate_model_from_config(config, to_train=False, download=download, recursive=recursive)
35+
def evaluate_model(config: [str, Path, dict], install: bool = False,
36+
download: bool = False, recursive: bool = False) -> dict:
37+
return train_evaluate_model_from_config(config, to_train=False, install=install,
38+
download=download, recursive=recursive)
3639

3740

3841
# check version

deeppavlov/_meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '1.0.0'
1+
__version__ = '1.0.1'
22
__author__ = 'Neural Networks and Deep Learning lab, MIPT'
33
__description__ = 'An open source library for building end-to-end dialog systems and training chatbots.'
44
__keywords__ = ['NLP', 'NER', 'SQUAD', 'Intents', 'Chatbot']

deeppavlov/core/commands/infer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import json
15-
import pickle
1615
import sys
1716
from itertools import islice
1817
from logging import getLogger
@@ -24,15 +23,18 @@
2423
from deeppavlov.core.common.params import from_params
2524
from deeppavlov.core.data.utils import jsonify_data
2625
from deeppavlov.download import deep_download
26+
from deeppavlov.utils.pip_wrapper import install_from_config
2727

2828
log = getLogger(__name__)
2929

3030

3131
def build_model(config: Union[str, Path, dict], mode: str = 'infer',
32-
load_trained: bool = False, download: bool = False) -> Chainer:
32+
load_trained: bool = False, install: bool = False, download: bool = False) -> Chainer:
3333
"""Build and return the model described in corresponding configuration file."""
3434
config = parse_config(config)
3535

36+
if install:
37+
install_from_config(config)
3638
if download:
3739
deep_download(config)
3840

deeppavlov/core/commands/train.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from deeppavlov.core.data.data_learning_iterator import DataLearningIterator
2525
from deeppavlov.core.data.utils import get_all_elems_from_json
2626
from deeppavlov.download import deep_download
27+
from deeppavlov.utils.pip_wrapper import install_from_config
2728

2829
log = getLogger(__name__)
2930

@@ -70,12 +71,15 @@ def train_evaluate_model_from_config(config: Union[str, Path, dict],
7071
iterator: Union[DataLearningIterator, DataFittingIterator] = None, *,
7172
to_train: bool = True,
7273
evaluation_targets: Optional[Iterable[str]] = None,
74+
install: bool = False,
7375
download: bool = False,
7476
start_epoch_num: Optional[int] = None,
7577
recursive: bool = False) -> Dict[str, Dict[str, float]]:
7678
"""Make training and evaluation of the model described in corresponding configuration file."""
7779
config = parse_config(config)
7880

81+
if install:
82+
install_from_config(config)
7983
if download:
8084
deep_download(config)
8185

deeppavlov/deep.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
parser.add_argument("-b", "--batch-size", dest="batch_size", default=None, help="inference batch size", type=int)
4242
parser.add_argument("-f", "--input-file", dest="file_path", default=None, help="Path to the input file", type=str)
4343
parser.add_argument("-d", "--download", action="store_true", help="download model components")
44+
parser.add_argument("-i", "--install", action="store_true", help="install model requirements")
4445

4546
parser.add_argument("--folds", help="number of folds", type=int, default=5)
4647

@@ -67,6 +68,8 @@ def main():
6768
args = parser.parse_args()
6869
pipeline_config_path = find_config(args.config_path)
6970

71+
if args.install or args.mode == 'install':
72+
install_from_config(pipeline_config_path)
7073
if args.download or args.mode == 'download':
7174
deep_download(pipeline_config_path)
7275

@@ -95,8 +98,6 @@ def main():
9598
rabbit_virtualhost=args.rabbit_virtualhost)
9699
elif args.mode == 'predict':
97100
predict_on_stream(pipeline_config_path, args.batch_size, args.file_path)
98-
elif args.mode == 'install':
99-
install_from_config(pipeline_config_path)
100101
elif args.mode == 'crossval':
101102
if args.folds < 2:
102103
log.error('Minimum number of Folds is 2')

docs/intro/quick_start.rst

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ Before making choice of an interface, install model's package requirements
2626
2727
python -m deeppavlov install <config_path>
2828
29-
* where ``<config_path>`` is path to the chosen model's config file (e.g.
30-
``deeppavlov/configs/classifiers/insults_kaggle_bert.json``) or just name without
31-
`.json` extension (e.g. ``insults_kaggle_bert``)
29+
* where ``<config_path>`` is model name without ``.json`` extension (e.g. ``insults_kaggle_bert``) or path to the
30+
chosen model's config file (e.g. ``deeppavlov/configs/classifiers/insults_kaggle_bert.json``)
3231

3332

3433
Command line interface (CLI)
@@ -38,19 +37,18 @@ To get predictions from a model interactively through CLI, run
3837

3938
.. code:: bash
4039
41-
python -m deeppavlov interact <config_path> [-d]
40+
python -m deeppavlov interact <config_path> [-d] [-i]
4241
43-
* ``-d`` downloads required data -- pretrained model files and embeddings
44-
(optional).
42+
* ``-d`` downloads required data -- pretrained model files and embeddings (optional).
43+
* ``-i`` installs model requirements (optional).
4544

4645
You can train it in the same simple way:
4746

4847
.. code:: bash
4948
50-
python -m deeppavlov train <config_path> [-d]
49+
python -m deeppavlov train <config_path> [-d] [-i]
5150
52-
Dataset will be downloaded regardless of whether there was ``-d`` flag or
53-
not.
51+
Dataset will be downloaded regardless of whether there was ``-d`` flag or not.
5452

5553
To train on your own data, you need to modify dataset reader path in the
5654
`train section doc <configuration.html#Train-config>`__. The data format is
@@ -60,9 +58,10 @@ There are even more actions you can perform with configs:
6058

6159
.. code:: bash
6260
63-
python -m deeppavlov <action> <config_path> [-d]
61+
python -m deeppavlov <action> <config_path> [-d] [-i]
6462
6563
* ``<action>`` can be
64+
* ``install`` to install model requirements (same as ``-i``),
6665
* ``download`` to download model's data (same as ``-d``),
6766
* ``train`` to train the model on the data specified in the config file,
6867
* ``evaluate`` to calculate metrics on the same dataset,
@@ -71,10 +70,11 @@ There are even more actions you can perform with configs:
7170
</integrations/rest_api>`),
7271
* ``risesocket`` to run a socket API server (see :doc:`docs
7372
</integrations/socket_api>`),
74-
* ``predict`` to get prediction for samples from `stdin` or from
75-
`<file_path>` if ``-f <file_path>`` is specified.
73+
* ``predict`` to get prediction for samples from ``stdin`` or from
74+
``<file_path>`` if ``-f <file_path>`` is specified.
7675
* ``<config_path>`` specifies path (or name) of model's config file
7776
* ``-d`` downloads required data
77+
* ``-i`` installs model requirements
7878

7979

8080
Python
@@ -86,13 +86,15 @@ To get predictions from a model interactively through Python, run
8686
8787
from deeppavlov import build_model
8888
89-
model = build_model(<config_path>, download=True)
89+
model = build_model(<config_path>, install=True, download=True)
9090
9191
# get predictions for 'input_text1', 'input_text2'
9292
model(['input_text1', 'input_text2'])
9393
94-
* where ``download=True`` downloads required data from web -- pretrained model
95-
files and embeddings (optional),
94+
where
95+
96+
* ``install=True`` installs model requirements (optional),
97+
* ``download=True`` downloads required data from web -- pretrained model files and embeddings (optional),
9698
* ``<config_path>`` is path to the chosen model's config file (e.g.
9799
``"deeppavlov/configs/ner/ner_ontonotes_bert_mult.json"``) or
98100
``deeppavlov.configs`` attribute (e.g.
@@ -104,13 +106,12 @@ You can train it in the same simple way:
104106
105107
from deeppavlov import train_model
106108
107-
model = train_model(<config_path>, download=True)
109+
model = train_model(<config_path>, install=True, download=True)
108110
109111
* ``download=True`` downloads pretrained model, therefore the pretrained
110112
model will be, first, loaded and then trained (optional).
111113

112-
Dataset will be downloaded regardless of whether there was ``-d`` flag or
113-
not.
114+
Dataset will be downloaded regardless of whether there was ``-d`` flag or not.
114115

115116
To train on your own data, you need to modify dataset reader path in the
116117
`train section doc <configuration.html#Train-config>`__. The data format is
@@ -122,7 +123,7 @@ You can also calculate metrics on the dataset specified in your config file:
122123
123124
from deeppavlov import evaluate_model
124125
125-
model = evaluate_model(<config_path>, download=True)
126+
model = evaluate_model(<config_path>, install=True, download=True)
126127
127128
128129
Using GPU
@@ -173,10 +174,10 @@ You can find a list of our out-of-the-box models `below <#out-of-the-box-pretrai
173174
Docker images
174175
~~~~~~~~~~~~~
175176

176-
You can run DeepPavlov models in :doc:`riseapi </integrations/rest_api>` mode
177-
via Docker without installing DP. Both your CPU and GPU (we support NVIDIA graphic
178-
processors) can be utilised, please refer our `CPU <https://hub.docker.com/r/deeppavlov/base-cpu>`_
179-
and `GPU <https://hub.docker.com/r/deeppavlov/base-gpu>`_ Docker images run instructions.
177+
You can run DeepPavlov models in :doc:`riseapi </integrations/rest_api>` mode or start Jupyter server
178+
via Docker without installing DeepPavlov. Both your CPU and GPU (we support NVIDIA graphic
179+
processors) can be utilised, please refer our `Docker <https://hub.docker.com/r/deeppavlov/deeppavlov>`_
180+
images run instructions.
180181

181182

182183
Out-of-the-box pretrained models

0 commit comments

Comments
 (0)