def build_wds_dataset(dataset_name, transform, split='test', data_dir='root', cache_dir=None):
    """
    Load a dataset in WebDataset format. Either local paths or HTTP URLs can be specified.
    Expected file structure is:
    ```
    data_dir/
        train/
            nshards.txt
            0.tar
            1.tar
            ...
        test/
            nshards.txt
            0.tar
            1.tar
            ...
        classnames.txt
        zeroshot_classification_templates.txt
        dataset_type.txt
    ```
    Classnames and templates are required for zeroshot classification, while dataset type
    (equal to "retrieval") is required for zeroshot retrieval datasets.

    You can use the `clip_benchmark_export_wds` or corresponding API
    (`clip_benchmark.webdataset_builder.convert_dataset`) to convert datasets to this format.

    Set `cache_dir` to a path to cache the dataset, otherwise, no caching will occur.
    """
    import webdataset as wds

    def read_txt(fname):
        if '://' in fname:
            stream = os.popen("curl -L -s --fail '%s'" % fname, 'r')
            value = stream.read()
            if stream.close():
                raise FileNotFoundError('Failed to retreive data')
        else:
            with open(fname, 'r') as file:
                value = file.read()
        return value

    # Special handling for Huggingface datasets
    # Git LFS files have a different file path to access the raw data than other files
    if data_dir.startswith('https://huggingface.co/datasets'):
        # Format: https://huggingface.co/datasets/<USERNAME>/<REPO>/tree/<BRANCH>
        *split_url_head, _, url_path = data_dir.split('/', 7)
        url_head = '/'.join(split_url_head)
        metadata_dir = '/'.join([url_head, 'raw', url_path])
        tardata_dir = '/'.join([url_head, 'resolve', url_path])
    else:
        metadata_dir = tardata_dir = data_dir
    # Get number of shards
    nshards_fname = os.path.join(metadata_dir, split, 'nshards.txt')
    nshards = int(read_txt(nshards_fname))  # Do not catch FileNotFound, nshards.txt should be mandatory
    # Get dataset type (classification or retrieval)
    type_fname = os.path.join(metadata_dir, 'dataset_type.txt')
    try:
        dataset_type = read_txt(type_fname).strip().lower()
    except FileNotFoundError:
        # print("WARNING: dataset_type.txt not found, assuming type=classification")
        dataset_type = 'classification'
    #
    filepattern = os.path.join(tardata_dir, split, '{0..%d}.tar' % (nshards - 1))
    # Load webdataset (support WEBP, PNG, and JPG for now)
    if not cache_dir or not isinstance(cache_dir, str):
        cache_dir = None
    dataset = wds.WebDataset(filepattern, cache_dir=cache_dir).decode(
        wds.autodecode.ImageHandler('pil', extensions=['webp', 'png', 'jpg', 'jpeg']))

    # Load based on classification or retrieval task
    if dataset_type == 'retrieval':
        dataset = (dataset
                   .to_tuple(['webp', 'png', 'jpg', 'jpeg'], 'txt')
                   .map_tuple(transform, str.splitlines)
                   )
        dataset.classes = dataset.templates = None
    else:
        label_type = 'npy' if dataset_type == 'multilabel' else 'cls'  # Special case for multilabel
        dataset = (dataset
                   .to_tuple(['webp', 'png', 'jpg', 'jpeg'], label_type)
                   .map_tuple(transform, None)
                   )
        # Get class names if present
        classnames_fname = os.path.join(metadata_dir, 'classnames.txt')
        try:
            dataset.classes = [line.strip() for line in read_txt(classnames_fname).splitlines() if line.strip()]
        except FileNotFoundError:
            print('WARNING: classnames.txt not found')
            dataset.classes = None
        # Get zeroshot classification templates if present
        templates_fname = os.path.join(metadata_dir, 'zeroshot_classification_templates.txt')
        try:
            dataset.templates = [line.strip() for line in read_txt(templates_fname).splitlines() if line.strip()]
        except FileNotFoundError:
            print('WARNING: zeroshot_classification_templates.txt not found')
            dataset.templates = None

    return dataset

    Load a dataset in WebDataset format. Either local paths or HTTP URLs can be specified.
    Expected file structure is:
    ```
    data_dir/
        train/
            nshards.txt
            0.tar
            1.tar
            ...
        test/
            nshards.txt
            0.tar
            1.tar
            ...
        classnames.txt
        zeroshot_classification_templates.txt
        dataset_type.txt
    ```
    Classnames and templates are required for zeroshot classification, while dataset type
    (equal to "retrieval") is required for zeroshot retrieval datasets.

    You can use the `clip_benchmark_export_wds` or corresponding API
    (`clip_benchmark.webdataset_builder.convert_dataset`) to convert datasets to this format.

    Set `cache_dir` to a path to cache the dataset, otherwise, no caching will occur.
    
clip_benchmark/clip_benchmark/datasets/builder.py