Spaces:
Sleeping
Sleeping
Download audio_loader.py from valiyevfagan/test_ui2: direct link, hf CLI and curl.
- Browser
- Download file 1.59 kB
-
https://huggingface.co/spaces/valiyevfagan/test_ui2/resolve/main/audio_loader.py
- Command line
-
hf download hf://spaces/valiyevfagan/test_ui2/audio_loader.py
-
curl -L -o audio_loader.py https://huggingface.co/spaces/valiyevfagan/test_ui2/resolve/main/audio_loader.py
1.59 kB
| """Lazy per-item audio loading and clean audio copy.""" | |
| import shutil | |
| import logging | |
| from pathlib import Path | |
| logger = logging.getLogger(__name__) | |
| def load_audio_bytes(folder_path: str, filename: str) -> bytes: | |
| """Load audio bytes for a single file on demand. | |
| Args: | |
| folder_path: Path to the audio folder. | |
| filename: Name of the WAV file. | |
| Returns: | |
| Raw bytes of the audio file. | |
| Raises: | |
| FileNotFoundError: If the audio file does not exist. | |
| """ | |
| audio_path = Path(folder_path) / filename | |
| if not audio_path.exists(): | |
| raise FileNotFoundError(f"Audio file not found: {filename}") | |
| return audio_path.read_bytes() | |
| def copy_to_clean(source_folder: str, filename: str, clean_audios_dir: str) -> None: | |
| """Copy accepted audio to the clean audios folder. | |
| Creates the clean audios directory if it doesn't exist. | |
| Args: | |
| source_folder: Path to the source audio folder. | |
| filename: Name of the WAV file to copy. | |
| clean_audios_dir: Path to the destination clean audios folder. | |
| Raises: | |
| IOError: If the copy fails. | |
| """ | |
| src = Path(source_folder) / filename | |
| dst_dir = Path(clean_audios_dir) | |
| dst_dir.mkdir(parents=True, exist_ok=True) | |
| dst = dst_dir / filename | |
| try: | |
| shutil.copy2(str(src), str(dst)) | |
| logger.info(f"Copied '{filename}' to clean audios: {clean_audios_dir}") | |
| except Exception as e: | |
| logger.error(f"Failed to copy '{filename}' to clean audios: {e}") | |
| raise IOError(f"Failed to copy audio file '{filename}'.") from e | |