Skip to content

Open-source State-of-the-art TTS model which runs on a CPU 😻

Python

Read voices.json and config.json as UTF-8 so loading works on Windows

kevin93273dcf5ea840f7
diff --git a/kittenml/kittentts2/model.py b/kittenml/kittentts2/model.py
index 8d356ee..af2fa70 100644
--- a/kittenml/kittentts2/model.py
+++ b/kittenml/kittentts2/model.py
@@ -263,7 +263,7 @@ class KittenTTS2:
voices_path = self.repo_dir / self.config.get("voices", "voices/voices.json")
if not voices_path.is_file():
return {}
- with open(voices_path) as f:
+ with open(voices_path, encoding="utf-8") as f:
entries = json.load(f)
root = voices_path.parent
return {name: {**spec,
diff --git a/kittenml/repo.py b/kittenml/repo.py
index 704aa01..6e1bbd1 100644
--- a/kittenml/repo.py
+++ b/kittenml/repo.py
@@ -28,10 +28,10 @@ def resolve_repo(model_name, cache_dir=None):
config_path = os.path.join(model_name, "config.json")
if not os.path.isfile(config_path):
raise FileNotFoundError(f"no config.json in {model_name}")
- with open(config_path) as f:
+ with open(config_path, encoding="utf-8") as f:
return model_name, None, json.load(f)
repo_id = model_name if "/" in model_name else f"KittenML/{model_name}"
config_path = hf_hub_download(repo_id=repo_id, filename="config.json", cache_dir=cache_dir)
- with open(config_path) as f:
+ with open(config_path, encoding="utf-8") as f:
return None, repo_id, json.load(f)
diff --git a/tests/test_json_encoding.py b/tests/test_json_encoding.py
new file mode 100644
index 0000000..138218d
--- /dev/null
+++ b/tests/test_json_encoding.py
@@ -0,0 +1,73 @@
+"""The voices and config files are UTF-8 and must be read as UTF-8 on every
+platform. On Windows the default text encoding is the ANSI code page (cp1252),
+so a bare ``open()`` there fails on any non-ASCII byte (issue #158). The tests
+reproduce that by running the loader in a child interpreter whose default
+encoding is ASCII (UTF-8 mode off, C locale, no locale coercion)."""
+
+import json
+import os
+import subprocess
+import sys
+import tempfile
+import textwrap
+import unittest
+from pathlib import Path
+
+REPO_ROOT = Path(__file__).resolve().parents[1]
+NON_ASCII = "Zo\u00eb \u201cwarm\u201d \u2014 caf\u00e9"
+
+
+def run_with_ascii_default_encoding(code):
+ env = dict(os.environ, PYTHONUTF8="0", PYTHONCOERCECLOCALE="0", LC_ALL="C", LANG="C",
+ PYTHONPATH=str(REPO_ROOT))
+ return subprocess.run([sys.executable, "-c", textwrap.dedent(code)], env=env,
+ capture_output=True, text=True, encoding="utf-8")
+
+
+class Utf8JsonLoadingTests(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.root = Path(self.tmp.name)
+
+ def tearDown(self):
+ self.tmp.cleanup()
+
+ def test_child_default_encoding_is_not_utf8(self):
+ # Guard: if the child still reads UTF-8 by default, the tests below
+ # would pass even with a bare open() and prove nothing.
+ result = run_with_ascii_default_encoding(
+ "import locale; print(locale.getpreferredencoding(False))")
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertNotIn("utf", result.stdout.lower().replace("-", ""))
+
+ def test_load_voices_reads_non_ascii_voices_json(self):
+ voices = self.root / "voices"
+ voices.mkdir()
+ entries = {"zoe": {"description": NON_ASCII, "reference": "zoe.wav",
+ "artifacts": "zoe.safetensors"}}
+ (voices / "voices.json").write_text(json.dumps(entries, ensure_ascii=False),
+ encoding="utf-8")
+ result = run_with_ascii_default_encoding(f"""
+ import pathlib, types
+ from kittenml.kittentts2.model import KittenTTS2
+ stub = types.SimpleNamespace(repo_dir=pathlib.Path({str(self.root)!r}), config={{}})
+ voices = KittenTTS2._load_voices(stub)
+ print(ascii(voices["zoe"]["description"]))
+ """)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(result.stdout.strip(), ascii(NON_ASCII))
+
+ def test_resolve_repo_reads_non_ascii_local_config(self):
+ (self.root / "config.json").write_text(json.dumps({"name": NON_ASCII}, ensure_ascii=False),
+ encoding="utf-8")
+ result = run_with_ascii_default_encoding(f"""
+ from kittenml.repo import resolve_repo
+ local_dir, repo_id, config = resolve_repo({str(self.root)!r})
+ print(ascii(config["name"]))
+ """)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(result.stdout.strip(), ascii(NON_ASCII))
+
+
+if __name__ == "__main__":
+ unittest.main()