forked from teticio/Deej-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt_to_hf.py
68 lines (59 loc) · 1.88 KB
/
pt_to_hf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import argparse
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
import torch
from audiodiffusion.audio_encoder import AudioEncoder
from huggingface_hub import Repository
if __name__ == "__main__":
"""
Entry point for the pt_to_hf script.
Converts a PyTorch MP3ToVec model to a Hugging Face MP3ToVec model.
Args:
--mp3tovec_model_file (str): Path to the MP3ToVec model file. Default is "models/mp3tovec.ckpt".
--hub_model_id (str): Hugging Face model ID. Default is "teticio/audio-encoder".
--output_dir (str): Hugging Face model path. Default is "models/audio-encoder".
--push_to_hub (bool): Push to Hugging Face hub. Default is False.
Returns:
None
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--mp3tovec_model_file",
type=str,
default="models/mp3tovec.ckpt",
help="MP3ToVec model file",
)
parser.add_argument(
"--hub_model_id",
type=str,
default="teticio/audio-encoder",
help="Hugging Face model ID",
)
parser.add_argument(
"--output_dir",
type=str,
default="models/audio-encoder",
help="Hugging Face model path",
)
parser.add_argument(
"--push_to_hub",
type=bool,
default=False,
help="Push to Hugging Face hub",
)
args = parser.parse_args()
audio_encoder = AudioEncoder()
audio_encoder.eval()
audio_encoder.load_state_dict(
{
k.replace("model.", ""): v
for k, v in torch.load(
args.mp3tovec_model_file, map_location=torch.device("cpu")
)["state_dict"].items()
},
)
if args.push_to_hub:
repo = Repository(args.output_dir, clone_from=args.hub_model_id)
audio_encoder.save_pretrained(args.output_dir)
if args.push_to_hub:
repo.push_to_hub() # type: ignore