Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,7 +52,8 @@ api = HfApi()
|
|
| 52 |
|
| 53 |
# Get all files in the repo
|
| 54 |
all_files = api.list_repo_files(REPO_ID)
|
| 55 |
-
|
|
|
|
| 56 |
# Group files by model folder
|
| 57 |
models = defaultdict(list)
|
| 58 |
for f in all_files:
|
|
@@ -76,27 +77,56 @@ available_models = list(models.keys())
|
|
| 76 |
print("Available models:", available_models)
|
| 77 |
|
| 78 |
# Load homer model
|
| 79 |
-
homer_model = load_model("homer")
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
def model_data(model_name):
|
| 85 |
-
# global n_spk, tgt_sr, net_g, vc, cpt, version, index_file
|
| 86 |
-
pth_files = [
|
| 87 |
-
os.path.join(model_root, model_name, f)
|
| 88 |
-
for f in os.listdir(os.path.join(model_root, model_name))
|
| 89 |
-
if f.endswith(".pth")
|
| 90 |
-
]
|
| 91 |
if len(pth_files) == 0:
|
| 92 |
-
raise ValueError(f"No pth file found
|
| 93 |
pth_path = pth_files[0]
|
| 94 |
print(f"Loading {pth_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
cpt = torch.load(pth_path, map_location="cpu")
|
| 96 |
tgt_sr = cpt["config"][-1]
|
| 97 |
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
| 98 |
if_f0 = cpt.get("f0", 1)
|
| 99 |
version = cpt.get("version", "v1")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
if version == "v1":
|
| 101 |
if if_f0 == 1:
|
| 102 |
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)
|
|
@@ -109,22 +139,21 @@ def model_data(model_name):
|
|
| 109 |
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
|
| 110 |
else:
|
| 111 |
raise ValueError("Unknown version")
|
|
|
|
| 112 |
del net_g.enc_q
|
| 113 |
net_g.load_state_dict(cpt["weight"], strict=False)
|
| 114 |
-
print("Model loaded")
|
| 115 |
net_g.eval().to(config.device)
|
|
|
|
| 116 |
if config.is_half:
|
| 117 |
net_g = net_g.half()
|
| 118 |
else:
|
| 119 |
net_g = net_g.float()
|
|
|
|
| 120 |
vc = VC(tgt_sr, config)
|
| 121 |
-
# n_spk = cpt["config"][-3]
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if f.endswith(".index")
|
| 127 |
-
]
|
| 128 |
if len(index_files) == 0:
|
| 129 |
print("No index file found")
|
| 130 |
index_file = ""
|
|
|
|
| 52 |
|
| 53 |
# Get all files in the repo
|
| 54 |
all_files = api.list_repo_files(REPO_ID)
|
| 55 |
+
print('ALL FILES')
|
| 56 |
+
print(all_files)
|
| 57 |
# Group files by model folder
|
| 58 |
models = defaultdict(list)
|
| 59 |
for f in all_files:
|
|
|
|
| 77 |
print("Available models:", available_models)
|
| 78 |
|
| 79 |
# Load homer model
|
| 80 |
+
#homer_model = load_model("homer")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def model_data(model_name, use_hf=False, repo_id="simpsonsaiorg/stream-models"):
|
| 86 |
+
"""
|
| 87 |
+
Load model data either from local weights folder or from HuggingFace Hub.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
model_name (str): name of the model (e.g. "homer", "bart")
|
| 91 |
+
use_hf (bool): whether to load from HuggingFace instead of local
|
| 92 |
+
repo_id (str): HuggingFace repo id
|
| 93 |
+
|
| 94 |
+
Returns:
|
| 95 |
+
tgt_sr, net_g, vc, version, index_file, if_f0
|
| 96 |
+
"""
|
| 97 |
+
# -----------------------
|
| 98 |
+
# 1. Locate .pth file
|
| 99 |
+
# -----------------------
|
| 100 |
+
if not use_hf:
|
| 101 |
+
# Local
|
| 102 |
+
model_path = os.path.join(model_root, model_name)
|
| 103 |
+
pth_files = [os.path.join(model_path, f) for f in os.listdir(model_path) if f.endswith(".pth")]
|
| 104 |
+
index_files = [os.path.join(model_path, f) for f in os.listdir(model_path) if f.endswith(".index")]
|
| 105 |
+
else:
|
| 106 |
+
# HuggingFace
|
| 107 |
+
pth_files = [hf_hub_download(repo_id=repo_id, filename=f"{model_name}/model.pth")]
|
| 108 |
+
try:
|
| 109 |
+
index_files = [hf_hub_download(repo_id=repo_id, filename=f"{model_name}/model.index")]
|
| 110 |
+
except Exception:
|
| 111 |
+
index_files = []
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
if len(pth_files) == 0:
|
| 114 |
+
raise ValueError(f"No .pth file found for model {model_name}")
|
| 115 |
pth_path = pth_files[0]
|
| 116 |
print(f"Loading {pth_path}")
|
| 117 |
+
|
| 118 |
+
# -----------------------
|
| 119 |
+
# 2. Load checkpoint
|
| 120 |
+
# -----------------------
|
| 121 |
cpt = torch.load(pth_path, map_location="cpu")
|
| 122 |
tgt_sr = cpt["config"][-1]
|
| 123 |
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
| 124 |
if_f0 = cpt.get("f0", 1)
|
| 125 |
version = cpt.get("version", "v1")
|
| 126 |
+
|
| 127 |
+
# -----------------------
|
| 128 |
+
# 3. Init network
|
| 129 |
+
# -----------------------
|
| 130 |
if version == "v1":
|
| 131 |
if if_f0 == 1:
|
| 132 |
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)
|
|
|
|
| 139 |
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
|
| 140 |
else:
|
| 141 |
raise ValueError("Unknown version")
|
| 142 |
+
|
| 143 |
del net_g.enc_q
|
| 144 |
net_g.load_state_dict(cpt["weight"], strict=False)
|
|
|
|
| 145 |
net_g.eval().to(config.device)
|
| 146 |
+
|
| 147 |
if config.is_half:
|
| 148 |
net_g = net_g.half()
|
| 149 |
else:
|
| 150 |
net_g = net_g.float()
|
| 151 |
+
|
| 152 |
vc = VC(tgt_sr, config)
|
|
|
|
| 153 |
|
| 154 |
+
# -----------------------
|
| 155 |
+
# 4. Index file
|
| 156 |
+
# -----------------------
|
|
|
|
|
|
|
| 157 |
if len(index_files) == 0:
|
| 158 |
print("No index file found")
|
| 159 |
index_file = ""
|