history_to_hash.py 996 B

1234567891011121314151617181920212223242526272829303132
  1. import hashlib
  2. import json
  3. from typing import Union
  4. from tts_webui.bark.get_bark_voice_hash import get_hash_from_voice_name
  5. from tts_webui.bark.FullGeneration import FullGeneration
  6. from tts_webui.bark.npz_tools import load_npz
  7. def history_to_hash(voice_name: Union[None, str, FullGeneration]):
  8. if voice_name is None:
  9. return get_md5_hex(b"None")
  10. if isinstance(voice_name, str):
  11. candidate = get_hash_from_voice_name(voice_name)
  12. if candidate is not None:
  13. return candidate
  14. else:
  15. npz = load_npz(voice_name)
  16. else:
  17. npz = voice_name
  18. npz_str = json.dumps(
  19. {
  20. "semantic_prompt": npz["semantic_prompt"].tolist(),
  21. "coarse_prompt": npz["coarse_prompt"].tolist(),
  22. "fine_prompt": npz["fine_prompt"].tolist(),
  23. }
  24. )
  25. npz_as_str = npz_str.encode("utf-8")
  26. return get_md5_hex(npz_as_str)
  27. def get_md5_hex(npz_as_str: bytes):
  28. return hashlib.md5(npz_as_str).hexdigest()