File size: 1,408 Bytes
6e802a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# naturalness.py: UTMOS22 naturalness (torch.hub tarepan/SpeechMOS).

import logging
from typing import Union

import numpy as np

logger = logging.getLogger(__name__)

UTMOS_REPO = "tarepan/SpeechMOS:v1.2.0"
UTMOS_ENTRY = "utmos22_strong"


class NaturalnessScorer:
    def __init__(self, device: str = "cuda"):
        self.device = device
        self._model = None

    def _ensure_model(self):
        if self._model is None:
            import torch
            logger.info("Loading UTMOS (%s)...", UTMOS_REPO)
            self._model = torch.hub.load(
                UTMOS_REPO, UTMOS_ENTRY, trust_repo=True).to(self.device).eval()

    def score(self, wav: Union[str, np.ndarray, "np.ndarray"],
              sr: int = None) -> dict:
        import torch
        self._ensure_model()
        if isinstance(wav, str):
            import soundfile as sf
            audio, sr = sf.read(wav, dtype="float32")
        else:
            if sr is None:
                raise ValueError("sr required when passing an array")
            audio = np.asarray(wav, dtype="float32")
        if audio.ndim > 1:
            audio = audio.mean(axis=-1)
        if audio.size == 0:
            return {"utmos": float("nan")}
        x = torch.from_numpy(np.ascontiguousarray(audio))[None].to(self.device)
        with torch.no_grad():
            val = float(self._model(x, sr).item())
        return {"utmos": val}