35 lines
2.1 KiB
JavaScript
35 lines
2.1 KiB
JavaScript
import { pipeline, env } from '@huggingface/transformers';
|
|||
|
|
import { createRequire } from 'module';
|
||
|
|
import os from 'os'; import path from 'path'; import fs from 'fs'; import vm from 'vm';
|
||
|
|
const require = createRequire(import.meta.url);
|
||
|
|
const Liturgy=require('./liturgy.js'), Guard=require('./mt-guard.js');
|
||
|
|
const Preach=require('./preach.js'), B=require('./bible-lexicon.js'), Spa=require('./spa-variants.js');
|
||
|
|
const esrc=fs.readFileSync('engine.js','utf8');
|
||
|
|
const sb={console}; vm.createContext(sb);
|
||
|
|
vm.runInContext(esrc.slice(esrc.indexOf('const SCRIPT_NO_SPACES'), esrc.indexOf('function applyFixes')), sb);
|
||
|
|
env.allowLocalModels=false;
|
||
|
|
env.cacheDir=path.join(os.homedir(),'Library/Application Support/Church Translate Models');
|
||
|
|
const pipe=await pipeline('translation','Xenova/nllb-200-distilled-600M',{dtype:'fp32'});
|
||
|
|
|
||
|
|
async function full(sentence, lang){
|
||
|
|
const set=Liturgy.lookup(sentence,lang); if(set) return [set,'liturgy'];
|
||
|
|
const named=B.wholeUtterance(sentence,lang); if(named) return [named,'name'];
|
||
|
|
const forMt=Preach.forTranslation(sentence);
|
||
|
|
const sd=Preach.stripShortPeriod(forMt);
|
||
|
|
const o=await pipe(sd.text,{src_lang:'eng_Latn',tgt_lang:lang});
|
||
|
|
let raw=(Array.isArray(o)?o[0]:o).translation_text.trim();
|
||
|
|
raw=Guard.clean(sd.text,raw).text;
|
||
|
|
raw=Preach.restorePeriod(raw,sd.hadPeriod);
|
||
|
|
if(lang==='spa_Latn') raw=Spa.toLatinAmerican(Spa.toChurchRegister(raw));
|
||
|
|
const bl=B.forLanguage(lang);
|
||
|
|
if(bl) raw=sb.applyTable(raw,Object.keys(bl).map(f=>({from:f,to:bl[f]})));
|
||
|
|
return [raw,'model'];
|
||
|
|
}
|
||
|
|
|
||
|
|
const CASES=['Hello.','What?','Amen.','Hallelujah.','Peter.','Okay.','Check.','Faith.','Hello?',
|
||
|
|
'No.','Jesus.','Yeah.','All right.','Right?','Holy Spirit.','Death.','Yeah?',
|
||
|
|
'Praise God.','Grace.','Hope.','Love.','Sin.','Heaven.','Prayer.','Worship.',
|
||
|
|
'Testing.','One two.','Mic check.','Good.','Wow.','Listen.','Look.','Come.','Stand.'];
|
||
|
|
console.log(' English'.padEnd(18)+'now'.padEnd(30)+'via');
|
||
|
|
for (const t of CASES){ const [r,how]=await full(t,'spa_Latn'); console.log(' '+t.padEnd(16)+String(r).slice(0,28).padEnd(30)+how); }
|