Files
wehub-resource-sync adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:17:32 +08:00

72 lines
2.8 KiB
Python

from typing import TYPE_CHECKING, Any
from gradio_client.documentation import document
if TYPE_CHECKING:
import numpy as np
@document()
def is_audio_correct_length(
audio: tuple[int, "np.ndarray"], min_length: float | None, max_length: float | None
) -> dict[str, Any]:
"""
Validates that the audio length is within the specified min and max length (in seconds).
Parameters:
audio: A tuple of (sample rate in Hz, audio data as numpy array).
min_length: Minimum length of audio in seconds. If None, no minimum length check is performed.
max_length: Maximum length of audio in seconds. If None, no maximum length check is performed.
Returns:
A dict corresponding to `gr.validate()` indicating whether the audio length is valid and an optional message.
"""
if min_length is not None or max_length is not None:
sample_rate, data = audio
duration = len(data) / sample_rate
if min_length is not None and duration < min_length:
return {
"__type__": "validate",
"is_valid": False,
"message": f"Audio is too short. It must be at least {min_length} seconds",
}
if max_length is not None and duration > max_length:
return {
"__type__": "validate",
"is_valid": False,
"message": f"Audio is too long. It must be at most {max_length} seconds",
}
return {"__type__": "validate", "is_valid": True}
@document()
def is_video_correct_length(
video: str, min_length: float | None, max_length: float | None
) -> dict[str, Any]:
"""
Validates that the video file length is within the specified min and max length (in seconds).
Parameters:
video: The path to the video file.
min_length: Minimum length of video in seconds. If None, no minimum length check is performed.
max_length: Maximum length of video in seconds. If None, no maximum length check is performed.
Returns:
A dict corresponding to `gr.validate()` indicating whether the audio length is valid and an optional message.
"""
from gradio.processing_utils import get_video_length
if min_length is not None or max_length is not None:
duration = get_video_length(video)
if min_length is not None and duration < min_length:
return {
"__type__": "validate",
"is_valid": False,
"message": f"Video is too short. It must be at least {min_length} seconds",
}
if max_length is not None and duration > max_length:
return {
"__type__": "validate",
"is_valid": False,
"message": f"Video is too long. It must be at most {max_length} seconds",
}
return {"__type__": "validate", "is_valid": True}