'AttributeError: module 'torchtext' has no attribute 'legacy'

I am trying to use torchtext to process test data, however, I get the error: "AttributeError: module 'torchtext' has no attribute 'legacy'", when I run the following code. Can anyone please guide me what the issue here? I am using python 3.10.4. Thanks

import pandas as pd
import torch
import torchtext
import spacy


def prep_data(file_path):

    TEXT=torchtext.legacy.data.Field(tokenize='spacy', tokenizer_language='en_core_web_sm')
    LABEL=torchtext.legacy.data.LabelField(dtype=torch.long)

    fields=[('clean_text', TEXT), ('label',LABEL)]
    dataset = torchtext.legacy.data.TabularDataset(
    path=file_path, format='csv',
    skip_header=True, fields=fields)

    print(dataset.examples[0])


   if __name__=="__main__":
       train_path='./data/train.csv'
       test_path='./data/test.csv'
       prep_data(train_path)


Solution 1:[1]

I addressed the same issue by updating the torchtext.

pip install torchtext==0.9

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tigers