diff --git a/litellm/llms/openai/transcriptions/handler.py b/litellm/llms/openai/transcriptions/handler.py index cd6c6cdcc1b..450eaab339e 100644 --- a/litellm/llms/openai/transcriptions/handler.py +++ b/litellm/llms/openai/transcriptions/handler.py @@ -6,6 +6,7 @@ from pydantic import BaseModel if TYPE_CHECKING: from aiohttp import ClientSession +from litellm.litellm_core_utils.audio_utils.subtitle_utils import SUBTITLE_RESPONSE_FORMATS from litellm.litellm_core_utils.audio_utils.utils import get_audio_file_name from litellm.litellm_core_utils.core_helpers import set_provider_response_headers_in_hidden_params from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj @@ -128,7 +129,11 @@ class OpenAIAudioTranscription(OpenAIChatCompletion): if isinstance(response, BaseModel): stringified_response = response.model_dump() else: - duration: Final = extract_duration_from_srt_or_vtt(response) + duration: Final = ( + extract_duration_from_srt_or_vtt(response) + if data.get("response_format") in SUBTITLE_RESPONSE_FORMATS + else None + ) stringified_response = { **TranscriptionResponse(text=response).model_dump(), **({"_audio_transcription_duration": duration} if duration is not None else {}), diff --git a/litellm/utils.py b/litellm/utils.py index a7d7447d7f3..dc6b779f3ab 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -10126,25 +10126,24 @@ def extract_duration_from_srt_or_vtt(srt_or_vtt_content: str) -> float | None: srt_or_vtt_content (str): The content of an SRT or VTT file as a string. Returns: - Optional[float]: The total duration in seconds, or None if no timestamps are found. + Optional[float]: The total duration in seconds, or None if no cue timestamps are found. """ - # Regular expression to match timestamps in the format "hh:mm:ss,ms" or "hh:mm:ss.ms" - timestamp_pattern: Final = r"(\d{2}):(\d{2}):(\d{2})[.,](\d{3})" + timestamp_pattern: Final = ( + r"^[ \t]*(?:\d{2,}:)?\d{2}:\d{2}[.,]\d{3}[ \t]+-->[ \t]+" + r"(?:(\d{2,}):)?(\d{2}):(\d{2})[.,](\d{3})" + ) - timestamps: Final[Sequence[tuple[str, str, str, str]]] = re.findall(timestamp_pattern, srt_or_vtt_content) + timestamps: Final[Sequence[tuple[str, str, str, str]]] = re.findall( + timestamp_pattern, srt_or_vtt_content, re.MULTILINE + ) - if not timestamps: - return None - - # Convert timestamps to seconds and find the max (end time) - durations: Final = [] - match: tuple[str, str, str, str] - for match in timestamps: - hours, minutes, seconds, milliseconds = map(int, match) - total_seconds = hours * 3600 + minutes * 60 + seconds + milliseconds / 1000.0 - durations.append(total_seconds) - - return max(durations) if durations else None + return max( + ( + int(hours or 0) * 3600 + int(minutes) * 60 + int(seconds) + int(milliseconds) / 1000.0 + for hours, minutes, seconds, milliseconds in timestamps + ), + default=None, + ) def _add_path_to_api_base(api_base: str, ending_path: str) -> str: diff --git a/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py b/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py index 959cd13c728..7c0a1eb50e3 100644 --- a/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py +++ b/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py @@ -68,8 +68,11 @@ def test_audio_transcriptions_records_provider_response_headers(): ("vtt", "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nHello\n\n00:00:01.000 --> 00:00:12.500\nWorld\n", 12.5), ("srt", "", None), ("text", "Hello world", None), + ("text", "The timecode is 00:00:12.500", None), + ("text", "00:00:01.000 --> 00:00:12.500\nHello world", None), + ("vtt", "WEBVTT\n\n00:01.500 --> 00:02.000\nHello world\n", 2.0), ), - ids=("srt", "vtt", "empty-subtitle", "plain-text"), + ids=("srt", "vtt", "empty-subtitle", "plain-text", "text-timecode", "text-cue", "short-vtt"), ) def test_sync_transcription_retains_text_and_charges_subtitle_duration( response_format: Literal["srt", "vtt", "text"], subtitle: str, expected_duration: float | None diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index a72aec07766..1a2f43db26c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -76,6 +76,7 @@ from litellm.utils import ( async_post_call_success_deployment_hook, calculate_max_parallel_requests, client, + extract_duration_from_srt_or_vtt, get_non_default_completion_params, get_optional_params_image_gen, get_prompt_cache_min_tokens, @@ -86,6 +87,33 @@ from litellm.utils import ( # Adds the parent directory to the system path +@pytest.mark.parametrize( + ("subtitle", "expected_duration"), + ( + ("1\n01:01:01,000 --> 01:01:12,500\nHello\n", 3672.5), + ("WEBVTT\n\n00:01.500 --> 00:02.000\nHello\n", 2.0), + ("WEBVTT\n\n59:59.000 --> 01:00:02.500 align:start\nHello\n", 3602.5), + ("WEBVTT\n\n100:00:00.000 --> 100:00:02.000\nHello\n", 360002.0), + ("1\n00:00:00,000 --> 00:00:03,000\nThe timecode is 99:00:00.000\n", 3.0), + ("WEBVTT\n\n00:00.000 --> 00:05.000\nHello\n\n00:03.000 --> 00:04.000\nWorld\n", 5.0), + ("", None), + ("The timecode is 00:00:12.500", None), + ), + ids=( + "srt-hours", + "vtt-no-hours", + "vtt-mixed-hours", + "vtt-long-hours", + "payload-timecode", + "overlap", + "empty", + "text", + ), +) +def test_subtitle_duration_uses_the_latest_cue_end_time(subtitle: str, expected_duration: float | None) -> None: + assert extract_duration_from_srt_or_vtt(subtitle) == expected_duration + + def test_non_ocr_wrapper_preserves_logging_executor_and_context(monkeypatch: pytest.MonkeyPatch) -> None: marker: Final = contextvars.ContextVar("non-ocr-logging-context", default="missing") token: Final = marker.set("caller-context")