{
 "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": "2025-07-14T04:04:31.284382Z",
     "iopub.status.busy": "2025-07-14T04:04:31.284250Z",
     "iopub.status.idle": "2025-07-14T04:04:33.037610Z",
     "shell.execute_reply": "2025-07-14T04:04:33.037099Z",
     "shell.execute_reply.started": "2025-07-14T04:04:31.284363Z"
    }
   },
   "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": "2025-07-14T04:04:33.038285Z",
     "iopub.status.busy": "2025-07-14T04:04:33.038097Z",
     "iopub.status.idle": "2025-07-14T04:04:33.040634Z",
     "shell.execute_reply": "2025-07-14T04:04:33.040220Z",
     "shell.execute_reply.started": "2025-07-14T04:04:33.038271Z"
    }
   },
   "outputs": [],
   "source": [
    "OUT_DATA_DIR = \"/app2/suno/data/dpo/auk_t1_npz/\"\n",
    "# NPZ_DIR = \"/app/suno/data/dpo/13b_npz\"\n",
    "NPZ_DIR = \"/app2/suno/data/dpo/auk_t1_npz/\"\n",
    "# NPZ_DIR = \"/app/suno/data/dpo/concat_npz\"\n",
    "JSON_DIR = \"/app2/suno/data/dpo/auk_t1_json/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-07-14T04:04:33.041199Z",
     "iopub.status.busy": "2025-07-14T04:04:33.041065Z",
     "iopub.status.idle": "2025-07-14T04:04:33.043145Z",
     "shell.execute_reply": "2025-07-14T04:04:33.042760Z",
     "shell.execute_reply.started": "2025-07-14T04:04:33.041186Z"
    }
   },
   "outputs": [],
   "source": [
    "data_csv_path = \"/home/tony/Data/Preference/auk_t1/interesting_clips_auk_t1_20250714.pkl\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/auk_t1/fully_merged_auk_t1.pkl\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/30b_v2/concat_clips_20240908_v0.pkl\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:36:59.932066Z",
     "start_time": "2024-05-26T00:36:56.069087Z"
    },
    "execution": {
     "iopub.execute_input": "2025-07-14T04:04:33.043683Z",
     "iopub.status.busy": "2025-07-14T04:04:33.043560Z",
     "iopub.status.idle": "2025-07-14T04:06:03.482400Z",
     "shell.execute_reply": "2025-07-14T04:06:03.481791Z",
     "shell.execute_reply.started": "2025-07-14T04:04:33.043670Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2593532, 90)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df = pd.read_csv(data_csv_path)\n",
    "df = pd.read_pickle(data_csv_path)\n",
    "df.shape\n",
    "# v2: (2341508, 31)\n",
    "# v3: (2609830, 31)\n",
    "# v4: (1926120, 35)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-07-14T04:06:03.484546Z",
     "iopub.status.busy": "2025-07-14T04:06:03.483999Z",
     "iopub.status.idle": "2025-07-14T04:06:05.328678Z",
     "shell.execute_reply": "2025-07-14T04:06:05.327961Z",
     "shell.execute_reply.started": "2025-07-14T04:06:03.484525Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2593532, 90)\n",
      "model_name\n",
      "chirp-auk-t1    2593532\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "df = df[df[\"model_name\"].str.contains(\"auk\")]\n",
    "print(df.shape)\n",
    "# for extend, also fetch their parents\n",
    "print(df[\"model_name\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-07-14T04:06:05.329581Z",
     "iopub.status.busy": "2025-07-14T04:06:05.329419Z",
     "iopub.status.idle": "2025-07-14T04:06:05.332038Z",
     "shell.execute_reply": "2025-07-14T04:06:05.331563Z",
     "shell.execute_reply.started": "2025-07-14T04:06:05.329564Z"
    }
   },
   "outputs": [],
   "source": [
    "# if \"extend\" in data_csv_path:\n",
    "# test_slice = df[\"metadata\"].apply(lambda x: ast.literal_eval(x))\n",
    "# test_slice_series = test_slice.apply(pd.Series)\n",
    "# df = pd.concat([df, test_slice_series], axis=1, join=\"inner\")"
   ]
  },
  {
   "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": "2025-07-14T04:06:05.332789Z",
     "iopub.status.busy": "2025-07-14T04:06:05.332639Z",
     "iopub.status.idle": "2025-07-14T04:06:06.613707Z",
     "shell.execute_reply": "2025-07-14T04:06:06.613047Z",
     "shell.execute_reply.started": "2025-07-14T04:06:05.332775Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2593532\n",
      "full 2593532\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",
    "# 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": "2025-07-14T04:06:06.614612Z",
     "iopub.status.busy": "2025-07-14T04:06:06.614457Z",
     "iopub.status.idle": "2025-07-14T04:06:19.190227Z",
     "shell.execute_reply": "2025-07-14T04:06:19.189464Z",
     "shell.execute_reply.started": "2025-07-14T04:06:06.614597Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 907501 907501\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))"
   ]
  },
  {
   "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": "2025-07-14T04:06:19.191453Z",
     "iopub.status.busy": "2025-07-14T04:06:19.190940Z",
     "iopub.status.idle": "2025-07-14T18:21:26.380865Z",
     "shell.execute_reply": "2025-07-14T18:21:26.380230Z",
     "shell.execute_reply.started": "2025-07-14T04:06:19.191433Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 908/908 [14:14:59<00:00, 56.50s/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": "2025-07-14T18:21:26.381642Z",
     "iopub.status.busy": "2025-07-14T18:21:26.381483Z",
     "iopub.status.idle": "2025-07-14T18:28:42.983867Z",
     "shell.execute_reply": "2025-07-14T18:28:42.983322Z",
     "shell.execute_reply.started": "2025-07-14T18:21:26.381625Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 2664 2664\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [02:27<00:00, 49.04s/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": "2025-07-14T18:28:42.984621Z",
     "iopub.status.busy": "2025-07-14T18:28:42.984467Z",
     "iopub.status.idle": "2025-07-14T18:28:42.987239Z",
     "shell.execute_reply": "2025-07-14T18:28:42.986836Z",
     "shell.execute_reply.started": "2025-07-14T18:28:42.984605Z"
    }
   },
   "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": "2025-07-14T18:28:42.987846Z",
     "iopub.status.busy": "2025-07-14T18:28:42.987710Z",
     "iopub.status.idle": "2025-07-14T18:28:43.276434Z",
     "shell.execute_reply": "2025-07-14T18:28:43.275962Z",
     "shell.execute_reply.started": "2025-07-14T18:28:42.987832Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "907501\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": "2025-07-14T18:28:43.277097Z",
     "iopub.status.busy": "2025-07-14T18:28:43.276955Z",
     "iopub.status.idle": "2025-07-14T18:28:52.705957Z",
     "shell.execute_reply": "2025-07-14T18:28:52.705411Z",
     "shell.execute_reply.started": "2025-07-14T18:28:43.277083Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 907500 907500\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": "2025-07-14T18:28:52.706690Z",
     "iopub.status.busy": "2025-07-14T18:28:52.706540Z",
     "iopub.status.idle": "2025-07-15T07:30:27.747092Z",
     "shell.execute_reply": "2025-07-15T07:30:27.746236Z",
     "shell.execute_reply.started": "2025-07-14T18:28:52.706675Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 908/908 [13:01:27<00:00, 51.64s/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": "2025-07-15T07:30:27.748028Z",
     "iopub.status.busy": "2025-07-15T07:30:27.747867Z",
     "iopub.status.idle": "2025-07-15T07:36:08.658841Z",
     "shell.execute_reply": "2025-07-15T07:36:08.658267Z",
     "shell.execute_reply.started": "2025-07-15T07:30:27.748012Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 796 796\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:50<00:00, 50.64s/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": "2025-07-15T07:36:08.659624Z",
     "iopub.status.busy": "2025-07-15T07:36:08.659465Z",
     "iopub.status.idle": "2025-07-15T07:36:08.662355Z",
     "shell.execute_reply": "2025-07-15T07:36:08.661948Z",
     "shell.execute_reply.started": "2025-07-15T07:36:08.659607Z"
    }
   },
   "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": []
  },
  {
   "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.15"
  },
  "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
}
