-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrained_voice_loader.py
More file actions
28 lines (24 loc) · 1001 Bytes
/
trained_voice_loader.py
File metadata and controls
28 lines (24 loc) · 1001 Bytes
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
#trained_voice_loader.py
import json
import torch
import numpy as np
from pathlib import Path
class TrainedVoiceLoader:
def __init__(self, model_path):
self.model_path = Path(model_path)
self.speakers = self._load_speakers()
def _load_speakers(self):
speakers_file = self.model_path / "speakers.json"
with open(speakers_file, "r") as f:
return json.load(f)
def get_voice_embedding(self, voice_name=None):
"""Get embedding by name or return the first one if name is None"""
if voice_name:
for speaker in self.speakers:
if speaker['name'] == voice_name:
return np.array(speaker['embedding'])
# Return first voice if no match or no name specified
return np.array(self.speakers[0]['embedding'])
def get_available_voices(self):
"""Return a list of available voice names"""
return [speaker['name'] for speaker in self.speakers]