Spaces:
Sleeping
Sleeping
Commit ·
a4b830e
1
Parent(s): a813bbc
feat(services): add logger to classifier
Browse files
src/emotion_analysis/services/classifier.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
from typing import Literal, Self
|
| 3 |
|
|
@@ -7,7 +8,6 @@ from torch import Tensor
|
|
| 7 |
from torch import device as torch_device
|
| 8 |
from torch import inference_mode as torch_inferece_mode
|
| 9 |
from torch.cuda import is_available as cuda_is_available
|
| 10 |
-
from torch.types import Number as TorchNumber
|
| 11 |
from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
|
| 12 |
from transformers.modeling_outputs import SequenceClassifierOutput
|
| 13 |
|
|
@@ -22,10 +22,21 @@ class Classifier:
|
|
| 22 |
/,
|
| 23 |
audio_max_duration: int | None = 30,
|
| 24 |
):
|
|
|
|
|
|
|
|
|
|
| 25 |
self._device = torch_device("cuda" if cuda_is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
| 26 |
self._model = AutoModelForAudioClassification.from_pretrained(
|
| 27 |
settings.MODEL_ID
|
| 28 |
).to(self._device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
self._feat_extractor = AutoFeatureExtractor.from_pretrained(
|
| 30 |
settings.FEATURE_EXTRACTOR_ID or settings.MODEL_ID
|
| 31 |
)
|
|
@@ -47,6 +58,8 @@ class Classifier:
|
|
| 47 |
truncation: bool = True,
|
| 48 |
return_tensors_type: Literal["pt", "tf"] = "pt",
|
| 49 |
) -> dict[str, Tensor]:
|
|
|
|
|
|
|
| 50 |
audio, _ = librosa_load(audio_path, sr=None)
|
| 51 |
audio = (
|
| 52 |
audio[: self._max_length]
|
|
@@ -65,6 +78,7 @@ class Classifier:
|
|
| 65 |
def _predict(self: Self, audio_sample: dict[str, Tensor]) -> Tensor:
|
| 66 |
with torch_inferece_mode():
|
| 67 |
prediction: SequenceClassifierOutput = self._model(**audio_sample)
|
|
|
|
| 68 |
return prediction.logits
|
| 69 |
|
| 70 |
def predict(
|
|
@@ -84,6 +98,8 @@ class Classifier:
|
|
| 84 |
.softmax(dim=-1)[0]
|
| 85 |
)
|
| 86 |
|
|
|
|
|
|
|
| 87 |
return (
|
| 88 |
{idx: prob.item() for idx, prob in enumerate(probs)}
|
| 89 |
if return_labeled_probs
|
|
|
|
| 1 |
+
from logging import Logger, getLogger
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import Literal, Self
|
| 4 |
|
|
|
|
| 8 |
from torch import device as torch_device
|
| 9 |
from torch import inference_mode as torch_inferece_mode
|
| 10 |
from torch.cuda import is_available as cuda_is_available
|
|
|
|
| 11 |
from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
|
| 12 |
from transformers.modeling_outputs import SequenceClassifierOutput
|
| 13 |
|
|
|
|
| 22 |
/,
|
| 23 |
audio_max_duration: int | None = 30,
|
| 24 |
):
|
| 25 |
+
self._log: Logger = getLogger(__name__)
|
| 26 |
+
|
| 27 |
+
self._log.debug("Selecting model device")
|
| 28 |
self._device = torch_device("cuda" if cuda_is_available() else "cpu")
|
| 29 |
+
self._log.info(f"Model will be initiated in {self._device}")
|
| 30 |
+
|
| 31 |
+
self._log.debug(f"Get pretrained {settings.MODEL_ID} to {self._device}")
|
| 32 |
self._model = AutoModelForAudioClassification.from_pretrained(
|
| 33 |
settings.MODEL_ID
|
| 34 |
).to(self._device)
|
| 35 |
+
self._log.info(f"Model {settings.MODEL_ID} available in {self._device}")
|
| 36 |
+
|
| 37 |
+
self._log.debug(
|
| 38 |
+
f"Get feature extractor {settings.FEATURE_EXTRACTOR_ID or settings.MODEL_ID}"
|
| 39 |
+
)
|
| 40 |
self._feat_extractor = AutoFeatureExtractor.from_pretrained(
|
| 41 |
settings.FEATURE_EXTRACTOR_ID or settings.MODEL_ID
|
| 42 |
)
|
|
|
|
| 58 |
truncation: bool = True,
|
| 59 |
return_tensors_type: Literal["pt", "tf"] = "pt",
|
| 60 |
) -> dict[str, Tensor]:
|
| 61 |
+
self._log.debug(f"Preprocessing audio: {audio_path}")
|
| 62 |
+
|
| 63 |
audio, _ = librosa_load(audio_path, sr=None)
|
| 64 |
audio = (
|
| 65 |
audio[: self._max_length]
|
|
|
|
| 78 |
def _predict(self: Self, audio_sample: dict[str, Tensor]) -> Tensor:
|
| 79 |
with torch_inferece_mode():
|
| 80 |
prediction: SequenceClassifierOutput = self._model(**audio_sample)
|
| 81 |
+
self._log.debug(f"Prediction logits: {prediction.logits}")
|
| 82 |
return prediction.logits
|
| 83 |
|
| 84 |
def predict(
|
|
|
|
| 98 |
.softmax(dim=-1)[0]
|
| 99 |
)
|
| 100 |
|
| 101 |
+
self._log.info(f"Prediction probabilities: {probs}")
|
| 102 |
+
|
| 103 |
return (
|
| 104 |
{idx: prob.item() for idx, prob in enumerate(probs)}
|
| 105 |
if return_labeled_probs
|