{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fetch the npz from s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:36:56.065152Z",
     "start_time": "2024-05-26T00:36:54.886147Z"
    },
    "execution": {
     "iopub.execute_input": "2024-10-13T03:27:46.567961Z",
     "iopub.status.busy": "2024-10-13T03:27:46.567800Z",
     "iopub.status.idle": "2024-10-13T03:27:47.361897Z",
     "shell.execute_reply": "2024-10-13T03:27:47.361445Z",
     "shell.execute_reply.started": "2024-10-13T03:27:46.567948Z"
    }
   },
   "outputs": [],
   "source": [
    "import ast\n",
    "import os\n",
    "import sys\n",
    "from collections import defaultdict\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "from suno_utils.utils.text import read_json, read_jsonl, write_json, write_jsonl\n",
    "from tqdm import tqdm\n",
    "\n",
    "sys.path.insert(0, \"/home/tony/Work/neon/sunoGPT/scripts/\")\n",
    "\n",
    "import numpy as np\n",
    "from data_preparation_7b import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:36:56.068073Z",
     "start_time": "2024-05-26T00:36:56.066469Z"
    },
    "execution": {
     "iopub.execute_input": "2024-10-13T03:27:47.362726Z",
     "iopub.status.busy": "2024-10-13T03:27:47.362517Z",
     "iopub.status.idle": "2024-10-13T03:27:47.364563Z",
     "shell.execute_reply": "2024-10-13T03:27:47.364235Z",
     "shell.execute_reply.started": "2024-10-13T03:27:47.362712Z"
    }
   },
   "outputs": [],
   "source": [
    "NPZ_DIR = \"/app/suno/data/dpo/30b_npz\"\n",
    "JSON_DIR = \"/app/suno/data/dpo/30b_json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T03:27:47.365207Z",
     "iopub.status.busy": "2024-10-13T03:27:47.364999Z",
     "iopub.status.idle": "2024-10-13T03:27:53.658263Z",
     "shell.execute_reply": "2024-10-13T03:27:53.657862Z",
     "shell.execute_reply.started": "2024-10-13T03:27:47.365196Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(281912, 68)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# data_csv_path = \"/home/tony/Data/Preference/13b_v0/interesting_clips_ft_1_20240717.csv\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v0/interesting_clips_v4_t_1_20240808_v22_slice.csv\"\n",
    "# df = pd.read_csv(data_path)\n",
    "# data_csv_path = \"/home/tony/Data/Preference/30b_v1/interesting_clips_v4_t_2_20240811.csv\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v0/interesting_clips_v4_t_1_20240808_v22_slice.pkl\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v1/interesting_clips_v4_t_2_20240827_full.pkl\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v2/interesting_clips_v4_t_3_20240912_full.pkl\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v2/interesting_clips_v4_t_3_20240919_full_l10.pkl\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v3/interesting_clips_v4_t_4_20240925_full.pkl\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v3/interesting_clips_v4_t_4_20240925_full_l10.pkl\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v3/interesting_clips_v4_t_4_20241007_full.pkl\"\n",
    "data_path = \"/home/tony/Data/Preference/30b_v3/interesting_clips_v4_t_4_20241012_last.pkl\"\n",
    "# data_path = \"/home/tony/Data/Preference/30b_v2/concat_clips_20240908_v0.pkl\"\n",
    "df = pd.read_pickle(data_path)\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T03:27:53.659519Z",
     "iopub.status.busy": "2024-10-13T03:27:53.659333Z",
     "iopub.status.idle": "2024-10-13T03:27:53.770319Z",
     "shell.execute_reply": "2024-10-13T03:27:53.769851Z",
     "shell.execute_reply.started": "2024-10-13T03:27:53.659506Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(281912, 68)\n",
      "model_name\n",
      "chirp-v3p5-engine-t-3    281912\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "df = df[df[\"model_name\"].str.contains(\"v3p5\")]\n",
    "print(df.shape)\n",
    "# for extend, also fetch their parents\n",
    "print(df[\"model_name\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T03:27:53.771080Z",
     "iopub.status.busy": "2024-10-13T03:27:53.770909Z",
     "iopub.status.idle": "2024-10-13T03:27:53.773194Z",
     "shell.execute_reply": "2024-10-13T03:27:53.772871Z",
     "shell.execute_reply.started": "2024-10-13T03:27:53.771066Z"
    }
   },
   "outputs": [],
   "source": [
    "def custom_parse(x):\n",
    "    try:\n",
    "        return json.loads(x)\n",
    "    except:\n",
    "        return {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T03:27:53.773880Z",
     "iopub.status.busy": "2024-10-13T03:27:53.773643Z",
     "iopub.status.idle": "2024-10-13T03:28:53.662700Z",
     "shell.execute_reply": "2024-10-13T03:28:53.662186Z",
     "shell.execute_reply.started": "2024-10-13T03:27:53.773868Z"
    }
   },
   "outputs": [],
   "source": [
    "# if \"extend\" in data_csv_path:\n",
    "test_slice = df[\"metadata\"].apply(lambda x: ast.literal_eval(str(x)))\n",
    "# test_slice = df[\"metadata\"].apply(lambda x: custom_parse(str(x)))\n",
    "test_slice_series = test_slice.apply(pd.Series)\n",
    "df = pd.concat([df, test_slice_series], axis=1, join=\"inner\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NPZ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:36:59.940064Z",
     "start_time": "2024-05-26T00:36:59.933947Z"
    },
    "execution": {
     "iopub.execute_input": "2024-10-13T03:28:53.683313Z",
     "iopub.status.busy": "2024-10-13T03:28:53.683114Z",
     "iopub.status.idle": "2024-10-13T03:28:53.727728Z",
     "shell.execute_reply": "2024-10-13T03:28:53.727230Z",
     "shell.execute_reply.started": "2024-10-13T03:28:53.683300Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "281912\n",
      "full 281912\n"
     ]
    }
   ],
   "source": [
    "# download all clips; 883k\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "\n",
    "s3_ids = df[\"s3_id\"].values\n",
    "print(len(s3_ids))\n",
    "if \"extend\" in data_path:\n",
    "    s3_ids = set(s3_ids).union(set(df[\"audio_prompt_id\"].values))\n",
    "print(\"full\", len(s3_ids))\n",
    "s3_paths = [f\"s3://suno-data-uploads/studio/uploads/{s3_id}.npz\" for s3_id in s3_ids]\n",
    "local_paths = [f\"{NPZ_DIR}/{s3_id}.npz\" for s3_id in s3_ids]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:37:00.153602Z",
     "start_time": "2024-05-26T00:36:59.941125Z"
    },
    "execution": {
     "iopub.execute_input": "2024-10-13T03:28:53.728521Z",
     "iopub.status.busy": "2024-10-13T03:28:53.728349Z",
     "iopub.status.idle": "2024-10-13T03:29:25.162335Z",
     "shell.execute_reply": "2024-10-13T03:29:25.161861Z",
     "shell.execute_reply.started": "2024-10-13T03:28:53.728508Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 28216 28216 28216\n"
     ]
    }
   ],
   "source": [
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_ids_set = set([os.path.basename(path).replace(\".npz\", \"\") for path in unfinished_paths])\n",
    "unfinished_ids = [i for i in s3_ids if i in unfinished_ids_set]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths), len(unfinished_ids))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:49:33.275556Z",
     "start_time": "2024-05-26T00:37:00.154648Z"
    },
    "execution": {
     "iopub.execute_input": "2024-10-13T03:29:25.163125Z",
     "iopub.status.busy": "2024-10-13T03:29:25.162945Z",
     "iopub.status.idle": "2024-10-13T03:56:11.453056Z",
     "shell.execute_reply": "2024-10-13T03:56:11.452440Z",
     "shell.execute_reply.started": "2024-10-13T03:29:25.163112Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29/29 [26:45<00:00, 55.38s/it]\n"
     ]
    }
   ],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:59:42.420025Z",
     "start_time": "2024-05-26T00:49:33.276845Z"
    },
    "execution": {
     "iopub.execute_input": "2024-10-13T03:56:11.454089Z",
     "iopub.status.busy": "2024-10-13T03:56:11.453801Z",
     "iopub.status.idle": "2024-10-13T03:56:48.215114Z",
     "shell.execute_reply": "2024-10-13T03:56:48.214528Z",
     "shell.execute_reply.started": "2024-10-13T03:56:11.454075Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 114 114\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:06<00:00,  6.59s/it]\n"
     ]
    }
   ],
   "source": [
    "# # deleted files are at: deleted; try to get them as well\n",
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))\n",
    "unfinished_deleted_s3_paths = [\n",
    "    path.replace(\"/uploads/\", \"/deleted/\") for path in unfinished_s3_paths\n",
    "]\n",
    "_ = download_s3_files(unfinished_deleted_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:59:42.423220Z",
     "start_time": "2024-05-26T00:59:42.421231Z"
    },
    "execution": {
     "iopub.execute_input": "2024-10-13T03:56:48.216181Z",
     "iopub.status.busy": "2024-10-13T03:56:48.215854Z",
     "iopub.status.idle": "2024-10-13T03:56:48.218796Z",
     "shell.execute_reply": "2024-10-13T03:56:48.218367Z",
     "shell.execute_reply.started": "2024-10-13T03:56:48.216166Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finish downloads\n"
     ]
    }
   ],
   "source": [
    "print(\"Finish downloads\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HOOT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T03:56:48.219578Z",
     "iopub.status.busy": "2024-10-13T03:56:48.219337Z",
     "iopub.status.idle": "2024-10-13T03:56:48.271910Z",
     "shell.execute_reply": "2024-10-13T03:56:48.271381Z",
     "shell.execute_reply.started": "2024-10-13T03:56:48.219566Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28216\n"
     ]
    }
   ],
   "source": [
    "s3_ids = unfinished_ids\n",
    "print(len(s3_ids))\n",
    "s3_paths = [f\"s3://suno-data-uploads/studio/uploads/{s3_id}_hoot.json\" for s3_id in s3_ids]\n",
    "local_paths = [f\"{JSON_DIR}/{s3_id}_hoot.json\" for s3_id in s3_ids]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T03:56:48.273590Z",
     "iopub.status.busy": "2024-10-13T03:56:48.273366Z",
     "iopub.status.idle": "2024-10-13T03:57:04.161527Z",
     "shell.execute_reply": "2024-10-13T03:57:04.160932Z",
     "shell.execute_reply.started": "2024-10-13T03:56:48.273577Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 28216 28216\n"
     ]
    }
   ],
   "source": [
    "finished_paths = os.listdir(JSON_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T03:57:04.162468Z",
     "iopub.status.busy": "2024-10-13T03:57:04.162210Z",
     "iopub.status.idle": "2024-10-13T04:23:34.637722Z",
     "shell.execute_reply": "2024-10-13T04:23:34.637082Z",
     "shell.execute_reply.started": "2024-10-13T03:57:04.162452Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29/29 [26:30<00:00, 54.84s/it]\n"
     ]
    }
   ],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T04:23:34.638787Z",
     "iopub.status.busy": "2024-10-13T04:23:34.638511Z",
     "iopub.status.idle": "2024-10-13T04:27:15.289556Z",
     "shell.execute_reply": "2024-10-13T04:27:15.288947Z",
     "shell.execute_reply.started": "2024-10-13T04:23:34.638771Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 3822 3822\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [03:23<00:00, 50.78s/it]\n"
     ]
    }
   ],
   "source": [
    "# # deleted files are at: deleted; try to get them as well\n",
    "finished_paths = os.listdir(JSON_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))\n",
    "unfinished_deleted_s3_paths = [\n",
    "    path.replace(\"/uploads/\", \"/deleted/\") for path in unfinished_s3_paths\n",
    "]\n",
    "_ = download_s3_files(unfinished_deleted_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-13T04:27:15.290572Z",
     "iopub.status.busy": "2024-10-13T04:27:15.290268Z",
     "iopub.status.idle": "2024-10-13T04:27:15.293300Z",
     "shell.execute_reply": "2024-10-13T04:27:15.292871Z",
     "shell.execute_reply.started": "2024-10-13T04:27:15.290557Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finish downloads hoot!\n"
     ]
    }
   ],
   "source": [
    "print(\"Finish downloads hoot!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.10.14"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
