Hello everyone! We are launching a series of articles dedicated to solving practical problems related to Natural Language Processing (NLP) and creating conversational agents (chatbots) using an open-source library , developed by our team at the Laboratory of Neural Systems and Deep Learning at MIPT. The main goal of this series is to familiarize a wide range of developers with DeepPavlov and demonstrate how to tackle practical NLP tasks without possessing deep knowledge in Machine Learning or a PhD in Mathematics.
NLP tasks include sentiment analysis, named entity parsing, and determining what the user wants from your bot: to order pizza or to obtain information, among other things. You can read more about NLP tasks and methods .
In this article, we will explain how to launch a REST server with pre-trained NLP models ready for use without any additional configuration or training.

Installing DeepPavlov
The instructions provided here and onward are for Linux. For Windows, refer to our
- Create and activate a virtual environment with the currently supported version of Python:
virtualenv env -p python3.7 source env/bin/activate - Install DeepPavlov in the virtual environment:
pip install deeppavlov
Running the REST server with the DeepPavlov model
Before we launch the server with the DeepPavlov model for the first time, it would be useful to discuss some architectural features of the library.
Any model in DP consists of:
- Python code;
- Downloadable components — serialized training results on specific data (embeddings, weights of neural networks, etc.);
- A configuration file (hereinafter referred to as the config), which contains information about the classes used by the model, URLs for downloading components, Python dependencies, and more.
We will discuss more about what is under the hood of DeepPavlov in the following articles; for now, it is enough to know that:
- Any model in DeepPavlov is identified by the name of its config;
- To run the model, its components must be downloaded from DeepPavlov servers;
- Additionally, the Python libraries used by the model must be installed.
The first model we will launch is a multilingual Named Entity Recognition (NER). This model classifies the words of the text according to the type of named entities they belong to (proper names, geographical locations, currency names, and others). The config name for the latest version of NER is:
ner_ontonotes_bert_mult
Launching the REST server with the model:
- Installing the model dependencies specified in its config into the active virtual environment:
python -m deeppavlov install ner_ontonotes_bert_mult - Downloading the serialized model components from the DeepPavlov servers:
python -m deeppavlov download ner_ontonotes_bert_multThe serialized components will be downloaded to the DeepPavlov home directory, which is by default located at
~/.deeppavlovDuring the download, the hash of the already downloaded components is verified against the hashes of the components on the server. If they match, the download is skipped, and the already existing files are used. The sizes of the downloaded components can vary on average from 0.5 to 8 Gb, in some cases reaching 20 Gb after extraction.
- Starting the REST server with the model:
python -m deeppavlov riseapi ner_ontonotes_bert_mult -p 5005
As a result of executing this command, a REST server with the model will be launched on port 5005 of the host machine (the default port is 5000).
After initializing the model, Swagger with API documentation and the ability to test it can be found at the URL http://127.0.0.1:5005. Let's test the model by sending a request to the endpoint http://127.0.0.1:5005/model A POST request with the following JSON content:
{
"x": [
"You can reach MIPT by train from Savyolovsky Station.",
"In southwestern Russia, a stack of rye is valued at 15 hryvnias"
]
}In response, we should receive the following JSON:
[
[
["In", "MIPT", "you", "can", "reach", "by", "train", "from", "Savyolovsky", "Station", "."],
["O", "B-FAC", "O", "O", "O", "O", "O", "B-FAC", "I-FAC", "O"]
],
[
["In", "southwest", "-", "Russia", "a", "stack", "of", "rye", "is", "valued", "at", "15", "hryvnias"],
["O", "B-LOC", "I-LOC", "I-LOC", "I-LOC", "O", "O", "O", "O", "B-MONEY", "I-MONEY"]
]
]In these examples, we will explore the DeepPavlov REST API.
DeepPavlov API
Each DeepPavlov model has at least one incoming argument. In the REST API, the arguments are named, and their names are the keys of the incoming dictionary. In most cases, the argument is the text that needs to be processed. More details about the arguments and the values returned by the models can be found in the MODELS section of the documentation.
In the example, a list of two strings was passed to the argument x, with each string receiving separate markup. In DeepPavlov, all models accept a list (batch) of values as input, which are processed independently.
The term 'batch' refers to the field of machine learning and implies a package of independent incoming values processed by the algorithm or neural network simultaneously. This approach allows for a significant reduction in processing time for a single batch element compared to the same value being processed individually. However, the processing result is only delivered after all elements have been processed. Therefore, when forming an incoming batch, it is essential to consider the model's processing speed and the required processing time for each individual element.
If the DeepPavlov model has multiple arguments, each one receives its own batch of values, and the model always outputs a single batch of responses. The elements of the outgoing batch are the results of processing the elements of the incoming batches with the same index.
In the provided example, the model's output was the tokenization of each string into tokens (words and punctuation) and the classification of each token in relation to the named entity (organization name, currency) it represents. Currently, the model ner_ontonotes_bert_mult is capable of recognizing 18 types of named entities, and detailed descriptions can be found .
Other out-of-the-box models of DeepPavlov
In addition to NER, the following out-of-the-box models are available in DeepPavlov at the time of writing:
Text Question Answering
The answer to a question about the text is provided as a fragment of that text. Model config: squad_ru_bert_infer
Example request:
{
"context_raw": [
"DeepPavlov is developed by the MIPT laboratory.",
"In Southwestern Russia, a stack of rye was valued at 15 hryvnias."
],
"question_raw": [
"Who develops DeepPavlov?",
"How much did the stack of rye cost in Russia?"
]
}Result:
[
["MIPT laboratory", 27, 31042.484375],
["15 hryvnias", 39, 1049.598876953125]
]
Insult Detection
Detection of the presence of an insult directed at the subject of the text (currently — only for the English language). Model config: insults_kaggle_conv_bert
Example request:
{
"x": [
"Money talks, bullshit walks.",
"You are not the brightest one."
]
}Result:
[
["Not Insult"],
["Insult"]
]Sentiment Analysis
Classification of the sentiment of the text (positive, neutral, negative). Model config: rusentiment_elmo_twitter_cnn
Example request:
{
"x": [
"I like the DeepPavlov library.",
"I've heard about the DeepPavlov library.",
"I'm annoyed by trolls and anonymous users."
]
}Result:
[
["positive"],
["neutral"],
["negative"]
]Paraphrase Detection
Determining whether two different texts have the same meaning. Model configuration: stand_paraphraser_ru
Request:
{
"text_a": [
"The city is falling asleep, the Mafia is waking up.",
"The US President threatened to terminate the agreement with Germany."
],
"text_b": [
"Night falls, all the city’s residents went to sleep, and the criminals woke up.",
"Germany does not intend to yield to threats from the US."
]
}Result:
[
[1],
[0]
]An up-to-date list of all out-of-the-box DeepPavlov models can always be found .
Conclusion
In this article, we introduced the DeepPavlov API and some text processing capabilities provided 'out of the box'. However, it should be noted that for any NLP task, the best results will be achieved by training the model on a dataset appropriate to the subject area (domain) of the task. Furthermore, even more models cannot be trained for all possible scenarios.
In the following articles, we will explore additional library settings, running DeepPavlov from Docker, and then move on to training models. And don’t forget that DeepPavlov has – ask your questions regarding the library and models. Thank you for your attention!
Source: habr.com
