Download extras/events/component/runtime.py from owensong/Inflect-Mini-v3: direct link, hf CLI and curl.
- Browser
- Download file 7.31 kB
-
https://huggingface.co/owensong/Inflect-Mini-v3/resolve/main/extras/events/component/runtime.py
- Command line
-
hf download hf://owensong/Inflect-Mini-v3/extras/events/component/runtime.py
-
curl -L -o runtime.py https://huggingface.co/owensong/Inflect-Mini-v3/resolve/main/extras/events/component/runtime.py
7.31 kB
| """CLI bridge to a manifest-pinned General MiniCore export; no dev paths.""" | |
| import sys | |
| sys.dont_write_bytecode=True | |
| from contextlib import contextmanager | |
| import argparse | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import types | |
| def require(ok,message): | |
| if not ok:raise ValueError(message) | |
| def load_source(path,name): | |
| module=types.ModuleType(name);module.__file__=str(path) | |
| exec(compile(path.read_bytes(),str(path),'exec'),module.__dict__) | |
| return module | |
| def verify_core(root,expected): | |
| root=Path(root).resolve(strict=True) | |
| require(hashlib.sha256((root/'assembly-manifest.json').read_bytes()).hexdigest()==expected,'MiniCore manifest mismatch') | |
| value=json.loads((root/'assembly-manifest.json').read_text()) | |
| require('general_synthesize.py' in value['files'],'General MiniCore export required') | |
| for name,h in value['files'].items(): | |
| path=root/name | |
| require(not Path(name).is_absolute() and '..' not in Path(name).parts and not path.is_symlink() and | |
| path.resolve().is_relative_to(root) and path.is_file(),'Unsafe Core asset') | |
| with path.open('rb') as f:require(hashlib.file_digest(f,'sha256').hexdigest()==h,'MiniCore asset changed: '+name) | |
| return root | |
| class GeneralCoreSpeech: | |
| """Default Core route unchanged; General frontend requires explicit opt-in.""" | |
| def __init__(self,root,manifest_sha256,*,profile='base',frontend='core'): | |
| root=verify_core(root,manifest_sha256) | |
| guard=load_source(Path(__file__).with_name('core_import_guard.py'),'_core_origins') | |
| guard.reject_foreign_modules(root) | |
| require(frontend in ('core','general'),'Unknown frontend policy');self.frontend_policy=frontend | |
| if frontend=='general': | |
| self.general=load_source(root/'general_synthesize.py','_approved_event_general') | |
| self.cli=self.general.verify(manifest_sha256) | |
| self.front=self.general.frontend()[-1] | |
| else:self.cli=load_source(root/'synthesize.py','_approved_event_core_cli') | |
| require(sys.modules.get('torch') is None or not sys.modules['torch'].cuda.is_initialized(),'Frontend must leave CUDA uninitialized') | |
| self.numeric=load_source(root/'baseline/synthesize.py','_approved_event_numeric') | |
| self.numbers=self.numeric.load_numeric(root/'baseline') | |
| require(profile in ('base','pitch'),'Qualified base/current-Q1500 profiles only') | |
| _,runtime,enhancer=self.cli.load_selected('mini','cpu',profile=profile,runtime='canonical',root=root) | |
| require(enhancer is None,'Native 24k integration only') | |
| self.runtime=runtime;self.model=runtime[2];self.torch=runtime[3] | |
| self.weak=load_source(root/'general/repair-code/weak_vowels_20261108.py','_approved_event_article') | |
| guard.reject_foreign_modules(root) | |
| import native_mini_inference as native | |
| self.rng_lock=native._RNG_LOCK;self.root=root;self.profile=profile | |
| self.torch.set_num_threads(2) | |
| def flags(self): | |
| t=self.torch | |
| previous=(t.backends.cudnn.enabled,t.backends.cudnn.allow_tf32,t.backends.cuda.matmul.allow_tf32) | |
| try: | |
| t.backends.cudnn.enabled=False;t.backends.cudnn.allow_tf32=False;t.backends.cuda.matmul.allow_tf32=False | |
| yield | |
| finally: | |
| t.backends.cudnn.enabled,t.backends.cudnn.allow_tf32,t.backends.cuda.matmul.allow_tf32=previous | |
| def __call__(self,text,*,voice='cassidy',seed=42,**kwargs): | |
| require(not kwargs and voice in ('hope','sophia','cassidy','james','jasper','mark') and type(seed) is int and | |
| 0<=seed<2**32-100,'Core request contract') | |
| prepared=self.numeric.prepare_text(text,self.numbers,'conservative');pieces=[];metadata=[];traces=[] | |
| if self.frontend_policy=='core': | |
| import io,soundfile as sf | |
| raw,metadata=self.cli.render(prepared,voice,self.runtime,None,seed,'FLOAT') | |
| wave,rate=sf.read(io.BytesIO(raw),dtype='float32') | |
| require(rate==24000 and wave.ndim==1,'Unexpected Core source clock') | |
| return dict(audio=self.torch.from_numpy(wave.copy()),sample_rate=rate,metadata=metadata) | |
| with self.model.base._inference_lock,self.rng_lock,self.flags(),self.torch.inference_mode(): | |
| for index,chunk in enumerate(prepared['chunks']): | |
| phones,tokens=self.front(chunk);self.model.base.phoneme_ids(phones) | |
| rows=self.weak.article_rows(phones,tokens);trace=[] | |
| with self.weak.protect_article(self.model,rows,trace): | |
| result=self.model.synthesize_phonemes(phones,voice,seed=seed+index) | |
| require(result['sample_rate']==24000,'Native Core clock') | |
| if pieces:pieces.append(self.torch.zeros(2880,dtype=self.torch.float32)) | |
| pieces.append(result['audio'].detach().cpu().float());metadata.append(result['metadata']);traces.append(trace) | |
| wave=self.torch.cat(pieces) | |
| require(wave.ndim==1 and wave.numel()>0 and bool(self.torch.isfinite(wave).all()) and float(wave.abs().max())<1,'Unsafe raw Core output') | |
| return dict(audio=wave,sample_rate=24000,metadata=dict(profile=self.profile,policy=metadata,article_trace=traces, | |
| source_audio_at_inference=False,frontend='general-only-no-verified-lexicon')) | |
| def main(): | |
| parser=argparse.ArgumentParser(description=__doc__) | |
| for name in ('core-root','core-manifest-sha256','component','component-sha256','text','output'): | |
| parser.add_argument('--'+name,required=True) | |
| parser.add_argument('--voice',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('--laugh-edge-room-ms',type=int,choices=(0,20),default=0) | |
| parser.add_argument('--subtype',choices=('PCM_16','FLOAT'),default='PCM_16');args=parser.parse_args() | |
| root=Path(args.component).resolve(strict=True);sdk=load_source(root/'sdk.py','_approved_events_sdk') | |
| sdk.verify(root,args.component_sha256) | |
| output=Path(args.output) | |
| require(not output.exists() and not output.is_symlink() and output.parent.is_dir() and | |
| not output.resolve().is_relative_to(root) and not output.resolve().is_relative_to(Path(args.core_root).resolve()),'New WAV outside Core/component required') | |
| core=GeneralCoreSpeech(args.core_root,args.core_manifest_sha256,profile=args.profile,frontend=args.frontend) | |
| adapter=sdk.ApprovedEvents(core,root,args.component_sha256,rng_lock=core.rng_lock) | |
| result=adapter.synthesize(args.text,voice=args.voice,seed=42,events=args.events,laugh_edge_room_ms=args.laugh_edge_room_ms) | |
| wave=result['audio'];t=core.torch | |
| require(wave.dtype==t.float32 and wave.ndim==1 and bool(t.isfinite(wave).all()) and float(wave.abs().max())<1,'Unsafe raw output BEFORE SoundFile') | |
| import soundfile as sf | |
| with output.open('xb') as f:sf.write(f,wave.numpy(),24000,format='WAV',subtype=args.subtype) | |
| print(json.dumps(dict(audio_sha256=hashlib.sha256(output.read_bytes()).hexdigest(),sample_rate=24000, | |
| metadata=result['metadata'],beta_events=True,trained_core_changed=False))) | |
| if __name__=='__main__':main() | |