Debian Codegraph @

setup/piper.py

raw
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
3
4 import json
5 import os
6 from contextlib import suppress
7
8 from setup.revendor import ReVendor
9
10
11 class PiperVoices(ReVendor):
12 description = 'Download the list of Piper voices'
13 NAME = 'piper_voices'
14 TAR_NAME = 'piper voice list'
15 VERSION = 'main'
16 DOWNLOAD_URL = 'https://download.calibre-ebook.com/piper-voices.json'
17 CAN_USE_SYSTEM_VERSION = False
18
19 @property
20 def output_file_path(self) -> str:
21 return os.path.join(self.RESOURCES, 'piper-voices.json')
22
23 def run(self, opts):
24 url = opts.path_to_piper_voices
25 if url:
26 with open(opts.path_to_piper_voices) as f:
27 src = f.read()
28 else:
29 url = opts.piper_voices_url
30 src = self.download_securely(url).decode('utf-8')
31 data = json.loads(src)
32 lang_map = {}
33 for voice in data.values():
34 language_code = voice['language']['code']
35 lang_entry = lang_map.setdefault(language_code, {})
36 voice_entry = lang_entry.setdefault(voice['name'], {})
37 quality_entry = voice_entry.setdefault(voice['quality'], {})
38 for f, metadata in voice['files'].items():
39 if f.endswith('.json'):
40 key = 'config'
41 elif f.endswith('.onnx'):
42 key = 'model'
43 else:
44 key = 'card'
45 quality_entry[key] = 'https://huggingface.co/rhasspy/piper-voices/resolve/main/' + f
46 if not lang_map:
47 raise SystemExit(f'Failed to read any piper voices from: {url}')
48 if 'en_US' not in lang_map:
49 raise SystemExit(f'Failed to read en_US piper voices from: {url}')
50 with open(self.output_file_path, 'w') as f:
51 json.dump({'version': 1, 'lang_map': lang_map}, f, indent=2, sort_keys=False)
52
53 def clean(self):
54 with suppress(FileNotFoundError):
55 os.remove(self.output_file_path)