Skip to content

Using Pretrained AllenNLP Models#

In this tutorial, we show how to use run the pretrained models in AllenNLP to make predictions. This tutorial uses the Named Entity Recognition model, but the same procedure applies to any of the available pretrained models.

Making Predictions on the Command Line#

Our demo lists the featured models in AllenNLP, as well as examples for how to run the model on the command line. For example, under the "Usage" tab of Named Entity Recognition model there is a "Prediction" section that reveals the following example.

echo '{"sentence": "Did Uriah honestly think he could beat The Legend of Zelda in under three hours?"}' > ner-examples.jsonl
allennlp predict \ \

If no predictor is specified (as in the above example), then AllenNLP will use the default predictor. You will need to create a custom predictor if you want to customize your input or output format. Here is an example of running the above example but specifying the predictor explicitly.

allennlp predict \ \
    ner-examples.jsonl \
    --predictor sentence-tagger

Making Predictions Programatically#

You can also make predictions from python, using AllenNLP as a library. The arguments will be the same as the JSON fields in the example, and they vary by the particular predictor. The following code example uses the default predictor (sentence-tagger) for the NER model.

from allennlp.predictors import Predictor
predictor = Predictor.from_path("")
results = predictor.predict(sentence="Did Uriah honestly think he could beat The Legend of Zelda in under three hours?")
for word, tag in zip(results["words"], results["tags"]):

And the sample output:

Did O
Uriah   U-PER
honestly    O
think   O
he  O
could   O
beat    O
Legend  I-MISC
of  I-MISC
Zelda   L-MISC
in  O
under   O
three   O
hours   O
?   O