File size: 2,955 Bytes
ea2601f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Abstract base class and shared types for cry classifiers."""

from __future__ import annotations

from abc import ABC, abstractmethod
from dataclasses import dataclass

import numpy as np

# ── Label sets ────────────────────────────────────────────────────────────────
LABELS_5CLASS = ["belly_pain", "burping", "discomfort", "hungry", "tired"]
LABELS_8CLASS = [
    "hungry", "burping", "scared", "belly_pain",
    "discomfort", "cold_hot", "lonely", "tired",
]

LABEL_EMOJI: dict[str, str] = {
    "belly_pain": "😣",
    "burping": "🫧",
    "discomfort": "πŸ˜–",
    "hungry": "🍼",
    "tired": "😴",
    "scared": "😨",
    "cold_hot": "🌑️",
    "lonely": "πŸ₯Ί",
    "cry": "βœ…",
    "not_cry": "❌",
}

# What each cry label means β€” shown in the terminal legend
LABEL_MEANING: dict[str, str] = {
    "belly_pain": "Baby has stomach cramps or gas β€” try gentle tummy massage or bicycle legs",
    "burping":    "Baby needs to burp β€” hold upright and pat back gently",
    "discomfort": "General discomfort β€” check diaper, clothing, temperature, or position",
    "hungry":     "Baby is hungry β€” time to feed",
    "tired":      "Baby is sleepy or overtired β€” needs soothing and rest",
    "scared":     "Baby is startled or frightened β€” comfort and hold close",
    "cold_hot":   "Baby is too cold or too warm β€” adjust clothing or room temperature",
    "lonely":     "Baby wants attention or closeness β€” pick up and cuddle",
}


def display_label(raw: str) -> str:
    """Return an emoji-prefixed human-friendly label."""
    emoji = LABEL_EMOJI.get(raw, "❓")
    name = raw.replace("_", " ").title()
    return f"{emoji} {name}"


# ── Prediction dataclass ─────────────────────────────────────────────────────
@dataclass
class CryPrediction:
    model_name: str
    label: str               # raw label
    display_label: str        # emoji + human name
    confidence: float         # 0.0 – 1.0
    latency_ms: float         # inference time in ms
    error: str | None = None


# ── Abstract classifier ──────────────────────────────────────────────────────
class CryClassifier(ABC):
    name: str = "unnamed"
    description: str = ""

    def __init__(self) -> None:
        self._loaded = False

    @abstractmethod
    def load(self) -> None:
        """Download weights (if needed) and initialize the model."""

    @abstractmethod
    def predict(self, audio_np: np.ndarray, sr: int) -> CryPrediction:
        """Run inference on a single audio window and return a prediction."""

    def is_loaded(self) -> bool:
        return self._loaded