code wiki / (root) / nx_hifigan_fetch.nx

nx_hifigan_fetch.nx source

↩ module page · 55 lines · 4264 B

1// nx_hifigan_fetch.nx -- fetch a REAL HiFi-GAN vocoder's config + weight-index from HuggingFace over our own TLS, 2// to GROUND the generator-graph assembly in the actual architecture (upsample rates / kernels / dilations / 3// channels) instead of guessing. Mirrors the proven researcher fetch (nx_https_fetch_follow + Mozilla CA, redirect- 4// following). config.json is tiny (KB) = low-risk; proves the model is reachable + gives the exact dims. Target: 5// microsoft/speecht5_hifigan (a standard HiFi-GAN, HF-hosted). The 50MB weights come later via nx_https_get_stream. 6// expect_exit: 0 license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_x509_trust_store.nx" 9import "nx_trust_store_load_from_certdata.nx" 10import "nx_https_fetch_follow.nx" 11const K_MAGIC_4194304: i64 = 4194304 12 13func df_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func df_putn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let d: *u8=sys_mmap(24); var k: i64=0; while m>0 {d[k]=(48+(m%10)) as u8; m=m/10; k=k+1} var i: i64=k-1; while i>=0 {let o: *u8=sys_mmap(1); o[0]=d[i]; sys_write(1,o,1); i=i-1} return 0 } 15func have_file(path: *u8) -> i64 { let fd: i64=sys_openat_rd(path); if fd<0 {return 0} sys_close(fd); return 1 } 16 17func fetch_save(url: *u8, opath: *u8, store: *TrustStore, out: *u8, cap: i64) -> i64 { 18 if have_file(opath)==1 { df_puts(opath); df_puts(" [have-skip]\n"); return 1 } 19 let status: *i64 = sys_mmap(8) as *i64 20 let n: i64 = nx_https_fetch_follow(url, store, out, cap, 6, status) 21 df_puts(url); df_puts(" status="); df_putn(status[0]); df_puts(" bytes="); df_putn(n) 22 if n<=0 { df_puts(" FETCH-FAIL\n"); return 0 } 23 var gz: i64=0; if n>=2 { if out[0]==0x1f as u8 { if out[1]==0x8b as u8 { gz=1 } } } 24 if gz==1 { df_puts(" [GZIP-skip]\n"); return 0 } 25 let fd: i64 = sys_openat_wr(opath, 0x1a4) 26 if fd<0 { df_puts(" SAVE-FAIL\n"); return 0 } 27 sys_write(fd, out, n); sys_close(fd) 28 df_puts(" SAVED\n") 29 // echo the config so we SEE the real architecture (it's small) 30 df_puts("---- config.json ----\n"); sys_write(1, out, n); df_puts("\n---- end ----\n") 31 return 1 32} 33 34func main() -> i64 { 35 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 36 if r<=0 { df_puts("HIFIGAN: certdata load failed\n"); return 1 } 37 let store: *TrustStore = r as *TrustStore 38 df_puts("CA roots="); df_putn(trust_store_count(store)); df_puts("\n") 39 let cap: i64 = K_MAGIC_4194304 40 let out: *u8 = sys_mmap(cap) 41 var ok: i64 = 0 42 df_puts("== HiFi-GAN vocoder ARCHITECTURE + FILE LIST (real, to ground the graph + pick the weight file) ==\n") 43 ok = ok + fetch_save("https://huggingface.co/microsoft/speecht5_hifigan/resolve/main/config.json" as *u8, "knowledge/fetched/hifigan_config.raw" as *u8, store, out, cap) 44 // HF API model-info: the "siblings" array lists EXACTLY which weight files are hosted (safetensors vs .bin). 45 ok = ok + fetch_save("https://huggingface.co/api/models/microsoft/speecht5_hifigan" as *u8, "knowledge/fetched/hifigan_files.raw" as *u8, store, out, cap) 46 // SpeechT5 feature-extraction config: the EXACT mel params (fft/hop/win/n_mels/fmin/fmax/log) the vocoder expects. 47 ok = ok + fetch_save("https://huggingface.co/microsoft/speecht5_tts/resolve/main/preprocessor_config.json" as *u8, "knowledge/fetched/speecht5_preproc.raw" as *u8, store, out, cap) 48 // SpeechT5 TTS acoustic model (text->mel): architecture config + file-list (size/format) to scope the arc. 49 ok = ok + fetch_save("https://huggingface.co/microsoft/speecht5_tts/resolve/main/config.json" as *u8, "knowledge/fetched/speecht5_tts_config.raw" as *u8, store, out, cap) 50 ok = ok + fetch_save("https://huggingface.co/api/models/microsoft/speecht5_tts" as *u8, "knowledge/fetched/speecht5_tts_files.raw" as *u8, store, out, cap) 51 df_puts("hifigan fetch: "); df_putn(ok); df_puts(" / 5\n") 52 if ok>=5 { df_puts("verdict=GREEN (vocoder + mel-config + acoustic-model config/files on disk)\n"); sys_exit(0); return 0 } 53 df_puts("verdict=FAIL (re-run; or the model uses a different path -> adjust URL)\n") 54 sys_exit(1); return 1 55}