{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e330c1de",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchaudio\n",
    "from transformers import HubertModel\n",
    "from sklearn.metrics import PrecisionRecallDisplay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ac3dd88",
   "metadata": {},
   "outputs": [],
   "source": [
    "# use hubert from HF for feature embedding\n",
    "model = HubertModel.from_pretrained(\"facebook/hubert-base-ls960\")\n",
    "arr, sr = torchaudio.load(\"my_audio.wav\")\n",
    "if sr != 16_000:\n",
    "    arr = torchaudio.functional.resample(arr, sr, 16_000)\n",
    "# use intermediate layer\n",
    "hidden_state = model(arr[None], output_hidden_states=True).hidden_states[6]\n",
    "# take mean over time\n",
    "feats = hidden_state.detach().cpu().numpy().squeeze().mean(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03a602e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load sk-learn classifier from here: https://dl.suno-models.io/bark/models/v0/classifier.pkl\n",
    "with open(\"classifier.pkl\", \"rb\") as f:\n",
    "    clf = pickle.load(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e423794",
   "metadata": {},
   "source": [
    "### Precision-recall curve on test set"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 35808 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "e1486424",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "668856bf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c87326bd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "decdbf09",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
