def infer_modality(content: Union[str, List[str], bytes], media_download_headers: Optional[dict] = None) -> Modality:
    """
    Infer the modality of the content. Video, audio, image or text.

    If the content is a URL, we will firstly infer the modality based on the file extension, and
    return the modality if it is known. This will be a short-circuit operatio, and we accept the edge cases
    that the content has an incorrect extension

    If the content is a bytes object, we will infer the modality based on the MIME type and return the modality.

    If the content is neither a URL nor a bytes object, we will return TEXT.
    """
    if isinstance(content, str):
        if not validate_url(content):
            return Modality.TEXT

        # Encode the URL
        encoded_url = encode_url(content)
        extension = get_url_file_extension(encoded_url)

        # Check if the URL has a file extension
        if extension:
            modality: Optional[Modality] = _infer_modality_based_on_extension(extension)
            if modality:
                return modality

        # Use context manager to handle content sample
        try:
            with fetch_content_sample(encoded_url, media_download_headers) as sample:
                mime = magic.from_buffer(sample.read(), mime=True)
                modality: Modality = _infer_modality_based_on_mime_type(mime)
                return modality
        except requests.exceptions.RequestException as e:
            raise MediaDownloadError(f"Error downloading media file {content}: {e}") from e
        except magic.MagicException as e:
            raise MediaDownloadError(f"Error determining MIME type for {encoded_url}: {e}") from e
        except IOError as e:
            raise MediaDownloadError(f"IO error while processing {encoded_url}: {e}") from e

    elif isinstance(content, bytes):
        # Use python-magic for byte content
        mime = magic.from_buffer(content, mime=True)
        return _infer_modality_based_on_mime_type(mime)
    else:
        return Modality.TEXT

    Infer the modality of the content. Video, audio, image or text.

    If the content is a URL, we will firstly infer the modality based on the file extension, and
    return the modality if it is known. This will be a short-circuit operatio, and we accept the edge cases
    that the content has an incorrect extension

    If the content is a bytes object, we will infer the modality based on the MIME type and return the modality.

    If the content is neither a URL nor a bytes object, we will return TEXT.
    
src/marqo/core/inference/modality_utils.py