其中包括:
1、Git 【点击下载】
2、Python 3.10【点击下载】,这是官方推荐的版本
3、Conda【点击下载】 ,推荐安装MiniConda,更精简更适合,不会夹带臃肿的环境包,注意不要选最新的 3.13 ,它对AI项目的兼容性不是很好,建议选择3.10~3.12,安装后将其添加到系统环境下,否则无法正常使用!
conda --version
1、克隆此仓库并安装到本地。
=================
git clone https://github.com/HeartMuLa/heartlib.git
cd heartlib
conda create -n heartmula python=3.10 # 创建虚拟环境
conda init
conda activate
conda activate heartmula# 激活并进入虚拟环境
pip install -e .
=================
2、使用以下命令从 huggingface下载预训练的模型、检查点,
在 heartlib 根目录下创建文件夹ckpt文件夹
=================
export HF_ENDPOINT=https://hf-mirror.com
hf download HeartMuLa/HeartMuLaGen --local-dir ./ckpt
hf download HeartMuLa/HeartMuLa-oss-3B --local-dir ./ckpt/HeartMuLa-oss-3B
hf download HeartMuLa/HeartCodec-oss-20260123 --local-dir ./ckpt/HeartCodec-oss
=================
下载完成后,./ckpt子文件夹结构应如下所示:
./ckpt/
├── HeartCodec-oss/
├── HeartMuLa-oss-3B/
├── gen_config.json
└── tokenizer.json
要生成音乐,请运行:python ./examples/run_music_generation.py --model_path=./ckpt --version="3B"
默认情况下,此命令将根据文件夹中提供的歌词和标签生成一段音乐./assets。输出的音乐将保存在./assets/output.mp3.
所有参数:
--model_path(必填):预训练模型检查点的路径--lyrics歌词文件路径(默认值./assets/lyrics.txt:)--tags标签文件路径(默认值./assets/tags.txt:)--save_path输出音频文件路径(默认值./assets/output.mp3:)--max_audio_length_ms音频最大长度(毫秒)(默认值:240000)--topk:生成过程中的 Top-k 采样参数(默认值:50)--temperature:生成采样温度(默认值:1.0)--cfg_scale:无分类器指导等级(默认值:1.5)--versionHeartMuLa 的版本,请在 [ 3B, 7B] 中选择。(默认值:3B)#7B版本尚未发布。
安装 triton模块:【点击下载】 或【网盘下载】,否则在生成的时候会报错提示模块没有加载!
歌词和标签的推荐格式:
=================
[Intro]
[Verse]
The sun creeps in across the floor
I hear the traffic outside the door
The coffee pot begins to hiss
It is another morning just like this
[Prechorus]
The world keeps spinning round and round
Feet are planted on the ground
I find my rhythm in the sound
[Chorus]
Every day the light returns
Every day the fire burns
We keep on walking down this street
Moving to the same steady beat
It is the ordinary magic that we meet
[Verse]
The hours tick deeply into noon
Chasing shadows,chasing the moon
Work is done and the lights go low
Watching the city start to glow
[Bridge]
It is not always easy,not always bright
Sometimes we wrestle with the night
But we make it to the morning light
[Chorus]
Every day the light returns
Every day the fire burns
We keep on walking down this street
Moving to the same steady beat
[Outro]
Just another day
Every single day
=================
CPU运行
要生成音乐,请运行:python3.10 ./examples/run_music_generation_cpu.py --model_path=./ckpt --version="3B"
run_music_generation_cpu.py 代码如下:
=================
from heartlib import HeartMuLaGenPipeline
import argparse
import torch
def str2bool(value):
if isinstance(value, bool):
return value
if value.lower() in ("yes", "y", "true", "t", "1"):
return True
elif value.lower() in ("no", "n", "false", "f", "0"):
return False
else:
raise argparse.ArgumentTypeError(f"Boolean value expected. Got: {value}")
def str2dtype(value):
value = value.lower()
if value == "float32" or value == "fp32":
return torch.float32
elif value == "float16" or value == "fp16":
return torch.float16
elif value == "bfloat16" or value == "bf16":
return torch.bfloat16
else:
raise argparse.ArgumentTypeError(f"Dtype not recognized: {value}")
def str2device(value):
value = value.lower()
return torch.device(value)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--model_path", type=str, required=True)
parser.add_argument("--version", type=str, default="3B")
parser.add_argument("--lyrics", type=str, default="./assets/lyrics.txt")
parser.add_argument("--tags", type=str, default="./assets/tags.txt")
parser.add_argument("--save_path", type=str, default="./assets/output.mp3")
parser.add_argument("--max_audio_length_ms", type=int, default=240_000)
parser.add_argument("--topk", type=int, default=50)
parser.add_argument("--temperature", type=float, default=1.0)
parser.add_argument("--cfg_scale", type=float, default=1.5)
# 修改默认设备为 CPU
parser.add_argument("--mula_device", type=str2device, default="cpu")
parser.add_argument("--codec_device", type=str2device, default="cpu")
# 修改默认数据类型为 float32(CPU 兼容性最好)
parser.add_argument("--mula_dtype", type=str2dtype, default="float32")
parser.add_argument("--codec_dtype", type=str2dtype, default="float32")
parser.add_argument("--lazy_load", type=str2bool, default=False)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
pipe = HeartMuLaGenPipeline.from_pretrained(
args.model_path,
device={
"mula": torch.device(args.mula_device),
"codec": torch.device(args.codec_device),
},
dtype={
"mula": args.mula_dtype,
"codec": args.codec_dtype,
},
version=args.version,
lazy_load=args.lazy_load,
)
with torch.no_grad():
pipe(
{
"lyrics": args.lyrics,
"tags": args.tags,
},
max_audio_length_ms=args.max_audio_length_ms,
save_path=args.save_path,
topk=args.topk,
temperature=args.temperature,
cfg_scale=args.cfg_scale,
)
print(f"Generated music saved to {args.save_path}")
=================
本站为个人博客,博客所发布的一切破解软件、补丁、注册机和注册信息及软件的文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站所有内容均来自网络,版权争议与本站无关,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容,如有需要,请去软件官网下载正版。
访问和下载本站内容,说明您已同意上述条款。
本站为非盈利性站点,不贩卖软件,不会收取任何费用,所有内容不作为商业行为。