allennlp.service.predictors

A Predictor is a wrapper for an AllenNLP Model that makes JSON predictions using JSON inputs. If you want to serve up a model through the web service (or using allennlp.commands.predict), you’ll need a Predictor that wraps it.

class allennlp.service.predictors.predictor.DemoModel(archive_file: str, predictor_name: str) → None[source]

Bases: object

A demo model is determined by both an archive file (representing the trained model) and a choice of predictor

predictor() → allennlp.service.predictors.predictor.Predictor[source]
class allennlp.service.predictors.predictor.Predictor(model: allennlp.models.model.Model, dataset_reader: allennlp.data.dataset_readers.dataset_reader.DatasetReader) → None[source]

Bases: allennlp.common.registrable.Registrable

a Predictor is a thin wrapper around an AllenNLP model that handles JSON -> JSON predictions that can be used for serving models through the web API or making predictions in bulk.

dump_line(outputs: typing.Dict[str, typing.Any]) → str[source]

If you don’t want your outputs in JSON-lines format you can override this function to output them differently.

classmethod from_archive(archive: allennlp.models.archival.Archive, predictor_name: str) → allennlp.service.predictors.predictor.Predictor[source]

Instantiate a Predictor from an Archive; that is, from the result of training a model. Optionally specify which Predictor subclass; otherwise, the default one for the model will be used.

load_line(line: str) → typing.Dict[str, typing.Any][source]

If your inputs are not in JSON-lines format (e.g. you have a CSV) you can override this function to parse them correctly.

predict_batch_json(inputs: typing.List[typing.Dict[str, typing.Any]], cuda_device: int = -1) → typing.List[typing.Dict[str, typing.Any]][source]
predict_json(inputs: typing.Dict[str, typing.Any], cuda_device: int = -1) → typing.Dict[str, typing.Any][source]
class allennlp.service.predictors.bidaf.BidafPredictor(model: allennlp.models.model.Model, dataset_reader: allennlp.data.dataset_readers.dataset_reader.DatasetReader) → None[source]

Bases: allennlp.service.predictors.predictor.Predictor

Wrapper for the BidirectionalAttentionFlow model.

class allennlp.service.predictors.decomposable_attention.DecomposableAttentionPredictor(model: allennlp.models.model.Model, dataset_reader: allennlp.data.dataset_readers.dataset_reader.DatasetReader) → None[source]

Bases: allennlp.service.predictors.predictor.Predictor

Wrapper for the DecomposableAttention model.

class allennlp.service.predictors.semantic_role_labeler.SemanticRoleLabelerPredictor(model: allennlp.models.model.Model, dataset_reader: allennlp.data.dataset_readers.dataset_reader.DatasetReader) → None[source]

Bases: allennlp.service.predictors.predictor.Predictor

Wrapper for the SemanticRoleLabeler model.

static make_srl_string(words: typing.List[str], tags: typing.List[str]) → str[source]
predict_batch_json(inputs: typing.List[typing.Dict[str, typing.Any]], cuda_device: int = -1) → typing.List[typing.Dict[str, typing.Any]][source]

Expects JSON that looks like [{"sentence": "..."}, {"sentence": "..."}, ...] and returns JSON that looks like

[
    {"words": [...],
     "verbs": [
        {"verb": "...", "description": "...", "tags": [...]},
        ...
        {"verb": "...", "description": "...", "tags": [...]},
    ]},
    {"words": [...],
     "verbs": [
        {"verb": "...", "description": "...", "tags": [...]},
        ...
        {"verb": "...", "description": "...", "tags": [...]},
    ]}
]
predict_json(inputs: typing.Dict[str, typing.Any], cuda_device: int = -1) → typing.Dict[str, typing.Any][source]

Expects JSON that looks like {"sentence": "..."} and returns JSON that looks like

{"words": [...],
 "verbs": [
    {"verb": "...", "description": "...", "tags": [...]},
    ...
    {"verb": "...", "description": "...", "tags": [...]},
]}
class allennlp.service.predictors.sentence_tagger.SentenceTaggerPredictor(model: allennlp.models.model.Model, dataset_reader: allennlp.data.dataset_readers.dataset_reader.DatasetReader) → None[source]

Bases: allennlp.service.predictors.predictor.Predictor

Wrapper for any model that takes in a sentence and returns a single set of tags for it. In particular, it can be used with the CrfTagger model and also the SimpleTagger model.

class allennlp.service.predictors.coref.CorefPredictor(model: allennlp.models.model.Model, dataset_reader: allennlp.data.dataset_readers.dataset_reader.DatasetReader) → None[source]

Bases: allennlp.service.predictors.predictor.Predictor

Wrapper for the CoreferenceResolver model.

class allennlp.service.predictors.constituency_parser.ConstituencyParserPredictor(model: allennlp.models.model.Model, dataset_reader: allennlp.data.dataset_readers.dataset_reader.DatasetReader) → None[source]

Bases: allennlp.service.predictors.predictor.Predictor

Wrapper for the SpanConstituencyParser model.

predict_batch_json(inputs: typing.List[typing.Dict[str, typing.Any]], cuda_device: int = -1) → typing.List[typing.Dict[str, typing.Any]][source]
predict_json(inputs: typing.Dict[str, typing.Any], cuda_device: int = -1) → typing.Dict[str, typing.Any][source]
class allennlp.service.predictors.simple_seq2seq.SimpleSeq2SeqPredictor(model: allennlp.models.model.Model, dataset_reader: allennlp.data.dataset_readers.dataset_reader.DatasetReader) → None[source]

Bases: allennlp.service.predictors.predictor.Predictor

Wrapper for the simple_seq2seq model.