Download src/audio.py from lducc/MediVoice: direct link, hf CLI and curl.
- Browser
- Download file 730 Bytes
-
https://huggingface.co/spaces/lducc/MediVoice/resolve/main/src/audio.py
- Command line
-
hf download hf://spaces/lducc/MediVoice/src/audio.py
-
curl -L -o audio.py https://huggingface.co/spaces/lducc/MediVoice/resolve/main/src/audio.py
730 Bytes
| import os | |
| import tempfile | |
| from typing import Optional, Tuple | |
| import numpy as np | |
| import librosa | |
| TARGET_SR = 16000 | |
| def load_audio(audio_bytes: Optional[bytes] = None, *, path: Optional[str] = None, filename: Optional[str] = None) -> Tuple[np.ndarray, int]: | |
| #Load audio → mono float32 numpy array at 16kHz | |
| if not path and not audio_bytes: | |
| raise ValueError("Provide either path or audio_bytes") | |
| if not path: | |
| ext = os.path.splitext(filename)[1] if filename else '.wav' | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as f: | |
| f.write(audio_bytes) | |
| path = f.name | |
| audio_np, _ = librosa.load(path, sr=TARGET_SR, mono=True) | |
| return audio_np, TARGET_SR | |