Spaces:
Running
Running
Download app.js from tomngdev/indra-ocr-web: direct link, hf CLI and curl.
- Browser
- Download file 72.1 kB
-
https://huggingface.co/spaces/tomngdev/indra-ocr-web/resolve/main/app.js
- Command line
-
hf download hf://spaces/tomngdev/indra-ocr-web/app.js
-
curl -L -o app.js https://huggingface.co/spaces/tomngdev/indra-ocr-web/resolve/main/app.js
72.1 kB
| import{InferenceSession as Je,Tensor as Qe,env as In}from"onnxruntime-web";function z(t){let n=Math.floor(t),e=t-n;if(e>0.5)return n+1;if(e<0.5)return n;return n%2===0?n:n+1}var G=(t,n,e)=>t<n?n:t>e?e:t,H=(t)=>{if(!t.length)return NaN;let n=[...t].sort((o,s)=>o-s),e=n.length>>1;return n.length%2?n[e]:(n[e-1]+n[e])/2};function Kt(t,n,e){let o=[];if(e==="area"&&n<t){let a=t/n;for(let r=0;r<n;r++){let l=r*a,c=(r+1)*a,u=[],h=Math.min(Math.ceil(c),t);for(let i=Math.max(0,Math.floor(l));i<h;i++){let p=(Math.min(c,i+1)-Math.max(l,i))/a;if(p>0)u.push([i,p])}o.push(u)}return o}let s=t/n;for(let a=0;a<n;a++){let r=(a+0.5)*s-0.5,l=Math.floor(r),c=r-l,u=l+1;if(l<0)l=0;if(u>t-1)u=t-1;o.push(l===u||c<=0?[[l,1]]:[[l,1-c],[u,c]])}return o}function X(t,n,e,o,s,a="linear"){if(o===n&&s===e)return t;let r=Kt(n,o,a),l=Kt(e,s,a),c=new Float64Array(o*e);for(let h=0;h<e;h++){let i=h*n;for(let p=0;p<o;p++){let d=0;for(let[m,f]of r[p])d+=t[i+m]*f;c[h*o+p]=d}}let u=new Uint8Array(o*s);for(let h=0;h<o;h++)for(let i=0;i<s;i++){let p=0;for(let[d,m]of l[i])p+=c[d*o+h]*m;u[i*o+h]=G(Math.round(p),0,255)}return u}function ut(t,n,e,o=1){let s=e*Math.PI/180,a=o*Math.cos(s),r=o*Math.sin(s);return[a,r,(1-a)*t-r*n,-r,a,r*t+(1-a)*n]}function kt(t,n,e="constant"){let{data:o,width:s,height:a}=t,{width:r,height:l}=t,c=n[0]*n[4]-n[1]*n[3],u=n[4]/c,h=-n[1]/c,i=(n[1]*n[5]-n[2]*n[4])/c,p=-n[3]/c,d=n[0]/c,m=(n[2]*n[3]-n[0]*n[5])/c,f=e==="replicate",g=(w,M)=>{if(f)w=w<0?0:w>=s?s-1:w,M=M<0?0:M>=a?a-1:M;else if(w<0||w>=s||M<0||M>=a)return 0;return o[M*s+w]},x=new o.constructor(r*l),A=x instanceof Uint8Array;for(let w=0;w<l;w++){let M=h*w+i,I=d*w+m,_=w*r;for(let k=0;k<r;k++){let E=u*k+M,S=p*k+I,L=Math.floor(E),R=Math.floor(S),y=E-L,v=S-R,T=g(L,R)*(1-y)*(1-v)+g(L+1,R)*y*(1-v)+g(L,R+1)*(1-y)*v+g(L+1,R+1)*y*v;x[_+k]=A?G(Math.round(T),0,255):T}}return{data:x,width:r,height:l}}function pt(t,n){let{data:e,width:o,height:s}=t;for(let a=0;a<n;a++){let r=new Uint8Array(o*s);for(let l=0;l<o;l++)for(let c=0;c<s;c++)r[l*s+c]=e[(s-1-c)*o+l];e=r,[o,s]=[s,o]}return{data:e,width:o,height:s}}var mt=32,_t=2,On=0.15;function Xt(t,n){let{data:e,width:o,height:s}=t,a=n/Math.max(s,o),r=Math.max(1,z(s*a)),l=Math.max(1,z(o*a)),c=r===s&&l===o?e:X(e,o,s,l,r,a<1?"area":"linear"),u=Math.ceil(r/mt)*mt,h=Math.ceil(l/mt)*mt,i=new Float32Array(u*h);for(let p=0;p<r;p++)for(let d=0;d<l;d++)i[p*h+d]=1-c[p*l+d]/255;return{x:i,ph:u,pw:h,scale:a}}function qt(t,n,e,o=0.5,s=2){let a=n*e,r=t.subarray(0,a),l=t.subarray(a,2*a),c=t.subarray(2*a,3*a),u=new Int32Array(a),h=new Float32Array(a);for(let m=0;m<a;m++)h[m]=1/(1+Math.exp(-r[m]));let i=[{area:0}],p=new Int32Array(a);for(let m=0;m<a;m++){if(u[m]!==0||h[m]<=o)continue;let f=i.length,g=0;p[g++]=m,u[m]=f;let x={area:0,x0:e,x1:-1,sumTop:0,sumBot:0,sumSc:0};while(g>0){let A=p[--g],w=A/e|0,M=A-w*e;if(x.area++,M<x.x0)x.x0=M;if(M>x.x1)x.x1=M;let I=(w+0.5)*_t;if(x.sumTop+=I-l[A],x.sumBot+=I+c[A],x.sumSc+=h[A],M>0&&u[A-1]===0&&h[A-1]>o)u[A-1]=f,p[g++]=A-1;if(M<e-1&&u[A+1]===0&&h[A+1]>o)u[A+1]=f,p[g++]=A+1;if(w>0&&u[A-e]===0&&h[A-e]>o)u[A-e]=f,p[g++]=A-e;if(w<n-1&&u[A+e]===0&&h[A+e]>o)u[A+e]=f,p[g++]=A+e}i.push(x)}let d=[];for(let m=1;m<i.length;m++){let f=i[m];if(f.area<s)continue;let g=f.sumTop/f.area,x=f.sumBot/f.area,A=Math.max(1,x-g),w=Math.min(On*A,0.5*A);d.push([f.x0*_t-w,g,(f.x1+1)*_t+w,x,f.sumSc/f.area])}return d}var jn=32,Dn=0.2,at=0.3,Et=8;function zn(t,n,e=[0.02,0.5,0.98]){let o=new Float64Array(256),s=0;for(let r=0;r<256;r++)s+=t[r],o[r]=s;let a=(r)=>{let l=0,c=256;while(l<c){let u=l+c>>1;if(o[u]>r)c=u;else l=u+1}return l};return e.map((r)=>{let l=r*(n-1),c=Math.floor(l),u=l-c,h=a(c),i=a(Math.min(c+1,n-1)),p=i-h;return u>=0.5?i-p*(1-u):h+p*u})}function Tt(t,n){let{height:e,width:o}=t,s=n[0],a=n[1],r=n[2],l=n[3],c=Math.max(0,Math.trunc(a)),u=Math.min(e,Math.trunc(l)+1),h=Math.max(0,Math.trunc(s)),i=Math.min(o,Math.trunc(r)+1);if(u<=c||i<=h)return!1;let p=new Int32Array(256),d=0;for(let x=c;x<u;x++){let A=x*o;for(let w=h;w<i;w++)p[t.data[A+w]]++,d++}let[m,f,g]=zn(p,d);return g-f>2*(f-m)+8}function rt(t,n,e=jn,o=4096,s=1,a=null){let r=(s*(1+2*Dn)-1)/2;e=e*s;let[l,c,u,h]=n,i=Math.max(1,h-c),p=l-at*i,d=u+at*i,m=c-r*i,f=h+r*i,{height:g,width:x}=t,A=Math.floor(p),w=Math.floor(m),M=Math.ceil(d),I=Math.ceil(f),_=Math.max(1,M-A),k=Math.max(1,I-w);if(a===null)a=Tt(t,n);let E=new Uint8Array(k*_).fill(a?0:255),S=Math.max(0,A),L=Math.max(0,w),R=Math.min(x,M),y=Math.min(g,I);if(R>S&&y>L)for(let B=L;B<y;B++){let P=B*x,N=(B-w)*_;for(let U=S;U<R;U++)E[N+(U-A)]=t.data[P+U]}let v=Math.max(Et,Math.min(o,z(_*e/k))),T=X(E,_,k,v,e,k>e?"area":"linear"),j=new Float32Array(T.length);if(a)for(let B=0;B<T.length;B++)j[B]=T[B]/255;else for(let B=0;B<T.length;B++)j[B]=1-T[B]/255;return{ink:j,w:v}}function tt(t){let n=t[0].ink.length/t[0].w,e=Math.max(...t.map((a)=>a.w));e=Math.ceil(e/Et)*Et;let o=new Float32Array(t.length*n*e),s=new BigInt64Array(t.length);return t.forEach((a,r)=>{for(let l=0;l<n;l++){let c=l*a.w,u=r*n*e+l*e;for(let h=0;h<a.w;h++)o[u+h]=a.ink[c+h]}s[r]=BigInt(a.w)}),{arr:o,w:e,widths:s}}function nt(t,n=64,e=1.3,o=64,s=40960){let a=t.map((l,c)=>c).sort((l,c)=>t[l]-t[c]),r=[];for(let l of a){let c=r[r.length-1];if(c&&c.length<n&&Math.max(t[l],o)<=e*Math.max(t[c[0]],o)&&(c.length+1)*Math.max(t[l],o)<=s)c.push(l);else r.push([l])}return r}var q=(t,n)=>{let e=0,o=t.length/2;while(e<o){let s=e+o>>1;if(t[s*2+1]<n)e=s+1;else o=s}return e<t.length/2&&t[e*2]<=n},Fn=new Int32Array([1470,1470,1472,1472,1475,1475,1478,1478,1488,1514,1519,1524,1984,2026,2036,2037,2042,2042,2046,2069,2074,2074,2084,2084,2088,2088,2096,2110,2112,2136,2142,2142,8207,8207,64285,64285,64287,64296,64298,64310,64312,64316,64318,64318,64320,64321,64323,64324,64326,64335,67584,67589,67592,67592,67594,67637,67639,67640,67644,67644,67647,67669,67671,67742,67751,67759,67808,67826,67828,67829,67835,67867,67872,67897,67903,67903,67968,68023,68028,68047,68050,68096,68112,68115,68117,68119,68121,68149,68160,68168,68176,68184,68192,68255,68288,68324,68331,68342,68352,68405,68416,68437,68440,68466,68472,68497,68505,68508,68521,68527,68608,68680,68736,68786,68800,68850,68858,68863,68938,68965,68975,68997,69006,69007,69248,69289,69293,69293,69296,69297,69376,69415,69488,69505,69510,69513,69552,69579,69600,69622,124928,125124,125127,125135,125184,125251,125259,125259,125264,125273,125278,125279]),Pn=new Int32Array([1544,1544,1547,1547,1549,1549,1563,1610,1645,1647,1649,1749,1765,1766,1774,1775,1786,1805,1807,1808,1810,1839,1869,1957,1969,1969,2144,2154,2160,2190,2208,2249,64336,64450,64467,64829,64848,64911,64914,64967,65008,65020,65136,65140,65142,65276,68864,68899,69314,69316,69424,69445,69457,69465,126065,126132,126209,126269,126464,126467,126469,126495,126497,126498,126500,126500,126503,126503,126505,126514,126516,126519,126521,126521,126523,126523,126530,126530,126535,126535,126537,126537,126539,126539,126541,126543,126545,126546,126548,126548,126551,126551,126553,126553,126555,126555,126557,126557,126559,126559,126561,126562,126564,126564,126567,126570,126572,126578,126580,126583,126585,126588,126590,126590,126592,126601,126603,126619,126625,126627,126629,126633,126635,126651]),Yt=new Int32Array([65,90,97,122,170,170,181,181,186,186,192,214,216,246,248,696,699,705,720,721,736,740,750,750,880,883,886,887,890,893,895,895,902,902,904,906,908,908,910,929,931,1013,1015,1154,1162,1327,1329,1366,1369,1417,2307,2361,2363,2363,2365,2368,2377,2380,2382,2384,2392,2401,2404,2432,2434,2435,2437,2444,2447,2448,2451,2472,2474,2480,2482,2482,2486,2489,2493,2496,2503,2504,2507,2508,2510,2510,2519,2519,2524,2525,2527,2529,2534,2545,2548,2554,2556,2557,2563,2563,2565,2570,2575,2576,2579,2600,2602,2608,2610,2611,2613,2614,2616,2617,2622,2624,2649,2652,2654,2654,2662,2671,2674,2676,2678,2678,2691,2691,2693,2701,2703,2705,2707,2728,2730,2736,2738,2739,2741,2745,2749,2752,2761,2761,2763,2764,2768,2768,2784,2785,2790,2800,2809,2809,2818,2819,2821,2828,2831,2832,2835,2856,2858,2864,2866,2867,2869,2873,2877,2878,2880,2880,2887,2888,2891,2892,2903,2903,2908,2909,2911,2913,2918,2935,2947,2947,2949,2954,2958,2960,2962,2965,2969,2970,2972,2972,2974,2975,2979,2980,2984,2986,2990,3001,3006,3007,3009,3010,3014,3016,3018,3020,3024,3024,3031,3031,3046,3058,3073,3075,3077,3084,3086,3088,3090,3112,3114,3129,3133,3133,3137,3140,3160,3162,3165,3165,3168,3169,3174,3183,3191,3191,3199,3200,3202,3212,3214,3216,3218,3240,3242,3251,3253,3257,3261,3268,3270,3272,3274,3275,3285,3286,3293,3294,3296,3297,3302,3311,3313,3315,3330,3340,3342,3344,3346,3386,3389,3392,3398,3400,3402,3404,3406,3407,3412,3425,3430,3455,3458,3459,3461,3478,3482,3505,3507,3515,3517,3517,3520,3526,3535,3537,3544,3551,3558,3567,3570,3572,3585,3632,3634,3635,3648,3654,3663,3675,3713,3714,3716,3716,3718,3722,3724,3747,3749,3749,3751,3760,3762,3763,3773,3773,3776,3780,3782,3782,3792,3801,3804,3807,3840,3863,3866,3892,3894,3894,3896,3896,3902,3911,3913,3948,3967,3967,3973,3973,3976,3980,4030,4037,4039,4044,4046,4058,4096,4140,4145,4145,4152,4152,4155,4156,4159,4183,4186,4189,4193,4208,4213,4225,4227,4228,4231,4236,4238,4252,4254,4293,4295,4295,4301,4301,4304,4680,4682,4685,4688,4694,4696,4696,4698,4701,4704,4744,4746,4749,4752,4784,4786,4789,4792,4798,4800,4800,4802,4805,4808,4822,4824,4880,4882,4885,4888,4954,4960,4988,4992,5007,5024,5109,5112,5117,5121,5759,5761,5786,5792,5880,5888,5905,5909,5909,5919,5937,5940,5942,5952,5969,5984,5996,5998,6000,6016,6067,6070,6070,6078,6085,6087,6088,6100,6106,6108,6108,6112,6121,6160,6169,6176,6264,6272,6276,6279,6312,6314,6314,6320,6389,6400,6430,6435,6438,6441,6443,6448,6449,6451,6456,6470,6509,6512,6516,6528,6571,6576,6601,6608,6618,6656,6678,6681,6682,6686,6741,6743,6743,6753,6753,6755,6756,6765,6770,6784,6793,6800,6809,6816,6829,6916,6963,6965,6965,6971,6971,6973,6977,6979,6988,6990,7018,7028,7039,7042,7073,7078,7079,7082,7082,7086,7141,7143,7143,7146,7148,7150,7150,7154,7155,7164,7211,7220,7221,7227,7241,7245,7306,7312,7354,7357,7367,7379,7379,7393,7393,7401,7404,7406,7411,7413,7415,7418,7418,7424,7615,7680,7957,7960,7965,7968,8005,8008,8013,8016,8023,8025,8025,8027,8027,8029,8029,8031,8061,8064,8116,8118,8124,8126,8126,8130,8132,8134,8140,8144,8147,8150,8155,8160,8172,8178,8180,8182,8188,8206,8206,8305,8305,8319,8319,8336,8348,8450,8450,8455,8455,8458,8467,8469,8469,8473,8477,8484,8484,8486,8486,8488,8488,8490,8493,8495,8505,8508,8511,8517,8521,8526,8527,8544,8584,9014,9082,9109,9109,9372,9449,9900,9900,10240,10495,11264,11492,11499,11502,11506,11507,11520,11557,11559,11559,11565,11565,11568,11623,11631,11632,11648,11670,11680,11686,11688,11694,11696,11702,11704,11710,11712,11718,11720,11726,11728,11734,11736,11742,12293,12295,12321,12329,12334,12335,12337,12341,12344,12348,12353,12438,12445,12447,12449,12538,12540,12543,12549,12591,12593,12686,12688,12735,12784,12828,12832,12879,12896,12923,12927,12976,12992,13003,13008,13174,13179,13277,13280,13310,13312,19903,19968,42124,42192,42508,42512,42539,42560,42606,42624,42653,42656,42735,42738,42743,42786,42887,42889,42957,42960,42961,42963,42963,42965,42972,42994,43009,43011,43013,43015,43018,43020,43044,43047,43047,43056,43063,43072,43123,43136,43203,43214,43225,43250,43262,43264,43301,43310,43334,43346,43347,43359,43388,43395,43442,43444,43445,43450,43451,43454,43469,43471,43481,43486,43492,43494,43518,43520,43560,43567,43568,43571,43572,43584,43586,43588,43595,43597,43597,43600,43609,43612,43643,43645,43695,43697,43697,43701,43702,43705,43709,43712,43712,43714,43714,43739,43755,43758,43765,43777,43782,43785,43790,43793,43798,43808,43814,43816,43822,43824,43881,43888,44004,44006,44007,44009,44012,44016,44025,44032,55203,55216,55238,55243,55291,57344,64109,64112,64217,64256,64262,64275,64279,65313,65338,65345,65370,65382,65470,65474,65479,65482,65487,65490,65495,65498,65500,65536,65547,65549,65574,65576,65594,65596,65597,65599,65613,65616,65629,65664,65786,65792,65792,65794,65794,65799,65843,65847,65855,65933,65934,66000,66044,66176,66204,66208,66256,66304,66339,66349,66378,66384,66421,66432,66461,66463,66499,66504,66517,66560,66717,66720,66729,66736,66771,66776,66811,66816,66855,66864,66915,66927,66938,66940,66954,66956,66962,66964,66965,66967,66977,66979,66993,66995,67001,67003,67004,67008,67059,67072,67382,67392,67413,67424,67431,67456,67461,67463,67504,67506,67514,69632,69632,69634,69687,69703,69709,69734,69743,69745,69746,69749,69749,69762,69810,69815,69816,69819,69825,69837,69837,69840,69864,69872,69881,69891,69926,69932,69932,69942,69959,69968,70002,70004,70006,70018,70069,70079,70088,70093,70094,70096,70111,70113,70132,70144,70161,70163,70190,70194,70195,70197,70197,70200,70205,70207,70208,70272,70278,70280,70280,70282,70285,70287,70301,70303,70313,70320,70366,70368,70370,70384,70393,70402,70403,70405,70412,70415,70416,70419,70440,70442,70448,70450,70451,70453,70457,70461,70463,70465,70468,70471,70472,70475,70477,70480,70480,70487,70487,70493,70499,70528,70537,70539,70539,70542,70542,70544,70581,70583,70586,70594,70594,70597,70597,70599,70602,70604,70605,70607,70607,70609,70609,70611,70613,70615,70616,70656,70711,70720,70721,70725,70725,70727,70747,70749,70749,70751,70753,70784,70834,70841,70841,70843,70846,70849,70849,70852,70855,70864,70873,71040,71089,71096,71099,71102,71102,71105,71131,71168,71218,71227,71228,71230,71230,71233,71236,71248,71257,71296,71338,71340,71340,71342,71343,71350,71350,71352,71353,71360,71369,71376,71395,71424,71450,71454,71454,71456,71457,71462,71462,71472,71494,71680,71726,71736,71736,71739,71739,71840,71922,71935,71942,71945,71945,71948,71955,71957,71958,71960,71989,71991,71992,71997,71997,71999,72002,72004,72006,72016,72025,72096,72103,72106,72147,72156,72159,72161,72164,72192,72192,72199,72200,72203,72242,72249,72250,72255,72262,72272,72272,72279,72280,72284,72329,72343,72343,72346,72354,72368,72440,72448,72457,72640,72673,72688,72697,72704,72712,72714,72751,72766,72773,72784,72812,72816,72847,72873,72873,72881,72881,72884,72884,72960,72966,72968,72969,72971,73008,73030,73030,73040,73049,73056,73061,73063,73064,73066,73102,73107,73108,73110,73110,73112,73112,73120,73129,73440,73458,73461,73464,73474,73488,73490,73525,73534,73535,73537,73537,73539,73561,73648,73648,73664,73684,73727,74649,74752,74862,74864,74868,74880,75075,77712,77810,77824,78911,78913,78918,78944,82938,82944,83526,90368,90397,90410,90412,90416,90425,92160,92728,92736,92766,92768,92777,92782,92862,92864,92873,92880,92909,92917,92917,92928,92975,92983,92997,93008,93017,93019,93025,93027,93047,93053,93071,93504,93561,93760,93850,93952,94026,94032,94087,94099,94111,94176,94177,94179,94179,94192,94193,94208,100343,100352,101589,101631,101640,110576,110579,110581,110587,110589,110590,110592,110882,110898,110898,110928,110930,110933,110933,110948,110951,110960,111355,113664,113770,113776,113788,113792,113800,113808,113817,113820,113820,113823,113823,117974,117999,118608,118723,118784,119029,119040,119078,119081,119142,119146,119154,119171,119172,119180,119209,119214,119272,119488,119507,119520,119539,119648,119672,119808,119892,119894,119964,119966,119967,119970,119970,119973,119974,119977,119980,119982,119993,119995,119995,119997,120003,120005,120069,120071,120074,120077,120084,120086,120092,120094,120121,120123,120126,120128,120132,120134,120134,120138,120144,120146,120485,120488,120512,120514,120538,120540,120570,120572,120596,120598,120628,120630,120654,120656,120686,120688,120712,120714,120744,120746,120770,120772,120779,120832,121343,121399,121402,121453,121460,121462,121475,121477,121483,122624,122654,122661,122666,122928,122989,123136,123180,123191,123197,123200,123209,123214,123215,123536,123565,123584,123627,123632,123641,124112,124139,124144,124153,124368,124397,124400,124410,124415,124415,124896,124902,124904,124907,124909,124910,124912,124926,127248,127278,127280,127337,127344,127404,127462,127490,127504,127547,127552,127560,127568,127569,131072,173791,173824,177977,177984,178205,178208,183969,183984,191456,191472,192093,194560,195101,196608,201546,201552,205743,983040,1048573,1048576,1114109]),Un=new Int32Array([48,57,178,179,185,185,1536,1541,1632,1641,1643,1644,1757,1757,1776,1785,2192,2193,2274,2274,8304,8304,8308,8313,8320,8329,9352,9371,65296,65305,66273,66299,68912,68921,68928,68937,69216,69246,118000,118009,120782,120831,127232,127242,130032,130041]),Hn=new Int32Array([4352,4447,8986,8987,9001,9002,9193,9196,9200,9200,9203,9203,9725,9726,9748,9749,9776,9783,9800,9811,9855,9855,9866,9871,9875,9875,9889,9889,9898,9899,9917,9918,9924,9925,9934,9934,9940,9940,9962,9962,9970,9971,9973,9973,9978,9978,9981,9981,9989,9989,9994,9995,10024,10024,10060,10060,10062,10062,10067,10069,10071,10071,10133,10135,10160,10160,10175,10175,11035,11036,11088,11088,11093,11093,11904,11929,11931,12019,12032,12245,12272,12350,12353,12438,12441,12543,12549,12591,12593,12686,12688,12773,12783,12830,12832,12871,12880,42124,42128,42182,43360,43388,44032,55203,63744,64255,65040,65049,65072,65106,65108,65126,65128,65131,65281,65376,65504,65510,94176,94180,94192,94193,94208,100343,100352,101589,101631,101640,110576,110579,110581,110587,110589,110590,110592,110882,110898,110898,110928,110930,110933,110933,110948,110951,110960,111355,119552,119638,119648,119670,126980,126980,127183,127183,127374,127374,127377,127386,127488,127490,127504,127547,127552,127560,127568,127569,127584,127589,127744,127776,127789,127797,127799,127868,127870,127891,127904,127946,127951,127955,127968,127984,127988,127988,127992,128062,128064,128064,128066,128252,128255,128317,128331,128334,128336,128359,128378,128378,128405,128406,128420,128420,128507,128591,128640,128709,128716,128716,128720,128722,128725,128727,128732,128735,128747,128748,128756,128764,128992,129003,129008,129008,129292,129338,129340,129349,129351,129535,129648,129660,129664,129673,129679,129734,129742,129756,129759,129769,129776,129784,131072,196605,196608,262141]),Gn=new Int32Array([768,879,1155,1161,1425,1469,1471,1471,1473,1474,1476,1477,1479,1479,1552,1562,1611,1631,1648,1648,1750,1756,1759,1764,1767,1768,1770,1773,1809,1809,1840,1866,1958,1968,2027,2035,2045,2045,2070,2073,2075,2083,2085,2087,2089,2093,2137,2139,2199,2207,2250,2273,2275,2306,2362,2362,2364,2364,2369,2376,2381,2381,2385,2391,2402,2403,2433,2433,2492,2492,2497,2500,2509,2509,2530,2531,2558,2558,2561,2562,2620,2620,2625,2626,2631,2632,2635,2637,2641,2641,2672,2673,2677,2677,2689,2690,2748,2748,2753,2757,2759,2760,2765,2765,2786,2787,2810,2815,2817,2817,2876,2876,2879,2879,2881,2884,2893,2893,2901,2902,2914,2915,2946,2946,3008,3008,3021,3021,3072,3072,3076,3076,3132,3132,3134,3136,3142,3144,3146,3149,3157,3158,3170,3171,3201,3201,3260,3260,3263,3263,3270,3270,3276,3277,3298,3299,3328,3329,3387,3388,3393,3396,3405,3405,3426,3427,3457,3457,3530,3530,3538,3540,3542,3542,3633,3633,3636,3642,3655,3662,3761,3761,3764,3772,3784,3790,3864,3865,3893,3893,3895,3895,3897,3897,3953,3966,3968,3972,3974,3975,3981,3991,3993,4028,4038,4038,4141,4144,4146,4151,4153,4154,4157,4158,4184,4185,4190,4192,4209,4212,4226,4226,4229,4230,4237,4237,4253,4253,4957,4959,5906,5908,5938,5939,5970,5971,6002,6003,6068,6069,6071,6077,6086,6086,6089,6099,6109,6109,6155,6157,6159,6159,6277,6278,6313,6313,6432,6434,6439,6440,6450,6450,6457,6459,6679,6680,6683,6683,6742,6742,6744,6750,6752,6752,6754,6754,6757,6764,6771,6780,6783,6783,6832,6862,6912,6915,6964,6964,6966,6970,6972,6972,6978,6978,7019,7027,7040,7041,7074,7077,7080,7081,7083,7085,7142,7142,7144,7145,7149,7149,7151,7153,7212,7219,7222,7223,7376,7378,7380,7392,7394,7400,7405,7405,7412,7412,7416,7417,7616,7679,8400,8432,11503,11505,11647,11647,11744,11775,12330,12333,12441,12442,42607,42610,42612,42621,42654,42655,42736,42737,43010,43010,43014,43014,43019,43019,43045,43046,43052,43052,43204,43205,43232,43249,43263,43263,43302,43309,43335,43345,43392,43394,43443,43443,43446,43449,43452,43453,43493,43493,43561,43566,43569,43570,43573,43574,43587,43587,43596,43596,43644,43644,43696,43696,43698,43700,43703,43704,43710,43711,43713,43713,43756,43757,43766,43766,44005,44005,44008,44008,44013,44013,64286,64286,65024,65039,65056,65071,66045,66045,66272,66272,66422,66426,68097,68099,68101,68102,68108,68111,68152,68154,68159,68159,68325,68326,68900,68903,68969,68973,69291,69292,69372,69375,69446,69456,69506,69509,69633,69633,69688,69702,69744,69744,69747,69748,69759,69761,69811,69814,69817,69818,69826,69826,69888,69890,69927,69931,69933,69940,70003,70003,70016,70017,70070,70078,70089,70092,70095,70095,70191,70193,70196,70196,70198,70199,70206,70206,70209,70209,70367,70367,70371,70378,70400,70401,70459,70460,70464,70464,70502,70508,70512,70516,70587,70592,70606,70606,70608,70608,70610,70610,70625,70626,70712,70719,70722,70724,70726,70726,70750,70750,70835,70840,70842,70842,70847,70848,70850,70851,71090,71093,71100,71101,71103,71104,71132,71133,71219,71226,71229,71229,71231,71232,71339,71339,71341,71341,71344,71349,71351,71351,71453,71453,71455,71455,71458,71461,71463,71467,71727,71735,71737,71738,71995,71996,71998,71998,72003,72003,72148,72151,72154,72155,72160,72160,72193,72202,72243,72248,72251,72254,72263,72263,72273,72278,72281,72283,72330,72342,72344,72345,72752,72758,72760,72765,72767,72767,72850,72871,72874,72880,72882,72883,72885,72886,73009,73014,73018,73018,73020,73021,73023,73029,73031,73031,73104,73105,73109,73109,73111,73111,73459,73460,73472,73473,73526,73530,73536,73536,73538,73538,73562,73562,78912,78912,78919,78933,90398,90409,90413,90415,92912,92916,92976,92982,94031,94031,94095,94098,94180,94180,113821,113822,118528,118573,118576,118598,119143,119145,119163,119170,119173,119179,119210,119213,119362,119364,121344,121398,121403,121452,121461,121461,121476,121476,121499,121503,121505,121519,122880,122886,122888,122904,122907,122913,122915,122916,122918,122922,123023,123023,123184,123190,123566,123566,123628,123631,124140,124143,124398,124399,125136,125142,125252,125258,917760,917999]),it=(t)=>q(Fn,t.codePointAt(0))||q(Pn,t.codePointAt(0)),Zt=(t)=>q(Yt,t.codePointAt(0)),Jt=(t)=>q(Yt,t.codePointAt(0))||q(Un,t.codePointAt(0)),Qt=(t)=>q(Hn,t.codePointAt(0)),tn=(t)=>q(Gn,t.codePointAt(0)),Wn=new Int32Array([48,57,178,179,185,185,1632,1641,1776,1785,1984,1993,2406,2415,2534,2543,2662,2671,2790,2799,2918,2927,3046,3055,3174,3183,3302,3311,3430,3439,3558,3567,3664,3673,3792,3801,3872,3881,4160,4169,4240,4249,4969,4977,6112,6121,6160,6169,6470,6479,6608,6618,6784,6793,6800,6809,6992,7001,7088,7097,7232,7241,7248,7257,8304,8304,8308,8313,8320,8329,9312,9320,9332,9340,9352,9360,9450,9450,9461,9469,9471,9471,10102,10110,10112,10120,10122,10130,42528,42537,43216,43225,43264,43273,43472,43481,43504,43513,43600,43609,44016,44025,65296,65305,66720,66729,68160,68163,68912,68921,68928,68937,69216,69224,69714,69722,69734,69743,69872,69881,69942,69951,70096,70105,70384,70393,70736,70745,70864,70873,71248,71257,71360,71369,71376,71395,71472,71481,71904,71913,72016,72025,72688,72697,72784,72793,73040,73049,73120,73129,73552,73561,90416,90425,92768,92777,92864,92873,93008,93017,93552,93561,118000,118009,120782,120831,123200,123209,123632,123641,124144,124153,124401,124410,125264,125273,127232,127242,130032,130041]),Lt=(t)=>q(Wn,t.codePointAt(0));var nn=16,F=["Text","Section-Header","Page-Header","Page-Footer","List-Group","Table","Caption","Footnote","Table-Of-Contents","Form"],St=new Set(["Table","Form","Table-Of-Contents"].map((t)=>F.indexOf(t))),Vn=".!?:;。!?:;.",Kn=(t)=>!!t&&[...t].every(Lt),Xn=(t)=>t!==t.toLowerCase()&&t===t.toUpperCase(),qn=(t)=>{let n=0;for(let e of t)n+=Qt(e)?2:1;return n};function en(t,n,e,o){let s=t.length,a=new Float32Array(s*nn);if(!s)return a;let r=H(t.map((l)=>Math.max(l[3]-l[1],0.001)));for(let l=0;l<s;l++){let[c,u,h,i]=t[l],p=Math.max(h-c,0.001),d=Math.max(i-u,0.001),m=l*nn;a[m+0]=c/n,a[m+1]=u/e,a[m+2]=h/n,a[m+3]=i/e,a[m+4]=(c+h)/(2*n),a[m+5]=(u+i)/(2*e),a[m+6]=p/n,a[m+7]=Math.log(d/r),a[m+8]=Math.log(p/d)/4,a[m+9]=r/e*20;let f=(o?.[l]??"").trim();if(!f)continue;let g=0;for(let A of f)if(Lt(A))g++;let x=f.slice(0,3);while(x.endsWith(".")||x.endsWith(")"))x=x.slice(0,-1);a[m+10]=Math.log1p(qn(f))/5,a[m+11]=g/f.length,a[m+12]=Number("•-–▪◦*·→".includes(f[0])||Kn(x)&&f.length>3),a[m+13]=Number(f.endsWith("-")),a[m+14]=Number(Vn.includes(f[f.length-1])),a[m+15]=Number(Xn(f[0]))}return a}var dt=(t)=>t>=0?-Math.log1p(Math.exp(-t)):t-Math.log1p(Math.exp(t));function Ct(t,n,e=4){if(n<=1)return Array.from({length:n},(a,r)=>r);let o=new Float64Array(n);for(let a=0;a<n;a++)for(let r=0;r<n;r++)o[a]+=dt(t[r*n+a])-dt(t[a*n+r]);let s=Array.from({length:n},(a,r)=>r).sort((a,r)=>o[a]-o[r]);for(let a=0;a<e;a++){let r=!1;for(let l of[...s]){let c=s.filter((f)=>f!==l),u=c.map((f)=>dt(t[f*n+l])),h=c.map((f)=>dt(t[l*n+f])),i=new Float64Array(n),p=0;for(let f=1;f<n;f++)i[f]=p+=u[f-1];p=0;for(let f=n-2;f>=0;f--)i[f]+=p+=h[f];let d=0;for(let f=1;f<n;f++)if(i[f]>i[d])d=f;let m=s.indexOf(l);if(i[d]>i[m]+0.000001)c.splice(d,0,l),s=c,r=!0}if(!r)break}return s}function et(t,n){if(!n)return[];let e=new Float64Array(n*n);for(let h=0;h<n*n;h++)e[h]=t[h];for(let h=0;h<n;h++)e[h*n+h]=-1/0;let o=new Uint8Array(n).fill(1),s=new Int32Array(n),a=new Float64Array(n);for(let h=0;h<n;h++){let i=0;for(let p=1;p<n;p++)if(e[h*n+p]>e[h*n+i])i=p;s[h]=i,a[h]=e[h*n+i]}let r=Array.from({length:n},(h,i)=>[i]),l=new Float64Array(e),c=new Float64Array(n).fill(1);for(let h=0;h<n-1;h++){let i=0;for(let f=1;f<n;f++)if(a[f]>a[i])i=f;let p=s[i];if(a[i]<=0)break;if(p<i)[i,p]=[p,i];for(let f=0;f<n;f++)l[i*n+f]+=l[p*n+f];for(let f=0;f<n;f++)l[f*n+i]+=l[f*n+p];l[i*n+i]=0,c[i]+=c[p],o[p]=0,r[i].push(...r[p]);let d=new Float64Array(n);for(let f=0;f<n;f++)d[f]=o[f]&&f!==i?l[i*n+f]/(c[i]*c[f]):-1/0;for(let f=0;f<n;f++)e[i*n+f]=e[f*n+i]=d[f],e[p*n+f]=e[f*n+p]=-1/0;a[p]=-1/0;let m=0;for(let f=1;f<n;f++)if(d[f]>d[m])m=f;s[i]=m,a[i]=d[m];for(let f=0;f<n;f++){if(!o[f]||f===i)continue;if(s[f]===i||s[f]===p){let g=0;for(let x=1;x<n;x++)if(e[f*n+x]>e[f*n+g])g=x;s[f]=g,a[f]=e[f*n+g]}if(d[f]>a[f])s[f]=i,a[f]=d[f]}}let u=[];for(let h=0;h<n;h++)if(o[h])u.push(r[h].sort((i,p)=>i-p));return u}function on(t){if(!t.length)return[];let n=H(t.map((e)=>e[3]-e[1]));return t.map((e,o)=>[z((e[1]+e[3])/2/Math.max(n*0.5,0.001)),e[0],o]).sort((e,o)=>e[0]-o[0]||e[1]-o[1]).map((e)=>e[2])}function gt(t){let n=(o,s)=>{let a=o.map((i)=>s?i[1]:i[0]),r=o.map((i)=>s?i[3]:i[2]),l=a.map((i,p)=>p).sort((i,p)=>a[i]-a[p]),c=r[l[0]],u=0,h=-1;for(let i=1;i<l.length;i++){let p=a[l[i]]-c;if(p>u)u=p,h=i;c=Math.max(c,r[l[i]])}return[u,h,l]},e=(o)=>{if(o.length<=1)return[...o];let s=o.map((d)=>t[d]),[a,r,l]=n(s,1),[c,u,h]=n(s,0);if(r<0&&u<0)return on(s).map((d)=>o[d]);let[i,p]=a>=c?[r,l]:[u,h];return e(p.slice(0,i).map((d)=>o[d])).concat(e(p.slice(i).map((d)=>o[d])))};return e(t.map((o,s)=>s))}function sn(t,n=1){if(!t.length)return[];let e=H(t.map((a)=>a[3]-a[1])),o=(a,r)=>{let l=a.map((d)=>r?d[1]:d[0]),c=a.map((d)=>r?d[3]:d[2]),u=l.map((d,m)=>m).sort((d,m)=>l[d]-l[m]),h=c[u[0]],i=0,p=-1;for(let d=1;d<u.length;d++){let m=l[u[d]]-h;if(m>i)i=m,p=d;h=Math.max(h,c[u[d]])}return[i,p,u]},s=(a)=>{let r=a.map((l)=>t[l]);if(a.length>1){let[l,c,u]=o(r,1),[h,i,p]=o(r,0),[d,m,f]=l>=h?[l,c,u]:[h,i,p];if(m>=0&&d>=n*e)return s(f.slice(0,m).map((g)=>a[g])).concat(s(f.slice(m).map((g)=>a[g])))}return[gt(r).map((l)=>a[l])]};return s(t.map((a,r)=>r))}function an(t,n,e,o){let s=e.length;if(s<=1)return Array.from({length:s},(u,h)=>h);let a=et(n,s),r=a.map((u)=>{let h=!1;if(o){let d=new Float64Array(F.length);for(let f of u)for(let g=0;g<F.length;g++)d[g]+=o[f*F.length+g];let m=0;for(let f=1;f<F.length;f++)if(d[f]>d[m])m=f;h=St.has(m)}let i=u.map((d)=>e[d]);return(h?on(i):gt(i)).map((d)=>u[d])}),l=a.length,c=new Float64Array(l*l);for(let u=0;u<l;u++)for(let h=u+1;h<l;h++){let i=0;for(let d of a[u])for(let m of a[h])i+=t[d*s+m];let p=i/(a[u].length*a[h].length);c[u*l+h]=p,c[h*l+u]=-p}return Ct(c,l).flatMap((u)=>r[u])}async function Yn(t){let n=new DecompressionStream("deflate-raw"),e=new Blob([t]).stream().pipeThrough(n);return new Uint8Array(await new Response(e).arrayBuffer())}async function Zn(t){let n=new DataView(t.buffer,t.byteOffset,t.byteLength);if(n.getUint32(0,!0)!==67324752)throw Error("lm file is not an .npz (zip) archive");let e=-1;for(let r=t.length-22;r>=Math.max(0,t.length-22-65536);r--)if(n.getUint32(r,!0)===101010256){e=r;break}if(e<0)throw Error("lm file: no zip central directory");let o=n.getUint16(e+10,!0),s=n.getUint32(e+16,!0),a=new Map;for(let r=0;r<o;r++){if(n.getUint32(s,!0)!==33639248)throw Error("lm file: bad zip entry");let l=n.getUint16(s+10,!0),c=n.getUint32(s+20,!0);if(c===4294967295)throw Error("lm file: zip64 is not supported");let u=n.getUint16(s+28,!0),h=n.getUint16(s+30,!0),i=n.getUint16(s+32,!0),p=n.getUint32(s+42,!0),d=new TextDecoder().decode(t.subarray(s+46,s+46+u)),m=n.getUint16(p+26,!0),f=n.getUint16(p+28,!0),g=p+30+m+f,x=t.slice(g,g+c);a.set(d.replace(/\.npy$/,""),l===0?x:await Yn(x)),s+=46+u+h+i}return a}function Jn(t){let n=new DataView(t.buffer,t.byteOffset,t.byteLength);if(t[0]!==147||t[1]!==78)throw Error("lm table is not an .npy file");let e=t[6],o=8,s=e<3?n.getUint16(o,!0):n.getUint32(o,!0);o+=e<3?2:4;let a=new TextDecoder().decode(t.subarray(o,o+s)),r=/'descr':\s*'([^']+)'/.exec(a)[1],l=/'shape':\s*\(([^)]*)\)/.exec(a)[1].split(",").map((h)=>parseInt(h,10)).filter((h)=>!Number.isNaN(h));o+=s;let c=l.reduce((h,i)=>h*i,1);if(r==="|u1")return{descr:r,shape:l,data:t.slice(o)};let u=t.slice(o);if(r==="<u8")return{descr:r,shape:l,data:new BigUint64Array(u.buffer,0,c)};if(r==="<f4")return{descr:r,shape:l,data:new Float32Array(u.buffer,0,c)};if(r==="<i8")return{descr:r,shape:l,data:new BigInt64Array(u.buffer,0,c)};throw Error(`lm table: unsupported dtype ${r}`)}class W{static async load(t){let n=await Zn(t),e=(i)=>Jn(n.get(i)).data,o=new W;if(o.order=Number(e("order")[0]),o.vocabSize=Number(e("vocab_size")[0]),o.bits=n.has("bits")?Number(e("bits")[0]):11,o.order*o.bits>61)throw Error(`order ${o.order} x ${o.bits} bits do not fit a key`);let s=(i)=>{let p=0;for(let x of i)p+=x.length;let d=new Float64Array(p),m=new Float64Array(p),f=new Float64Array(p),g=0;for(let x of i){for(let A=0;A<x.length;A++)d[g+A]=Number(x[A]>>26n),m[g+A]=Number(x[A]&0x3ffffffn);g+=x.length}return{hi:d,lo:m}},a=(i)=>BigInt(i)<<BigInt(61),r=[],l=[],c=[],u=[];for(let i=0;i<o.order;i++)r.push(e(`k${i}`)),l.push(e(`v${i}`)),c.push(e(`hk${i}`)),u.push(e(`hb${i}`));let h=(i,p,d)=>{let m=i.reduce((w,M)=>w+M.length,0),f=new Float64Array(m),g=new Float64Array(m),x=new Float64Array(m),A=0;for(let w=0;w<i.length;w++){let M=d(w),I=i[w];for(let _=0;_<I.length;_++){let k=I[_]|M;f[A]=Number(k>>26n),g[A]=Number(k&0x3ffffffn),x[A]=p[w][_],A++}}return{hi:f,lo:g,vs:x}};return o.p=h(r,l,a),o.h=h(c,u,a),o.hb=u,o.ikcache=new Map,o}get maxLogp(){if(this._maxLogp===void 0){let t=-Math.log(this.vocabSize);for(let e=0;e<this.p.vs.length;e++)if(this.p.vs[e]>t)t=this.p.vs[e];let n=0;for(let e of this.hb){let o=-1/0;for(let s=0;s<e.length;s++)if(e[s]>o)o=e[s];if(o>0)n+=o}this._maxLogp=t+n}return this._maxLogp}static#t(t,n,e){let o=0,s=t.hi.length;while(o<s){let a=o+s>>1,r=t.hi[a];if(r<n||r===n&&t.lo[a]<e)o=a+1;else s=a}return o}static#n(t,n,e,o){return n<t.hi.length&&t.hi[n]===e&&t.lo[n]===o}logpKeys(t){let n=Array(t.length),e=[];for(let c=0;c<t.length;c++){let u=this.ikcache.get(t[c]);if(u===void 0)e.push(c);else n[c]=u}if(!e.length)return n;let o=this.order,s=BigInt(this.bits),a=1n,r=Math.log(this.vocabSize),l=this.ikcache;for(let c of e){let u=t[c],h=u>>s,i=u&(a<<s)-a,p=(g)=>BigInt(g)<<BigInt(61),d=0;for(let g=1;g<o;g++){let x=h&(a<<s*BigInt(g))-a|p(g),A=Number(x>>26n),w=Number(x&0x3ffffffn),M=W.#t(this.h,A,w);if(!W.#n(this.h,M,A,w))break;d=g}let m=0,f=null;for(let g=d;g>=0;g--){let x=h&(a<<s*BigInt(g))-a,A=x<<s|i|p(g),w=Number(A>>26n),M=Number(A&0x3ffffffn),I=W.#t(this.p,w,M);if(W.#n(this.p,I,w,M)){f=m+this.p.vs[I];break}let _=x|p(g),k=W.#t(this.h,Number(_>>26n),Number(_&0x3ffffffn));m+=W.#n(this.h,k,Number(_>>26n),Number(_&0x3ffffffn))?this.h.vs[k]:0}if(f===null)f=m-r;if(n[c]=f,l.size<1e6)l.set(u,f)}return n}}var K=(t,n)=>{if(t===-1/0)return n;if(n===-1/0)return t;let e=t>n?t:n;return e+Math.log(Math.exp(t-e)+Math.exp(n-e))};function rn(t,n,e,o,s=0.5,a=1.5,r=8,l=0.999,c=0.001){let u=BigInt(o.bits),h=(1n<<u*BigInt(o.order-1))-1n,i=e.length/t.length,p=new Map([["",[0,-1/0,0,[],0n]]]),d=Math.log(l),m=Math.log(c),f=t.length;for(let w=0;w<f;w++){let M=t[w];if(M>d){p=new Map([...p].map(([y,v])=>[y,[K(v[0],v[1])+M,-1/0,v[2],v[3],v[4]]]));continue}let I=new Map,_=(y,v,T,j,B,P)=>{let N=I.get(y);if(N===void 0)I.set(y,[v,T,j,B,P]);else I.set(y,[K(N[0],v),K(N[1],T),N[2],N[3],N[4]])},k=[];for(let y=0;y<i;y++){let v=Number(e[w*i+y]),T=n[w*i+y];if(v!==0&&T>=m)k.push([v,T])}for(let[y,v]of p){let T=y.length?Number(y.slice(y.lastIndexOf(",")+1)):null;_(y,K(v[0],v[1])+M,-1/0,v[2],v[3],v[4]);for(let[j,B]of k)if(j===T)_(y,-1/0,v[1]+B,v[2],v[3],v[4])}let E=p.size>=r?Math.min(...[...I.values()].map((y)=>K(y[0],y[1])+y[2])):-1/0,S=a+s*o.maxLogp,L=[];for(let[y,v]of p){let T=K(v[0],v[1]),j=y.length?Number(y.slice(y.lastIndexOf(",")+1)):null;for(let[B,P]of k){let N=B===j?v[0]+P:T+P;if(N===-1/0)continue;let U=y?`${y},${B}`:`${B}`;if(I.has(U))_(U,-1/0,N,v[2],v[3],v[4]);else if(N+v[2]+S>=E-0.000000001)L.push([y,B,N,v[2],v[3],v[4]])}}let R=L.length?o.logpKeys(L.map((y)=>y[5]<<u|BigInt(y[1]+1))):[];L.forEach(([y,v,T,j,B,P],N)=>{_(y?`${y},${v}`:`${v}`,-1/0,T,j+s*R[N]+a,[...B,w],(P<<u|BigInt(v+1))&h)}),p=new Map([...I].sort((y,v)=>K(v[1][0],v[1][1])+v[1][2]-(K(y[1][0],y[1][1])+y[1][2])).slice(0,r))}let g=null,x=-1/0;for(let[w,M]of p){let I=K(M[0],M[1])+M[2];if(I>x)x=I,g=w}return[g?g.split(",").filter(Boolean).map(Number):[],g?p.get(g)[3]:[]]}var Qn=0.5,$t=0.9,te=0.1,ne=1,ee=2,oe=4,se=["mathcls.onnx","math_enc.onnx","math_dec_init.onnx","math_dec_step.onnx"];function ae(t){let n=[],e=[],o=!1;for(let s of t)if(s==="$"){if(o)n.push("$"+e.join(" ")+"$"),e.length=0;o=!o}else if(o)e.push(s);else n.push(s);if(o&&e.length)n.push("$"+e.join(" ")+"$");return n.join("").split(/\s+/).filter(Boolean).join(" ")}var wt=(t,n)=>{if(!t.inputNames)return n;let e={};for(let o in n)if(t.inputNames.includes(o))e[o]=n[o];return e};class xt{constructor(t,n,e){this.Tensor=t,this.cls=n.cls,this.enc=n.enc,this.init=n.init,this.step=n.step;let o=e.config;this.height=o.height,this.maxTokens=o.max_tokens,this.layers=o.dec_layers,this.heads=o.dec_heads,this.headDim=o.channels/o.dec_heads,this.itos=["","","","",...e.tokens]}static async load(t){let n;try{n=await t.json("math_meta.json")}catch{return null}let e={};for(let l of se)e[l]=await t.session(l);let{"mathcls.onnx":o,"math_enc.onnx":s,"math_dec_init.onnx":a,"math_dec_step.onnx":r}=e;return new xt(t.Tensor,{cls:o,enc:s,init:a,step:r},n)}async flags(t,n=64){let e=Array(t.length).fill(!1);for(let o of nt(t.map((s)=>s.w),n)){let s=o.map((h)=>t[h]),{arr:a,w:r,widths:l}=tt(s),c=s[0].ink.length/s[0].w,{p:u}=await this.cls.run(wt(this.cls,{x:new this.Tensor("float32",a,[o.length,1,c,r]),widths:new this.Tensor("int64",l,[o.length])}));o.forEach((h,i)=>e[h]=u.data[i]>Qn)}return e}async read(t,n,e=64){let o=n.map((a)=>rt(t,a,this.height)),s=o.map(()=>({text:"",conf:0}));for(let a of nt(o.map((r)=>r.w),e,4,64,655360)){let r=a.map((_)=>o[_]),{arr:l,w:c,widths:u}=tt(r),h=a.length,i=this.Tensor,{memory:p,mask:d}=await this.enc.run(wt(this.enc,{x:new i("float32",l,[h,1,this.height,c]),widths:new i("int64",u,[h])})),{cross_k:m,cross_v:f}=await this.init.run(wt(this.init,{memory:p})),g={data:new Float32Array(0),dims:[this.layers,h,this.heads,0,this.headDim]},x={data:new Float32Array(0),dims:g.dims},A=new BigInt64Array(h).fill(BigInt(ne)),w=Array.from({length:h},()=>[]),M=Array.from({length:h},()=>[]),I=new Uint8Array(h);for(let _=0;_<this.maxTokens-1;_++){let k=await this.step.run(wt(this.step,{token:new i("int64",A,[h]),pos:new i("int64",BigInt64Array.from([BigInt(_)]),[]),self_k:new i("float32",g.data,g.dims),self_v:new i("float32",x.data,x.dims),cross_k:m,cross_v:f,mask:d})),{logits:E,new_k:S,new_v:L}=k;g={data:S.data,dims:S.dims},x={data:L.data,dims:L.dims};let R=E.dims[1];for(let y=0;y<h;y++){let v=y*R,T=-1/0,j=0;for(let N=0;N<R;N++){let U=E.data[v+N];if(U>T)T=U,j=N}if(A[y]=BigInt(j),I[y])continue;let B=0;for(let N=0;N<R;N++)B+=Math.exp(E.data[v+N]-T);let P=1/B;if(M[y].push(P),j===ee)I[y]=!0;else if(M[y].length>=8&&M[y].reduce((N,U)=>N+U,0)/M[y].length<$t-te)I[y]=!0;else if(j>=oe)w[y].push(j)}if(I.every(Boolean))break}a.forEach((_,k)=>{s[_]={text:ae(w[k].map((E)=>this.itos[E])),conf:M[k].length?M[k].reduce((E,S)=>E+S,0)/M[k].length:0}})}return s}}var re=F.indexOf("Page-Header"),ie=F.indexOf("Page-Footer"),ce=F.indexOf("Section-Header"),le=F.indexOf("List-Group"),cn=F.indexOf("Table"),he=/^\$[^$]+\$$/,fe=/^\s*([•\-–▪◦*·→]|\(?\d{1,3}[.)]|\(?[a-zA-Z][.)])(\s|$)/,Mt=(t)=>z(t*10)/10,un=(t)=>t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">"),ln=(t)=>{let n=t.codePointAt(0);return n>=12288&&n<=12543||n>=13312&&n<=40959||n>=63744&&n<=64255||n>=65280&&n<=65519||n>=131072&&n<=195103},ue=(t)=>String.fromCodePoint(t.codePointAt(0)),pe=(t)=>t.length>1&&t.charCodeAt(t.length-1)>=56320&&t.charCodeAt(t.length-1)<=57343?t.slice(-2):t.slice(-1),hn=(t)=>/\p{L}/u.test(t),me=(t)=>/\p{Ll}/u.test(t);function Bt(t){let n="";for(let e of t){if(e=e.trim(),!e)continue;let o=ue(e);if(n.length>1&&n.endsWith("-")&&hn(n[n.length-2])&&hn(o)&&me(o))n=n.slice(0,-1)+e;else if(n&&ln(pe(n))&&ln(o))n+=e;else n=n?n+" "+e:e}return n}function pn(t,n,e){let o=[];for(let s of t){let a=o[o.length-1];if(a&&n[a[a.length-1]*e+s]>0)a.push(s);else o.push([s])}return o}var de=(t)=>t.reduce((n,e)=>n+e,0)/t.length;function ge(t){let n=t.map((s)=>(s[1]+s[3])/2),e=H(t.map((s)=>s[3]-s[1])),o=[];for(let s of n.map((a,r)=>r).sort((a,r)=>n[a]-n[r])){let a=o[o.length-1];if(a&&Math.abs(n[s]-de(a.map((r)=>n[r])))<0.5*e)a.push(s);else o.push([s])}return o.map((s)=>s.slice().sort((a,r)=>t[a][0]-t[r][0]))}function we(t,n){if(t.length<3)return t;let e=H(n.map((l)=>l[3]-l[1])),o=t.slice(0,-1).map((l,c)=>{let u=t[c+1];return Math.min(...u.map((h)=>n[h][1]))-Math.max(...l.map((h)=>n[h][3]))}),s=H(o),a=[t[0].slice()],r=t[0];for(let l=0;l<o.length;l++){let c=t[l+1];if(o[l]<0.5*s&&o[l]<0.6*e&&c.length<r.length)a[a.length-1].push(...c);else a.push(c.slice());r=c}return a}var fn=(t,n)=>{let e=0,o=t.length;while(e<o){let s=e+o>>1;if(t[s]>n)o=s;else e=s+1}return e};function Nt(t,n){let e=ge(t),o=Math.max(...t.map((h)=>h[2]))-Math.min(...t.map((h)=>h[0]));if(e.length>2&&e[0].length===1&&Math.max(...e.map((h)=>h.length))>1&&t[e[0][0]][2]-t[e[0][0]][0]>0.6*o){let h=t.map((i,p)=>p).filter((i)=>i!==e[0][0]);return n[e[0][0]]+` | |
| `+Nt(h.map((i)=>t[i]),h.map((i)=>n[i]))}let s=Math.max(...e.map((h)=>h.length)),a=e.filter((h)=>h.length===s),r=we(e,t),l=Array.from({length:s},(h,i)=>[H(a.map((p)=>t[p[i]][0])),H(a.map((p)=>t[p[i]][2]))]),c=[-1/0];for(let h=0;h<s-1;h++)c.push((l[h][1]+l[h+1][0])/2);c.push(1/0);let u=["<table>"];for(let h=0;h<r.length;h++){let i=h===0&&r.length>1?"th":"td",p=new Map;for(let f of r[h].slice().sort((g,x)=>t[g][1]-t[x][1]||t[g][0]-t[x][0])){let[g,,x]=t[f],A=fn(c,g)-1,w=fn(c,x)-1,M=[];for(let k=Math.max(A,0);k<=Math.min(w,s-1);k++)if(Math.min(x,l[k][1])-Math.max(g,l[k][0])>0.5*(l[k][1]-l[k][0]))M.push(k);if(!M.length)M=[Math.min(Math.max(A,0),s-1)];let I=M[0],_=p.get(I);p.set(I,{n:_?Math.max(_.n,M.length):M.length,text:_?_.text+" "+n[f]:n[f]})}let d=[],m=0;while(m<s){let f=p.get(m);if(f)d.push(`<${i}${f.n>1?` colspan="${f.n}"`:""}>${un(f.text)}</${i}>`),m+=f.n;else d.push(`<${i}></${i}>`),m+=1}u.push("<tr>"+d.join("")+"</tr>")}return u.push("</table>"),u.join(` | |
| `)}function xe(t,n,e){let o=et(n.subarray(2*e*e),e),s=0;for(let a of o){if(a.length<2)continue;let r=1/0;for(let l of a)for(let c of a)r=Math.min(r,Math.min(t[l][2],t[c][2])-Math.max(t[l][0],t[c][0]));if(r<0)s++}return s<=0.1*o.length}function Me(t,n){let e=Math.round(Math.sqrt(t.length/3)),o=n.length,s=new Float32Array(3*o*o);for(let a=0;a<3;a++)for(let r=0;r<o;r++)for(let l=0;l<o;l++)s[a*o*o+r*o+l]=t[a*e*e+n[r]*e+n[l]];return s}function ye(t,n,e,o){let s=et(e.subarray(2*o*o),o),a=s.map((w)=>[Math.min(...w.map((M)=>t[M][0])),Math.min(...w.map((M)=>t[M][1])),Math.max(...w.map((M)=>t[M][2])),Math.max(...w.map((M)=>t[M][3]))]),r=s.map((w)=>w.slice().sort((M,I)=>t[M][1]-t[I][1]||t[M][0]-t[I][0]).map((M)=>n[M]).join(" ")),l=s.length,c=(w)=>{let M=new Float32Array(l*l);for(let I=0;I<l;I++)for(let _=I+1;_<l;_++){let k=0;for(let S of s[I])for(let L of s[_])k+=e[w*o*o+S*o+L];let E=k/(s[I].length*s[_].length);M[I*l+_]=M[_*l+I]=E}return M},u=(w)=>Math.min(...w.map((M)=>a[M][1])),h=(w)=>Math.min(...w.map((M)=>a[M][0])),i=et(c(0),l).sort((w,M)=>u(w)-u(M)),p=et(c(1),l).sort((w,M)=>h(w)-h(M)),d=new Map;i.forEach((w,M)=>w.forEach((I)=>d.set(I,M)));let m=new Map;p.forEach((w,M)=>w.forEach((I)=>m.set(I,M)));let f=p.map((w)=>H(w.map((M)=>(a[M][0]+a[M][2])/2))),g=H(t.map((w)=>w[3]-w[1])),x=new Map;for(let w=0;w<l;w++){let M=m.get(w),I=1;while(M+I<p.length&&a[w][0]-g<=f[M+I]&&f[M+I]<=a[w][2]+g)I++;let _=d.get(w)*p.length+M,k=x.get(_);x.set(_,{span:k?Math.max(k.span,I):I,text:k?k.text+" "+r[w]:r[w]})}let A=["<table>"];for(let w=0;w<i.length;w++){let M=w===0&&i.length>1?"th":"td",I=[],_=0;while(_<p.length){let k=x.get(w*p.length+_);if(k)I.push(`<${M}${k.span>1?` colspan="${k.span}"`:""}>${un(k.text)}</${M}>`),_+=k.span;else I.push(`<${M}></${M}>`),_+=1}A.push("<tr>"+I.join("")+"</tr>")}return A.push("</table>"),A.join(` | |
| `)}function mn(t,n,e){let o=t.length/e,s=0,a=-1/0;for(let r=0;r<o;r++){let l=0;for(let c of n)l+=t[c*o+r];if(l>a)a=l,s=r}return s}function dn(t,n,e,o,s){let a=t.length;return pn(e,s,a).map((r)=>{let l=r.map((c)=>t[c]);return{label:F[mn(o,r,a)],bbox:[Mt(Math.min(...l.map((c)=>c[0]))),Mt(Math.min(...l.map((c)=>c[1]))),Mt(Math.max(...l.map((c)=>c[2]))),Mt(Math.max(...l.map((c)=>c[3])))],text:r.map((c)=>n[c]).join(` | |
| `)}})}function gn(t,n,e,o,s,a={}){let{keepFurniture:r=!1,cells:l=null,tab:c=null,pictures:u=null}=a,h=t.length,i=[],p=[];for(let d of pn(e,s,h)){let m=i.length;if(Ae(i,d,t,n,o,r,l,c,h),i.length>m){let f=d.map((g)=>t[g]);p.push([Math.min(...f.map((g)=>g[0])),Math.min(...f.map((g)=>g[1])),Math.max(...f.map((g)=>g[2])),Math.max(...f.map((g)=>g[3]))])}}if(u&&u.length){let d=new Map;u.forEach(([f,g,x,A],w)=>{let M=(g+A)/2,I=p.findIndex(([k,E,S,L])=>(E+L)/2>M&&Math.min(x,S)>Math.max(f,k)),_=I===-1?i.length:I;if(!d.has(_))d.set(_,[]);d.get(_).push(``)});let m=[];for(let f=0;f<=i.length;f++)if(m.push(...d.get(f)??[]),f<i.length)m.push(i[f]);i.length=0,i.push(...m)}return i.filter(Boolean).join(` | |
| `)}function Ae(t,n,e,o,s,a,r,l,c){let u=mn(s,n,c),h=n.map((i)=>o[i]);if((u===re||u===ie)&&!a)return;if(h.every((i)=>he.test(i.trim()))){t.push(h.map((i)=>"$$"+i.trim().slice(1,-1)+"$$").join(` | |
| `));return}if(u===ce)t.push("## "+Bt(h));else if(u===cn&&n.length>1&&l){let i=n.map((d)=>e[d]),p=Me(l,n);if(xe(i,p,n.length))t.push(ye(i,h,p,n.length));else{let d=[],m=[];for(let f of n)for(let[g,x]of r?.get(f)||[[e[f],o[f]]])d.push(g),m.push(x);t.push(Nt(d,m))}}else if(u===cn&&n.length>1){let i=[],p=[];for(let d of n)for(let[m,f]of r?.get(d)||[[e[d],o[d]]])i.push(m),p.push(f);t.push(Nt(i,p))}else if(u===le){let i=[];for(let p of h)if(!i.length||fe.test(p))i.push([p]);else i[i.length-1].push(p);t.push(i.map(Bt).join(` | |
| `))}else t.push(Bt(h))}class ct{constructor(t){this.chars=t,this.index=new Map(t.map((n,e)=>[n,e+1])),this.hasCommands=t.some((n)=>n.length>1),this.jamo=this.index.has("ᄀ")}static fromMeta(t){if(typeof t==="string")t=JSON.parse(t);if(t.blank!==0)throw Error("only blank=0 vocabularies are supported");return new ct(t.chars)}decode(t){let n=[];for(let o of t)if(o>0&&o<=this.chars.length)n.push(this.chars[o-1]);if(this.jamo)return n.join("").normalize("NFC");if(!this.hasCommands)return n.join("");let e="";for(let o=0;o<n.length;o++){let s=n[o],a=n[o+1]??"";if(e+=s,s.length>2&&s[0]==="\\"&&/^\p{L}+$/u.test(s.slice(1))&&/^\p{L}/u.test(a))e+=" "}return e}}var be=new Map([..."()[]{}<>«»‹›"].map((t,n)=>[t,")(][}{><»«›‹"[n]]));var Mn=(t)=>it(t),Rt=(t)=>{for(let n of t)if(it(n))return!0;return!1},ve=(t)=>{let n=0,e=0;for(let o of t)if(it(o))n++;else if(Zt(o))e++;return n>e};function Ie(t){let n=[];for(let e of t)if(n.length&&tn(e))n[n.length-1]+=e;else n.push(e);return n}var ke=(t)=>it(t)?"R":Jt(t)?"L":"N";function wn(t,n,e){let o=[],s=null,a=null;for(let r=0;r<=t.length;r++){let l=r<t.length?t[r]:"#";if(l===n){if(s===null)s=r;a=r}else if(e.has(l)||l==="#"){if(s!==null)o.push([s,a+1]),s=a=null}}return o}var xn=(t)=>{let n=String.fromCodePoint(t.codePointAt(0));return(be.get(n)??n)+t.slice(n.length)};function yn(t,n=null,e=!0){if(!Rt(t))return t;let o=Ie(t),s=o.map(ke);if(n===null?ve(t):n){let l=[],c=[...wn(s,"L",new Set(["R"])),[o.length,o.length]],u=0;for(let[h,i]of c){for(;u<h;u++)l.push(e?xn(o[u]):o[u]);if(h<i)l.push(o.slice(h,i).join(""));u=i}return l.reverse().join("")}let r=o.slice();for(let[l,c]of wn(s,"R",new Set(["L"]))){let u=o.slice(l,c).reverse();for(let h=0;h<u.length;h++)r[l+h]=e?xn(u[h]):u[h]}return r.join("")}var _e=9,Ee=12,Te=2048,Le=768,Se=0.95,yt=4,An=0.5,Ce=3,bn=1.8,$e=1.6,Be=3,Ne=0.9,Re=0.5,Oe=1.5,je=8,De=0.003,ze=["base","cjk","rtl","other"],Fe=0.95;var Pe=0.004,vn=45,Ot=0.3,Ue=1600,V=(t)=>z(t*10)/10,He=(t)=>z(t*100)/100,Ge=(t)=>z(t*1e4)/1e4,We=new Set(".·…‥_-–—="),Ve=new Set("-–—"),Ke=new Set("•◦▪▫●○■□‣⁃∙*"),Xe=/^(?:\d{1,3}|[A-Za-z])[.)]$|^\((?:\d{1,3}|[A-Za-z]|[ivxlcIVXLC]{1,5})\)$/,qe=2.5;function Ye(t,n,e=!1,o=null){let s=n.map(()=>!0),a=t.map((c)=>c[3]-c[1]),r=t.map((c)=>c[2]-c[0]);for(let c=0;c<n.length;c++){let u=n[c].text.replace(/ /g,"");if(u&&[...u].every((h)=>We.has(h))){let h=r[c]>=3*a[c],i=[...u].some((p)=>Ve.has(p));if(u.length>=3||h||!i)s[c]=!1}}for(let c=0;c<n.length;c++){let u=n[c].text.trim();if(!s[c]||!(Ke.has(u)||Xe.test(u)))continue;let h=null,i=null;for(let f=0;f<n.length;f++){if(f===c||!s[f]||!n[f].text.trim())continue;if(Math.min(t[c][3],t[f][3])-Math.max(t[c][1],t[f][1])<0.5*Math.min(a[c],a[f]))continue;let x=e?t[c][0]-t[f][2]:t[f][0]-t[c][2];if(-0.3*a[c]<=x&&x<=qe*Math.max(a[c],a[f])&&(i===null||x<i))h=f,i=x}if(h===null)continue;let p=n[h].toks,d=p.length>0&&typeof p[0][0]==="string",m=d?" ":o;if(!e&&n[c].toks.length&&p.length&&m!==null){if(typeof n[c].toks[0][0]==="string"===d){let f=(t[c][2]+t[h][0])/2;n[h].toks=[...n[c].toks,[m,f,...n[c].toks[n[c].toks.length-1].slice(2)],...p]}}n[h]={text:`${u} ${n[h].text.trim()}`,conf:n[h].conf,toks:n[h].toks},t[h]=[Math.min(t[c][0],t[h][0]),Math.min(t[c][1],t[h][1]),Math.max(t[c][2],t[h][2]),Math.max(t[c][3],t[h][3])],s[c]=!1}let l=[];for(let c=0;c<s.length;c++)if(s[c])l.push(c);return{boxes:l.map((c)=>t[c]),rec:l.map((c)=>n[c])}}class At{static async create(t,n={}){let e=new At;e.Tensor=t.Tensor,e.tableHeads=n.tableHeads??!1,e.furniture=n.furniture??!1;let o=await t.meta();if(e.vocab=ct.fromMeta(o.vocab),e.longSide=o.det_long_side,e.height=o.rec_height,e.window=o.rec_window??1,e.det=await t.session("det.onnx"),e.rec=await t.session(n.recInt8?"rec.int8.onnx":"rec.onnx"),e.#t=null,e.order=e.orient=null,n.order!==!1)try{e.order=await t.session("order.onnx")}catch{}if(n.orient!==!1)try{e.orient=await t.session("orient.onnx")}catch{}if(e.script=null,e.groups=new Map,e.scriptGroups=o.script_groups??ze,n.cjk!==!1){for(let s of Object.keys(o)){if(!s.startsWith("vocab_"))continue;let a=s.slice(6);try{let r=await t.session(`rec_${a}.onnx`);e.groups.set(a,{sess:r,vocab:ct.fromMeta(o[s])})}catch{}}if(e.groups.size)try{e.script=await t.session("script.onnx")}catch{e.script=null}}if(e.lms=new Map,n.lm!==!1){let s=o.lm_params??{},a=(l)=>!!l.outputNames&&["blank","topv","topi"].every((c)=>l.outputNames.includes(c)),r=async(l,c)=>{if(!a(c))return;try{let u=await W.load(await t.bytes(l==="base"?"lm.npz":`lm_${l}.npz`)),h=s[l]??{};e.lms.set(c,{lm:u,alpha:h.alpha??Re,beta:h.beta??Oe,gate:h.gate??Ne})}catch{}};await r("base",e.rec);for(let l of e.groups.keys()){let c=e.groups.get(l);if(c)await r(l,c.sess)}}if(e.math=null,n.math){if(t.json)e.math=await xt.load(t);if(!e.math)throw Error("math: true needs the math models (mathcls.onnx, math_enc.onnx, math_dec_init.onnx, math_dec_step.onnx, math_meta.json)")}return e.adaptive=n.adaptive??!0,e._pageRtl=!1,e}#t;async#n(t,n){let{x:e,ph:o,pw:s,scale:a}=Xt(t,n),{out:r}=await this.det.run({x:new this.Tensor("float32",e,[1,1,o,s])}),[,l,c,u]=r.dims;return this.#t=l>3?{logit:r.data.subarray(3*c*u,4*c*u),w:u,h:c,scale:a}:null,{boxes:qt(r.data,c,u),scale:a}}async detect(t){let{boxes:n,scale:e}=await this.#n(t,this.longSide);if(this.adaptive&&n.length>=8){let o=H(n.map((s)=>s[3]-s[1]));if(o<_e){let s=Math.floor(Math.min(Te,this.longSide*Ee/Math.max(o,1)));if(s=Math.floor(s/32)*32,s>this.longSide)({boxes:n,scale:e}=await this.#n(t,s))}}for(let o of n)o[0]=G(o[0]/e,0,t.width),o[2]=G(o[2]/e,0,t.width),o[1]=G(o[1]/e,0,t.height),o[3]=G(o[3]/e,0,t.height);return n}async detectAll(t){this.#t=null;let n=await this.detect(t),e=this.#t;if(!e)return[n,[]];return[n,Ze(e.logit,e.w,e.h,e.scale,t.width,t.height)]}async#f(t,n=64){let e=t.map(()=>"base"),o=t.map(()=>!0);for(let s of nt(t.map((a)=>a.w),n)){let{arr:a,w:r,widths:l}=tt(s.map((i)=>t[i])),{p:c}=await this.script.run({x:new this.Tensor("float32",a,[s.length,1,this.height,r]),widths:new this.Tensor("int64",l,[s.length])}),u=c.dims.length>1,h=u?c.dims[1]:1;s.forEach((i,p)=>{let d,m;if(!u){let f=c.data[p];d=f>0.5?"cjk":"base",m=Math.max(f,1-f)}else{let f=0;for(let g=1;g<h;g++)if(c.data[p*h+g]>c.data[p*h+f])f=g;d=this.scriptGroups[f],m=c.data[p*h+f]}e[i]=this.groups.has(d)?d:"base",o[i]=m>Fe})}return{names:e,sure:o}}async recognize(t,n,e=64,o=!1){let s=this.window,a=s>1?n.map((i)=>Tt(t,i)):null,r=n.map((i,p)=>rt(t,i,this.height,4096,1,a?a[p]:null)),l=s>1?n.map((i,p)=>rt(t,i,this.height,4096,s,a?a[p]:null)):r,c=r.map(()=>({text:"",conf:0,toks:[]})),u;if(o&&this.groups.has("cjk")){let{sess:i,vocab:p}=this.groups.get("cjk");u=[[i,p,r.map((d,m)=>m),!0]]}else if(this.script&&r.length){let{names:i,sure:p}=await this.#f(r,e);u=[[this.rec,this.vocab,i.flatMap((d,m)=>d==="base"||!p[m]?[m]:[]),!1]].concat([...this.groups].map(([d,{sess:m,vocab:f}])=>[m,f,i.flatMap((g,x)=>g===d?[x]:[]),!0]))}else u=[[this.rec,this.vocab,r.map((i,p)=>p),!1]];let h=new Float64Array(r.length).fill(-1);for(let[i,p,d,m]of u){if(!d.length)continue;for(let f of nt(d.map((g)=>r[g].w),e)){let g=f.map((T)=>d[T]),x=l===r||i!==this.rec?r:l,{arr:A,w,widths:M}=tt(g.map((T)=>x[T])),I=this.lms.get(i),_=A.length/(f.length*w),k=await i.run({x:new this.Tensor("float32",A,[f.length,1,_,w]),widths:new this.Tensor("int64",M,[f.length])}),{ids:E,conf:S,lengths:L}=k,R=I&&k.blank&&k.topv&&k.topi,y=E.dims[1],v=w/Math.max(y,1);for(let T=0;T<g.length;T++){let j=g[T],B=Number(L.data[T]),P=Array.from({length:B},(D,Q)=>Number(E.data[T*y+Q])),N=[];for(let D=0;D<B;D++)if(P[D]!==0&&(D===0||P[D]!==P[D-1]))N.push(D);let[U,Cn,$n,Bn]=n[j],Ht=Math.max(1,Bn-Cn),Gt=Math.floor(U-at*Ht),Nn=(Math.ceil($n+at*Ht)-Gt)/r[j].w,ft=N.map((D)=>P[D]),Wt=N,J=N.length?N.reduce((D,Q)=>D+S.data[T*y+Q],0)/N.length:0;if(R&&J>=An&&J<I.gate){let D=k.topv.dims[2];[ft,Wt]=rn(k.blank.data.subarray(T*y,T*y+B),k.topv.data.subarray(T*y*D,T*y*D+B*D),k.topi.data.subarray(T*y*D,T*y*D+B*D),I.lm,I.alpha,I.beta,je,0.999,De)}let Vt=Wt.map((D)=>Gt+(D+0.5)*v*Nn),Rn=m?ft.map((D,Q)=>[p.chars[D-1],Vt[Q]]):ft.map((D,Q)=>[D,Vt[Q]]);if(J<=h[j])continue;h[j]=J,c[j]=J>=An?{text:p.decode(ft),conf:J,toks:Rn}:{text:"",conf:J,toks:[]}}}}if(this.math&&r.length){let p=(await this.math.flags(r)).flatMap((d,m)=>d?[m]:[]);if(p.length){let d=await this.math.read(t,p.map((m)=>n[m]));p.forEach((m,f)=>{let{text:g,conf:x}=d[f];if(g.includes("$")&&x>=$t){let A=(n[m][0]+n[m][2])/2;c[m]={text:g,conf:1,toks:[[g,A]]}}})}}return c}async readingOrder(t,n,e){if(t.length<2||!this.order)return{order:t.length?gt(t):[],layout:null};let o=en(t,e[0],e[1],n),s=t.length,a=await this.order.run({feats:new this.Tensor("float32",o,[1,s,16]),mask:new this.Tensor("bool",new Uint8Array(s).fill(1),[1,s])}),r=a.a.data,l=a.cls?a.cls.data:null;if(a.same)return{order:an(r,a.same.data,t,l),layout:{cls:l,same:a.same.data,tab:this.tableHeads&&a.tab?a.tab.data:null,classes:F}};return{order:Ct(r,s),layout:null}}#e(t){return t===" "||t===this.vocab.index.get(" ")}#i(t){if(!t.length)return"";if(typeof t[0][0]==="string"){let n=t.map((e)=>e[0]).join("").split(/\s+/).filter(Boolean).join(" ");if(t[0].length===2){if(n=n.normalize("NFC"),Rt(n))n=yn(n,this._pageRtl||null)}return n}return this.vocab.decode(t.map((n)=>n[0]))}#u(t,n){let e=t.map((s)=>s[0]),o=t.map((s)=>s[2]);return t.map((s,a)=>{let r=Math.max(1,s[3]-s[1]),l=n[a].toks.filter((u)=>!this.#e(u[0])).map((u)=>u[1]),c=[];for(let u=1;u<l.length;u++){let h=l[u-1],i=l[u];if(i-h<=$e*r)continue;let p=h+0.35*r,d=i-0.35*r,m=0;for(let f=0;f<t.length;f++){if(f===a)continue;if(Math.abs(o[f]-p)<0.5*r||Math.abs(e[f]-d)<0.5*r)m++}if(m>=Be)c.push((h+i)/2)}return c})}#o(t,n,e,o=[]){let s=Math.max(1,t[3]-t[1]),a=[[]],r=null,l=[...o].sort((u,h)=>u-h);for(let u of n){let h=u[1];if(!this.#e(u[0])){if(r!==null&&(h-r>e*s||l.some((p)=>r<p&&p<h)))a.push([]);r=h}a[a.length-1].push(u)}if(a.length===1)return[[t.slice(0,4),this.#i(n),n]];let c=[];for(let u of a){let h=this.#i(u).trim(),i=u.filter((p)=>!this.#e(p[0])).map((p)=>p[1]);if(h&&i.length)c.push([[Math.max(t[0],Math.min(...i)-0.35*s),t[1],Math.min(t[2],Math.max(...i)+0.35*s),t[3]],h,u])}return c}async#p(t,n,e,o,s=!1,a=null){let r=Date.now(),l=0,c=0;for(let E of e)for(let S of E.text)if(/\p{L}/u.test(S)){if(c++,Mn(S))l++}this._pageRtl=l>c-l;let u=t[0],h=(E)=>this._pageRtl?[u-E[2],E[1],u-E[0],E[3]]:E,i=[],p=[],d=n.length?this.#u(n,e):[];n.forEach((E,S)=>{for(let[L,R,y]of this.#o(E,e[S].toks,Ce,d[S]))i.push(L),p.push({text:R,conf:e[S].conf,toks:y})});let m=Ye(i,p,this._pageRtl,this.vocab.index.get(" ")??null),{boxes:f,rec:g}=m,x=g.map((E)=>E.text),{order:A,layout:w}=await this.readingOrder(f.map(h),x,t);if(s&&w){let E=sn(f);A=E.flat();let S=f.length,L=new Float32Array(S*S).fill(-1);for(let v of E)for(let T of v)for(let j of v)L[T*S+j]=1;let R=w.cls.length/S,y=w.cls.slice();for(let v of St)for(let T=0;T<S;T++)y[T*R+v]=-1e4;w={...w,cls:y,same:L,tab:null}}else if(w&&w.tab){let E=F.indexOf("Table"),S=f.map((L,R)=>{let y=0;for(let v=1;v<F.length;v++)if(w.cls[R*F.length+v]>w.cls[R*F.length+y])y=v;return y===E});if(S.some(Boolean)){let L=[],R=[];if(f.forEach((y,v)=>{let T=S[v]?this.#o(y,g[v].toks,bn):[[y,g[v].text,g[v].toks]];for(let[j,B,P]of T)L.push(j),R.push({text:B,conf:g[v].conf,toks:P})}),L.length!==f.length)f=L,g=R,x=g.map((y)=>y.text),{order:A,layout:w}=await this.readingOrder(f.map(h),x,t)}}o.orderMs=Date.now()-r,o.totalMs=o.orientMs+o.detMs+o.recMs+o.orderMs;let M=s?"order=xycut":"order=model",I=A.map((E)=>({box:f[E].slice(0,4).map(V),text:g[E].text,conf:Ge(g[E].conf)})),_=null,k=[];if(w){k=dn(f.map(h),x,A,w.cls,w.same);for(let L of k)L.bbox=h(L.bbox).map(V);let E=new Map(f.map((L,R)=>[R,this.#o(L,g[R].toks,bn).map(([y,v])=>[h(y),v])])),S=a&&a.length;if(_=gn(f.map(h),x,A,w.cls,w.same,{keepFurniture:this.furniture,cells:E,tab:this.tableHeads?w.tab:null,pictures:S?a.map(h):null}),S)for(let L of a){let R=(L[1]+L[3])/2,y=k.findIndex((v)=>(v.bbox[1]+v.bbox[3])/2>R&&Math.min(L[2],v.bbox[2])>Math.max(L[0],v.bbox[0]));k.splice(y===-1?k.length:y,0,{label:"Picture",bbox:[V(L[0]),V(L[1]),V(L[2]),V(L[3])],text:""})}}else if(f.length<2)_=x.join(` | |
| `);return{lines:I,md:_,blocks:k,note:M}}#c(t,n){let{width:e,height:o}=t,s=n/Math.max(o,e),a=Math.max(1,z(e*s)),r=Math.max(1,z(o*s)),l=X(t.data,e,o,a,r,"area"),c=new Uint8Array(a*r);for(let u=0;u<c.length;u++)c[u]=255-l[u];return{data:c,width:a,height:r}}#s(t,n){let e=kt(t,ut(t.width/2,t.height/2,n)),{width:o,height:s}=t,a=new Float64Array(s);for(let l=0;l<s;l++){let c=0,u=l*o;for(let h=0;h<o;h++)c+=e.data[u+h];a[l]=c}let r=0;for(let l=1;l<s;l++){let c=a[l]-a[l-1];r+=c*c}return r}#a(t,n=800){let e=this.#c(t,n),o=0;for(let g=0;g<e.data.length;g++)o+=e.data[g];if(o/e.data.length<0.765)return 0;let s=this.#c(t,Math.floor(n/2)),a=[];for(let g=0;;g++){let x=-vn+g*2;if(x>vn+0.000001)break;a.push(x)}let r=a.map((g)=>this.#s(s,g)),l=-1,c=-1;for(let g=0;g<a.length;g++)if(l<0||r[g]>r[l])c=l,l=g;else if(c<0||r[g]>r[c])c=g;let u=[0,a[c],a[l]],h=[],i=new Set;for(let g of u)for(let x=0;x<=8;x++){let A=g-2+x*0.5;if(!i.has(A))i.add(A),h.push(A)}let p=h[0],d=-1/0;for(let g of h){let x=this.#s(e,g);if(x>d)d=x,p=g}let m=[];for(let g=0;;g++){let x=p-0.5+g*0.1;if(x>p+0.5+0.000001)break;m.push(x)}let f=m[0];d=-1/0;for(let g of m){let x=this.#s(e,g);if(x>d)d=x,f=g}return f}#m(t,n=Ue){let e=n/Math.max(t.height,t.width);if(e>=1)return t;let o=Math.max(1,z(t.width*e)),s=Math.max(1,z(t.height*e));return{data:X(t.data,t.width,t.height,o,s,"area"),width:o,height:s}}#r(t,n){return kt(t,ut(t.width/2,t.height/2,n),"replicate")}#l(t,n,e,o){let s=ut(e/2,o/2,-n),a=[t[0],t[2],t[0],t[2]],r=[t[1],t[1],t[3],t[3]],l=a.map((u,h)=>s[0]*u+s[1]*r[h]+s[2]),c=r.map((u,h)=>s[3]*a[h]+s[4]*u+s[5]);return[V(G(Math.min(...l),0,e)),V(G(Math.min(...c),0,o)),V(G(Math.max(...l),0,e)),V(G(Math.max(...c),0,o))]}async upright(t,n=!0,e=null){let o=this.#a(t);if(e===null)e=n?await this.orientation(Math.abs(o)>=Ot?this.#r(t,o):t):0;return this.#h(t,e,o,null)}#h(t,n,e,o){let s=n===yt?1:-n;if(n===yt)t=pt(t,3);else if(n)t=pt(t,n);if(n===1||n===3||n===yt){let a=o?pt(o,s===1?3:-s):t;e=this.#a(a)}if(Math.abs(e)>=Ot)t=this.#r(t,e);else e=0;return{gray:t,turns:n,skew:e}}async orientation(t){if(!this.orient)return 0;let n=Le,{height:e,width:o}=t,s=n/Math.max(e,o),a=Math.max(1,z(e*s)),r=Math.max(1,z(o*s)),l=a===e&&r===o?t.data:X(t.data,o,e,r,a,"area"),c=new Float32Array(n*n);for(let i=0;i<a;i++)for(let p=0;p<r;p++)c[i*n+p]=1-l[i*r+p]/255;let{p:u}=await this.orient.run({x:new this.Tensor("float32",c,[1,1,n,n])}),h=0;for(let i=1;i<u.data.length;i++)if(u.data[i]>u.data[h])h=i;return u.data[h]>Se?h:0}async run(t,n=!0){let e={},o=Date.now(),s=this.#m(t),a=this.#a(s),r=n?await this.orientation(Math.abs(a)>=Ot?this.#r(s,a):s):0,l=Date.now()-o,c=await this.#d(t,r,a,s);return c.timings.orientMs+=l,c}async#d(t,n,e,o){let s={},a=Date.now(),r=this.#h(t,n,e,o);t=r.gray,s.orientMs=Date.now()-a,a=Date.now();let[l,c]=await this.detectAll(t);s.detMs=Date.now()-a,a=Date.now();let u=r.turns===yt,h=await this.recognize(t,l,64,u);s.recMs=Date.now()-a;let i=await this.#p([t.width,t.height],l,h,s,u,u?null:c),p=u?0:90*r.turns,d=He(r.skew);if(r.skew){for(let m of i.lines)m.box=this.#l(m.box,r.skew,t.width,t.height);for(let m of i.blocks)m.bbox=this.#l(m.bbox,r.skew,t.width,t.height)}if(u){let m=t.height,f=(g)=>[m-g[3],g[0],m-g[1],g[2]];for(let g of i.lines)g.box=f(g.box);for(let g of i.blocks)g.bbox=f(g.bbox)}return{lines:i.lines,source:"ocr",rotation:p,skew:d,vertical:u,markdown:i.md,blocks:i.blocks,pictures:i.blocks.filter((m)=>m.label==="Picture").map((m)=>m.bbox),text:i.lines.map((m)=>m.text).join(` | |
| `),note:i.note,timings:s}}}function Ze(t,n,e,o,s,a){let r=t.length,l=new Uint8Array(r);for(let m=0;m<r;m++)l[m]=t[m]>0?1:0;let c=new Int32Array(r),u=[],h=new Int32Array(r);for(let m=0;m<r;m++){if(!l[m]||c[m])continue;let f=u.length+1,g=0;h[g++]=m,c[m]=f;let x={x0:n,x1:-1,y0:e,y1:-1};while(g>0){let A=h[--g],w=A/n|0,M=A-w*n;if(M<x.x0)x.x0=M;if(M>x.x1)x.x1=M;if(w<x.y0)x.y0=w;if(w>x.y1)x.y1=w;for(let I=-1;I<=1;I++){let _=w+I;if(_<0||_>=e)continue;for(let k=-1;k<=1;k++){let E=M+k;if(E<0||E>=n)continue;let S=_*n+E;if(l[S]&&!c[S])c[S]=f,h[g++]=S}}}u.push(x)}let i=2/o,p=Pe*a*s,d=[];for(let m of u){let f=[m.x0*i,m.y0*i,Math.min((m.x1+1)*i,s),Math.min((m.y1+1)*i,a)];if((f[2]-f[0])*(f[3]-f[1])>=p)d.push(f)}return d}var t1="v1.1.0";async function n1(t,n){let e=await fetch(t);if(!e.ok)throw Error(`${e.status} ${e.statusText} for ${t}`);if(!e.body||!n)return new Uint8Array(await e.arrayBuffer());let o=e.headers.has("content-length")?+e.headers.get("content-length"):null,s=e.body.getReader(),a=[],r=0;for(;;){let{done:u,value:h}=await s.read();if(u)break;a.push(h),r+=h.byteLength,n(r,o)}let l=new Uint8Array(r),c=0;for(let u of a)l.set(u,c),c+=u.byteLength;return l}async function e1(t,n,e){let o=await e(n),s=await Je.create(o,{executionProviders:["wasm"],graphOptimizationLevel:"all"});return{run:(a)=>s.run(a),inputNames:s.inputNames,outputNames:s.outputNames}}function o1(t){let n=/^([^/@:\s]+)\/([^/@:\s]+)(?:@(.+))?$/.exec(t);return n?`https://huggingface.co/${n[1]}/${n[2]}/resolve/${n[3]||t1}`:t}async function kn(t,n={}){let e=n.threads??4;if(t=o1(t),!/\/$/.test(t))t+="/";if(n.wasmPaths)In.wasm.wasmPaths=n.wasmPaths;In.wasm.numThreads=e;let o=typeof n.progress==="function"?n.progress:null,s=new Map,a=(d)=>{if(!s.has(d)){let m=n1(t+d,o?(f,g)=>o({file:d,loaded:f,total:g}):null).then((f)=>(o?.({file:d,loaded:f.byteLength,total:f.byteLength,done:!0}),f));s.set(d,m)}return s.get(d)},r=["meta.json","det.onnx",n.recInt8?"rec.int8.onnx":"rec.onnx"];if(n.order!==!1)r.push("order.onnx");if(n.orient!==!1)r.push("orient.onnx");if(n.math)r.push("mathcls.onnx","math_enc.onnx","math_dec_init.onnx","math_dec_step.onnx","math_meta.json");await Promise.allSettled(r.map(a));let l=async(d)=>JSON.parse(new TextDecoder().decode(await a(d))),c=null;try{c=await l("meta.json")}catch{}let u=(c?.script_groups??["base","cjk","rtl","other"]).filter((d)=>d!=="base"),h=[];if(n.cjk!==!1)h.push("script.onnx",...u.map((d)=>`rec_${d}.onnx`));if(n.lm!==!1){if(h.push("lm.npz"),n.cjk!==!1)h.push(...u.map((d)=>`lm_${d}.npz`))}await Promise.allSettled(h.map(a));let i={Tensor:Qe,meta:()=>l("meta.json"),json:l,bytes:async(d)=>new Uint8Array(await a(d)),session:(d)=>e1(t,d,a)},p=await At.create(i,n);return p.device="wasm",p}async function _n(t,n={}){if(typeof t==="string"||t instanceof URL){let s=await fetch(t);if(!s.ok)throw Error(`${s.status} ${s.statusText} for ${t}`);t=await s.blob()}let e=!(typeof ImageBitmap<"u"&&t instanceof ImageBitmap),o=e?await createImageBitmap(t):t;try{let{width:s,height:a}=o,r=n.maxSide,l=r>0?Math.min(1,r/Math.max(s,a)):1,c=Math.max(1,Math.round(s*l)),u=Math.max(1,Math.round(a*l)),i=(typeof OffscreenCanvas<"u"?new OffscreenCanvas(c,u):Object.assign(document.createElement("canvas"),{width:c,height:u})).getContext("2d",{willReadFrequently:!0});i.fillStyle="#fff",i.fillRect(0,0,c,u),i.imageSmoothingEnabled=!0,i.imageSmoothingQuality="high",i.drawImage(o,0,0,c,u);let{data:p}=i.getImageData(0,0,c,u),d=new Uint8Array(c*u);for(let m=0,f=0;m<d.length;m++,f+=4)d[m]=p[f]*9797+p[f+1]*19234+p[f+2]*3737>>15;return{data:d,width:c,height:u}}finally{if(e)o.close()}}var O=(t)=>document.getElementById(t),b={run:O("run"),gear:O("gear"),panel:O("panel"),models:O("models"),cjk:O("cjk"),orient:O("orient"),pbar:O("pbar"),pfill:O("pbar").firstElementChild,viewport:O("viewport"),wrap:O("pageWrap"),cv:O("cv"),overlay:O("overlay"),empty:O("empty"),samples:O("samples"),pick:O("pick"),open:O("open"),file:O("file"),zin:O("zin"),zout:O("zout"),zfit:O("zfit"),zpct:O("zpct"),dims:O("dims"),tabs:[...document.querySelectorAll(".tab")],copy:O("copy"),lines:O("lines"),mdview:O("mdview"),ph:O("ph"),scroll:O("scroll"),status:O("status"),chips:O("chips")},C={gray:null,shown:null,page:null,view:"text",zoom:"fit",busy:!1},Z=(t,n=!1)=>{b.status.textContent=t,b.status.classList.toggle("err",n)},En=(t)=>`${(t/1048576).toFixed(1)} MB`,ot=null,lt=null,jt=new Map;function s1(t){jt.set(t.file,{loaded:t.loaded,total:t.total});let n=0,e=0,o=!0;for(let{loaded:s,total:a}of jt.values())if(n+=s,a==null)o=!1;else e+=a;b.pbar.hidden=!1,b.pbar.classList.toggle("indet",!o),b.pfill.style.transform=o?`scaleX(${e?n/e:0})`:"",Z(o?`downloading models ${En(n)} / ${En(e)} — ${t.file}`:`downloading models — ${t.file}`)}async function a1(){let t=b.models.value.trim()||"datalab-to/indra-ocr@v1.1.0",n={cjk:b.cjk.checked,orient:b.orient.checked},e=JSON.stringify([t,n.cjk,n.orient]);if(ot&<===e)return ot;ot=null,lt=e,jt.clear();try{return ot=await kn(t,{...n,progress:s1}),b.pbar.hidden=!0,ot}catch(o){throw lt=null,b.pbar.hidden=!0,o}}for(let t of[b.models,b.cjk,b.orient])t.addEventListener("change",()=>{if(lt!==null)Z("settings changed — models reload on the next run");lt=null});b.gear.addEventListener("click",()=>{b.panel.hidden=!b.panel.hidden,b.gear.setAttribute("aria-expanded",String(!b.panel.hidden))});document.addEventListener("pointerdown",(t)=>{if(!b.panel.hidden&&!t.target.closest(".settings"))b.panel.hidden=!0,b.gear.setAttribute("aria-expanded","false")});function ht(t,n){let e=document.createElement("canvas");e.width=t,e.height=n;let o=e.getContext("2d");return o.fillStyle="#fdfdfb",o.fillRect(0,0,t,n),o.fillStyle="#1a1a1e",o}var st=(t,n)=>`${n?"bold ":""}${t}px Georgia, "Times New Roman", serif`,bt=(t)=>`${t}px ui-monospace, Menlo, Consolas, monospace`;function Tn({flip:t=!1}={}){let o=ht(1080,560);if(o.font=st(38,!0),o.fillText("Notes from a Quiet Machine",64,104),o.font=st(25),["This page was recognized without leaving the browser:","line detection, recognition, reading order and orientation","all run locally on WebAssembly, via ONNX Runtime.","Your pixels stay put — only the model weights travel,","once, and then the browser caches them.","Filed under on-device intelligence"].forEach((a,r)=>o.fillText(a,64,180+r*54)),!t)return o.canvas;let s=ht(1080,560);return s.translate(540,280),s.rotate(Math.PI),s.drawImage(o.canvas,-540,-280),s.canvas}function r1(){let e=ht(1080,620);e.font=st(36,!0),e.fillText("Reading Order, Demonstrated",64,96),e.font=st(23);let o=["Documents rarely read in one straight run:","columns, sidebars and tables all compete","for attention. Detection only sees lines,","so ordering is a problem of its own."],s=["1. find the lines","2. read every line, left to right","3. decide what belongs to what block","4. put the blocks back in order"];return o.forEach((a,r)=>e.fillText(a,64,180+r*56)),s.forEach((a,r)=>e.fillText(a,610,180+r*56)),e.canvas}function i1(){let e=ht(640,860);e.font=bt(30),e.textAlign="center",e.fillText("CORNER BOOKSTORE",320,96),e.font=bt(22),e.fillText("123 Inkwell Lane · tel 555-0110",320,136),e.textAlign="left";let o=(a,r,l)=>{e.fillText(a,56,l),e.textAlign="right",e.fillText(r,584,l),e.textAlign="left"},s=220;for(let[a,r]of[["Map of Invisible Cities","18.50"],["The Index Card","9.75"],["Brass Bookmark","4.00"],["Absolute Python 4e","52.00"]])o(a,r,s),s+=52;return e.fillText("—".repeat(24),56,s+14),o("SUBTOTAL","84.25",s+70),o("TAX 8%","6.74",s+122),e.font=bt(30),o("TOTAL","90.99",s+190),e.font=bt(20),o("2026-10-19 14:32","VISA ..0424",s+260),e.textAlign="center",e.fillText("thank you — come again",320,s+330),e.canvas}function c1(){let e=ht(1080,520);return e.font=st(40,!0),e.fillText("光学字符识别",64,100),e.font=st(26),["检测、识别与阅读顺序模型都运行在浏览器里。","图片不会离开这台设备,识别结果实时生成。","标点也要保留:,。、;!?——(2026)"].forEach((o,s)=>e.fillText(o,64,190+s*60)),e.canvas}var l1=[{label:"Article",draw:Tn},{label:"Two columns",draw:r1},{label:"Receipt",draw:i1},{label:"中文",draw:c1},{label:"Upside down",draw:()=>Tn({flip:!0})}];for(let t of l1){let n=t.draw(),e=document.createElement("button"),o=document.createElement("span");o.className="thumb",o.appendChild(n);let s=document.createElement("span");s.className="lbl",s.textContent=t.label,e.append(o,s),e.onclick=()=>{n.toBlob(async(a)=>{Z(`sample: ${t.label}…`),await It(a)},"image/png")},b.samples.appendChild(e)}function h1({data:t,width:n,height:e}){let o=new Uint8Array(n*e);for(let s=0;s<e;s++)for(let a=0;a<n;a++)o[a*e+(e-1-s)]=t[s*n+a];return{data:o,width:e,height:n}}function Ln(t){C.shown=t,b.cv.width=t.width,b.cv.height=t.height;let n=b.cv.getContext("2d"),e=n.createImageData(t.width,t.height);for(let o=0,s=0;o<t.data.length;o++,s+=4)e.data[s]=e.data[s+1]=e.data[s+2]=t.data[o],e.data[s+3]=255;n.putImageData(e,0,0),b.dims.textContent=`${t.width} × ${t.height} px`,b.overlay.replaceChildren(),zt()}function Dt(){let t=b.viewport.clientWidth-48,n=b.viewport.clientHeight-48;return Math.max(0.05,Math.min(1,t/C.shown.width,n/C.shown.height))}function zt(){let t=C.zoom==="fit",n=t?Dt():C.zoom;b.cv.style.width=`${Math.round(C.shown.width*n)}px`,b.zpct.textContent=t?"fit":`${Math.round(n*100)}%`}b.zin.onclick=()=>Ft((C.zoom==="fit"?Dt():C.zoom)*1.25);b.zout.onclick=()=>Ft(Math.min(5,Math.max(0.15,(C.zoom==="fit"?Dt():C.zoom)/1.25)));b.zfit.onclick=()=>Ft("fit");function Ft(t){C.zoom=t,zt()}new ResizeObserver(()=>{if(C.zoom==="fit"&&C.shown)zt()}).observe(b.viewport);async function It(t){let n=await _n(t);C.gray=n,C.page=null,C.zoom="fit",Ln(n),b.empty.hidden=!0,b.wrap.hidden=!1,b.pick.hidden=!1,b.run.disabled=!1,f1(),await Sn()}function f1(){b.lines.replaceChildren(),b.lines.hidden=!0,b.mdview.hidden=!0,b.ph.hidden=!1,b.chips.replaceChildren(),b.tabs.find((t)=>t.dataset.v==="md").disabled=!0,Pt("text")}function Pt(t){C.view=t;for(let n of b.tabs)n.classList.toggle("on",n.dataset.v===t);b.mdview.hidden=t!=="md",b.lines.hidden=t!=="text"||!C.page,b.ph.hidden=!(t==="text"&&!C.page)}for(let t of b.tabs)t.onclick=()=>!t.disabled&&Pt(t.dataset.v);b.copy.onclick=async()=>{let t=C.view==="md"?C.page?.markdown:C.page?.text;if(!t)return;try{await navigator.clipboard.writeText(t),Z(C.view==="md"?"markdown copied":"text copied")}catch{Z("clipboard blocked by the browser",!0)}};function vt(t,n){if(b.overlay.children[t]?.classList.toggle("hot",n),b.lines.children[t]?.classList.toggle("hot",n),n)b.lines.children[t]?.scrollIntoView({block:"nearest"})}function u1(){let t=C.page;b.overlay.replaceChildren(),b.lines.replaceChildren(),t.lines.forEach((e,o)=>{let[s,a,r,l]=e.box,c=document.createElement("div");c.className="box",c.style.cssText=`left:${s/C.shown.width*100}%;top:${a/C.shown.height*100}%;width:${(r-s)/C.shown.width*100}%;height:${(l-a)/C.shown.height*100}%`,c.onmouseenter=()=>vt(o,!0),c.onmouseleave=()=>vt(o,!1),b.overlay.appendChild(c);let u=document.createElement("div");u.className="row";let h=Math.round((e.conf??0)*100),i=document.createElement("span");i.className="idx",i.textContent=o+1;let p=document.createElement("span");p.className="txt",p.textContent=e.text;let d=document.createElement("span");d.className="conf",d.title=`confidence ${h}%`,d.appendChild(document.createElement("i")).style.width=`${h}%`;let m=document.createElement("span");m.className="pct",m.textContent=`${h}%`,u.append(i,p,d,m),u.onmouseenter=()=>vt(o,!0),u.onmouseleave=()=>vt(o,!1),b.lines.appendChild(u)}),b.ph.hidden=!0,b.lines.hidden=C.view!=="text";let n=b.tabs.find((e)=>e.dataset.v==="md");n.disabled=t.markdown==null,b.mdview.textContent=t.markdown??"",p1()}function Y(t,n=""){let e=document.createElement("span");e.className=`chip ${n}`.trim(),e.textContent=t,b.chips.appendChild(e)}function p1(){let{page:t}=C;if(b.chips.replaceChildren(),Y(`${t.lines.length} lines`,"good"),t.rotation)Y(`↻ ${t.rotation}°`,"good");if(t.skew)Y(`deskew ${t.skew}°`);let n=t.timings??{};if(n.orientMs!=null)Y(`orient ${n.orientMs}ms`);if(n.detMs!=null)Y(`det ${n.detMs}ms`);if(n.recMs!=null)Y(`rec ${n.recMs}ms`);if(n.orderMs!=null)Y(`order ${n.orderMs}ms`)}async function Sn(){if(!C.gray||C.busy)return;C.busy=!0,b.run.disabled=!0;let t=performance.now();try{await a1(),Z("running OCR…"),await new Promise((o)=>setTimeout(o,20));let n=await ot.run(C.gray);if(C.page=n,n.rotation)Ln(m1(C.gray,n.rotation/90));Pt(C.view),u1();let e=Math.round(performance.now()-t);Z("done — hover a line to locate it on the page"+(n.rotation?` (page was turned ${n.rotation}°)`:"")),Y(`${e} ms`)}catch(n){Z("failed: "+(n?.message??n),!0),console.error(n)}b.run.disabled=!1,C.busy=!1}function m1(t,n){for(;n>0;n--)t=h1(t);return t}b.run.onclick=()=>Sn();b.pick.onclick=()=>{b.empty.hidden=!b.empty.hidden};b.open.onclick=()=>b.file.click();b.file.onchange=()=>{if(b.file.files[0])It(b.file.files[0]);b.file.value=""};var Ut=0;window.addEventListener("dragenter",(t)=>{if(t.preventDefault(),++Ut===1)b.viewport.classList.add("drag")});window.addEventListener("dragleave",()=>{if(--Ut===0)b.viewport.classList.remove("drag")});window.addEventListener("dragover",(t)=>t.preventDefault());window.addEventListener("drop",(t)=>{t.preventDefault(),Ut=0,b.viewport.classList.remove("drag");let n=t.dataTransfer.files[0];if(n)It(n)});window.addEventListener("paste",(t)=>{let e=[...t.clipboardData?.items??[]].find((o)=>o.type.startsWith("image/"))?.getAsFile();if(e)It(e)}); | |