Upload folder using huggingface_hub
Browse files- README.md +121 -12
- example.py +199 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -61,6 +61,7 @@ A PyTorch model for contextless phoneme prediction from speech audio. CUPE proce
|
|
| 61 |
|
| 62 |
|
| 63 |
Github link: [github.com/tabahi/contexless-phonemes-CUPE](https://github.com/tabahi/contexless-phonemes-CUPE)
|
|
|
|
| 64 |
|
| 65 |
## Trained Models
|
| 66 |
|
|
@@ -95,9 +96,83 @@ Two 30.1M parameter models are available in the [checkpoints directory](https://
|
|
| 95 |
|
| 96 |
---
|
| 97 |
|
| 98 |
-
# Usage
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
```python
|
| 103 |
import torch
|
|
@@ -105,38 +180,72 @@ import torchaudio
|
|
| 105 |
from model2i import CUPEEmbeddingsExtractor # Main CUPE model feature extractor
|
| 106 |
import windowing # Provides slice_windows, stich_window_predictions
|
| 107 |
|
|
|
|
| 108 |
cupe_ckpt_path = "./ckpt/en_libri1000_uj01d_e199_val_GER=0.2307.ckpt"
|
| 109 |
extractor = CUPEEmbeddingsExtractor(cupe_ckpt_path, device="cuda")
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
dummy_wav = torch.zeros(1, max_wav_len, dtype=torch.float32, device="cpu")
|
| 112 |
audio_batch = dummy_wav.unsqueeze(0) # Add batch dimension
|
| 113 |
|
| 114 |
# Window the audio
|
| 115 |
windowed_audio = windowing.slice_windows(
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
)
|
| 121 |
batch_size, num_windows, window_size = windowed_audio.shape
|
| 122 |
windows_flat = windowed_audio.reshape(-1, window_size)
|
| 123 |
|
|
|
|
| 124 |
logits, _ = extractor.predict(windows_flat, return_embeddings=False, groups_only=False)
|
| 125 |
|
| 126 |
# Reshape and stitch window predictions
|
|
|
|
| 127 |
logits = logits.reshape(batch_size, num_windows, frames_per_window, -1)
|
| 128 |
logits = windowing.stich_window_predictions(
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
)
|
| 136 |
|
| 137 |
print(logits.shape) # [B, T, 66]
|
| 138 |
```
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
# Custom dataset for training
|
| 141 |
- See [mapper.py](https://huggingface.co/Tabahi/CUPE-2i/blob/main/mapper.py) for the tokenization for 66 phonemes and 16 phoneme groups.
|
| 142 |
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
Github link: [github.com/tabahi/contexless-phonemes-CUPE](https://github.com/tabahi/contexless-phonemes-CUPE)
|
| 64 |
+
Huggingface link: [huggingface.co/Tabahi/CUPE-2i](https://huggingface.co/Tabahi/CUPE-2i)
|
| 65 |
|
| 66 |
## Trained Models
|
| 67 |
|
|
|
|
| 96 |
|
| 97 |
---
|
| 98 |
|
|
|
|
| 99 |
|
| 100 |
+
# Quick Start
|
| 101 |
+
|
| 102 |
+
## Installation
|
| 103 |
+
|
| 104 |
+
```bash
|
| 105 |
+
pip install torch torchaudio huggingface_hub
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
## Easy Usage with Automatic Download
|
| 109 |
+
|
| 110 |
+
See [example.py](https://huggingface.co/Tabahi/CUPE-2i/blob/main/run.py) for a bootstrap example.
|
| 111 |
+
|
| 112 |
+
To use `huggingface_hub` to automatically download and run:
|
| 113 |
+
|
| 114 |
+
```python
|
| 115 |
+
import torch
|
| 116 |
+
import torchaudio
|
| 117 |
+
from huggingface_hub import hf_hub_download
|
| 118 |
+
import importlib.util
|
| 119 |
+
|
| 120 |
+
def load_cupe_model(model_name="english", device="auto"):
|
| 121 |
+
"""Load CUPE model with automatic downloading from Hugging Face Hub"""
|
| 122 |
+
|
| 123 |
+
model_files = {
|
| 124 |
+
"english": "en_libri1000_uj01d_e199_val_GER=0.2307.ckpt",
|
| 125 |
+
"multilingual-mls": "multi_MLS8_uh02_e36_val_GER=0.2334.ckpt",
|
| 126 |
+
"multilingual-mswc": "multi_mswc38_ug20_e59_val_GER=0.5611.ckpt"
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
if device == "auto":
|
| 130 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 131 |
+
|
| 132 |
+
# Download files automatically from Hugging Face Hub
|
| 133 |
+
repo_id = "Tabahi/CUPE-2i"
|
| 134 |
+
model_file = hf_hub_download(repo_id=repo_id, filename="model2i.py")
|
| 135 |
+
windowing_file = hf_hub_download(repo_id=repo_id, filename="windowing.py")
|
| 136 |
+
checkpoint = hf_hub_download(repo_id=repo_id, filename=f"ckpt/{model_files[model_name]}")
|
| 137 |
+
|
| 138 |
+
# Import modules dynamically
|
| 139 |
+
spec = importlib.util.spec_from_file_location("model2i", model_file)
|
| 140 |
+
model2i = importlib.util.module_from_spec(spec)
|
| 141 |
+
spec.loader.exec_module(model2i)
|
| 142 |
+
|
| 143 |
+
spec = importlib.util.spec_from_file_location("windowing", windowing_file)
|
| 144 |
+
windowing = importlib.util.module_from_spec(spec)
|
| 145 |
+
spec.loader.exec_module(windowing)
|
| 146 |
+
|
| 147 |
+
# Initialize model
|
| 148 |
+
extractor = model2i.CUPEEmbeddingsExtractor(checkpoint, device=device)
|
| 149 |
+
return extractor, windowing
|
| 150 |
+
|
| 151 |
+
# Example usage
|
| 152 |
+
extractor, windowing = load_cupe_model("english")
|
| 153 |
+
|
| 154 |
+
# Load and process your audio
|
| 155 |
+
audio, sr = torchaudio.load("your_audio.wav")
|
| 156 |
+
if sr != 16000:
|
| 157 |
+
resampler = torchaudio.transforms.Resample(sr, 16000)
|
| 158 |
+
audio = resampler(audio)
|
| 159 |
+
|
| 160 |
+
# Add batch dimension and process
|
| 161 |
+
audio_batch = audio.unsqueeze(0)
|
| 162 |
+
windowed_audio = windowing.slice_windows(audio_batch, 16000, 120, 80)
|
| 163 |
+
batch_size, num_windows, window_size = windowed_audio.shape
|
| 164 |
+
windows_flat = windowed_audio.reshape(-1, window_size)
|
| 165 |
+
|
| 166 |
+
# Get predictions
|
| 167 |
+
logits_phonemes, logits_groups = extractor.predict(windows_flat, return_embeddings=False, groups_only=False)
|
| 168 |
+
|
| 169 |
+
print(f"Phoneme logits shape: {logits_phonemes.shape}") # [num_windows, frames_per_window, 66]
|
| 170 |
+
print(f"Group logits shape: {logits_groups.shape}") # [num_windows, frames_per_window, 16]
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
## Advanced Usage (Manual Setup)
|
| 174 |
+
|
| 175 |
+
If you prefer to download files manually or need more control, see [run.py](https://huggingface.co/Tabahi/CUPE-2i/blob/main/run.py):
|
| 176 |
|
| 177 |
```python
|
| 178 |
import torch
|
|
|
|
| 180 |
from model2i import CUPEEmbeddingsExtractor # Main CUPE model feature extractor
|
| 181 |
import windowing # Provides slice_windows, stich_window_predictions
|
| 182 |
|
| 183 |
+
# Load model from local checkpoint
|
| 184 |
cupe_ckpt_path = "./ckpt/en_libri1000_uj01d_e199_val_GER=0.2307.ckpt"
|
| 185 |
extractor = CUPEEmbeddingsExtractor(cupe_ckpt_path, device="cuda")
|
| 186 |
|
| 187 |
+
# Prepare audio
|
| 188 |
+
sample_rate = 16000
|
| 189 |
+
window_size_ms = 120
|
| 190 |
+
stride_ms = 80
|
| 191 |
+
max_wav_len = 10 * sample_rate # 10 seconds
|
| 192 |
+
|
| 193 |
dummy_wav = torch.zeros(1, max_wav_len, dtype=torch.float32, device="cpu")
|
| 194 |
audio_batch = dummy_wav.unsqueeze(0) # Add batch dimension
|
| 195 |
|
| 196 |
# Window the audio
|
| 197 |
windowed_audio = windowing.slice_windows(
|
| 198 |
+
audio_batch.to("cuda"),
|
| 199 |
+
sample_rate,
|
| 200 |
+
window_size_ms,
|
| 201 |
+
stride_ms
|
| 202 |
)
|
| 203 |
batch_size, num_windows, window_size = windowed_audio.shape
|
| 204 |
windows_flat = windowed_audio.reshape(-1, window_size)
|
| 205 |
|
| 206 |
+
# Get predictions
|
| 207 |
logits, _ = extractor.predict(windows_flat, return_embeddings=False, groups_only=False)
|
| 208 |
|
| 209 |
# Reshape and stitch window predictions
|
| 210 |
+
frames_per_window = logits.shape[1]
|
| 211 |
logits = logits.reshape(batch_size, num_windows, frames_per_window, -1)
|
| 212 |
logits = windowing.stich_window_predictions(
|
| 213 |
+
logits,
|
| 214 |
+
original_audio_length=audio_batch.size(2),
|
| 215 |
+
cnn_output_size=frames_per_window,
|
| 216 |
+
sample_rate=sample_rate,
|
| 217 |
+
window_size_ms=window_size_ms,
|
| 218 |
+
stride_ms=stride_ms
|
| 219 |
)
|
| 220 |
|
| 221 |
print(logits.shape) # [B, T, 66]
|
| 222 |
```
|
| 223 |
|
| 224 |
+
## Available Models
|
| 225 |
+
|
| 226 |
+
| Model Name | Languages | PER | GER | Description |
|
| 227 |
+
|------------|-----------|-----|-----|-------------|
|
| 228 |
+
| `"english"` | English | 0.25 | 0.23 | Best quality for English speech |
|
| 229 |
+
| `"multilingual-mls"` | 8 European languages | 0.31 | 0.26 | en, de, fr, es, pt, it, pl, nl |
|
| 230 |
+
| `"multilingual-mswc"` | 38 languages | 0.49 | 0.39 | Broad language coverage |
|
| 231 |
+
|
| 232 |
+
## Output Format
|
| 233 |
+
|
| 234 |
+
- **Phoneme logits**: `(time_frames, 66)` - 66 IPA phoneme classes
|
| 235 |
+
- **Group logits**: `(time_frames, 16)` - 16 phoneme groups
|
| 236 |
+
- **Time resolution**: ~16ms per frame (~62.5 FPS)
|
| 237 |
+
- **Mapping**: See [mapper.py](https://huggingface.co/Tabahi/CUPE-2i/blob/main/mapper.py) for phoneme-to-index mapping
|
| 238 |
+
|
| 239 |
+
## Key Features
|
| 240 |
+
|
| 241 |
+
**No manual downloads** - automatic via Hugging Face Hub
|
| 242 |
+
**Multiple languages** - English + 37 other languages
|
| 243 |
+
**Real-time capable** - faster than real-time on GPU
|
| 244 |
+
**Frame-level timing** - 16ms resolution
|
| 245 |
+
**Contextless** - each frame processed independently
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
|
| 249 |
# Custom dataset for training
|
| 250 |
- See [mapper.py](https://huggingface.co/Tabahi/CUPE-2i/blob/main/mapper.py) for the tokenization for 66 phonemes and 16 phoneme groups.
|
| 251 |
|
example.py
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CUPE: Easy usage with automatic downloading from Hugging Face Hub
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torchaudio
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
import importlib.util
|
| 9 |
+
import sys
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
def load_cupe_model(model_name="english", device="auto"):
|
| 13 |
+
"""
|
| 14 |
+
Load CUPE model with automatic downloading from Hugging Face Hub
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
model_name: "english", "multilingual-mls", or "multilingual-mswc"
|
| 18 |
+
device: "auto", "cpu", or "cuda"
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
Tuple of (extractor, windowing_module)
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
# Model checkpoint mapping
|
| 25 |
+
model_files = {
|
| 26 |
+
"english": "en_libri1000_uj01d_e199_val_GER=0.2307.ckpt",
|
| 27 |
+
"multilingual-mls": "multi_MLS8_uh02_e36_val_GER=0.2334.ckpt",
|
| 28 |
+
"multilingual-mswc": "multi_mswc38_ug20_e59_val_GER=0.5611.ckpt"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if model_name not in model_files:
|
| 32 |
+
raise ValueError(f"Model {model_name} not available. Choose from: {list(model_files.keys())}")
|
| 33 |
+
|
| 34 |
+
if device == "auto":
|
| 35 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 36 |
+
|
| 37 |
+
print(f"Loading CUPE {model_name} model...")
|
| 38 |
+
|
| 39 |
+
# Download model files from Hugging Face Hub
|
| 40 |
+
repo_id = "Tabahi/CUPE-2i"
|
| 41 |
+
|
| 42 |
+
model_file = hf_hub_download(repo_id=repo_id, filename="model2i.py")
|
| 43 |
+
windowing_file = hf_hub_download(repo_id=repo_id, filename="windowing.py")
|
| 44 |
+
checkpoint_file = hf_hub_download(repo_id=repo_id, filename=f"ckpt/{model_files[model_name]}")
|
| 45 |
+
|
| 46 |
+
# Dynamically import the modules
|
| 47 |
+
def import_module_from_file(module_name, file_path):
|
| 48 |
+
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 49 |
+
module = importlib.util.module_from_spec(spec)
|
| 50 |
+
sys.modules[module_name] = module
|
| 51 |
+
spec.loader.exec_module(module)
|
| 52 |
+
return module
|
| 53 |
+
|
| 54 |
+
model2i = import_module_from_file("model2i", model_file)
|
| 55 |
+
windowing = import_module_from_file("windowing", windowing_file)
|
| 56 |
+
|
| 57 |
+
# Initialize the model
|
| 58 |
+
extractor = model2i.CUPEEmbeddingsExtractor(checkpoint_file, device=device)
|
| 59 |
+
|
| 60 |
+
print(f"Model loaded on {device}")
|
| 61 |
+
return extractor, windowing
|
| 62 |
+
|
| 63 |
+
def predict_phonemes(audio_path, model_name="english", device="auto"):
|
| 64 |
+
"""
|
| 65 |
+
Predict phonemes from audio file
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
audio_path: Path to audio file
|
| 69 |
+
model_name: CUPE model variant to use
|
| 70 |
+
device: Device to run inference on
|
| 71 |
+
|
| 72 |
+
Returns:
|
| 73 |
+
Dictionary with predictions and metadata
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
# Load model
|
| 77 |
+
extractor, windowing = load_cupe_model(model_name, device)
|
| 78 |
+
|
| 79 |
+
# Audio processing parameters
|
| 80 |
+
sample_rate = 16000
|
| 81 |
+
window_size_ms = 120
|
| 82 |
+
stride_ms = 80
|
| 83 |
+
|
| 84 |
+
# Load and preprocess audio
|
| 85 |
+
audio, orig_sr = torchaudio.load(audio_path)
|
| 86 |
+
|
| 87 |
+
# Convert to mono if stereo
|
| 88 |
+
if audio.shape[0] > 1:
|
| 89 |
+
audio = audio.mean(dim=0, keepdim=True)
|
| 90 |
+
|
| 91 |
+
# Resample to 16kHz if needed
|
| 92 |
+
if orig_sr != sample_rate:
|
| 93 |
+
resampler = torchaudio.transforms.Resample(orig_sr, sample_rate)
|
| 94 |
+
audio = resampler(audio)
|
| 95 |
+
|
| 96 |
+
# Move to device and add batch dimension
|
| 97 |
+
audio = audio.to(device)
|
| 98 |
+
audio_batch = audio.unsqueeze(0)
|
| 99 |
+
|
| 100 |
+
print(f"Processing audio: {audio.shape[1]/sample_rate:.2f}s duration")
|
| 101 |
+
|
| 102 |
+
# Window the audio
|
| 103 |
+
windowed_audio = windowing.slice_windows(
|
| 104 |
+
audio_batch,
|
| 105 |
+
sample_rate,
|
| 106 |
+
window_size_ms,
|
| 107 |
+
stride_ms
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
batch_size, num_windows, window_size = windowed_audio.shape
|
| 111 |
+
windows_flat = windowed_audio.reshape(-1, window_size)
|
| 112 |
+
|
| 113 |
+
# Get model predictions
|
| 114 |
+
logits_phonemes, logits_groups = extractor.predict(
|
| 115 |
+
windows_flat,
|
| 116 |
+
return_embeddings=False,
|
| 117 |
+
groups_only=False
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Reshape and stitch predictions
|
| 121 |
+
frames_per_window = logits_phonemes.shape[1]
|
| 122 |
+
|
| 123 |
+
logits_phonemes = logits_phonemes.reshape(batch_size, num_windows, frames_per_window, -1)
|
| 124 |
+
logits_groups = logits_groups.reshape(batch_size, num_windows, frames_per_window, -1)
|
| 125 |
+
|
| 126 |
+
phoneme_logits = windowing.stich_window_predictions(
|
| 127 |
+
logits_phonemes,
|
| 128 |
+
original_audio_length=audio_batch.size(2),
|
| 129 |
+
cnn_output_size=frames_per_window,
|
| 130 |
+
sample_rate=sample_rate,
|
| 131 |
+
window_size_ms=window_size_ms,
|
| 132 |
+
stride_ms=stride_ms
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
group_logits = windowing.stich_window_predictions(
|
| 136 |
+
logits_groups,
|
| 137 |
+
original_audio_length=audio_batch.size(2),
|
| 138 |
+
cnn_output_size=frames_per_window,
|
| 139 |
+
sample_rate=sample_rate,
|
| 140 |
+
window_size_ms=window_size_ms,
|
| 141 |
+
stride_ms=stride_ms
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Convert to probabilities and predictions
|
| 145 |
+
phoneme_probs = torch.softmax(phoneme_logits.squeeze(0), dim=-1)
|
| 146 |
+
group_probs = torch.softmax(group_logits.squeeze(0), dim=-1)
|
| 147 |
+
|
| 148 |
+
phoneme_preds = torch.argmax(phoneme_probs, dim=-1)
|
| 149 |
+
group_preds = torch.argmax(group_probs, dim=-1)
|
| 150 |
+
|
| 151 |
+
# Calculate timestamps (approximately 16ms per frame)
|
| 152 |
+
num_frames = phoneme_probs.shape[0]
|
| 153 |
+
timestamps_ms = torch.arange(num_frames) * 16 # ~16ms per frame
|
| 154 |
+
|
| 155 |
+
print(f"✓ Processed {num_frames} frames ({num_frames*16}ms total)")
|
| 156 |
+
|
| 157 |
+
return {
|
| 158 |
+
'phoneme_probabilities': phoneme_probs.cpu().numpy(),
|
| 159 |
+
'phoneme_predictions': phoneme_preds.cpu().numpy(),
|
| 160 |
+
'group_probabilities': group_probs.cpu().numpy(),
|
| 161 |
+
'group_predictions': group_preds.cpu().numpy(),
|
| 162 |
+
'timestamps_ms': timestamps_ms.cpu().numpy(),
|
| 163 |
+
'model_info': {
|
| 164 |
+
'model_name': model_name,
|
| 165 |
+
'sample_rate': sample_rate,
|
| 166 |
+
'frames_per_second': 1000/16, # ~62.5 fps
|
| 167 |
+
'num_phoneme_classes': phoneme_probs.shape[-1],
|
| 168 |
+
'num_group_classes': group_probs.shape[-1]
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
# Example usage
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
|
| 175 |
+
# Simple example
|
| 176 |
+
audio_file = "samples/Schwa-What.mp3.wav" # Replace with your audio file
|
| 177 |
+
|
| 178 |
+
if not os.path.exists(audio_file):
|
| 179 |
+
print(f"Audio file {audio_file} does not exist. Please provide a valid path.")
|
| 180 |
+
sys.exit(1)
|
| 181 |
+
# Predict with English model
|
| 182 |
+
results = predict_phonemes(
|
| 183 |
+
audio_path=audio_file,
|
| 184 |
+
model_name="english", # or "multilingual-mls" or "multilingual-mswc"
|
| 185 |
+
device="cpu"
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
print(f"\nResults:")
|
| 189 |
+
print(f"Phoneme predictions shape: {results['phoneme_predictions'].shape}")
|
| 190 |
+
print(f"Group predictions shape: {results['group_predictions'].shape}")
|
| 191 |
+
print(f"Timestamps shape: {results['timestamps_ms'].shape}")
|
| 192 |
+
print(f"Model info: {results['model_info']}")
|
| 193 |
+
|
| 194 |
+
# Show first 10 predictions with timestamps
|
| 195 |
+
print(f"\nFirst 10 frame predictions:")
|
| 196 |
+
for i in range(min(10, len(results['phoneme_predictions']))):
|
| 197 |
+
print(f"Frame {i}: phoneme={results['phoneme_predictions'][i]}, "
|
| 198 |
+
f"group={results['group_predictions'][i]}, "
|
| 199 |
+
f"time={results['timestamps_ms'][i]:.0f}ms")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=1.9.0
|
| 2 |
+
torchaudio>=0.9.0
|
| 3 |
+
huggingface_hub>=0.10.0
|
| 4 |
+
numpy
|
| 5 |
+
tqdm
|