Spaces:
Sleeping
Sleeping
| """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 βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| 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 | |
| def load(self) -> None: | |
| """Download weights (if needed) and initialize the model.""" | |
| 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 | |