Random Allennlp Predictor Usage with Sample Text

  • Share this:

Code introduction


This function randomly selects a predictor from the Allennlp library and uses a randomly generated text for prediction.


Technology Stack : Allennlp, Predictor, Instance, Tokenizer, Vocabulary

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from allennlp.predictors.predictor import Predictor
from allennlp.data import Instance
from allennlp.data.tokenizers import Tokenizer
from allennlp.data.vocabulary import Vocabulary

def random_predictor_usage():
    # List of available predictors in Allennlp
    predictors = ["bert-base-uncased", "squad", "text-classifier", "coref", "sentiment"]
    
    # Randomly select a predictor
    predictor_name = random.choice(predictors)
    
    # Load the predictor
    predictor = Predictor.from_path(f"https://storage.googleapis.com/allennlp-public-models/{predictor_name}")
    
    # Generate a random text for prediction
    text = "The quick brown fox jumps over the lazy dog."
    
    # Create an instance for the predictor
    instance = Instance.from_tokens(predictor.tokenizer.tokenize(text), label=None)
    
    # Get the vocabulary for the instance
    vocab = Vocabulary.from_files(predictor.tokenizer.vocab_path)
    
    # Predict using the predictor
    prediction = predictor.predict(instance=instance, vocabulary=vocab)
    
    return prediction

# Code Information