DeepPavlov for Developers: #1 NLP Tools and Chatbot Creation

Hi all! We are opening a series of articles devoted to solving practical problems related to natural language processing (Natural Language Processing or simply NLP) and creating conversational agents (chat bots) using an open-source library DeepPavlov, which is being developed by our team at the MIPT Laboratory of Neural Systems and Deep Learning. The main goal of the cycle is to introduce a wide range of developers to DeepPavlov and show how you can solve applied NLP problems without having deep knowledge of Machine Learning and PhD in Mathematics.

NLP tasks include determining the tone of the text, parsing named entities, determining what the interlocutor wants from your bot: order a pizza or get background information, and much more. You can read more about the tasks and methods of NLP here.

In this article, we will show you how to run a REST server with pre-trained NLP models ready to use without any additional configuration or training.

DeepPavlov for Developers: #1 NLP Tools and Chatbot Creation

Installing DeepPavlov

Instructions for Linux will be given here and below. 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
    

Launching a REST server with the DeepPavlov model

Before we start 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 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 it is enough for us 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 need to install the Python libraries used by it.

The first model we will run will be multilingual Named Entity Recognition (NER). The model classifies the words of the text according to the type of named entities to which they belong (proper names, geographical names, currency names, and others). Config name for the most recent version of NER:

ner_ontonotes_bert_mult

We start the REST server with the model:

  1. Install the model dependencies specified in its config into the active virtual environment:
    python -m deeppavlov install ner_ontonotes_bert_mult
    
  2. Download serialized model components from DeepPavlov servers:
    python -m deeppavlov download ner_ontonotes_bert_mult
    

    The serialized components will be downloaded to the DeepPavlov home directory, which is located by default

    ~/.deeppavlov

    When downloading, the hash of already downloaded components is compared with the hashes of the components located on the server. If there is a match, the download is skipped and existing files are used. The sizes of downloaded components can vary on average from 0.5 to 8 Gb, in some cases reaching 20 Gb after unzipping.

  3. We start the REST server with the model:
    python -m deeppavlov riseapi ner_ontonotes_bert_mult -p 5005
    

As a result of executing this command, the REST server with the model will be launched on port 5005 of the host machine (the default port is 5000).

After the model is initialized, Swagger with API documentation and the ability to test can be found at the URL http://127.0.0.1:5005. Let's test the model by sending it to the endpoint http://127.0.0.1:5005/model POST request with the following JSON content:

{
  "x": [
    "В МФТИ можно добраться на электричке с Савёловского Вокзала.",
    "В юго-западной Руси стог жита оценен в 15 гривен"
  ]
}

In response, we should receive the following 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"]
  ]
]

Using these examples, we will analyze the DeepPavlov REST API.

DeepPavlov API

Each DeepPavlov model has at least one input argument. In the REST API, the arguments are named, their names are the keys of the incoming dictionary. In most cases, the argument is the text to be processed. More information about arguments and values ​​returned by models can be found in the MODELS section of the documentation. DeepPavlov

In the example, a list of two strings was passed to the x argument, each of which was given a separate markup. In DeepPavlov, all models take 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 batch of independent input values ​​processed by an algorithm or neural network at the same time. This approach allows you to reduce (often significantly) the processing time of one batch element by the model compared to the same value passed to the input separately. But the result of processing is given only after processing of all elements. Therefore, when generating an 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 receives its own batch of values, and at the output the model always produces one batch of answers. The elements of the outgoing batch are the results of processing the elements of the incoming batches with the same index.

In the above example, the result of the model was the breakdown of each line into tokens (words and punctuation marks) and the classification of the token relative to the named entity (organization name, currency) that it represents. At the moment the model ner_ontonotes_bert_mult is able to recognize 18 types of named entities, a detailed description can be found here.

Other out-of-the-box DeepPavlov models

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 the question to the text is a fragment of this text. Model config: squad_en_bert_infer

Request example:

{
  "context_raw": [
    "DeepPavlov разрабатывается лабораторией МФТИ.",
    "В юго-западной Руси стог жита оценен в 15 гривен."
  ],
  "question_raw": [
    "Кем разрабатывается DeepPavlov?",
    "Сколько стоил стог жита на Руси?"
  ]
}

Result:

[
  ["лабораторией МФТИ", 27, 31042.484375],
  ["15 гривен", 39, 1049.598876953125]
]

Stroke Detection

Identification of the presence of an insult to the person to whom the text is addressed (at the time of this 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 the sentiment 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_en

Inquiry:

{
  "text_a": [
    "Город погружается в сон, просыпается Мафия.",
    "Президент США пригрозил расторжением договора с Германией."
  ],
  "text_b": [
    "Наступает ночь, все жители города пошли спать, а преступники проснулись.",
    "Германия не собирается поддаваться угрозам со стороны США."
  ]
}

Result:

[
  [1],
  [0]
]

An up-to-date list of all DeepPavlov out-of-the-box models can always be found here.

Conclusion

In this article, we got acquainted with the DeepPavlov API and some of the text processing features of the library provided out of the box. At the same time, it should be borne in mind that for any NLP task, the best result will be achieved when the model is trained on a data set corresponding to the subject area (domain) of the task. In addition, even more models, in principle, cannot be trained for all occasions.

In the following articles, we will look at additional library settings, launch DeepPavlov from Docker, and then move on to training models. And don't forget that DeepPavlov has forum - ask your questions about the library and models. Thank you for your attention!

Source: habr.com

Add a comment