Repository: huggingface/transformers
Pull request: fix Image.open failure in case "tests/models/prompt_depth_anything/te…


- Intel XPU: @IlyasMoutawwakil

```
======================================================================== FAILURES ========================================================================
_________________________________________________ PromptDepthAnythingModelIntegrationTest.test_inference _________________________________________________

self = <tests.models.prompt_depth_anything.test_modeling_prompt_depth_anything.PromptDepthAnythingModelIntegrationTest testMethod=test_inference>

    def test_inference(self):
        image_processor = AutoImageProcessor.from_pretrained("depth-anything/prompt-depth-anything-vits-hf")
        model = PromptDepthAnythingForDepthEstimation.from_pretrained(
            "depth-anything/prompt-depth-anything-vits-hf"
        ).to(torch_device)

>       image = prepare_img()
                ^^^^^^^^^^^^^

tests/models/prompt_depth_anything/test_modeling_prompt_depth_anything.py:264:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tests/models/prompt_depth_anything/test_modeling_prompt_depth_anything.py:220: in prepare_img
    image = Image.open(requests.get(url, stream=True).raw)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

fp = <_io.BytesIO object at 0x712c38143100>, mode = 'r', formats = ['BMP', 'DIB', 'GIF', 'JPEG', 'PPM', 'PNG', ...]

    def open(fp, mode="r", formats=None):
        """
        Opens and identifies the given image file.

        This is a lazy operation; this function identifies the file, but
        the file remains open and the actual image data is not read from
        the file until you try to process the data (or call the
        :py:meth:`~PIL.Image.Image.load` method).  See
        :py:func:`~PIL.Image.new`. See :ref:`file-handling`.

        :param fp: A filename (string), pathlib.Path object or a file object.
           The file object must implement ``file.read``,
           ``file.seek``, and ``file.tell`` methods,
           and be opened in binary mode. The file object will also seek to zero
           before reading.
        :param mode: The mode.  If given, this argument must be "r".
        :param formats: A list or tuple of formats to attempt to load the file in.
           This can be used to restrict the set of formats checked.
           Pass ``None`` to try all supported formats. You can print the set of
           available formats by running ``python3 -m PIL`` or using
           the :py:func:`PIL.features.pilinfo` function.
        :returns: An :py:class:`~PIL.Image.Image` object.
        :exception FileNotFoundError: If the file cannot be found.
        :exception PIL.UnidentifiedImageError: If the image cannot be opened and
           identified.
        :exception ValueError: If the ``mode`` is not "r", or if a ``StringIO``
           instance is used for ``fp``.
        :exception TypeError: If ``formats`` is not ``None``, a list or a tuple.
        """

        if mode != "r":
            msg = f"bad mode {repr(mode)}"
            raise ValueError(msg)
        elif isinstance(fp, io.StringIO):
            msg = (
                "StringIO cannot be used to open an image. "
                "Binary data must be used instead."
            )
            raise ValueError(msg)

        if formats is None:
            formats = ID
        elif not isinstance(formats, (list, tuple)):
            msg = "formats must be a list or tuple"
            raise TypeError(msg)

        exclusive_fp = False
        filename = ""
        if isinstance(fp, Path):
            filename = str(fp.resolve())
        elif is_path(fp):
            filename = fp

        if filename:
            fp = builtins.open(filename, "rb")
            exclusive_fp = True

        try:
            fp.seek(0)
        except (AttributeError, io.UnsupportedOperation):
            fp = io.BytesIO(fp.read())
            exclusive_fp = True

        prefix = fp.read(16)

        preinit()

        accept_warnings = []

        def _open_core(fp, filename, prefix, formats):
            for i in formats:
                i = i.upper()
                if i not in OPEN:
                    init()
                try:
                    factory, accept = OPEN[i]
                    result = not accept or accept(prefix)
                    if type(result) in [str, bytes]:
                        accept_warnings.append(result)
                    elif result:
                        fp.seek(0)
                        im = factory(fp, filename)
                        _decompression_bomb_check(im.size)
                        return im
                except (SyntaxError, IndexError, TypeError, struct.error):
                    # Leave disabled by default, spams the logs with image
                    # opening failures that are entirely expected.
                    # logger.debug("", exc_info=True)
                    continue
                except BaseException:
                    if exclusive_fp:
                        fp.close()
                    raise
            return None

        im = _open_core(fp, filename, prefix, formats)

        if im is None and formats is ID:
            checked_formats = formats.copy()
            if init():
                im = _open_core(
                    fp,
                    filename,
                    prefix,
                    tuple(format for format in formats if format not in checked_formats),
                )

        if im:
            im._exclusive_fp = exclusive_fp
            return im

        if exclusive_fp:
            fp.close()
        for message in accept_warnings:
            warnings.warn(message)
        msg = "cannot identify image file %r" % (filename if filename else fp)
>       raise UnidentifiedImageError(msg)
E       PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x712c38143100>

/opt/venv/lib/python3.12/site-packages/PIL/Image.py:3280: UnidentifiedImageError
```


The root cause of this error is that the content retrieved is not actual image bytes. A common case is that a GitHub blob URL with ?raw=true returns an HTML or redirect page instead of raw image data.




Changed files:
filename: tests/models/llava_next/test_modeling_llava_next.py
patch: @@ -130,7 +130,7 @@ def test_flash_attention_2_padding_matches_padding_free_with_position_ids(self):
 class LlavaNextForConditionalGenerationIntegrationTest(unittest.TestCase):
     def setUp(self):
         self.processor = AutoProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
-        url = "https://github.com/haotian-liu/LLaVA/blob/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg?raw=true"
+        url = "https://raw.githubusercontent.com/haotian-liu/LLaVA/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg"
         self.image = Image.open(requests.get(url, stream=True).raw)
 
         self.prompt = "
 <image>\nWhat is shown in this image? 
"

filename: tests/models/prompt_depth_anything/test_modeling_prompt_depth_anything.py
patch: @@ -216,14 +216,14 @@ def _validate_backbone_init():
 
 
 def prepare_img():
-    url = "https://github.com/DepthAnything/PromptDA/blob/main/assets/example_images/image.jpg?raw=true"
+    url = "https://raw.githubusercontent.com/DepthAnything/PromptDA/main/assets/example_images/image.jpg"
     image = Image.open(requests.get(url, stream=True).raw)
     return image
 
 
 def prepare_prompt_depth():
     prompt_depth_url = (
-        "https://github.com/DepthAnything/PromptDA/blob/main/assets/example_images/arkit_depth.png?raw=true"
+        "https://raw.githubusercontent.com/DepthAnything/PromptDA/main/assets/example_images/arkit_depth.png"
     )
     prompt_depth = Image.open(requests.get(prompt_depth_url, stream=True).raw)
     return prompt_depth

filename: tests/models/video_llama_3/test_modeling_video_llama_3.py
patch: @@ -796,7 +796,7 @@ def setUp(self):
                 ],
             }
         ]
-        url = "https://github.com/DAMO-NLP-SG/VideoLLaMA3/raw/refs/heads/main/assets/sora.png"
+        url = "https://raw.githubusercontent.com/DAMO-NLP-SG/VideoLLaMA3/main/assets/sora.png"
         self.image = Image.open(requests.get(url, stream=True).raw)
 
     def tearDown(self):