{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"4\"\n",
    "import time\n",
    "import torch\n",
    "import torchaudio\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from suno_utils.tasks.ear import load_model\n",
    "from suno_utils.audio.conversion import Audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/tony/Work/glockenspiel/suno_utils/suno_utils/utils/s3.py:325: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
      "  data = read_f(tmp_filepath)\n",
      "/home/tony/anaconda3/envs/suno_env_dev/lib/python3.10/site-packages/torch/nn/modules/transformer.py:375: UserWarning: enable_nested_tensor is True, but self.use_nested_tensor is False because encoder_layer.self_attn.batch_first was not True(use batch_first for better inference performance)\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "model = load_model(\n",
    "    \"s3://suno-data/christian/checkpoints/ear/ear_v2_s3080.pt\", compile=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original score: 24.546947703642005\n",
      "No ctx score: 20.546498953127394\n",
      "Ctx score: 12.751556779824051\n"
     ]
    }
   ],
   "source": [
    "# audio file we want to test\n",
    "original_audio_filepath = \"/home/christian/audio/reference-audio-wav/02 Dreams.wav\"\n",
    "diff_no_ctx_audio_filepath = (\n",
    "    \"/home/christian/code/christian/notebooks/audio/dreams-no-ctx.mp3\"\n",
    ")\n",
    "diff_ctx_audio_filepath = (\n",
    "    \"/home/christian/code/christian/notebooks/audio/dreams-with-ctx-2.mp3\"\n",
    ")\n",
    "\n",
    "original_score = model.get_score(original_audio_filepath)\n",
    "no_ctx_score = model.get_score(diff_no_ctx_audio_filepath)\n",
    "ctx_score = model.get_score(diff_ctx_audio_filepath)\n",
    "\n",
    "# higher is better quality\n",
    "print(f\"Original score: {original_score}\")\n",
    "print(f\"No ctx score: {no_ctx_score}\")\n",
    "print(f\"Ctx score: {ctx_score}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "negative_id = \"df9e8d5a-720e-4382-a10a-875b9b1e488a\"  # negative_id\n",
    "positive_id = \"30c532ed-9422-4db3-8f4c-2b56c3aed34a\"  # positive_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_negative_audio = Audio.from_s3(\n",
    "    f\"s3://suno-data-uploads/studio/uploads/{negative_id}.mp3\",\n",
    "    n_channels=2,\n",
    ")\n",
    "full_positive_audio = Audio.from_s3(\n",
    "    f\"s3://suno-data-uploads/studio/uploads/{positive_id}.mp3\",\n",
    "    n_channels=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Negative score: ([16.731348037719727, 16.648983001708984, 16.510379791259766, 15.283489227294922, 16.589393615722656, 17.40402603149414, 17.215360641479492, 16.01370620727539, 15.838603019714355, 16.841480255126953, 15.627289772033691, 15.178884506225586, 14.905057907104492, 16.478097915649414, 17.415067672729492, 18.36737632751465, 17.516324996948242, 18.150596618652344, 19.197296142578125, 19.55816650390625, 15.752933502197266, 15.659541130065918, 16.29912757873535, 17.817991256713867, 16.63869285583496, 16.30901527404785, 18.04009437561035, 17.317567825317383, 17.125614166259766, 15.609891891479492, 16.857179641723633, 16.941354751586914, 17.418489456176758, 17.137941360473633, 15.716611862182617, 18.219684600830078, 17.03537940979004, 17.64090919494629, 16.97809600830078], 16.871462675241325)\n",
      "Positive score: ([14.42691421508789, 16.5977783203125, 14.952119827270508, 16.1341552734375, 14.65555191040039, 12.330068588256836, 13.210272789001465, 15.690802574157715, 15.548036575317383, 15.748737335205078, 17.562685012817383, 14.128913879394531, 13.02000904083252, 16.05008316040039, 16.679595947265625, 16.866722106933594, 15.655074119567871, 15.820582389831543, 13.882599830627441, 16.017162322998047, 15.579368591308594, 16.765872955322266, 12.169157028198242, 16.002220153808594, 13.292767524719238, 12.816887855529785, 16.882190704345703, 14.350850105285645, 15.097795486450195, 15.148151397705078, 16.98543930053711, 16.1918888092041, 16.815595626831055, 14.738590240478516, 16.355398178100586, 13.624937057495117, 17.38184928894043, 16.33260726928711, 13.291391372680664, 15.26865291595459, 16.289121627807617, 19.07379722595215, 15.465764999389648, 15.841094017028809, 14.44749641418457, 14.5206880569458, 14.952502250671387, 14.929201126098633], 15.324773808320364)\n"
     ]
    }
   ],
   "source": [
    "negative_score = model.get_score(full_negative_audio, return_scores=True)\n",
    "positive_score = model.get_score(full_positive_audio, return_scores=True)\n",
    "\n",
    "# higher is better quality\n",
    "print(f\"Negative score: {negative_score}\")\n",
    "print(f\"Positive score: {positive_score}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_env_dev",
   "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.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
