Home

Awesome

Build Status Website Status

<img src="docs/deploy-max-to-ibm-cloud-with-kubernetes-button.png" width="400px">

IBM Developer Model Asset Exchange: Named Entity Tagger

This repository contains code to instantiate and deploy a named entity recognition model. This model annotates each word or term in a piece of text with a tag representing the entity type, taken from a list of 17 entity tags from the The Groningen Meaning Bank (GMB) dataset. These tags cover 8 types of named entities: persons, locations, organizations, geo-political entities, artifacts, events, natural objects, time, as well as a tag for 'no entity' (see the GMB dataset manual page for the full entity definitions). The entity types furthermore may be tagged with either a "B-" tag or "I-" tag. A "B-" tag indicates the first term of a new entity (or only term of a single-term entity), while subsequent terms in an entity will have an "I-" tag. For example, "New York" would be tagged as ["B-GEO", "I-GEO"] while "London" would be tagged as "B-GEO".

The model consists of a recurrent neural network architecture with a bi-directional LSTM layer applied to character-level embedding vectors, which are combined with pre-trained GloVe 6B word vector embeddings; finally a second bi-directional LSTM layer is applied to this combined vector representation. The input to the model is a string and the output is a list of terms in the input text (after applying simple tokenization), together with a list of predicted entity tags for each term.

The model is based on Guillaume Genthial's Named Entity Recognition with TensorFlow model, adapted to use the Keras framework. The model was trained on a subset of the GMB dataset version 2.2.0 by the IBM CODAIT team. The model files are hosted on IBM Cloud Object Storage. The code in this repository deploys the model as a web service in a Docker container. This repository was developed as part of the IBM Code Model Asset Exchange and the public API is powered by IBM Cloud.

Model Metadata

DomainApplicationIndustryFrameworkTraining DataInput Data Format
Natural Language ProcessingNamed Entity RecognitionGeneralKerasGroningen Meaning Bank (GMB) DatasetText

Note the underlying dataset is primarily based on news articles and so the model should perform relatively better on input related to general news, business, geo-political and sporting events. The dataset covers a period up until 2014, which governs the entities the model will be aware of.

References

Licenses

ComponentLicenseLink
This repositoryApache 2.0LICENSE
Model WeightsApache 2.0LICENSE
Model Code (3rd party)Apache 2.0TensorFlow NER Repo License

Pre-requisites:

Deployment options

Deploy from Quay

To run the docker image, which automatically starts the model serving API, run:

$ docker run -it -p 5000:5000 quay.io/codait/max-named-entity-tagger

This will pull a pre-built image from the Quay.io container registry (or use an existing image if already cached locally) and run it. If you'd rather checkout and build the model locally you can follow the run locally steps below.

Deploy on Red Hat OpenShift

You can deploy the model-serving microservice on Red Hat OpenShift by following the instructions for the OpenShift web console or the OpenShift Container Platform CLI in this tutorial, specifying quay.io/codait/max-named-entity-tagger as the image name.

Deploy on Kubernetes

You can also deploy the model on Kubernetes using the latest docker image on Quay.

On your Kubernetes cluster, run the following commands:

$ kubectl apply -f https://github.com/IBM/MAX-Named-Entity-Tagger/raw/master/max-named-entity-tagger.yaml

The model will be available internally at port 5000, but can also be accessed externally through the NodePort.

A more elaborate tutorial on how to deploy this MAX model to production on IBM Cloud can be found here.

Run Locally

  1. Build the Model
  2. Deploy the Model
  3. Use the Model
  4. Development
  5. Cleanup

1. Build the Model

Clone this repository locally. In a terminal, run the following command:

$ git clone https://github.com/IBM/MAX-Named-Entity-Tagger.git

Change directory into the repository base folder:

$ cd MAX-Named-Entity-Tagger

To build the docker image locally, run:

$ docker build -t max-named-entity-tagger .

All required model assets will be downloaded during the build process. Note that currently this docker image is CPU only (we will add support for GPU images later).

2. Deploy the Model

To run the docker image, which automatically starts the model serving API, run:

$ docker run -it -p 5000:5000 max-named-entity-tagger

3. Use the Model

The API server automatically generates an interactive Swagger documentation page. Go to http://localhost:5000 to load it. From there you can explore the API and also create test requests.

Use the model/predict endpoint to post text and get predicted tags from the API. Below are a few example sentences you can use:

John lives in Brussels and works for the EU.

Matthew drove to San Francisco on Friday to watch the Golden State Warriors play the Knicks.

Marina works for IBM and lives in London. She supports Chelsea FC.

INSERT SWAGGER UI SCREENSHOT HERE

You can also test it on the command line, for example:

$ curl -X POST -H 'Content-Type: application/json' -d '{"text":"John lives in Brussels and works for the EU"}' 'http://localhost:5000/model/predict'

You should see a JSON response like that below:

{
    "status": "ok",
    "prediction": {
        "entities": [
            "B-PER",
            "O",
            "O",
            "B-GEO",
            "O",
            "O",
            "O",
            "O",
            "B-ORG"
        ],
        "input_terms": [
            "John",
            "lives",
            "in",
            "Brussels",
            "and",
            "works",
            "for",
            "the",
            "EU"
        ]
    }
}

4. Development

To run the Flask API app in debug mode, edit config.py to set DEBUG = True under the application settings. You will then need to rebuild the docker image (see step 1).

5. Cleanup

To stop the Docker container, type CTRL + C in your terminal.

Resources and Contributions

If you are interested in contributing to the Model Asset Exchange project or have any queries, please follow the instructions here.