Spaces:
Running
Running
File size: 2,164 Bytes
d358e26 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torchaudio\n",
"from IPython.display import Audio, display\n",
"\n",
"from models.model import Vocos\n",
"from utils.audio import LogMelSpectrogram\n",
"from config import MelConfig, VocosConfig\n",
"\n",
"from pathlib import Path\n",
"import random\n",
"\n",
"def load_and_resample_audio(audio_path, target_sr):\n",
" y, sr = torchaudio.load(audio_path)\n",
" if y.size(0) > 1:\n",
" y = y[0, :].unsqueeze(0) # shape: [2, time] -> [time] -> [1, time]\n",
" if sr != target_sr:\n",
" y = torchaudio.functional.resample(y, sr, target_sr)\n",
" return y\n",
"\n",
"device = 'cpu'\n",
"\n",
"mel_config = MelConfig()\n",
"vocos_config = VocosConfig()\n",
"\n",
"mel_extractor = LogMelSpectrogram(mel_config)\n",
"model = Vocos(vocos_config, mel_config).to(device)\n",
"model.load_state_dict(torch.load('./checkpoints/generator_0.pt', map_location='cpu'))\n",
"model.eval()\n",
"\n",
"audio_paths = list(Path('./audios').rglob('*.wav'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"audio_path = random.choice(audio_paths)\n",
"with torch.inference_mode():\n",
" audio = load_and_resample_audio(audio_path, mel_config.sample_rate).to(device)\n",
" mel = mel_extractor(audio)\n",
" recon_audio = model(mel)\n",
"display(Audio(audio, rate=mel_config.sample_rate))\n",
"display(Audio(recon_audio, rate=mel_config.sample_rate))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "lxn_vits",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|