NLP tasks include determining the tonality of the text, parsing named entities, determining what the other party wants from your bot: order pizza or get help information and much more. You can read more about NLP tasks and methods here .
In this article, we’ll show you how to run REST North with pre-trained NLP models that are ready to use without any additional configuration or training.
All articles in the series:
1. DeepPavlov for developers: # 1 NLP tools and create chat bots
2. DeepPavlov for developers: # 2 setup and deployment
Install DeepPavlov
Hereinafter, instructions for Linux will be given. For Windows, see our documentation.
- Create and activate a virtual environment with the current supported version of Python:
virtualelnv env -p python3.7 source env/bin/activate
- Install DeepPavlov in a virtual environment:
pip install deeppavlov
Starting a REST server with DeepPavlov model
Before we launch the server with the DeepPavlov model for the first time, it will be useful to talk about some features of the library architecture.
Any model in DP consists of:
- Python code;
- Downloadable components - serialized learning outcomes on specific data (embeddings, neural network weights, etc.);
- A configuration file (hereinafter referred to as the config), which contains information about the classes used by the model, URLs of downloaded components, Python dependencies, and more.
We will tell you more about what is under the hood of DeepPavlov in the following articles, for now we only need to know that:
- Any model in DeepPavlov is identified by the name of its config;
- To run the model, you need to download its components from the DeepPavlov servers;
- Also, to run the model, you must install the Python libraries it uses.
The first model we will launch will be the multilingual Named Entity Recognition (NER). The model classifies text words according to the type of named entities to which they belong (proper names, geographical names, currency names and others). The name of the config for the most recent version of NER:
ner_ontonotes_bert_mult
We start the REST server with the model:
- We install the model dependencies indicated in its config in the active virtual environment:
python -m deeppavlov install ner_ontonotes_bert_mult
- Download serialized model components from DeepPavlov servers:
python -m deeppavlov download ner_ontonotes_bert_mult
Serialized components will be downloaded to the DeepPavlov home directory, which is located by default~/.deeppavlov
- We start the REST server with the model:
python -m deeppavlov riseapi ner_ontonotes_bert_mult -p 5005
As a result of this command, a REST server will be launched with a model on the 5005 port of the host machine (the default port is 5000).
After initializing the model, Swagger with API documentation and the ability to test, can be found at URL http:
http://127.0.0.1:5005
. We will test the model by sending an endpoint with the following JSON content to the http://127.0.0.1►005/model POST request:
{ "x": [ " .", " - 15 " ] }
In response, we should get this JSON:
[ [ ["", "", "", "", "", "", "", "", "", "."], ["O", "B-FAC", "O", "O", "O", "O", "O", "B-FAC", "I-FAC", "O"] ], [ ["", "", "-", "", "", "", "", "", "", "15", ""], ["O", "B-LOC", "I-LOC", "I-LOC", "I-LOC", "O", "O", "O", "O", "B-MONEY", "I-MONEY"] ] ]
For these examples, we will analyze the REST API DeepPavlov.
API DeepPavlov
Each DeepPavlov model has at least one input argument. In the REST API, arguments are named, their names are keys of the incoming dictionary. In most cases, an argument is the text that needs to be processed. You can learn more about arguments and values returned by models in the MODELS section of DeepPavlov documentation .
In the example, a list of two lines was passed to the argument x, each of which was given a separate markup. In DeepPavlov, all models accept as input a list (batch) of values that are processed independently.
The term “batch” refers to the field of machine learning and refers to a package of independent input values processed by an algorithm or neural network at the same time. This approach allows you to reduce (often - significantly) the model processing time of one element of the batch compared to the same value transferred to the input separately. But the result of processing is issued only after processing all the elements. Therefore, when forming the incoming batch, it will be necessary to take into account the speed of the model and the required processing time for each of its individual elements.
If there are several arguments of the DeepPavlov model, each of them has its own values batch, and at the output the model always gives one answer batch. Outgoing batches are the result of processing elements of incoming batches with the same index.
In the above example, the result of the model was a breakdown of each line into tokens (words and punctuation marks) and classification of the token relative to a named entity (name of organization, currency) that it represents. At the moment, the ner_ontonotes_bert_mult model is capable of recognizing 18 types of named entities, a detailed description can be found here .
Other out-of-the-box models of DeepPavlov
In addition to the NER in DeepPavlov, the following out-of the-box models are available at the time of writing:
Text Question Answering
The answer to the question to the text is a fragment of this text. Model config : squad_ru_bert_infer
Request example:
{ "context_raw": [ "DeepPavlov .", " - 15 ." ], "question_raw": [ " DeepPavlov?", " ?" ] }
Result:
[ [" ", 27, 31042.484375], ["15 ", 39, 1049.598876953125] ]
Insult detection
Identification of the presence of insult to the person to whom the text is addressed (at the time of writing, only for English). Model config: insults_kaggle_conv_bert
Request example:
{ "x": [ "Money talks, bullshit walks.", "You are not the brightest one." ] }
Result:
[ ["Not Insult"], ["Insult"] ]
Sentiment analysis
Classification of tonality of the text (positive, neutral, negative). Model config : rusentiment_elmo_twitter_cnn
Request example:
{ "x": [ " DeepPavlov.", " DeepPavlov.", " ." ] }
Result:
[ ["positive"], ["neutral"], ["negative"] ]
Paraphrase detection
Determining whether two different texts have the same meaning. Model config : stand_paraphraser_ru
Request:
{ "text_a": [ " , .", " ." ], "text_b": [ " , , .", " ." ] }
Result:
[ [1], [0] ]
The current list of all out-of-the-box DeepPavlov models can always be found here .
Conclusion
In this article, we met with the DeepPavlov API and some of the text processing capabilities of the library provided out of the box. It should be borne in mind that for any NLP task, the best result will be achieved when training the model on a data set corresponding to the subject domain (domain) of the task. In addition, even more models in principle cannot be trained for all occasions.
In the following articles, we will consider additional library settings, launch DeepPavlov from Docker, and after that we will move on to teaching models. And do not forget that DeepPavlov has a forum - ask your questions regarding the library and models. Thanks for attention!