Skip to content

test_encode_audio_with_dict_input fail #479

@tukwila

Description

@tukwila

Describe the bug
A clear and concise description of what the bug is.

The following ut fail to test guidellm.extras.audio with dict input parameters:

import pytest
import torch
from guidellm.extras.audio import encode_audio, _decode_audio, _encode_audio

def test_encode_audio_with_dict_input():
        audio_dict = {
            "data": torch.randn(1, 16000),
            "sample_rate": 16000
        }
        
        result = encode_audio(audio=audio_dict)
        
        assert result["type"] == "audio_file"
        assert result["audio_bytes"] > 0
        assert result["audio_samples"] == 16000
        assert result["audio_seconds"] == 1.0


if __name__ == "__main__":
    pytest.main([__file__, "-v"])

save the ut into file such as: test_dict_input.py,
then run pytest cmd: pytest test_dict_input.py

failure info:

____________________________________________________________________ test_encode_audio_with_dict_input ____________________________________________________________________

    def test_encode_audio_with_dict_input():
            audio_dict = {
                "data": torch.randn(1, 16000),
                "sample_rate": 16000
            }
    
>           result = encode_audio(audio=audio_dict)

tests/integration/test_dict_input.py:11: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
src/guidellm/extras/audio.py:58: in encode_audio
    samples = _decode_audio(audio, sample_rate=sample_rate, max_duration=max_duration)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

audio = {'data': tensor([[-0.1090, -0.5127,  0.6118,  ..., -0.2266,  0.4273,  1.2508]]), 'sample_rate': 16000}, sample_rate = 16000, max_duration = None

    def _decode_audio(  # noqa: C901, PLR0912
        audio: AudioDecoder
        | bytes
        | str
        | Path
        | np.ndarray
        | torch.Tensor
        | dict[str, Any],
        sample_rate: int | None = None,
        max_duration: float | None = None,
    ) -> AudioSamples:
        """Decode audio from various input types into AudioSamples."""
        # If input is a dict, unwrap it into a function call
        if isinstance(audio, dict):
            sample_rate = audio.get("sample_rate", audio.get("sampling_rate", sample_rate))
            if "data" not in audio and "url" not in audio:
                raise ValueError(
                    f"Audio dict must contain either 'data' or 'url' keys, got {audio}"
                )
            return _decode_audio(
>               audio=audio.get("data") or audio.get("url"),
                sample_rate=sample_rate,
                max_duration=max_duration,
            )
E           RuntimeError: Boolean value of Tensor with more than one value is ambiguous

src/guidellm/extras/audio.py:111: RuntimeError

Expected behavior
A clear and concise description of what you expected to happen.

ut case with dict input should pass.

Environment
Include all relevant environment information:

  1. OS [e.g. Ubuntu 20.04]:
  2. Python version [e.g. 3.12.2]:

To Reproduce
Exact steps to reproduce the behavior:

Errors
If applicable, add a full print-out of any errors or exceptions that are raised or include screenshots to help explain your problem.

Additional context
Add any other context about the problem here. Also include any relevant files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions