Upload folder using huggingface_hub
Browse files- config.json +3 -3
- model.py +54 -70
config.json
CHANGED
|
@@ -6,11 +6,11 @@
|
|
| 6 |
"AutoConfig": "model.INF5Config",
|
| 7 |
"AutoModel": "model.INF5Model"
|
| 8 |
},
|
| 9 |
-
"ckpt_path": "
|
| 10 |
"model_type": "inf5",
|
| 11 |
"remove_sil": true,
|
| 12 |
"speed": 1.0,
|
| 13 |
"torch_dtype": "float32",
|
| 14 |
"transformers_version": "4.49.0",
|
| 15 |
-
"vocab_path": "
|
| 16 |
-
}
|
|
|
|
| 6 |
"AutoConfig": "model.INF5Config",
|
| 7 |
"AutoModel": "model.INF5Model"
|
| 8 |
},
|
| 9 |
+
"ckpt_path": "model_last.pt",
|
| 10 |
"model_type": "inf5",
|
| 11 |
"remove_sil": true,
|
| 12 |
"speed": 1.0,
|
| 13 |
"torch_dtype": "float32",
|
| 14 |
"transformers_version": "4.49.0",
|
| 15 |
+
"vocab_path": "vocab.txt"
|
| 16 |
+
}
|
model.py
CHANGED
|
@@ -19,51 +19,25 @@ import io
|
|
| 19 |
from pydub import AudioSegment, silence
|
| 20 |
from huggingface_hub import hf_hub_download
|
| 21 |
from safetensors.torch import load_file
|
| 22 |
-
import os
|
| 23 |
|
| 24 |
class INF5Config(PretrainedConfig):
|
| 25 |
model_type = "inf5"
|
| 26 |
|
| 27 |
-
def __init__(self,
|
|
|
|
| 28 |
speed: float = 1.0, remove_sil: bool = True, **kwargs):
|
| 29 |
super().__init__(**kwargs)
|
| 30 |
-
|
| 31 |
-
self.
|
|
|
|
|
|
|
|
|
|
| 32 |
self.speed = speed
|
| 33 |
self.remove_sil = remove_sil
|
| 34 |
|
| 35 |
class INF5Model(PreTrainedModel):
|
| 36 |
config_class = INF5Config
|
| 37 |
|
| 38 |
-
# def __init__(self, config):
|
| 39 |
-
# super().__init__(config)
|
| 40 |
-
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 41 |
-
|
| 42 |
-
# # Load vocoder
|
| 43 |
-
# self.vocoder = torch.compile(load_vocoder(vocoder_name="vocos", is_local=False, device=device))
|
| 44 |
-
|
| 45 |
-
# # Download and load model weights
|
| 46 |
-
# # safetensors_path = hf_hub_download(config.name_or_path, filename="model.safetensors")
|
| 47 |
-
# # print(f"Loading model weights from {safetensors_path} (safetensors)...")
|
| 48 |
-
# # state_dict = load_file(safetensors_path, device=str(device))
|
| 49 |
-
|
| 50 |
-
# # Download vocab.txt from HF Hub
|
| 51 |
-
# vocab_path = hf_hub_download(config.name_or_path, filename="checkpoints/vocab.txt")
|
| 52 |
-
|
| 53 |
-
# self.ema_model = torch.compile(load_model(
|
| 54 |
-
# DiT,
|
| 55 |
-
# dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4),
|
| 56 |
-
# mel_spec_type="vocos",
|
| 57 |
-
# vocab_file=vocab_path,
|
| 58 |
-
# device=device
|
| 59 |
-
# )
|
| 60 |
-
# )
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# # # Load state dict into model
|
| 64 |
-
# # self.ema_model.load_state_dict(state_dict, strict=False)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
def __init__(self, config):
|
| 68 |
super().__init__(config)
|
| 69 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -73,10 +47,18 @@ class INF5Model(PreTrainedModel):
|
|
| 73 |
load_vocoder(vocoder_name="vocos", is_local=False, device=device)
|
| 74 |
)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Download vocab.txt from HF Hub
|
| 77 |
-
vocab_path = hf_hub_download(
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
ckpt_candidates = [
|
|
|
|
| 80 |
"checkpoints/model.safetensors",
|
| 81 |
"model.safetensors",
|
| 82 |
"checkpoints/pytorch_model.bin",
|
|
@@ -87,12 +69,15 @@ class INF5Model(PreTrainedModel):
|
|
| 87 |
"checkpoint.pt"
|
| 88 |
]
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
ckpt_path = None
|
| 91 |
-
from huggingface_hub import hf_hub_download, RepositoryNotFoundError, hf_hub_download
|
| 92 |
|
| 93 |
for fname in ckpt_candidates:
|
| 94 |
try:
|
| 95 |
-
ckpt_path = hf_hub_download(repo_id=
|
| 96 |
print(f"Found checkpoint on hub: {fname} -> {ckpt_path}")
|
| 97 |
break
|
| 98 |
except Exception as e:
|
|
@@ -106,7 +91,7 @@ class INF5Model(PreTrainedModel):
|
|
| 106 |
"Could not find a checkpoint file on the Hub. "
|
| 107 |
"Tried: " + ", ".join(ckpt_candidates) + ".\n"
|
| 108 |
"If your checkpoint is stored under a different path or name, "
|
| 109 |
-
"update ckpt_candidates or pass the path via config (e.g. config.
|
| 110 |
"If the file is >5GB, ensure Git LFS is enabled for the repo (hf lfs-enable-largefiles)."
|
| 111 |
)
|
| 112 |
|
|
@@ -135,7 +120,6 @@ class INF5Model(PreTrainedModel):
|
|
| 135 |
text (str): The text to be synthesized.
|
| 136 |
ref_audio_path (str): Path to the reference audio file.
|
| 137 |
ref_text (str): The reference text.
|
| 138 |
-
|
| 139 |
Returns:
|
| 140 |
np.array: Generated waveform.
|
| 141 |
"""
|
|
@@ -189,41 +173,41 @@ class INF5Model(PreTrainedModel):
|
|
| 189 |
|
| 190 |
|
| 191 |
if __name__ == '__main__':
|
| 192 |
-
model = INF5Model(INF5Config(
|
| 193 |
model.save_pretrained("INF5")
|
| 194 |
model.config.save_pretrained("INF5")
|
| 195 |
-
|
| 196 |
-
import numpy as np
|
| 197 |
-
import soundfile as sf
|
| 198 |
-
from transformers import AutoConfig, AutoModel
|
| 199 |
|
| 200 |
-
AutoConfig.register("inf5", INF5Config)
|
| 201 |
-
AutoModel.register(INF5Config, INF5Model)
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
ref_text="भਹੰਪੀ ਵਿੱਚ ਸਮਾਰਕਾਂ ਦੇ ਭਵਨ ਨਿਰਮਾਣ ਕਲਾ ਦੇ ਵੇਰਵੇ ਗੁੰਝਲਦਾਰ ਅਤੇ ਹੈਰਾਨ ਕਰਨ ਵਾਲੇ ਹਨ, ਜੋ ਮੈਨੂੰ ਖੁਸ਼ ਕਰਦੇ ਹਨ।")
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
sf.write("samples/namaste.wav", np.array(audio, dtype=np.float32), samplerate=24000)
|
| 211 |
-
|
| 212 |
-
from huggingface_hub import HfApi
|
| 213 |
-
|
| 214 |
-
repo_id = "svp19/INF5" # Change to your HF repo
|
| 215 |
-
|
| 216 |
-
# Upload model directory to HF
|
| 217 |
-
api = HfApi()
|
| 218 |
-
api.upload_folder(
|
| 219 |
-
folder_path="INF5",
|
| 220 |
-
repo_id=repo_id,
|
| 221 |
-
repo_type="model"
|
| 222 |
-
)
|
| 223 |
-
print(f"Model pushed to https://huggingface.co/{repo_id} 🚀")
|
| 224 |
-
|
| 225 |
-
print("Verify Upload")
|
| 226 |
-
from transformers import AutoModel
|
| 227 |
-
model = AutoModel.from_pretrained(repo_id)
|
| 228 |
-
print("Success")
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
from pydub import AudioSegment, silence
|
| 20 |
from huggingface_hub import hf_hub_download
|
| 21 |
from safetensors.torch import load_file
|
|
|
|
| 22 |
|
| 23 |
class INF5Config(PretrainedConfig):
|
| 24 |
model_type = "inf5"
|
| 25 |
|
| 26 |
+
def __init__(self, ckpt_repo_id: str = None, vocab_repo_id: str = None,
|
| 27 |
+
ckpt_filename: str = None, vocab_filename: str = "vocab.txt",
|
| 28 |
speed: float = 1.0, remove_sil: bool = True, **kwargs):
|
| 29 |
super().__init__(**kwargs)
|
| 30 |
+
# If not specified, use the model's own repo for both
|
| 31 |
+
self.ckpt_repo_id = ckpt_repo_id
|
| 32 |
+
self.vocab_repo_id = vocab_repo_id
|
| 33 |
+
self.ckpt_filename = ckpt_filename
|
| 34 |
+
self.vocab_filename = vocab_filename
|
| 35 |
self.speed = speed
|
| 36 |
self.remove_sil = remove_sil
|
| 37 |
|
| 38 |
class INF5Model(PreTrainedModel):
|
| 39 |
config_class = INF5Config
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def __init__(self, config):
|
| 42 |
super().__init__(config)
|
| 43 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 47 |
load_vocoder(vocoder_name="vocos", is_local=False, device=device)
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# Determine which repo to use for vocab
|
| 51 |
+
# Default to the model's own repo if not specified
|
| 52 |
+
vocab_repo = config.vocab_repo_id or config.name_or_path
|
| 53 |
+
|
| 54 |
# Download vocab.txt from HF Hub
|
| 55 |
+
vocab_path = hf_hub_download(repo_id=vocab_repo, filename=config.vocab_filename)
|
| 56 |
+
|
| 57 |
+
# Determine which repo to use for checkpoint
|
| 58 |
+
ckpt_repo = config.ckpt_repo_id or config.name_or_path
|
| 59 |
|
| 60 |
ckpt_candidates = [
|
| 61 |
+
"model_last.pt", # Try this first since it's in your repo
|
| 62 |
"checkpoints/model.safetensors",
|
| 63 |
"model.safetensors",
|
| 64 |
"checkpoints/pytorch_model.bin",
|
|
|
|
| 69 |
"checkpoint.pt"
|
| 70 |
]
|
| 71 |
|
| 72 |
+
# If a specific checkpoint filename is provided, use only that
|
| 73 |
+
if config.ckpt_filename:
|
| 74 |
+
ckpt_candidates = [config.ckpt_filename]
|
| 75 |
+
|
| 76 |
ckpt_path = None
|
|
|
|
| 77 |
|
| 78 |
for fname in ckpt_candidates:
|
| 79 |
try:
|
| 80 |
+
ckpt_path = hf_hub_download(repo_id=ckpt_repo, filename=fname)
|
| 81 |
print(f"Found checkpoint on hub: {fname} -> {ckpt_path}")
|
| 82 |
break
|
| 83 |
except Exception as e:
|
|
|
|
| 91 |
"Could not find a checkpoint file on the Hub. "
|
| 92 |
"Tried: " + ", ".join(ckpt_candidates) + ".\n"
|
| 93 |
"If your checkpoint is stored under a different path or name, "
|
| 94 |
+
"update ckpt_candidates or pass the path via config (e.g. config.ckpt_filename). "
|
| 95 |
"If the file is >5GB, ensure Git LFS is enabled for the repo (hf lfs-enable-largefiles)."
|
| 96 |
)
|
| 97 |
|
|
|
|
| 120 |
text (str): The text to be synthesized.
|
| 121 |
ref_audio_path (str): Path to the reference audio file.
|
| 122 |
ref_text (str): The reference text.
|
|
|
|
| 123 |
Returns:
|
| 124 |
np.array: Generated waveform.
|
| 125 |
"""
|
|
|
|
| 173 |
|
| 174 |
|
| 175 |
if __name__ == '__main__':
|
| 176 |
+
model = INF5Model(INF5Config())
|
| 177 |
model.save_pretrained("INF5")
|
| 178 |
model.config.save_pretrained("INF5")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
# import numpy as np
|
| 182 |
+
# import soundfile as sf
|
| 183 |
+
# from transformers import AutoConfig, AutoModel
|
|
|
|
| 184 |
|
| 185 |
+
# AutoConfig.register("inf5", INF5Config)
|
| 186 |
+
# AutoModel.register(INF5Config, INF5Model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
+
# model = AutoModel.from_pretrained("INF5")
|
| 189 |
+
# audio = model("नमस्ते! संगीत की तरह जीवन भी खूबसूरत होता है, बस इसे सही ताल में जीना आना चाहिए.",
|
| 190 |
+
# ref_audio_path="prompts/PAN_F_HAPPY_00001.wav",
|
| 191 |
+
# ref_text="भਹੰਪੀ ਵਿੱਚ ਸਮਾਰਕਾਂ ਦੇ ਭਵਨ ਨਿਰਮਾਣ ਕਲਾ ਦੇ ਵੇਰਵੇ ਗੁੰਝਲਦਾਰ ਅਤੇ ਹੈਰਾਨ ਕਰਨ ਵਾਲੇ ਹਨ, ਜੋ ਮੈਨੂੰ ਖੁਸ਼ ਕਰਦੇ ਹਨ।")
|
| 192 |
+
|
| 193 |
+
# if audio.dtype == np.int16:
|
| 194 |
+
# audio = audio.astype(np.float32) / 32768.0
|
| 195 |
+
# sf.write("samples/namaste.wav", np.array(audio, dtype=np.float32), samplerate=24000)
|
| 196 |
+
|
| 197 |
+
# from huggingface_hub import HfApi
|
| 198 |
+
|
| 199 |
+
# repo_id = "svp19/INF5" # Change to your HF repo
|
| 200 |
+
|
| 201 |
+
# # Upload model directory to HF
|
| 202 |
+
# api = HfApi()
|
| 203 |
+
# api.upload_folder(
|
| 204 |
+
# folder_path="INF5",
|
| 205 |
+
# repo_id=repo_id,
|
| 206 |
+
# repo_type="model"
|
| 207 |
+
# )
|
| 208 |
+
# print(f"Model pushed to https://huggingface.co/{repo_id}")
|
| 209 |
+
|
| 210 |
+
# print("Verify Upload")
|
| 211 |
+
# from transformers import AutoModel
|
| 212 |
+
# model = AutoModel.from_pretrained(repo_id)
|
| 213 |
+
# print("Success")
|