cmvr-visual/inference/scripts/download_dialogue_models.py

229 lines
8.3 KiB
Python
Raw Normal View History

"""Install pinned dialogue weights sequentially, resuming incomplete transfers."""
import fcntl
import hashlib
import json
import shutil
import subprocess
import tarfile
from pathlib import Path
def main():
work = Path("/workspace/models/dialogue-v1")
work.mkdir(parents=True, exist_ok=True)
lock = (work / "fetch.lock").open("a")
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
jobs = [
(
"FunAudioLLM/SenseVoiceSmall-GGUF",
"90c1c61912018b70ada0fcc024ea24aca62f2e63",
"sensevoice",
["sensevoice-small-f16.gguf"],
),
(
"Wespeaker/wespeaker-resnet34-LM",
"f0c48c298fd835726c27956a5d617bad7115627e",
"wespeaker",
["voxceleb_resnet34_LM.onnx"],
),
(
"jonathandinu/face-parsing",
"a99b26503a09c8cc0de4aad7057d39ac9224979e",
"face-parser",
["config.json", "preprocessor_config.json", "model.safetensors"],
),
(
"TMElyralab/MuseTalk",
"c9ac4cdcedcae22c186fbbf67261961e246c4760",
"musetalk",
["musetalkV15/musetalk.json", "musetalkV15/unet.pth"],
),
(
"openai/whisper-tiny",
"169d4a4341b33bc18d8881c4b69c2e104e1cc0af",
"whisper",
["config.json", "preprocessor_config.json", "model.safetensors"],
),
(
"stabilityai/sd-vae-ft-mse",
"31f26fdeee1355a5c34592e401dd41e45d25a493",
"sd-vae",
["config.json", "diffusion_pytorch_model.safetensors"],
),
(
"fal/AuraFace-v1",
"af6d057c9b0ec4071d4c49c80e3539258798b609",
"auraface",
["LICENSE.md", "scrfd_10g_bnkps.onnx", "glintr100.onnx"],
),
(
"openbmb/VoxCPM2",
"7936080b41163874f38610f46f438e2c0757f9fd",
"voxcpm2",
[
"config.json",
"special_tokens_map.json",
"tokenizer_config.json",
"tokenization_voxcpm2.py",
"tokenizer.json",
"model.safetensors",
"audiovae.pth",
],
),
]
receipt = {}
for repo, rev, d, files in jobs:
base = "https://hf-mirror.com/"
opts = [
"curl",
"-fsSL",
"--retry",
"4",
"--limit-rate",
"3800k",
"--connect-timeout",
"20",
"--speed-time",
"90",
"--speed-limit",
"1024",
]
meta = json.loads(
subprocess.check_output(
opts
+ [base + "api/models/" + repo + "/revision/" + rev + "?blobs=true"]
)
)
if meta["sha"] != rev:
raise ValueError("Unexpected repository revision")
for name in files:
p = work / d / name
p.parent.mkdir(parents=True, exist_ok=True)
rec = next(x for x in meta["siblings"] if x["rfilename"] == name)
tmp = p.with_suffix(p.suffix + ".part")
if not p.exists():
subprocess.run(
opts
+ [
"-C",
"-",
"-o",
str(tmp),
base + repo + "/resolve/" + rev + "/" + name,
],
check=True,
)
f = p if p.exists() else tmp
if f.stat().st_size != rec["size"]:
raise ValueError("Incomplete checkpoint " + name)
with f.open("rb") as stream:
if rec.get("lfs"):
checksum = hashlib.file_digest(stream, "sha256").hexdigest()
if checksum != rec["lfs"]["sha256"]:
raise ValueError("LFS checksum mismatch " + name)
else:
data = stream.read()
if (
hashlib.sha1(
("blob " + str(len(data)) + "\0").encode() + data
).hexdigest()
!= rec["blobId"]
):
raise ValueError("Git blob checksum mismatch " + name)
checksum = hashlib.sha256(data).hexdigest()
if not p.exists():
tmp.replace(p)
receipt[str(p.relative_to(work))] = {
"revision": rev,
"size_bytes": rec["size"],
"sha256": checksum,
}
print("verified", d, name, rec["size"], flush=True)
for name, sha in {
"SyncNetModel.py": "41b85aed0006546cb2d6dc9180c5bc3dac737ab80eff643022e397a349c30c22",
"syncnet_v2.model": "961e8696f888fce4f3f3a6c3d5b3267cf5b343100b238e79b2659bff2c605442",
}.items():
target = work / "syncnet" / name
target.parent.mkdir(exist_ok=True)
if not target.exists():
if name.endswith(".py"):
import base64
data = json.loads(
subprocess.check_output(
opts
+ [
"https://api.github.com/repos/joonson/syncnet_python/contents/SyncNetModel.py?ref=907c0b579c2e2d83f0eae1b2ac9e720cde4e5623"
]
)
)
target.write_bytes(base64.b64decode(data["content"]))
else:
temporary = target.with_suffix(".part")
subprocess.run(
opts
+ [
"-C",
"-",
"-o",
str(temporary),
"https://www.robots.ox.ac.uk/~vgg/software/lipsync/data/syncnet_v2.model",
],
check=True,
)
temporary.replace(target)
with target.open("rb") as stream:
if hashlib.file_digest(stream, "sha256").hexdigest() != sha:
raise ValueError("Invalid SyncNet file " + name)
receipt[str(target.relative_to(work))] = {
"revision": "907c0b579c2e2d83f0eae1b2ac9e720cde4e5623",
"sha256": sha,
"size_bytes": target.stat().st_size,
}
archive = work / "sensevoice" / "funasr-llamacpp-linux-x64.tar.gz"
runtime_revision = "6991744856587fa44379e8b5dcc432debffeb1be"
release = "https://github.com/QwenAudio/SenseVoice/releases/download/runtime-llamacpp-v0.2.1/"
archive_sha = "1bc83dd36d11b1a2b9a45faaf77a5d1014000ac641c93dfec73d24d71ec51caf"
download = archive if archive.exists() else archive.with_suffix(".part")
if not archive.exists():
subprocess.run(
opts + ["-C", "-", "-o", str(download), release + archive.name], check=True
)
with download.open("rb") as stream:
if hashlib.file_digest(stream, "sha256").hexdigest() != archive_sha:
raise ValueError("SenseVoice runtime archive checksum mismatch")
if not archive.exists():
download.replace(archive)
binary = Path("/workspace/envs/dialogue-v1/bin/llama-funasr-sensevoice")
with tarfile.open(archive) as bundle:
member = next(
m
for m in bundle.getmembers()
if m.isfile() and Path(m.name).name == binary.name
)
temp_binary = binary.with_suffix(".next")
with bundle.extractfile(member) as source, temp_binary.open("wb") as dest:
shutil.copyfileobj(source, dest)
with temp_binary.open("rb") as stream:
if (
hashlib.file_digest(stream, "sha256").hexdigest()
!= "b5b0d437f09b425b785edbb741405854ccb62bb83eadf2d63512c4b7be10b873"
):
raise ValueError("SenseVoice executable checksum mismatch")
temp_binary.chmod(0o755)
temp_binary.replace(binary)
receipt[str(archive.relative_to(work))] = {
"revision": runtime_revision,
"sha256": archive_sha,
"size_bytes": archive.stat().st_size,
}
temp = work / "receipt.json.tmp"
temp.write_text(json.dumps(receipt, indent=2, sort_keys=True))
temp.replace(work / "receipt.json")
if __name__ == "__main__":
main()