Download extras/events/command.py from owensong/Inflect-Mini-v3: direct link, hf CLI and curl.
- Browser
- Download file 6.11 kB
-
https://huggingface.co/owensong/Inflect-Mini-v3/resolve/main/extras/events/command.py
- Command line
-
hf download hf://owensong/Inflect-Mini-v3/extras/events/command.py
-
curl -L -o command.py https://huggingface.co/owensong/Inflect-Mini-v3/resolve/main/extras/events/command.py
6.11 kB
| """Eight vocal-event tags across six voices. CPU, native 24 kHz.""" | |
| import sys | |
| sys.dont_write_bytecode = True | |
| import argparse | |
| import hashlib | |
| import json | |
| from pathlib import Path, PurePosixPath | |
| import types | |
| CORE_SHA = '3df1dd36fe67562448c2e9560ae8d8610cd5a6369fd21f966bc002fab1308f95' | |
| COMPONENT_SHA = '55ecf645c7aa2b28d2112a0d25e000f48a723e19638c8f5ba160fdc5e76f4045' | |
| BANK_MANIFEST_SHA = '161cbf0d348323eef7018b1fd8f7264dc968726abe01da99eb6edb8ad555f9f0' | |
| VOICES = ('cassidy', 'hope', 'sophia', 'james', 'jasper', 'mark') | |
| TAGS = ('sigh', 'gasp', 'cough', 'laugh', 'throat_clear', 'inhale', 'exhale', 'yawn') | |
| def sha(path): | |
| with Path(path).open('rb') as stream: | |
| return hashlib.file_digest(stream, 'sha256').hexdigest() | |
| def verify_tree(root, manifest_name, expected): | |
| raw_root = Path(root) | |
| if raw_root.is_symlink(): | |
| raise ValueError('Linked release root') | |
| root = raw_root.resolve(strict=True) | |
| manifest = root / manifest_name | |
| if manifest.is_symlink() or sha(manifest) != expected: | |
| raise ValueError('Release manifest pin mismatch') | |
| data = json.loads(manifest.read_text(encoding='utf-8')) | |
| for name, digest in data['files'].items(): | |
| rel = PurePosixPath(name) | |
| if rel.is_absolute() or '..' in rel.parts or '\\' in name or ':' in name: | |
| raise ValueError('Unsafe release path') | |
| path = root.joinpath(*rel.parts) | |
| if (not path.is_file() or not path.resolve().is_relative_to(root) | |
| or any(root.joinpath(*rel.parts[:i]).is_symlink() for i in range(1, len(rel.parts) + 1)) | |
| or sha(path) != digest): | |
| raise ValueError('Changed release asset: ' + name) | |
| return root | |
| def module(path, name): | |
| result = types.ModuleType(name) | |
| result.__file__ = str(path) | |
| exec(compile(path.read_bytes(), str(path), 'exec'), result.__dict__) | |
| return result | |
| def load(package_root, *, events_root=None, diagnostic=False, profile='base', frontend='core', laugh_edge_room_ms=0): | |
| package = Path(package_root).resolve(strict=True) | |
| here = Path(events_root).resolve(strict=True) if events_root else package / 'extras/events' | |
| core = verify_tree(package / 'core', 'assembly-manifest.json', CORE_SHA) | |
| component = verify_tree(package / 'extras/events/component', 'manifest.json', COMPONENT_SHA) | |
| bank = verify_tree(here / 'multivoice', 'manifest.json', BANK_MANIFEST_SHA) | |
| bridge = module(component / 'runtime.py', '_integrated_native_bridge') | |
| sdk = module(component / 'sdk.py', '_integrated_shared_sdk') | |
| speech = bridge.GeneralCoreSpeech(core, CORE_SHA, profile=profile, frontend=frontend) | |
| base = sdk.ApprovedEvents(speech, component, COMPONENT_SHA, rng_lock=speech.rng_lock) | |
| source = module(bank / 'runtime.py', '_integrated_bank_runtime') | |
| shim = module(bank / 'adapter.py', '_integrated_bank_adapter') | |
| adapter = shim.make_adapter(source, base, bank, sha(bank / 'bank.json'), diagnostic=diagnostic, | |
| laugh_edge_room_ms=laugh_edge_room_ms) | |
| return adapter | |
| def main(): | |
| here = Path(__file__).resolve().parent | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--core-root', type=Path, default=here.parents[1] / 'core') | |
| parser.add_argument('--component', type=Path, default=here / 'component') | |
| parser.add_argument('--verify', action='store_true') | |
| parser.add_argument('--text') | |
| parser.add_argument('--output', type=Path) | |
| parser.add_argument('--voice', choices=VOICES, default='cassidy') | |
| parser.add_argument('--profile', choices=('base', 'pitch'), default='base') | |
| parser.add_argument('--frontend', choices=('core', 'general'), default='core') | |
| parser.add_argument('--events', action='store_true') | |
| parser.add_argument('--seed', type=int, default=42) | |
| parser.add_argument('--laugh-edge-room-ms', type=int, choices=(0, 20), default=0) | |
| parser.add_argument('--subtype', choices=('PCM_16', 'FLOAT'), default='PCM_16') | |
| parser.add_argument('--allow-unreviewed-private', action='store_true') | |
| args = parser.parse_args() | |
| package = args.core_root.resolve(strict=True).parent | |
| if args.component.resolve(strict=True) != package / 'extras/events/component': | |
| parser.error('Use the shared component belonging to this package') | |
| verify_tree(args.core_root, 'assembly-manifest.json', CORE_SHA) | |
| verify_tree(args.component, 'manifest.json', COMPONENT_SHA) | |
| bank = verify_tree(here / 'multivoice', 'manifest.json', BANK_MANIFEST_SHA) | |
| if args.verify: | |
| print(json.dumps(dict(verified=True, voices=VOICES, tags=TAGS, owner_release_authorized=True, | |
| bank_manifest_sha256=BANK_MANIFEST_SHA, | |
| historical_level_review=json.loads((bank / 'bank.json').read_text())['level_calibration']['human_approved']))) | |
| return | |
| if not args.text or args.output is None or not 0 <= args.seed < 2**32 - 100: | |
| parser.error('Text, new output, and supported seed required') | |
| output = args.output.resolve() | |
| if args.output.is_symlink() or output.exists() or not output.parent.is_dir() or output.is_relative_to(package) or output.is_relative_to(here): | |
| parser.error('New output outside the entire package and staged event directory required') | |
| adapter = load(package, events_root=here, diagnostic=args.allow_unreviewed_private, | |
| profile=args.profile, frontend=args.frontend, laugh_edge_room_ms=args.laugh_edge_room_ms) | |
| result = adapter.synthesize(args.text, voice=args.voice, seed=args.seed, events=args.events) | |
| import soundfile as sf | |
| with output.open('xb') as stream: | |
| sf.write(stream, result['audio'].detach().cpu().numpy(), 24000, format='WAV', subtype=args.subtype) | |
| print(json.dumps(dict(audio_sha256=sha(output), sample_rate=24000, metadata=result['metadata'], | |
| diagnostic=args.allow_unreviewed_private, owner_release_authorized=True))) | |
| if __name__ == '__main__': | |
| main() | |