From fa02b5a3894af5242024dd01a784f4a9dbcde09c Mon Sep 17 00:00:00 2001 From: hww <3188753874@qq.com> Date: Tue, 28 Oct 2025 20:09:29 +0800 Subject: [PATCH] test music_gen demo --- .../13.musicgen/run_musicgen.ipynb | 296 ++++++------------ 1 file changed, 88 insertions(+), 208 deletions(-) diff --git a/Season2.step_into_llm/13.musicgen/run_musicgen.ipynb b/Season2.step_into_llm/13.musicgen/run_musicgen.ipynb index e04f60f..49eab07 100644 --- a/Season2.step_into_llm/13.musicgen/run_musicgen.ipynb +++ b/Season2.step_into_llm/13.musicgen/run_musicgen.ipynb @@ -39,10 +39,10 @@ " python =3.9\n", " mindspore = 2.3.1\n", " mindnlp = 0.4.0\n", - " jieba\n", - " soundfile \n", - " librosa\n", - "\n", + " jieba = 0.42.1\n", + " soundfile = 0.13.1\n", + " librosa = 0.10.1\n", + " cffi = 1.15.1\n", "**在线运行代码平台链接:**\n", "- 1. [华为云AI Gallery](https://pangu.huaweicloud.com/gallery/asset-detail.html?id=c72241ed-465f-418d-b58a-ed4aabb0eb73)\n", "- 2. [大模型平台AI实验室统一入口](https://xihe.mindspore.cn/projects)" @@ -55,71 +55,82 @@ "id": "77ee39cc-654b-4f0e-b601-013e484c16f0" }, "source": [ - "## Load the Model\n", + "## Load the Model and Dataset\n", "\n", "The pre-trained MusicGen small, medium and large checkpoints can be loaded from the [pre-trained weights](https://huggingface.co/models?search=facebook/musicgen-) on the Hugging Face Hub. Change the repo id with the checkpoint size you wish to load. We'll default to the small checkpoint, which is the fastest of the three but has the lowest audio quality:" ] }, { "cell_type": "code", - "execution_count": 4, - "id": "b0d87424-9f38-4658-ba47-2a465d52ad77", - "metadata": { - "id": "b0d87424-9f38-4658-ba47-2a465d52ad77" - }, + "execution_count": 1, + "id": "933c003f-82f1-49c2-9c7e-e21072e04c23", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found existing installation: mindformers 0.8.0\n", + "Uninstalling mindformers-0.8.0:\n", + " Successfully uninstalled mindformers-0.8.0\n" + ] + } + ], + "source": [ + "!pip uninstall mindformers -y" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2cdcff55-2979-4cea-85de-c134ec4eb489", + "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for type is zero.\n", + "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/numpy/core/getlimits.py:500: UserWarning: The value of the smallest subnormal for type is zero.\n", " setattr(self, word, getattr(machar, word).flat[0])\n", - "/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for type is zero.\n", + "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for type is zero.\n", " return self._float_to_str(self.smallest_subnormal)\n", - "/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for type is zero.\n", + "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/numpy/core/getlimits.py:500: UserWarning: The value of the smallest subnormal for type is zero.\n", " setattr(self, word, getattr(machar, word).flat[0])\n", - "/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for type is zero.\n", + "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for type is zero.\n", " return self._float_to_str(self.smallest_subnormal)\n", + "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n", "Building prefix dict from the default dictionary ...\n", - "Dumping model to file cache /tmp/jieba.cache\n", - "Loading model cost 1.012 seconds.\n", - "Prefix dict has been built successfully.\n" + "Loading model from cache /tmp/jieba.cache\n", + "Loading model cost 1.296 seconds.\n", + "Prefix dict has been built successfully.\n", + "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/torch_npu/utils/path_manager.py:79: UserWarning: Warning: The /usr/local/Ascend/ascend-toolkit/latest owner does not match the current user.\n", + " warnings.warn(f\"Warning: The {path} owner does not match the current user.\")\n", + "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/torch_npu/utils/path_manager.py:79: UserWarning: Warning: The /usr/local/Ascend/ascend-toolkit/8.0.RC1/aarch64-linux/ascend_toolkit_install.info owner does not match the current user.\n", + " warnings.warn(f\"Warning: The {path} owner does not match the current user.\")\n" ] - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "75a95ec53fc947d5988d6827e7d5053c", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - " 0%| | 0.00/1.55k [00:00\n", - " \n", + " \n", " Your browser does not support the audio element.\n", " \n", " " @@ -224,7 +222,7 @@ "" ] }, - "execution_count": 6, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -248,7 +246,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "id": "04291f52-0a75-4ddb-9eff-e853d0f17288", "metadata": { "id": "04291f52-0a75-4ddb-9eff-e853d0f17288" @@ -272,7 +270,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "id": "d75ad107-e19b-47f3-9cf1-5102ab4ae74a", "metadata": { "id": "d75ad107-e19b-47f3-9cf1-5102ab4ae74a" @@ -284,7 +282,7 @@ "5.12" ] }, - "execution_count": 8, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -311,87 +309,17 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "id": "5fba4154-13f6-403a-958b-101d6eacfb6e", "metadata": { "id": "5fba4154-13f6-403a-958b-101d6eacfb6e" }, "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "58ec6de737fd4523ae119fb576f5d490", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - " 0%| | 0.00/275 [00:00\n", - " \n", + " \n", " Your browser does not support the audio element.\n", " \n", " " @@ -408,7 +336,7 @@ "" ] }, - "execution_count": 9, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -459,68 +387,20 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "56a5c28a-f6c1-4ac8-ae08-6776a2b2c5b8", "metadata": { "id": "56a5c28a-f6c1-4ac8-ae08-6776a2b2c5b8" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n", - "Requirement already satisfied: soundfile in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (0.12.1)\n", - "Collecting librosa\n", - " Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8c/8a/2d231b35456506b7c98b3ab9bbf07917b205fed8615d2e59e976ab497fff/librosa-0.10.2.post1-py3-none-any.whl (260 kB)\n", - "Requirement already satisfied: cffi>=1.0 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from soundfile) (1.17.0)\n", - "Collecting audioread>=2.1.9 (from librosa)\n", - " Downloading https://pypi.tuna.tsinghua.edu.cn/packages/57/8d/30aa32745af16af0a9a650115fbe81bde7c610ed5c21b381fca0196f3a7f/audioread-3.0.1-py3-none-any.whl (23 kB)\n", - "Requirement already satisfied: numpy!=1.22.0,!=1.22.1,!=1.22.2,>=1.20.3 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from librosa) (1.26.4)\n", - "Requirement already satisfied: scipy>=1.2.0 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from librosa) (1.13.1)\n", - "Requirement already satisfied: scikit-learn>=0.20.0 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from librosa) (1.5.1)\n", - "Requirement already satisfied: joblib>=0.14 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from librosa) (1.4.2)\n", - "Requirement already satisfied: decorator>=4.3.0 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from librosa) (5.1.1)\n", - "Collecting numba>=0.51.0 (from librosa)\n", - " Downloading https://pypi.tuna.tsinghua.edu.cn/packages/3b/bd/f1985719ff34e37e07bb18f9d3acd17e5a21da255f550c8eae031e2ddf5f/numba-0.60.0-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (3.4 MB)\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m37.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[?25hCollecting pooch>=1.1 (from librosa)\n", - " Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a8/87/77cc11c7a9ea9fd05503def69e3d18605852cd0d4b0d3b8f15bbeb3ef1d1/pooch-1.8.2-py3-none-any.whl (64 kB)\n", - "Collecting soxr>=0.3.2 (from librosa)\n", - " Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c3/c6/f3d54e8c579aa5f192d62c87cd86b88e0b1d6fcab7b541663a3816f1eb06/soxr-0.4.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB)\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m57.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[?25hRequirement already satisfied: typing-extensions>=4.1.1 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from librosa) (4.11.0)\n", - "Collecting lazy-loader>=0.1 (from librosa)\n", - " Downloading https://pypi.tuna.tsinghua.edu.cn/packages/83/60/d497a310bde3f01cb805196ac61b7ad6dc5dcf8dce66634dc34364b20b4f/lazy_loader-0.4-py3-none-any.whl (12 kB)\n", - "Collecting msgpack>=1.0 (from librosa)\n", - " Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ad/61/225d64e983e51f960cac41fd1084188764fcc7430e75f609ad9d86e47839/msgpack-1.0.8-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (375 kB)\n", - "Requirement already satisfied: pycparser in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from cffi>=1.0->soundfile) (2.22)\n", - "Requirement already satisfied: packaging in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from lazy-loader>=0.1->librosa) (24.1)\n", - "Collecting llvmlite<0.44,>=0.43.0dev0 (from numba>=0.51.0->librosa)\n", - " Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c8/c6/9324eb5de2ba9d99cbed853d85ba7a318652a48e077797bec27cf40f911d/llvmlite-0.43.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (42.9 MB)\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m42.9/42.9 MB\u001b[0m \u001b[31m95.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", - "\u001b[?25hRequirement already satisfied: platformdirs>=2.5.0 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from pooch>=1.1->librosa) (4.2.2)\n", - "Requirement already satisfied: requests>=2.19.0 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from pooch>=1.1->librosa) (2.32.3)\n", - "Requirement already satisfied: threadpoolctl>=3.1.0 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from scikit-learn>=0.20.0->librosa) (3.5.0)\n", - "Requirement already satisfied: charset-normalizer<4,>=2 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from requests>=2.19.0->pooch>=1.1->librosa) (3.3.2)\n", - "Requirement already satisfied: idna<4,>=2.5 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from requests>=2.19.0->pooch>=1.1->librosa) (3.7)\n", - "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from requests>=2.19.0->pooch>=1.1->librosa) (2.2.2)\n", - "Requirement already satisfied: certifi>=2017.4.17 in /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages (from requests>=2.19.0->pooch>=1.1->librosa) (2024.7.4)\n", - "Installing collected packages: soxr, msgpack, llvmlite, lazy-loader, audioread, pooch, numba, librosa\n", - "Successfully installed audioread-3.0.1 lazy-loader-0.4 librosa-0.10.2.post1 llvmlite-0.43.0 msgpack-1.0.8 numba-0.60.0 pooch-1.8.2 soxr-0.4.0\n" - ] - } - ], + "outputs": [], "source": [ - "from mindnlp.dataset import load_dataset\n", "\n", - "dataset = load_dataset(\"sanchit-gandhi/gtzan\", split=\"train\", streaming=True)\n", "sample = next(iter(dataset.create_dict_iterator(output_numpy=True)))[\"audio\"]" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 9, "id": "79fd7ab3-4d1f-4838-aff8-13d6fa568b3c", "metadata": {}, "outputs": [], @@ -539,7 +419,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 10, "id": "3787d4e6-6d1c-479b-8c92-c8a58d176144", "metadata": {}, "outputs": [ @@ -547,7 +427,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "/\r" + "|\r" ] }, { @@ -555,7 +435,7 @@ "text/html": [ "\n", " \n", " " @@ -564,7 +444,7 @@ "" ] }, - "execution_count": 14, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -590,7 +470,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 11, "id": "5495f568-51ca-439d-b47b-8b52e89b78f1", "metadata": { "id": "5495f568-51ca-439d-b47b-8b52e89b78f1" @@ -601,7 +481,7 @@ "text/html": [ "\n", " \n", " " @@ -610,7 +490,7 @@ "" ] }, - "execution_count": 15, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -654,7 +534,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 12, "id": "0zM4notb8Y1g", "metadata": { "id": "0zM4notb8Y1g" @@ -673,7 +553,7 @@ "}" ] }, - "execution_count": 16, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -694,7 +574,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 13, "id": "ensSj1IB81dA", "metadata": { "id": "ensSj1IB81dA" @@ -723,7 +603,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 14, "id": "KAExrhDl9YvS", "metadata": { "id": "KAExrhDl9YvS" @@ -749,9 +629,9 @@ "provenance": [] }, "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "MindSpore", "language": "python", - "name": "python3" + "name": "mindspore" }, "language_info": { "codemirror_mode": { @@ -763,7 +643,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.19" + "version": "3.9.18" } }, "nbformat": 4,