11import * as ort from 'onnxruntime-web' ;
2- ort . env . wasm . wasmPaths = '/public/wasm/onnxruntime/' ;
3- console . log ( ort . env . wasm . wasmPaths )
42
53let sessionFull : any = null ;
6- let mergesPath : string = "public/vocab/merges_all_18k.txt" ;
7- let vocabPath : string = "public/vocab/vocab_all_18k.json" ;
8- let modelPath : string = "public/onnx/mail_180226_02.onnx" ;
4+ const mergesPath : string = "public/vocab/merges_all_18k.txt" ;
5+ const vocabPath : string = "public/vocab/vocab_all_18k.json" ;
6+ const modelPath : string = "public/onnx/mail_180226_02.onnx" ;
7+ const wasmPath : string = '/public/wasm/onnxruntime/' ;
98
9+ ort . env . wasm . wasmPaths = wasmPath ;
1010const LENTOKENS = 128 ;
1111const finalVocab = await loadVocab ( vocabPath ) ;
1212const vstr = await v2str ( vocabPath ) ;
13- console . log ( "cpp vocab" , vstr ) ;
1413const mstr = await m2str ( mergesPath ) ;
15- console . log ( "cpp merges:" , mstr ) ;
1614
1715try {
1816 sessionFull = await ort . InferenceSession . create ( modelPath , {
2220} catch ( e ) {
2321 console . error ( "Failed to load full model:" , e ) ;
2422}
25-
26-
27- export async function loadTokenizer ( ) {
23+ const tensorial = new ort . Tensor ( "int64" , new BigInt64Array ( LENTOKENS ) , [ 1 , LENTOKENS ] ) ;
24+ const atten = new ort . Tensor ( 'float32' , new Float32Array ( LENTOKENS ) , [ 1 , LENTOKENS ] )
25+ const input_tensor = {
26+ input : tensorial ,
27+ attention : atten ,
28+ } ;
29+
30+ async function loadTokenizer ( ) {
2831 return new Promise ( ( resolve ) => {
2932 const script = document . createElement ( 'script' )
3033 script . src = 'public/wasm/tokenizer/tokenizer.js'
@@ -40,7 +43,9 @@ export async function loadTokenizer() {
4043}
4144
4245const tokenizer : any = await loadTokenizer ( )
43- export async function loadVocab ( path : string ) {
46+
47+
48+ async function loadVocab ( path : string ) {
4449 const response = await fetch ( path ) ;
4550 let text = await response . text ( ) ;
4651 text = text . slice ( 1 , - 1 ) ;
@@ -92,7 +97,7 @@ async function m2str(path: string) {
9297}
9398
9499
95- function runTokenizer ( text : string ) {
100+ function runTokenizer ( text : string ) : [ BigInt64Array , Float32Array ] {
96101
97102 const vocabPtr = vstr . map ( str => {
98103 const utf8Length = tokenizer . lengthBytesUTF8 ( str ) ;
@@ -144,7 +149,7 @@ function runTokenizer(text: string) {
144149
145150function color ( v : any ) {
146151 const g = Math . round ( 255 * v ) ;
147- return `rgb(0,0, ${ g } )` ;
152+ return `rgb(${ 255 - g } , ${ 255 - g } ,255 )` ;
148153}
149154
150155
@@ -161,18 +166,11 @@ export async function spamOrHam(message: string | undefined, result: any, explan
161166 if ( message ) {
162167 let messagec = message . replaceAll ( "\n" , " " )
163168 console . time ( "Token" ) ;
164- console . log ( messagec ) ;
165- const [ outputTokens , attention ] = runTokenizer ( messagec ) ;
169+ const [ outputTokens , atten ] = runTokenizer ( messagec ) ;
166170 console . timeEnd ( "Token" )
167- console . log ( "Texto tokenizado:" , outputTokens ) ;
168- console . log ( "Mascara tokens:" , attention ) ;
169171 console . time ( "Tensor" ) ;
170- const tensorial = new ort . Tensor ( "int64" , outputTokens , [ 1 , LENTOKENS ] ) ;
171- const atten = new ort . Tensor ( 'float32' , attention , [ 1 , LENTOKENS ] )
172- const input_tensor = {
173- input : tensorial ,
174- attention : atten ,
175- } ;
172+ input_tensor . input . data . set ( outputTokens ) ;
173+ input_tensor . attention . data . set ( atten ) ;
176174 console . timeEnd ( "Tensor" )
177175 console . time ( "Inferencia" )
178176 let running = null ;
@@ -182,7 +180,6 @@ export async function spamOrHam(message: string | undefined, result: any, explan
182180 let relevancies ;
183181 if ( Object . values ( running ) . length > 1 ) {
184182 relevancies = running . saliencies . data ;
185- console . log ( "Relevancies" , relevancies ) ;
186183 }
187184 console . log ( "Salida del modelo: " , spam ) ;
188185 let s_result = "" ;
0 commit comments