Broken Image render on the hub website
Opened by FrancescoSaverioZuppichini (closed)

### Describe the bug

Hi :wave: 

Not sure if this is the right place to ask, but I am trying to load a huge amount of datasets on the hub (:partying_face: ) but I am facing a little issue with the `image` type

![image](https://user-images.githubusercontent.com/15908060/228587875-427a37f1-3a31-4e17-8bbe-0f759003910d.png)

See this [dataset](https://huggingface.co/datasets/Francesco/cell-towers), basically for some reason the first image has numerical bytes inside, not sure if that is okay, but the image render feature **doesn't work**

So the dataset is stored in the following way

```python
    builder.download_and_prepare(output_dir=str(output_dir))

    ds = builder.as_dataset(split="train")
    # [NOTE] no idea how to push it from the builder folder
    ds.push_to_hub(repo_id=repo_id)
    builder.as_dataset(split="validation").push_to_hub(repo_id=repo_id)
    ds = builder.as_dataset(split="test")
    ds.push_to_hub(repo_id=repo_id)
   ```
   
   The build is this class
   
   ```python
       class COCOLikeDatasetBuilder(datasets.GeneratorBasedBuilder):

        VERSION = datasets.Version("1.0.0")

        def _info(self):
            features = datasets.Features(
                {
                    "image_id": datasets.Value("int64"),
                    "image": datasets.Image(),
                    "width": datasets.Value("int32"),
                    "height": datasets.Value("int32"),
                    "objects": datasets.Sequence(
                        {
                            "id": datasets.Value("int64"),
                            "area": datasets.Value("int64"),
                            "bbox": datasets.Sequence(
                                datasets.Value("float32"), length=4
                            ),
                            "category": datasets.ClassLabel(names=categories),
                        }
                    ),
                }
            )
            return datasets.DatasetInfo(
                description=description,
                features=features,
                homepage=homepage,
                license=license,
                citation=citation,
            )

        def _split_generators(self, dl_manager):
            archive = dl_manager.download(url)

            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "annotation_file_path": "train/_annotations.coco.json",
                        "files": dl_manager.iter_archive(archive),
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    gen_kwargs={
                        "annotation_file_path": "test/_annotations.coco.json",
                        "files": dl_manager.iter_archive(archive),
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    gen_kwargs={
                        "annotation_file_path": "valid/_annotations.coco.json",
                        "files": dl_manager.iter_archive(archive),
                    },
                ),
            ]

        def _generate_examples(self, annotation_file_path, files):
            def process_annot(annot, category_id_to_category):
                return {
                    "id": annot["id"],
                    "area": annot["area"],
                    "bbox": annot["bbox"],
                    "category": category_id_to_category[annot["category_id"]],
                }

            image_id_to_image = {}
            idx = 0

            # This loop relies on the ordering of the files in the archive:
            # Annotation files come first, then the images.
            for path, f in files:
                file_name = os.path.basename(path)
                if annotation_file_path in path:
                    annotations = json.load(f)
                    category_id_to_category = {
                        category["id"]: category["name"]
                        for category in annotations["categories"]
                    }
                    print(category_id_to_category)
                    image_id_to_annotations = collections.defaultdict(list)
                    for annot in annotations["annotations"]:
                        image_id_to_annotations[annot["image_id"]].append(annot)
                    image_id_to_image = {
                        annot["file_name"]: annot for annot in annotations["images"]
                    }
                elif file_name in image_id_to_image:
                    image = image_id_to_image[file_name]
                    objects = [
                        process_annot(annot, category_id_to_category)
                        for annot in image_id_to_annotations[image["id"]]
                    ]
                    print(file_name)
                    yield idx, {
                        "image_id": image["id"],
                        "image": {"path": path, "bytes": f.read()},
                        "width": image["width"],
                        "height": image["height"],
                        "objects": objects,
                    }
                    idx += 1
```

Basically, I want to add to the hub every dataset I come across on coco format

Thanks 

Fra

### Steps to reproduce the bug

In this case, you can just navigate on the [dataset](https://huggingface.co/datasets/Francesco/cell-towers)

### Expected behavior

I was expecting the image rendering feature to work

### Environment info

Not a lot to share, I am using `datasets` from a fresh venv

Comments:
Hi! 

You can fix the viewer by adding the `dataset_info` YAML field deleted in https://huggingface.co/datasets/Francesco/cell-towers/commit/b95b59ddd91ebe9c12920f0efe0ed415cd0d4298 back to the metadata section of the card. 

To avoid this issue in the feature, you can use `huggingface_hub`'s [RepoCard](https://huggingface.co/docs/huggingface_hub/package_reference/cards) API  to update the dataset card instead of `upload_file`:
```python
from huggingface_hub import DatasetCard
# Load card
card = DatasetCard.load("<namespace>/<repo_id>")
# Modify card content
card.content = ...
# Push card to the Hub
card.push_to_hub("<namespace>/<repo_id>")
```

However, the best solution would be to use the features info stored in the header of the Parquet shards generated with `push_to_hub` on the viewer side to avoid unexpected issues such as this one. This shouldn't be too hard to address.

Thanks for reporting @FrancescoSaverioZuppichini.

For future issues with your specific dataset, you can use its "Community" tab to start a conversation: https://huggingface.co/datasets/Francesco/cell-towers/discussions/new

Thanks @albertvillanova , @mariosasko I was not aware of this requirement from the doc (must have skipped :sweat_smile: )

Confirmed, adding back `dataset_info` fixed the issu