mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-13 08:52:02 +00:00
refactor(transcription): inline the unknown frame count check
This commit is contained in:
parent
e6a0534785
commit
b6d25f2752
2 changed files with 8 additions and 7 deletions
|
|
@ -34,10 +34,6 @@ def normalized_audio_duration_seconds(value: object) -> float | None:
|
|||
LIBSNDFILE_UNKNOWN_FRAME_COUNT: Final = 2**63 - 1
|
||||
|
||||
|
||||
def soundfile_duration_seconds(frames: int, samplerate: int) -> float | None:
|
||||
return None if frames >= LIBSNDFILE_UNKNOWN_FRAME_COUNT else frames / samplerate
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProcessedAudioFile:
|
||||
"""
|
||||
|
|
@ -347,7 +343,10 @@ def calculate_request_duration(file: FileTypes) -> float | None:
|
|||
# Extract duration using soundfile
|
||||
file_object: Final = io.BytesIO(file_content)
|
||||
with sf.SoundFile(file_object) as audio:
|
||||
return soundfile_duration_seconds(len(audio), audio.samplerate)
|
||||
if len(audio) >= LIBSNDFILE_UNKNOWN_FRAME_COUNT:
|
||||
return None
|
||||
duration: Final = len(audio) / audio.samplerate
|
||||
return duration
|
||||
|
||||
except Exception:
|
||||
# Silently fail if duration extraction fails
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ from litellm.constants import (
|
|||
TRANSCRIBE_MEDIA_FETCH_ATTEMPTS,
|
||||
TRANSCRIBE_MEDIA_LAST_MODIFIED_TOLERANCE_SECONDS,
|
||||
)
|
||||
from litellm.litellm_core_utils.audio_utils.utils import soundfile_duration_seconds
|
||||
from litellm.litellm_core_utils.audio_utils.utils import LIBSNDFILE_UNKNOWN_FRAME_COUNT
|
||||
from litellm.litellm_core_utils.aws_partition import get_aws_dns_suffix
|
||||
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
|
||||
from litellm.litellm_core_utils.litellm_logging import (
|
||||
|
|
@ -524,7 +524,9 @@ async def write_media_within_limit(response: httpx.Response, media_file: IO[byte
|
|||
def media_file_seconds(path: Path) -> float | None:
|
||||
try:
|
||||
with soundfile.SoundFile(str(path)) as audio:
|
||||
return soundfile_duration_seconds(len(audio), audio.samplerate)
|
||||
if len(audio) >= LIBSNDFILE_UNKNOWN_FRAME_COUNT:
|
||||
return None
|
||||
return len(audio) / audio.samplerate
|
||||
except (RuntimeError, ValueError, OSError) as e:
|
||||
verbose_proxy_logger.warning("Transcribe media could not be decoded for its duration: %s", e)
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue