{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fetch the npz from s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OUT_DATA_DIR = \"/app/suno/data/dpo/v0_1_full/\"\n",
    "# NPZ_DIR = \"/app/suno/data/dpo/13b_npz\"\n",
    "NPZ_DIR = \"/app/suno/data/dpo/13b_s32_npz\"\n",
    "# NPZ_DIR = \"/app/suno/data/dpo/concat_npz\"\n",
    "JSON_DIR = \"/app/suno/data/dpo/13b_s32_json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data_csv_path = \"/home/tony/Data/Preference/13b_v0/interesting_clips_ft_1_20240718.csv\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/13b_v0/interesting_clips_v3p5_s_8_20240813.csv\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/13b_v0/interesting_clips_v3p5_s_8_20240830_full.pkl\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/30b_v2/interesting_clips_13b_s8_20240925_full_l10.pkl\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/13b_v1/interesting_clips_13b_s8_v29_20241029_full.pkl\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/13b_v31/interesting_clips_v4_h_s_31_20241214_full.pkl\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/13b_v32/interesting_clips_v4_h_s_32_20250219_full.pkl\"\n",
    "data_csv_path = \"/home/tony/Data/Preference/13b_v32/interesting_clips_v4_h_s_32_20250303_full_long.pkl\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/30b_v2/concat_clips_20240908_v0.pkl\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df[df[\"model_name\"].str.contains(\"v4\")]\n",
    "print(df.shape)\n",
    "# for extend, also fetch their parents\n",
    "print(df[\"model_name\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Finish downloads\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hoot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Finish downloads hoot!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
