{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0d83737",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !nvidia-smi\n",
    "# !echo $HOSTNAME"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fed7c348",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"3\"\n",
    "\n",
    "import json\n",
    "from suno_utils.audio import Audio\n",
    "import numpy as np\n",
    "from suno_utils.utils.s3 import read_from_s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88c3355d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, \"/home/christian/code/neon/sunoDiff/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd1bb050",
   "metadata": {},
   "outputs": [],
   "source": [
    "#dit_model_filepath = \"s3://suno-data/georg/tmp/2b_prefix_ft.pt\"\n",
    "dit_model_filepath = \"/app/suno/checkpoints/2025-05-19_15-27-59_s697/last_ckpt_infer.pt\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13171812",
   "metadata": {},
   "outputs": [],
   "source": [
    "# musicfm\n",
    "#semantic_model_filepath = \"\" #\"s3://suno-data/minz/models/musicfm_v2_epoch138.pt\"\n",
    "#semantic_clusters_filepath = \"\" #\"s3://suno-data/minz/models/musicfm_v2_fixed_centroids_5s_l6_4k.npy\"\n",
    "\n",
    "# mert25\n",
    "semantic_model_filepath=\"/home/georg/notebooks/gpu_nb/tmp/mert_25.pt\"\n",
    "semantic_clusters_filepath=\"/home/georg/notebooks/gpu_nb/tmp/mert_25_2x4k.npy\"\n",
    "\n",
    "#codec_filepath=\"/app/suno/christian/models/codecs/dac_2c_25x12.pt\"\n",
    "#codec_filepath=\"/home/georg/notebooks/gpu_nb/tmp/100hz_vae_peaq_kl_0.005.pth\"\n",
    "#codec_filepath=\"/home/georg/notebooks/gpu_nb/tmp/25hz_vae_peaq_kl_0.005.pth\"\n",
    "#codec_filepath=\"/app/suno/christian/models/codecs/vae_37epoch.ckpt\"\n",
    "#codec_filepath = \"s3://suno-data/minz/models/dac_vae_fixed_25hz.pth\"\n",
    "codec_filepath = \"s3://suno-data/minz/models/dac_vae_tuned_25hz.pth\"\n",
    "#codec_filepath = \"s3://suno-data/minz/models/convnext_vae_tuned_25hz.pth\"\n",
    "\n",
    "if \"dac_vae_fixed\" in codec_filepath or \"dac_vae_tuned\" in codec_filepath:\n",
    "    patch_size = 1\n",
    "    codec_scale_factor = 0.4\n",
    "elif \"convnext_vae_tuned\" in codec_filepath:\n",
    "    patch_size = 1\n",
    "    codec_scale_factor = 1.0\n",
    "else:\n",
    "    patch_size = 1\n",
    "    codec_scale_factor = 2.5\n",
    "\n",
    "import torch\n",
    "from suno_utils.audio import Audio\n",
    "from generation import preload_models, generate, _retrieve_models\n",
    "_ = preload_models(\n",
    "    tokenizer_filepath=\"/home/georg/notebooks/gpu_nb/tmp/tokenizer_60k.json\",\n",
    "    semantic_model_filepath=semantic_model_filepath,\n",
    "    semantic_clusters_filepath=semantic_clusters_filepath,\n",
    "    codec_filepath=codec_filepath,\n",
    "    dit_model_filepath=dit_model_filepath,\n",
    "    weights_precision=torch.bfloat16,\n",
    "    model_type=\"prefix\",\n",
    "    codec_scale_factor=codec_scale_factor,\n",
    ")\n",
    "\n",
    "models = _retrieve_models()\n",
    "model_duration_s = 30\n",
    "if models[\"dit_model\"].ctx_len is not None:\n",
    "    model_duration_s = 6 * 60\n",
    "else:\n",
    "    model_duration_s = models[\"dit_model\"].block_size // models[\"dit_model\"].io_hz\n",
    "duration_s = 2*60 if model_duration_s >= 2*60 else 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "776b734c",
   "metadata": {},
   "outputs": [],
   "source": [
    "gen_id = \"a5e2198a-f352-4abb-9a24-7f81b143ded3\" # stone\n",
    "gen_id = \"\"\n",
    "\n",
    "s3_filepath = f\"s3://suno-data-uploads/studio/uploads/{gen_id}.npz\"\n",
    "mp3_filepath = f\"s3://suno-data-uploads/studio/uploads/{gen_id}.mp3\"\n",
    "\n",
    "print(s3_filepath)\n",
    "data = read_from_s3(s3_filepath, read_f=np.load)\n",
    "\n",
    "audio = Audio.from_s3(mp3_filepath, n_channels=2)#.get_slice(0, 120.01)\n",
    "\n",
    "if \"v3.0_raw\" in data:\n",
    "    codes = data[\"v3.0_raw\"]\n",
    "elif \"v3.5_raw\" in data:\n",
    "    codes = data[\"v3.5_raw\"]\n",
    "elif \"v4.0_raw\" in data:\n",
    "    codes = data[\"v4.0_raw\"]\n",
    "else:\n",
    "    raise ValueError(\"No codes found\")\n",
    "\n",
    "os.system(f\"aws s3 cp s3://suno-data-uploads/studio/uploads/{gen_id}_hoot.json text_data.json\")\n",
    "text_data = open(\"text_data.json\", \"r\", encoding=\"utf-8\").read()\n",
    "aligned_lyrics = json.loads(text_data)\n",
    "print(aligned_lyrics)\n",
    "audio.normalize_volume().play()\n",
    "\n",
    "tags = \"pop\"\n",
    "\n",
    "lyrics = \"\"\n",
    "for elem in aligned_lyrics:\n",
    "    if \"word\" in elem:\n",
    "        lyrics += elem[\"word\"]\n",
    "\n",
    "semantic_codes = torch.from_numpy(codes[:, 0]).long()#.cuda()\n",
    "print(semantic_codes.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7880dd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(lyrics)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "425ea613",
   "metadata": {},
   "outputs": [],
   "source": [
    "short_lyrics = \"\"\"\n",
    "[verse]\n",
    "oh, my love\n",
    "My friend you know\n",
    "it's been a while\n",
    "Without thinking of you\n",
    "but the thought makes me smile\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98fb6f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import IPython\n",
    "\n",
    "seeds = [np.random.randint(0, 2**32-1) for _ in range(3)]\n",
    "pred_audios = []\n",
    "\n",
    "semantic_codes_pad = torch.ones_like(semantic_codes)\n",
    "semantic_codes_pad[:] = 4000\n",
    "\n",
    "for seed in seeds:\n",
    "    pred_audio = generate(\n",
    "        semantic_codes[:750], \n",
    "        lyrics=lyrics,\n",
    "        tags=\"pop\", \n",
    "        sampler_type=\"dpmpp\", #dpmpp, euler, rk4, pingpong\n",
    "        objective=\"rectified_flow\",\n",
    "        text_cfg_coef=3.0,\n",
    "        ctx_cfg_coef=1.0,\n",
    "        sem_cfg_coef=1.0,\n",
    "        steps=32,\n",
    "        seed=seed,\n",
    "        sigma_max=1.0,\n",
    "        semantic_skip_factor=1,\n",
    "        downscale_ctx_vector=True,\n",
    "    )\n",
    "    pred_audio.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34504c37",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_diff",
   "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.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
