Fairseq Random Sentence Generator for Language Translation

  • Share this:

Code introduction


This code defines a function that uses the Fairseq library to generate random sentences between a given source and target language. It first loads the dictionaries and the model, then generates a random sample and uses the generator to produce the sentence.


Technology Stack : Fairseq, PyTorch, Dictionary, FairseqModel, SequenceGenerator

Code Type : The type of code

Code Difficulty :


                
                    
def random_sentence_generator(src_lang, tgt_lang, model_path, max_len=50):
    import torch
    from fairseq.models import FairseqModel
    from fairseq.data import Dictionary
    from fairseq.sequence_generator import SequenceGenerator

    # Load the dictionary
    src_dict = Dictionary.load(f'{model_path}/dict.{src_lang}.txt')
    tgt_dict = Dictionary.load(f'{model_path}/dict.{tgt_lang}.txt')

    # Load the model
    model = FairseqModel.from_pretrained(model_path, checkpoint_file='checkpoint_best.pt')

    # Initialize the generator
    generator = SequenceGenerator([model], [tgt_dict])

    # Generate a random sentence
    sample = torch.randint(0, src_dict.nwords(), (1, max_len))
    sample = sample.to('cuda' if torch.cuda.is_available() else 'cpu')
    with torch.no_grad():
        hypotheses = generator.generate(sample, max_len=max_len)
        generated_sentence = tgt_dict.decode(hypotheses[0])

    return generated_sentence