Skip to content

Classifier

transcribe.classifier

SpeakerClassifier

A class to run audio classification.

Attributes:

Name Type Description
audio_url str

S3 URL pointing to the audio.

Source code in src/transcribe/classifier.py
class SpeakerClassifier:
    """
    A class to run audio classification.

    Attributes:
        audio_url (str): S3 URL pointing to the audio.
    """

    def __init__(self, audio_url: str):
        """Constructor for the `SpeakerClassifier` class.

        Args:
            audio_url (str): S3 URL pointing to the audio.
        """
        self.audio_url = audio_url
        api = "audio-classifier-adult-child"
        self.url = f"https://ety3wzgylf.execute-api.ap-southeast-1.amazonaws.com/{api}"
        self.headers = {
            "Authorization": os.environ["API_KEY"],
            "Content-Type": "application/json",
        }
        self.payload = {"audio_url": self.audio_url}

    def predict(self) -> str:
        """Predicts the audio's speaker type, either child or adult.

        Returns:
            str: "ADULT" or "CHILD", optionally "None" if errs.
        """
        try:
            response = requests.post(
                self.url, headers=self.headers, data=json.dumps(self.payload)
            )
        except Exception as exc:
            print(f"Failed to predict for audio {self.audio_url}")
            print(exc)
            return "None"
        else:
            if response.ok:
                return response.json()["body"]["prediction"]
            else:
                return "None"

__init__(audio_url)

Constructor for the SpeakerClassifier class.

Parameters:

Name Type Description Default
audio_url str

S3 URL pointing to the audio.

required
Source code in src/transcribe/classifier.py
def __init__(self, audio_url: str):
    """Constructor for the `SpeakerClassifier` class.

    Args:
        audio_url (str): S3 URL pointing to the audio.
    """
    self.audio_url = audio_url
    api = "audio-classifier-adult-child"
    self.url = f"https://ety3wzgylf.execute-api.ap-southeast-1.amazonaws.com/{api}"
    self.headers = {
        "Authorization": os.environ["API_KEY"],
        "Content-Type": "application/json",
    }
    self.payload = {"audio_url": self.audio_url}

predict()

Predicts the audio's speaker type, either child or adult.

Returns:

Name Type Description
str str

"ADULT" or "CHILD", optionally "None" if errs.

Source code in src/transcribe/classifier.py
def predict(self) -> str:
    """Predicts the audio's speaker type, either child or adult.

    Returns:
        str: "ADULT" or "CHILD", optionally "None" if errs.
    """
    try:
        response = requests.post(
            self.url, headers=self.headers, data=json.dumps(self.payload)
        )
    except Exception as exc:
        print(f"Failed to predict for audio {self.audio_url}")
        print(exc)
        return "None"
    else:
        if response.ok:
            return response.json()["body"]["prediction"]
        else:
            return "None"