import{S as Th,i as _h,s as vh,e as o,k as c,w as u,t,M as Ih,c as i,d as e,m as h,a as p,x as m,h as l,b as d,N as kh,G as a,g as r,y as j,q as f,o as M,B as g,v as Ch}from"../../chunks/vendor-hf-doc-builder.js";import{T as Eh}from"../../chunks/Tip-hf-doc-builder.js";import{I as Cs}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as w}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Ah}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function Zh(At){let U,is,I,x,Z,N,Es,R,As,ps,k,rs;return{c(){U=o("p"),is=t("The task illustrated in this tutorial is supported by the following model architectures:"),I=c(),x=o("p"),Z=o("a"),N=t("LayoutLM"),Es=t(", "),R=o("a"),As=t("LayoutLMv2"),ps=t(", "),k=o("a"),rs=t("LayoutLMv3"),this.h()},l(b){U=i(b,"P",{});var F=p(U);is=l(F,"The task illustrated in this tutorial is supported by the following model architectures:"),F.forEach(e),I=h(b),x=i(b,"P",{});var V=p(x);Z=i(V,"A",{href:!0});var D=p(Z);N=l(D,"LayoutLM"),D.forEach(e),Es=l(V,", "),R=i(V,"A",{href:!0});var Ca=p(R);As=l(Ca,"LayoutLMv2"),Ca.forEach(e),ps=l(V,", "),k=i(V,"A",{href:!0});var Zs=p(k);rs=l(Zs,"LayoutLMv3"),Zs.forEach(e),V.forEach(e),this.h()},h(){d(Z,"href","../model_doc/layoutlm"),d(R,"href","../model_doc/layoutlmv2"),d(k,"href","../model_doc/layoutlmv3")},m(b,F){r(b,U,F),a(U,is),r(b,I,F),r(b,x,F),a(x,Z),a(Z,N),a(x,Es),a(x,R),a(R,As),a(x,ps),a(x,k),a(k,rs)},d(b){b&&e(U),b&&e(I),b&&e(x)}}}function Rh(At){let U,is,I,x,Z,N,Es,R,As,ps,k,rs,b,F,V,D,Ca,Zs,cs,P,Tn,Ea,_n,vn,Rs,In,kn,Cn,Je,En,Zt,hs,Rt,Aa,An,Xt,Za,Zn,$t,Xs,Gt,$s,Bt,Gs,Wt,Ra,Rn,Nt,Xa,Xn,Vt,Bs,Ft,$a,$n,Qt,Ws,qt,O,ds,Ue,Ns,Gn,Te,Bn,zt,Q,Wn,Vs,Nn,Vn,Fs,Fn,Qn,Yt,Qs,St,Ga,qn,Ht,qs,Lt,Ba,zn,Dt,T,Wa,_e,Yn,Sn,Hn,Na,ve,Ln,Dn,Pn,Va,Ie,On,Kn,so,Fa,ke,ao,eo,to,us,Ce,lo,no,Ee,oo,io,po,Qa,Ae,ro,co,Pt,ms,ho,Ze,uo,mo,Ot,zs,Kt,X,jo,Re,fo,Mo,K,go,Xe,wo,yo,xo,Ys,bo,Jo,sl,Ss,al,q,Uo,qa,To,_o,Hs,$e,vo,Io,ko,el,Ls,tl,za,Co,ll,Ds,nl,Ps,Ya,yr,ol,ss,js,Ge,Os,Eo,Be,Ao,il,fs,Zo,Sa,Ro,Xo,pl,Ks,rl,as,Ms,We,sa,$o,Ne,Go,cl,gs,Bo,Ve,Wo,No,hl,aa,dl,ws,Vo,ea,Fo,Qo,ul,ta,ml,es,ys,Fe,la,qo,Qe,zo,jl,_,Yo,qe,So,Ho,ze,Lo,Do,Ye,Po,Oo,Se,Ko,si,He,ai,ei,fl,na,Ml,$,ti,Le,li,ni,De,oi,ii,Pe,pi,ri,gl,Ha,ci,wl,y,hi,Oe,di,ui,Ke,mi,ji,st,fi,Mi,at,gi,wi,et,yi,xi,tt,bi,Ji,lt,Ui,Ti,nt,_i,vi,yl,oa,xl,La,Ii,bl,ia,Jl,Da,ki,Ul,pa,Tl,Pa,Ci,_l,z,Oa,ot,Ei,Ai,Zi,Ka,it,Ri,Xi,$i,xs,pt,Gi,Bi,rt,Wi,Ni,vl,se,Vi,Il,ra,kl,ae,Fi,Cl,ca,El,ee,Qi,Al,ha,Zl,ts,bs,ct,da,qi,ht,zi,Rl,Y,Yi,te,Si,Hi,ua,Li,Di,Xl,ls,Js,dt,ma,Pi,ut,Oi,$l,le,Ki,Gl,C,ja,sp,ne,ap,ep,tp,fa,lp,oe,np,op,ip,Ma,pp,ie,rp,cp,hp,ga,dp,pe,up,mp,jp,wa,fp,re,Mp,gp,Bl,ya,Wl,v,wp,ce,yp,xp,mt,bp,Jp,jt,Up,Tp,ft,_p,vp,Mt,Ip,kp,Nl,xa,Vl,he,Cp,Fl,ba,Ql,Us,Ep,de,Ap,Zp,ql,Ja,zl,Ts,Rp,gt,Xp,$p,Yl,Ua,Sl,ns,_s,wt,Ta,Gp,yt,Bp,Hl,vs,Wp,ue,Np,Vp,Ll,me,Fp,Dl,_a,Pl,je,Qp,Ol,va,Kl,fe,qp,sn,E,xt,zp,Yp,bt,Sp,Hp,os,Lp,Jt,Dp,Pp,Ut,Op,Kp,sr,A,ar,Tt,er,tr,_t,lr,nr,vt,or,ir,It,pr,rr,cr,kt,hr,an,Ia,en;return N=new Cs({}),k=new Ah({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/document_question_answering.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/document_question_answering.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/document_question_answering.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/document_question_answering.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/document_question_answering.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/document_question_answering.ipynb"}]}}),hs=new Eh({props:{$$slots:{default:[Zh]},$$scope:{ctx:At}}}),Xs=new w({props:{code:"cGlwJTIwaW5zdGFsbCUyMC1xJTIwdHJhbnNmb3JtZXJzJTIwZGF0YXNldHM=",highlighted:"pip install -q transformers datasets"}}),$s=new w({props:{code:"cGlwJTIwaW5zdGFsbCUyMCdnaXQlMkJodHRwcyUzQSUyRiUyRmdpdGh1Yi5jb20lMkZmYWNlYm9va3Jlc2VhcmNoJTJGZGV0ZWN0cm9uMi5naXQnJTBBcGlwJTIwaW5zdGFsbCUyMHRvcmNodmlzaW9u",highlighted:`pip install <span class="hljs-string">&#x27;git+https://github.com/facebookresearch/detectron2.git&#x27;</span>
pip install torchvision`}}),Gs=new w({props:{code:"c3VkbyUyMGFwdCUyMGluc3RhbGwlMjB0ZXNzZXJhY3Qtb2NyJTBBcGlwJTIwaW5zdGFsbCUyMC1xJTIwcHl0ZXNzZXJhY3Q=",highlighted:`sudo apt install tesseract-ocr
pip install -q pytesseract`}}),Bs=new w({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

<span class="hljs-meta">&gt;&gt;&gt; </span>notebook_login()`}}),Ws=new w({props:{code:"bW9kZWxfY2hlY2twb2ludCUyMCUzRCUyMCUyMm1pY3Jvc29mdCUyRmxheW91dGxtdjItYmFzZS11bmNhc2VkJTIyJTBBYmF0Y2hfc2l6ZSUyMCUzRCUyMDQ=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>model_checkpoint = <span class="hljs-string">&quot;microsoft/layoutlmv2-base-uncased&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>batch_size = <span class="hljs-number">4</span>`}}),Ns=new Cs({}),Qs=new w({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBJTBBZGF0YXNldCUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJuaWVsc3IlMkZkb2N2cWFfMTIwMF9leGFtcGxlcyUyMiklMEFkYXRhc2V0",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

<span class="hljs-meta">&gt;&gt;&gt; </span>dataset = load_dataset(<span class="hljs-string">&quot;nielsr/docvqa_1200_examples&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>dataset
DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;id&#x27;</span>, <span class="hljs-string">&#x27;image&#x27;</span>, <span class="hljs-string">&#x27;query&#x27;</span>, <span class="hljs-string">&#x27;answers&#x27;</span>, <span class="hljs-string">&#x27;words&#x27;</span>, <span class="hljs-string">&#x27;bounding_boxes&#x27;</span>, <span class="hljs-string">&#x27;answer&#x27;</span>],
        num_rows: <span class="hljs-number">1000</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;id&#x27;</span>, <span class="hljs-string">&#x27;image&#x27;</span>, <span class="hljs-string">&#x27;query&#x27;</span>, <span class="hljs-string">&#x27;answers&#x27;</span>, <span class="hljs-string">&#x27;words&#x27;</span>, <span class="hljs-string">&#x27;bounding_boxes&#x27;</span>, <span class="hljs-string">&#x27;answer&#x27;</span>],
        num_rows: <span class="hljs-number">200</span>
    })
})`}}),qs=new w({props:{code:"ZGF0YXNldCU1QiUyMnRyYWluJTIyJTVELmZlYXR1cmVz",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-string">&quot;train&quot;</span>].features'}}),zs=new w({props:{code:"dXBkYXRlZF9kYXRhc2V0JTIwJTNEJTIwZGF0YXNldC5tYXAobGFtYmRhJTIwZXhhbXBsZSUzQSUyMCU3QiUyMnF1ZXN0aW9uJTIyJTNBJTIwZXhhbXBsZSU1QiUyMnF1ZXJ5JTIyJTVEJTVCJTIyZW4lMjIlNUQlN0QlMkMlMjByZW1vdmVfY29sdW1ucyUzRCU1QiUyMnF1ZXJ5JTIyJTVEKSUwQXVwZGF0ZWRfZGF0YXNldCUyMCUzRCUyMHVwZGF0ZWRfZGF0YXNldC5tYXAoJTBBJTIwJTIwJTIwJTIwbGFtYmRhJTIwZXhhbXBsZSUzQSUyMCU3QiUyMmFuc3dlciUyMiUzQSUyMGV4YW1wbGUlNUIlMjJhbnN3ZXJzJTIyJTVEJTVCMCU1RCU3RCUyQyUyMHJlbW92ZV9jb2x1bW5zJTNEJTVCJTIyYW5zd2VyJTIyJTJDJTIwJTIyYW5zd2VycyUyMiU1RCUwQSk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>updated_dataset = dataset.<span class="hljs-built_in">map</span>(<span class="hljs-keyword">lambda</span> example: {<span class="hljs-string">&quot;question&quot;</span>: example[<span class="hljs-string">&quot;query&quot;</span>][<span class="hljs-string">&quot;en&quot;</span>]}, remove_columns=[<span class="hljs-string">&quot;query&quot;</span>])
<span class="hljs-meta">&gt;&gt;&gt; </span>updated_dataset = updated_dataset.<span class="hljs-built_in">map</span>(
<span class="hljs-meta">... </span>    <span class="hljs-keyword">lambda</span> example: {<span class="hljs-string">&quot;answer&quot;</span>: example[<span class="hljs-string">&quot;answers&quot;</span>][<span class="hljs-number">0</span>]}, remove_columns=[<span class="hljs-string">&quot;answer&quot;</span>, <span class="hljs-string">&quot;answers&quot;</span>]
<span class="hljs-meta">... </span>)`}}),Ss=new w({props:{code:"dXBkYXRlZF9kYXRhc2V0JTIwJTNEJTIwdXBkYXRlZF9kYXRhc2V0LmZpbHRlcihsYW1iZGElMjB4JTNBJTIwbGVuKHglNUIlMjJ3b3JkcyUyMiU1RCklMjAlMkIlMjBsZW4oeCU1QiUyMnF1ZXN0aW9uJTIyJTVELnNwbGl0KCkpJTIwJTNDJTIwNTEyKQ==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>updated_dataset = updated_dataset.<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> x: <span class="hljs-built_in">len</span>(x[<span class="hljs-string">&quot;words&quot;</span>]) + <span class="hljs-built_in">len</span>(x[<span class="hljs-string">&quot;question&quot;</span>].split()) &lt; <span class="hljs-number">512</span>)'}}),Ls=new w({props:{code:"dXBkYXRlZF9kYXRhc2V0JTIwJTNEJTIwdXBkYXRlZF9kYXRhc2V0LnJlbW92ZV9jb2x1bW5zKCUyMndvcmRzJTIyKSUwQXVwZGF0ZWRfZGF0YXNldCUyMCUzRCUyMHVwZGF0ZWRfZGF0YXNldC5yZW1vdmVfY29sdW1ucyglMjJib3VuZGluZ19ib3hlcyUyMik=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>updated_dataset = updated_dataset.remove_columns(<span class="hljs-string">&quot;words&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>updated_dataset = updated_dataset.remove_columns(<span class="hljs-string">&quot;bounding_boxes&quot;</span>)`}}),Ds=new w({props:{code:"dXBkYXRlZF9kYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQlNUIxMSU1RCU1QiUyMmltYWdlJTIyJTVE",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>updated_dataset[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">11</span>][<span class="hljs-string">&quot;image&quot;</span>]'}}),Os=new Cs({}),Ks=new w({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Qcm9jZXNzb3IlMEElMEFwcm9jZXNzb3IlMjAlM0QlMjBBdXRvUHJvY2Vzc29yLmZyb21fcHJldHJhaW5lZChtb2RlbF9jaGVja3BvaW50KQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoProcessor

<span class="hljs-meta">&gt;&gt;&gt; </span>processor = AutoProcessor.from_pretrained(model_checkpoint)`}}),sa=new Cs({}),aa=new w({props:{code:"aW1hZ2VfcHJvY2Vzc29yJTIwJTNEJTIwcHJvY2Vzc29yLmltYWdlX3Byb2Nlc3NvciUwQSUwQSUwQWRlZiUyMGdldF9vY3Jfd29yZHNfYW5kX2JveGVzKGV4YW1wbGVzKSUzQSUwQSUyMCUyMCUyMCUyMGltYWdlcyUyMCUzRCUyMCU1QmltYWdlLmNvbnZlcnQoJTIyUkdCJTIyKSUyMGZvciUyMGltYWdlJTIwaW4lMjBleGFtcGxlcyU1QiUyMmltYWdlJTIyJTVEJTVEJTBBJTIwJTIwJTIwJTIwZW5jb2RlZF9pbnB1dHMlMjAlM0QlMjBpbWFnZV9wcm9jZXNzb3IoaW1hZ2VzKSUwQSUwQSUyMCUyMCUyMCUyMGV4YW1wbGVzJTVCJTIyaW1hZ2UlMjIlNUQlMjAlM0QlMjBlbmNvZGVkX2lucHV0cy5waXhlbF92YWx1ZXMlMEElMjAlMjAlMjAlMjBleGFtcGxlcyU1QiUyMndvcmRzJTIyJTVEJTIwJTNEJTIwZW5jb2RlZF9pbnB1dHMud29yZHMlMEElMjAlMjAlMjAlMjBleGFtcGxlcyU1QiUyMmJveGVzJTIyJTVEJTIwJTNEJTIwZW5jb2RlZF9pbnB1dHMuYm94ZXMlMEElMEElMjAlMjAlMjAlMjByZXR1cm4lMjBleGFtcGxlcw==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>image_processor = processor.image_processor


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">get_ocr_words_and_boxes</span>(<span class="hljs-params">examples</span>):
<span class="hljs-meta">... </span>    images = [image.convert(<span class="hljs-string">&quot;RGB&quot;</span>) <span class="hljs-keyword">for</span> image <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;image&quot;</span>]]
<span class="hljs-meta">... </span>    encoded_inputs = image_processor(images)

<span class="hljs-meta">... </span>    examples[<span class="hljs-string">&quot;image&quot;</span>] = encoded_inputs.pixel_values
<span class="hljs-meta">... </span>    examples[<span class="hljs-string">&quot;words&quot;</span>] = encoded_inputs.words
<span class="hljs-meta">... </span>    examples[<span class="hljs-string">&quot;boxes&quot;</span>] = encoded_inputs.boxes

<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> examples`}}),ta=new w({props:{code:"ZGF0YXNldF93aXRoX29jciUyMCUzRCUyMHVwZGF0ZWRfZGF0YXNldC5tYXAoZ2V0X29jcl93b3Jkc19hbmRfYm94ZXMlMkMlMjBiYXRjaGVkJTNEVHJ1ZSUyQyUyMGJhdGNoX3NpemUlM0QyKQ==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>dataset_with_ocr = updated_dataset.<span class="hljs-built_in">map</span>(get_ocr_words_and_boxes, batched=<span class="hljs-literal">True</span>, batch_size=<span class="hljs-number">2</span>)'}}),la=new Cs({}),na=new w({props:{code:"dG9rZW5pemVyJTIwJTNEJTIwcHJvY2Vzc29yLnRva2VuaXplcg==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = processor.tokenizer'}}),oa=new w({props:{code:"ZGVmJTIwc3ViZmluZGVyKHdvcmRzX2xpc3QlMkMlMjBhbnN3ZXJfbGlzdCklM0ElMEElMjAlMjAlMjAlMjBtYXRjaGVzJTIwJTNEJTIwJTVCJTVEJTBBJTIwJTIwJTIwJTIwc3RhcnRfaW5kaWNlcyUyMCUzRCUyMCU1QiU1RCUwQSUyMCUyMCUyMCUyMGVuZF9pbmRpY2VzJTIwJTNEJTIwJTVCJTVEJTBBJTIwJTIwJTIwJTIwZm9yJTIwaWR4JTJDJTIwaSUyMGluJTIwZW51bWVyYXRlKHJhbmdlKGxlbih3b3Jkc19saXN0KSkpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaWYlMjB3b3Jkc19saXN0JTVCaSU1RCUyMCUzRCUzRCUyMGFuc3dlcl9saXN0JTVCMCU1RCUyMGFuZCUyMHdvcmRzX2xpc3QlNUJpJTIwJTNBJTIwaSUyMCUyQiUyMGxlbihhbnN3ZXJfbGlzdCklNUQlMjAlM0QlM0QlMjBhbnN3ZXJfbGlzdCUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG1hdGNoZXMuYXBwZW5kKGFuc3dlcl9saXN0KSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHN0YXJ0X2luZGljZXMuYXBwZW5kKGlkeCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBlbmRfaW5kaWNlcy5hcHBlbmQoaWR4JTIwJTJCJTIwbGVuKGFuc3dlcl9saXN0KSUyMC0lMjAxKSUwQSUyMCUyMCUyMCUyMGlmJTIwbWF0Y2hlcyUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJldHVybiUyMG1hdGNoZXMlNUIwJTVEJTJDJTIwc3RhcnRfaW5kaWNlcyU1QjAlNUQlMkMlMjBlbmRfaW5kaWNlcyU1QjAlNUQlMEElMjAlMjAlMjAlMjBlbHNlJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwTm9uZSUyQyUyMDAlMkMlMjAw",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">subfinder</span>(<span class="hljs-params">words_list, answer_list</span>):
<span class="hljs-meta">... </span>    matches = []
<span class="hljs-meta">... </span>    start_indices = []
<span class="hljs-meta">... </span>    end_indices = []
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> idx, i <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(<span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(words_list))):
<span class="hljs-meta">... </span>        <span class="hljs-keyword">if</span> words_list[i] == answer_list[<span class="hljs-number">0</span>] <span class="hljs-keyword">and</span> words_list[i : i + <span class="hljs-built_in">len</span>(answer_list)] == answer_list:
<span class="hljs-meta">... </span>            matches.append(answer_list)
<span class="hljs-meta">... </span>            start_indices.append(idx)
<span class="hljs-meta">... </span>            end_indices.append(idx + <span class="hljs-built_in">len</span>(answer_list) - <span class="hljs-number">1</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> matches:
<span class="hljs-meta">... </span>        <span class="hljs-keyword">return</span> matches[<span class="hljs-number">0</span>], start_indices[<span class="hljs-number">0</span>], end_indices[<span class="hljs-number">0</span>]
<span class="hljs-meta">... </span>    <span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>        <span class="hljs-keyword">return</span> <span class="hljs-literal">None</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>`}}),ia=new w({props:{code:"ZXhhbXBsZSUyMCUzRCUyMGRhdGFzZXRfd2l0aF9vY3IlNUIlMjJ0cmFpbiUyMiU1RCU1QjElNUQlMEF3b3JkcyUyMCUzRCUyMCU1QndvcmQubG93ZXIoKSUyMGZvciUyMHdvcmQlMjBpbiUyMGV4YW1wbGUlNUIlMjJ3b3JkcyUyMiU1RCU1RCUwQW1hdGNoJTJDJTIwd29yZF9pZHhfc3RhcnQlMkMlMjB3b3JkX2lkeF9lbmQlMjAlM0QlMjBzdWJmaW5kZXIod29yZHMlMkMlMjBleGFtcGxlJTVCJTIyYW5zd2VyJTIyJTVELmxvd2VyKCkuc3BsaXQoKSklMEFwcmludCglMjJRdWVzdGlvbiUzQSUyMCUyMiUyQyUyMGV4YW1wbGUlNUIlMjJxdWVzdGlvbiUyMiU1RCklMEFwcmludCglMjJXb3JkcyUzQSUyMiUyQyUyMHdvcmRzKSUwQXByaW50KCUyMkFuc3dlciUzQSUyMCUyMiUyQyUyMGV4YW1wbGUlNUIlMjJhbnN3ZXIlMjIlNUQpJTBBcHJpbnQoJTIyc3RhcnRfaW5kZXglMjIlMkMlMjB3b3JkX2lkeF9zdGFydCklMEFwcmludCglMjJlbmRfaW5kZXglMjIlMkMlMjB3b3JkX2lkeF9lbmQp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>example = dataset_with_ocr[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">1</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>words = [word.lower() <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> example[<span class="hljs-string">&quot;words&quot;</span>]]
<span class="hljs-meta">&gt;&gt;&gt; </span>match, word_idx_start, word_idx_end = subfinder(words, example[<span class="hljs-string">&quot;answer&quot;</span>].lower().split())
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Question: &quot;</span>, example[<span class="hljs-string">&quot;question&quot;</span>])
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Words:&quot;</span>, words)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Answer: &quot;</span>, example[<span class="hljs-string">&quot;answer&quot;</span>])
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;start_index&quot;</span>, word_idx_start)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;end_index&quot;</span>, word_idx_end)
Question:  Who <span class="hljs-keyword">is</span> <span class="hljs-keyword">in</span>  cc <span class="hljs-keyword">in</span> this letter?
Words: [<span class="hljs-string">&#x27;wie&#x27;</span>, <span class="hljs-string">&#x27;baw&#x27;</span>, <span class="hljs-string">&#x27;brown&#x27;</span>, <span class="hljs-string">&#x27;&amp;&#x27;</span>, <span class="hljs-string">&#x27;williamson&#x27;</span>, <span class="hljs-string">&#x27;tobacco&#x27;</span>, <span class="hljs-string">&#x27;corporation&#x27;</span>, <span class="hljs-string">&#x27;research&#x27;</span>, <span class="hljs-string">&#x27;&amp;&#x27;</span>, <span class="hljs-string">&#x27;development&#x27;</span>, <span class="hljs-string">&#x27;internal&#x27;</span>, <span class="hljs-string">&#x27;correspondence&#x27;</span>, <span class="hljs-string">&#x27;to:&#x27;</span>, <span class="hljs-string">&#x27;r.&#x27;</span>, <span class="hljs-string">&#x27;h.&#x27;</span>, <span class="hljs-string">&#x27;honeycutt&#x27;</span>, <span class="hljs-string">&#x27;ce:&#x27;</span>, <span class="hljs-string">&#x27;t.f.&#x27;</span>, <span class="hljs-string">&#x27;riehl&#x27;</span>, <span class="hljs-string">&#x27;from:&#x27;</span>, <span class="hljs-string">&#x27;.&#x27;</span>, <span class="hljs-string">&#x27;c.j.&#x27;</span>, <span class="hljs-string">&#x27;cook&#x27;</span>, <span class="hljs-string">&#x27;date:&#x27;</span>, <span class="hljs-string">&#x27;may&#x27;</span>, <span class="hljs-string">&#x27;8,&#x27;</span>, <span class="hljs-string">&#x27;1995&#x27;</span>, <span class="hljs-string">&#x27;subject:&#x27;</span>, <span class="hljs-string">&#x27;review&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;existing&#x27;</span>, <span class="hljs-string">&#x27;brainstorming&#x27;</span>, <span class="hljs-string">&#x27;ideas/483&#x27;</span>, <span class="hljs-string">&#x27;the&#x27;</span>, <span class="hljs-string">&#x27;major&#x27;</span>, <span class="hljs-string">&#x27;function&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;the&#x27;</span>, <span class="hljs-string">&#x27;product&#x27;</span>, <span class="hljs-string">&#x27;innovation&#x27;</span>, <span class="hljs-string">&#x27;graup&#x27;</span>, <span class="hljs-string">&#x27;is&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;develop&#x27;</span>, <span class="hljs-string">&#x27;marketable&#x27;</span>, <span class="hljs-string">&#x27;nove!&#x27;</span>, <span class="hljs-string">&#x27;products&#x27;</span>, <span class="hljs-string">&#x27;that&#x27;</span>, <span class="hljs-string">&#x27;would&#x27;</span>, <span class="hljs-string">&#x27;be&#x27;</span>, <span class="hljs-string">&#x27;profitable&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;manufacture&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;sell.&#x27;</span>, <span class="hljs-string">&#x27;novel&#x27;</span>, <span class="hljs-string">&#x27;is&#x27;</span>, <span class="hljs-string">&#x27;defined&#x27;</span>, <span class="hljs-string">&#x27;as:&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;new&#x27;</span>, <span class="hljs-string">&#x27;kind,&#x27;</span>, <span class="hljs-string">&#x27;or&#x27;</span>, <span class="hljs-string">&#x27;different&#x27;</span>, <span class="hljs-string">&#x27;from&#x27;</span>, <span class="hljs-string">&#x27;anything&#x27;</span>, <span class="hljs-string">&#x27;seen&#x27;</span>, <span class="hljs-string">&#x27;or&#x27;</span>, <span class="hljs-string">&#x27;known&#x27;</span>, <span class="hljs-string">&#x27;before.&#x27;</span>, <span class="hljs-string">&#x27;innovation&#x27;</span>, <span class="hljs-string">&#x27;is&#x27;</span>, <span class="hljs-string">&#x27;defined&#x27;</span>, <span class="hljs-string">&#x27;as:&#x27;</span>, <span class="hljs-string">&#x27;something&#x27;</span>, <span class="hljs-string">&#x27;new&#x27;</span>, <span class="hljs-string">&#x27;or&#x27;</span>, <span class="hljs-string">&#x27;different&#x27;</span>, <span class="hljs-string">&#x27;introduced;&#x27;</span>, <span class="hljs-string">&#x27;act&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;innovating;&#x27;</span>, <span class="hljs-string">&#x27;introduction&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;new&#x27;</span>, <span class="hljs-string">&#x27;things&#x27;</span>, <span class="hljs-string">&#x27;or&#x27;</span>, <span class="hljs-string">&#x27;methods.&#x27;</span>, <span class="hljs-string">&#x27;the&#x27;</span>, <span class="hljs-string">&#x27;products&#x27;</span>, <span class="hljs-string">&#x27;may&#x27;</span>, <span class="hljs-string">&#x27;incorporate&#x27;</span>, <span class="hljs-string">&#x27;the&#x27;</span>, <span class="hljs-string">&#x27;latest&#x27;</span>, <span class="hljs-string">&#x27;technologies,&#x27;</span>, <span class="hljs-string">&#x27;materials&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;know-how&#x27;</span>, <span class="hljs-string">&#x27;available&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;give&#x27;</span>, <span class="hljs-string">&#x27;then&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;unique&#x27;</span>, <span class="hljs-string">&#x27;taste&#x27;</span>, <span class="hljs-string">&#x27;or&#x27;</span>, <span class="hljs-string">&#x27;look.&#x27;</span>, <span class="hljs-string">&#x27;the&#x27;</span>, <span class="hljs-string">&#x27;first&#x27;</span>, <span class="hljs-string">&#x27;task&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;the&#x27;</span>, <span class="hljs-string">&#x27;product&#x27;</span>, <span class="hljs-string">&#x27;innovation&#x27;</span>, <span class="hljs-string">&#x27;group&#x27;</span>, <span class="hljs-string">&#x27;was&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;assemble,&#x27;</span>, <span class="hljs-string">&#x27;review&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;categorize&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;list&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;existing&#x27;</span>, <span class="hljs-string">&#x27;brainstorming&#x27;</span>, <span class="hljs-string">&#x27;ideas.&#x27;</span>, <span class="hljs-string">&#x27;ideas&#x27;</span>, <span class="hljs-string">&#x27;were&#x27;</span>, <span class="hljs-string">&#x27;grouped&#x27;</span>, <span class="hljs-string">&#x27;into&#x27;</span>, <span class="hljs-string">&#x27;two&#x27;</span>, <span class="hljs-string">&#x27;major&#x27;</span>, <span class="hljs-string">&#x27;categories&#x27;</span>, <span class="hljs-string">&#x27;labeled&#x27;</span>, <span class="hljs-string">&#x27;appearance&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;taste/aroma.&#x27;</span>, <span class="hljs-string">&#x27;these&#x27;</span>, <span class="hljs-string">&#x27;categories&#x27;</span>, <span class="hljs-string">&#x27;are&#x27;</span>, <span class="hljs-string">&#x27;used&#x27;</span>, <span class="hljs-string">&#x27;for&#x27;</span>, <span class="hljs-string">&#x27;novel&#x27;</span>, <span class="hljs-string">&#x27;products&#x27;</span>, <span class="hljs-string">&#x27;that&#x27;</span>, <span class="hljs-string">&#x27;may&#x27;</span>, <span class="hljs-string">&#x27;differ&#x27;</span>, <span class="hljs-string">&#x27;from&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;visual&#x27;</span>, <span class="hljs-string">&#x27;and/or&#x27;</span>, <span class="hljs-string">&#x27;taste/aroma&#x27;</span>, <span class="hljs-string">&#x27;point&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;view&#x27;</span>, <span class="hljs-string">&#x27;compared&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;canventional&#x27;</span>, <span class="hljs-string">&#x27;cigarettes.&#x27;</span>, <span class="hljs-string">&#x27;other&#x27;</span>, <span class="hljs-string">&#x27;categories&#x27;</span>, <span class="hljs-string">&#x27;include&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;combination&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;the&#x27;</span>, <span class="hljs-string">&#x27;above,&#x27;</span>, <span class="hljs-string">&#x27;filters,&#x27;</span>, <span class="hljs-string">&#x27;packaging&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;brand&#x27;</span>, <span class="hljs-string">&#x27;extensions.&#x27;</span>, <span class="hljs-string">&#x27;appearance&#x27;</span>, <span class="hljs-string">&#x27;this&#x27;</span>, <span class="hljs-string">&#x27;category&#x27;</span>, <span class="hljs-string">&#x27;is&#x27;</span>, <span class="hljs-string">&#x27;used&#x27;</span>, <span class="hljs-string">&#x27;for&#x27;</span>, <span class="hljs-string">&#x27;novel&#x27;</span>, <span class="hljs-string">&#x27;cigarette&#x27;</span>, <span class="hljs-string">&#x27;constructions&#x27;</span>, <span class="hljs-string">&#x27;that&#x27;</span>, <span class="hljs-string">&#x27;yield&#x27;</span>, <span class="hljs-string">&#x27;visually&#x27;</span>, <span class="hljs-string">&#x27;different&#x27;</span>, <span class="hljs-string">&#x27;products&#x27;</span>, <span class="hljs-string">&#x27;with&#x27;</span>, <span class="hljs-string">&#x27;minimal&#x27;</span>, <span class="hljs-string">&#x27;changes&#x27;</span>, <span class="hljs-string">&#x27;in&#x27;</span>, <span class="hljs-string">&#x27;smoke&#x27;</span>, <span class="hljs-string">&#x27;chemistry&#x27;</span>, <span class="hljs-string">&#x27;two&#x27;</span>, <span class="hljs-string">&#x27;cigarettes&#x27;</span>, <span class="hljs-string">&#x27;in&#x27;</span>, <span class="hljs-string">&#x27;cne.&#x27;</span>, <span class="hljs-string">&#x27;emulti-plug&#x27;</span>, <span class="hljs-string">&#x27;te&#x27;</span>, <span class="hljs-string">&#x27;build&#x27;</span>, <span class="hljs-string">&#x27;yaur&#x27;</span>, <span class="hljs-string">&#x27;awn&#x27;</span>, <span class="hljs-string">&#x27;cigarette.&#x27;</span>, <span class="hljs-string">&#x27;eswitchable&#x27;</span>, <span class="hljs-string">&#x27;menthol&#x27;</span>, <span class="hljs-string">&#x27;or&#x27;</span>, <span class="hljs-string">&#x27;non&#x27;</span>, <span class="hljs-string">&#x27;menthol&#x27;</span>, <span class="hljs-string">&#x27;cigarette.&#x27;</span>, <span class="hljs-string">&#x27;*cigarettes&#x27;</span>, <span class="hljs-string">&#x27;with&#x27;</span>, <span class="hljs-string">&#x27;interspaced&#x27;</span>, <span class="hljs-string">&#x27;perforations&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;enable&#x27;</span>, <span class="hljs-string">&#x27;smoker&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;separate&#x27;</span>, <span class="hljs-string">&#x27;unburned&#x27;</span>, <span class="hljs-string">&#x27;section&#x27;</span>, <span class="hljs-string">&#x27;for&#x27;</span>, <span class="hljs-string">&#x27;future&#x27;</span>, <span class="hljs-string">&#x27;smoking.&#x27;</span>, <span class="hljs-string">&#x27;\xABshort&#x27;</span>, <span class="hljs-string">&#x27;cigarette,&#x27;</span>, <span class="hljs-string">&#x27;tobacco&#x27;</span>, <span class="hljs-string">&#x27;section&#x27;</span>, <span class="hljs-string">&#x27;30&#x27;</span>, <span class="hljs-string">&#x27;mm.&#x27;</span>, <span class="hljs-string">&#x27;\xABextremely&#x27;</span>, <span class="hljs-string">&#x27;fast&#x27;</span>, <span class="hljs-string">&#x27;buming&#x27;</span>, <span class="hljs-string">&#x27;cigarette.&#x27;</span>, <span class="hljs-string">&#x27;\xABnovel&#x27;</span>, <span class="hljs-string">&#x27;cigarette&#x27;</span>, <span class="hljs-string">&#x27;constructions&#x27;</span>, <span class="hljs-string">&#x27;that&#x27;</span>, <span class="hljs-string">&#x27;permit&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;significant&#x27;</span>, <span class="hljs-string">&#x27;reduction&#x27;</span>, <span class="hljs-string">&#x27;iretobacco&#x27;</span>, <span class="hljs-string">&#x27;weight&#x27;</span>, <span class="hljs-string">&#x27;while&#x27;</span>, <span class="hljs-string">&#x27;maintaining&#x27;</span>, <span class="hljs-string">&#x27;smoking&#x27;</span>, <span class="hljs-string">&#x27;mechanics&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;visual&#x27;</span>, <span class="hljs-string">&#x27;characteristics.&#x27;</span>, <span class="hljs-string">&#x27;higher&#x27;</span>, <span class="hljs-string">&#x27;basis&#x27;</span>, <span class="hljs-string">&#x27;weight&#x27;</span>, <span class="hljs-string">&#x27;paper:&#x27;</span>, <span class="hljs-string">&#x27;potential&#x27;</span>, <span class="hljs-string">&#x27;reduction&#x27;</span>, <span class="hljs-string">&#x27;in&#x27;</span>, <span class="hljs-string">&#x27;tobacco&#x27;</span>, <span class="hljs-string">&#x27;weight.&#x27;</span>, <span class="hljs-string">&#x27;\xABmore&#x27;</span>, <span class="hljs-string">&#x27;rigid&#x27;</span>, <span class="hljs-string">&#x27;tobacco&#x27;</span>, <span class="hljs-string">&#x27;column;&#x27;</span>, <span class="hljs-string">&#x27;stiffing&#x27;</span>, <span class="hljs-string">&#x27;agent&#x27;</span>, <span class="hljs-string">&#x27;for&#x27;</span>, <span class="hljs-string">&#x27;tobacco;&#x27;</span>, <span class="hljs-string">&#x27;e.g.&#x27;</span>, <span class="hljs-string">&#x27;starch&#x27;</span>, <span class="hljs-string">&#x27;*colored&#x27;</span>, <span class="hljs-string">&#x27;tow&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;cigarette&#x27;</span>, <span class="hljs-string">&#x27;papers;&#x27;</span>, <span class="hljs-string">&#x27;seasonal&#x27;</span>, <span class="hljs-string">&#x27;promotions,&#x27;</span>, <span class="hljs-string">&#x27;e.g.&#x27;</span>, <span class="hljs-string">&#x27;pastel&#x27;</span>, <span class="hljs-string">&#x27;colored&#x27;</span>, <span class="hljs-string">&#x27;cigarettes&#x27;</span>, <span class="hljs-string">&#x27;for&#x27;</span>, <span class="hljs-string">&#x27;easter&#x27;</span>, <span class="hljs-string">&#x27;or&#x27;</span>, <span class="hljs-string">&#x27;in&#x27;</span>, <span class="hljs-string">&#x27;an&#x27;</span>, <span class="hljs-string">&#x27;ebony&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;ivory&#x27;</span>, <span class="hljs-string">&#x27;brand&#x27;</span>, <span class="hljs-string">&#x27;containing&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;mixture&#x27;</span>, <span class="hljs-string">&#x27;of&#x27;</span>, <span class="hljs-string">&#x27;all&#x27;</span>, <span class="hljs-string">&#x27;black&#x27;</span>, <span class="hljs-string">&#x27;(black&#x27;</span>, <span class="hljs-string">&#x27;paper&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;tow)&#x27;</span>, <span class="hljs-string">&#x27;and&#x27;</span>, <span class="hljs-string">&#x27;ail&#x27;</span>, <span class="hljs-string">&#x27;white&#x27;</span>, <span class="hljs-string">&#x27;cigarettes.&#x27;</span>, <span class="hljs-string">&#x27;499150498&#x27;</span>]
Answer:  T.F. Riehl
start_index <span class="hljs-number">17</span>
end_index <span class="hljs-number">18</span>`}}),pa=new w({props:{code:"ZW5jb2RpbmclMjAlM0QlMjB0b2tlbml6ZXIoZXhhbXBsZSU1QiUyMnF1ZXN0aW9uJTIyJTVEJTJDJTIwZXhhbXBsZSU1QiUyMndvcmRzJTIyJTVEJTJDJTIwZXhhbXBsZSU1QiUyMmJveGVzJTIyJTVEKSUwQXRva2VuaXplci5kZWNvZGUoZW5jb2RpbmclNUIlMjJpbnB1dF9pZHMlMjIlNUQp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>encoding = tokenizer(example[<span class="hljs-string">&quot;question&quot;</span>], example[<span class="hljs-string">&quot;words&quot;</span>], example[<span class="hljs-string">&quot;boxes&quot;</span>])
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.decode(encoding[<span class="hljs-string">&quot;input_ids&quot;</span>])
[CLS] who <span class="hljs-keyword">is</span> <span class="hljs-keyword">in</span> cc <span class="hljs-keyword">in</span> this letter? [SEP] wie baw brown &amp; williamson tobacco corporation research &amp; development ...`}}),ra=new w({props:{code:" ",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">encode_dataset</span>(<span class="hljs-params">examples, max_length=<span class="hljs-number">512</span></span>):
<span class="hljs-meta">... </span>    questions = examples[<span class="hljs-string">&quot;question&quot;</span>]
<span class="hljs-meta">... </span>    words = examples[<span class="hljs-string">&quot;words&quot;</span>]
<span class="hljs-meta">... </span>    boxes = examples[<span class="hljs-string">&quot;boxes&quot;</span>]
<span class="hljs-meta">... </span>    answers = examples[<span class="hljs-string">&quot;answer&quot;</span>]

<span class="hljs-meta">... </span>    <span class="hljs-comment"># encode the batch of examples and initialize the start_positions and end_positions</span>
<span class="hljs-meta">... </span>    encoding = tokenizer(questions, words, boxes, max_length=max_length, padding=<span class="hljs-string">&quot;max_length&quot;</span>, truncation=<span class="hljs-literal">True</span>)
<span class="hljs-meta">... </span>    start_positions = []
<span class="hljs-meta">... </span>    end_positions = []

<span class="hljs-meta">... </span>    <span class="hljs-comment"># loop through the examples in the batch</span>
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(questions)):
<span class="hljs-meta">... </span>        cls_index = encoding[<span class="hljs-string">&quot;input_ids&quot;</span>][i].index(tokenizer.cls_token_id)

<span class="hljs-meta">... </span>        <span class="hljs-comment"># find the position of the answer in example&#x27;s words</span>
<span class="hljs-meta">... </span>        words_example = [word.lower() <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> words[i]]
<span class="hljs-meta">... </span>        answer = answers[i]
<span class="hljs-meta">... </span>        match, word_idx_start, word_idx_end = subfinder(words_example, answer.lower().split())

<span class="hljs-meta">... </span>        <span class="hljs-keyword">if</span> match:
<span class="hljs-meta">... </span>            <span class="hljs-comment"># if match is found, use \`token_type_ids\` to find where words start in the encoding</span>
<span class="hljs-meta">... </span>            token_type_ids = encoding[<span class="hljs-string">&quot;token_type_ids&quot;</span>][i]
<span class="hljs-meta">... </span>            token_start_index = <span class="hljs-number">0</span>
<span class="hljs-meta">... </span>            <span class="hljs-keyword">while</span> token_type_ids[token_start_index] != <span class="hljs-number">1</span>:
<span class="hljs-meta">... </span>                token_start_index += <span class="hljs-number">1</span>

<span class="hljs-meta">... </span>            token_end_index = <span class="hljs-built_in">len</span>(encoding[<span class="hljs-string">&quot;input_ids&quot;</span>][i]) - <span class="hljs-number">1</span>
<span class="hljs-meta">... </span>            <span class="hljs-keyword">while</span> token_type_ids[token_end_index] != <span class="hljs-number">1</span>:
<span class="hljs-meta">... </span>                token_end_index -= <span class="hljs-number">1</span>

<span class="hljs-meta">... </span>            word_ids = encoding.word_ids(i)[token_start_index : token_end_index + <span class="hljs-number">1</span>]
<span class="hljs-meta">... </span>            start_position = cls_index
<span class="hljs-meta">... </span>            end_position = cls_index

<span class="hljs-meta">... </span>            <span class="hljs-comment"># loop over word_ids and increase \`token_start_index\` until it matches the answer position in words</span>
<span class="hljs-meta">... </span>            <span class="hljs-comment"># once it matches, save the \`token_start_index\` as the \`start_position\` of the answer in the encoding</span>
<span class="hljs-meta">... </span>            <span class="hljs-keyword">for</span> <span class="hljs-built_in">id</span> <span class="hljs-keyword">in</span> word_ids:
<span class="hljs-meta">... </span>                <span class="hljs-keyword">if</span> <span class="hljs-built_in">id</span> == word_idx_start:
<span class="hljs-meta">... </span>                    start_position = token_start_index
<span class="hljs-meta">... </span>                <span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>                    token_start_index += <span class="hljs-number">1</span>

<span class="hljs-meta">... </span>            <span class="hljs-comment"># similarly loop over \`word_ids\` starting from the end to find the \`end_position\` of the answer</span>
<span class="hljs-meta">... </span>            <span class="hljs-keyword">for</span> <span class="hljs-built_in">id</span> <span class="hljs-keyword">in</span> word_ids[::-<span class="hljs-number">1</span>]:
<span class="hljs-meta">... </span>                <span class="hljs-keyword">if</span> <span class="hljs-built_in">id</span> == word_idx_end:
<span class="hljs-meta">... </span>                    end_position = token_end_index
<span class="hljs-meta">... </span>                <span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>                    token_end_index -= <span class="hljs-number">1</span>

<span class="hljs-meta">... </span>            start_positions.append(start_position)
<span class="hljs-meta">... </span>            end_positions.append(end_position)

<span class="hljs-meta">... </span>        <span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>            start_positions.append(cls_index)
<span class="hljs-meta">... </span>            end_positions.append(cls_index)

<span class="hljs-meta">... </span>    encoding[<span class="hljs-string">&quot;image&quot;</span>] = examples[<span class="hljs-string">&quot;image&quot;</span>]
<span class="hljs-meta">... </span>    encoding[<span class="hljs-string">&quot;start_positions&quot;</span>] = start_positions
<span class="hljs-meta">... </span>    encoding[<span class="hljs-string">&quot;end_positions&quot;</span>] = end_positions

<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> encoding`}}),ca=new w({props:{code:"ZW5jb2RlZF90cmFpbl9kYXRhc2V0JTIwJTNEJTIwZGF0YXNldF93aXRoX29jciU1QiUyMnRyYWluJTIyJTVELm1hcCglMEElMjAlMjAlMjAlMjBlbmNvZGVfZGF0YXNldCUyQyUyMGJhdGNoZWQlM0RUcnVlJTJDJTIwYmF0Y2hfc2l6ZSUzRDIlMkMlMjByZW1vdmVfY29sdW1ucyUzRGRhdGFzZXRfd2l0aF9vY3IlNUIlMjJ0cmFpbiUyMiU1RC5jb2x1bW5fbmFtZXMlMEEpJTBBZW5jb2RlZF90ZXN0X2RhdGFzZXQlMjAlM0QlMjBkYXRhc2V0X3dpdGhfb2NyJTVCJTIydGVzdCUyMiU1RC5tYXAoJTBBJTIwJTIwJTIwJTIwZW5jb2RlX2RhdGFzZXQlMkMlMjBiYXRjaGVkJTNEVHJ1ZSUyQyUyMGJhdGNoX3NpemUlM0QyJTJDJTIwcmVtb3ZlX2NvbHVtbnMlM0RkYXRhc2V0X3dpdGhfb2NyJTVCJTIydGVzdCUyMiU1RC5jb2x1bW5fbmFtZXMlMEEp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>encoded_train_dataset = dataset_with_ocr[<span class="hljs-string">&quot;train&quot;</span>].<span class="hljs-built_in">map</span>(
<span class="hljs-meta">... </span>    encode_dataset, batched=<span class="hljs-literal">True</span>, batch_size=<span class="hljs-number">2</span>, remove_columns=dataset_with_ocr[<span class="hljs-string">&quot;train&quot;</span>].column_names
<span class="hljs-meta">... </span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>encoded_test_dataset = dataset_with_ocr[<span class="hljs-string">&quot;test&quot;</span>].<span class="hljs-built_in">map</span>(
<span class="hljs-meta">... </span>    encode_dataset, batched=<span class="hljs-literal">True</span>, batch_size=<span class="hljs-number">2</span>, remove_columns=dataset_with_ocr[<span class="hljs-string">&quot;test&quot;</span>].column_names
<span class="hljs-meta">... </span>)`}}),ha=new w({props:{code:"ZW5jb2RlZF90cmFpbl9kYXRhc2V0LmZlYXR1cmVz",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>encoded_train_dataset.features
{<span class="hljs-string">&#x27;image&#x27;</span>: <span class="hljs-type">Sequence</span>(feature=<span class="hljs-type">Sequence</span>(feature=<span class="hljs-type">Sequence</span>(feature=Value(dtype=<span class="hljs-string">&#x27;uint8&#x27;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
 <span class="hljs-string">&#x27;input_ids&#x27;</span>: <span class="hljs-type">Sequence</span>(feature=Value(dtype=<span class="hljs-string">&#x27;int32&#x27;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
 <span class="hljs-string">&#x27;token_type_ids&#x27;</span>: <span class="hljs-type">Sequence</span>(feature=Value(dtype=<span class="hljs-string">&#x27;int8&#x27;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
 <span class="hljs-string">&#x27;attention_mask&#x27;</span>: <span class="hljs-type">Sequence</span>(feature=Value(dtype=<span class="hljs-string">&#x27;int8&#x27;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
 <span class="hljs-string">&#x27;bbox&#x27;</span>: <span class="hljs-type">Sequence</span>(feature=<span class="hljs-type">Sequence</span>(feature=Value(dtype=<span class="hljs-string">&#x27;int64&#x27;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
 <span class="hljs-string">&#x27;start_positions&#x27;</span>: Value(dtype=<span class="hljs-string">&#x27;int64&#x27;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
 <span class="hljs-string">&#x27;end_positions&#x27;</span>: Value(dtype=<span class="hljs-string">&#x27;int64&#x27;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>)}`}}),da=new Cs({}),ma=new Cs({}),ya=new w({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvckRvY3VtZW50UXVlc3Rpb25BbnN3ZXJpbmclMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvckRvY3VtZW50UXVlc3Rpb25BbnN3ZXJpbmcuZnJvbV9wcmV0cmFpbmVkKG1vZGVsX2NoZWNrcG9pbnQp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForDocumentQuestionAnswering

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForDocumentQuestionAnswering.from_pretrained(model_checkpoint)`}}),xa=new w({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluaW5nQXJndW1lbnRzJTBBJTBBJTIzJTIwUkVQTEFDRSUyMFRISVMlMjBXSVRIJTIwWU9VUiUyMFJFUE8lMjBJRCUwQXJlcG9faWQlMjAlM0QlMjAlMjJNYXJpYUslMkZsYXlvdXRsbXYyLWJhc2UtdW5jYXNlZF9maW5ldHVuZWRfZG9jdnFhJTIyJTBBJTBBdHJhaW5pbmdfYXJncyUyMCUzRCUyMFRyYWluaW5nQXJndW1lbnRzKCUwQSUyMCUyMCUyMCUyMG91dHB1dF9kaXIlM0RyZXBvX2lkJTJDJTBBJTIwJTIwJTIwJTIwcGVyX2RldmljZV90cmFpbl9iYXRjaF9zaXplJTNENCUyQyUwQSUyMCUyMCUyMCUyMG51bV90cmFpbl9lcG9jaHMlM0QyMCUyQyUwQSUyMCUyMCUyMCUyMHNhdmVfc3RlcHMlM0QyMDAlMkMlMEElMjAlMjAlMjAlMjBsb2dnaW5nX3N0ZXBzJTNENTAlMkMlMEElMjAlMjAlMjAlMjBldmFsdWF0aW9uX3N0cmF0ZWd5JTNEJTIyc3RlcHMlMjIlMkMlMEElMjAlMjAlMjAlMjBsZWFybmluZ19yYXRlJTNENWUtNSUyQyUwQSUyMCUyMCUyMCUyMHNhdmVfdG90YWxfbGltaXQlM0QyJTJDJTBBJTIwJTIwJTIwJTIwcmVtb3ZlX3VudXNlZF9jb2x1bW5zJTNERmFsc2UlMkMlMEElMjAlMjAlMjAlMjBwdXNoX3RvX2h1YiUzRFRydWUlMkMlMEEp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># REPLACE THIS WITH YOUR REPO ID</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>repo_id = <span class="hljs-string">&quot;MariaK/layoutlmv2-base-uncased_finetuned_docvqa&quot;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments(
<span class="hljs-meta">... </span>    output_dir=repo_id,
<span class="hljs-meta">... </span>    per_device_train_batch_size=<span class="hljs-number">4</span>,
<span class="hljs-meta">... </span>    num_train_epochs=<span class="hljs-number">20</span>,
<span class="hljs-meta">... </span>    save_steps=<span class="hljs-number">200</span>,
<span class="hljs-meta">... </span>    logging_steps=<span class="hljs-number">50</span>,
<span class="hljs-meta">... </span>    evaluation_strategy=<span class="hljs-string">&quot;steps&quot;</span>,
<span class="hljs-meta">... </span>    learning_rate=<span class="hljs-number">5e-5</span>,
<span class="hljs-meta">... </span>    save_total_limit=<span class="hljs-number">2</span>,
<span class="hljs-meta">... </span>    remove_unused_columns=<span class="hljs-literal">False</span>,
<span class="hljs-meta">... </span>    push_to_hub=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>)`}}),ba=new w({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERlZmF1bHREYXRhQ29sbGF0b3IlMEElMEFkYXRhX2NvbGxhdG9yJTIwJTNEJTIwRGVmYXVsdERhdGFDb2xsYXRvcigp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DefaultDataCollator

<span class="hljs-meta">&gt;&gt;&gt; </span>data_collator = DefaultDataCollator()`}}),Ja=new w({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluZXIlMEElMEF0cmFpbmVyJTIwJTNEJTIwVHJhaW5lciglMEElMjAlMjAlMjAlMjBtb2RlbCUzRG1vZGVsJTJDJTBBJTIwJTIwJTIwJTIwYXJncyUzRHRyYWluaW5nX2FyZ3MlMkMlMEElMjAlMjAlMjAlMjBkYXRhX2NvbGxhdG9yJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMHRyYWluX2RhdGFzZXQlM0RlbmNvZGVkX3RyYWluX2RhdGFzZXQlMkMlMEElMjAlMjAlMjAlMjBldmFsX2RhdGFzZXQlM0RlbmNvZGVkX3Rlc3RfZGF0YXNldCUyQyUwQSUyMCUyMCUyMCUyMHRva2VuaXplciUzRHByb2Nlc3NvciUyQyUwQSklMEElMEF0cmFpbmVyLnRyYWluKCk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer

<span class="hljs-meta">&gt;&gt;&gt; </span>trainer = Trainer(
<span class="hljs-meta">... </span>    model=model,
<span class="hljs-meta">... </span>    args=training_args,
<span class="hljs-meta">... </span>    data_collator=data_collator,
<span class="hljs-meta">... </span>    train_dataset=encoded_train_dataset,
<span class="hljs-meta">... </span>    eval_dataset=encoded_test_dataset,
<span class="hljs-meta">... </span>    tokenizer=processor,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()`}}),Ua=new w({props:{code:"dHJhaW5lci5jcmVhdGVfbW9kZWxfY2FyZCgpJTBBdHJhaW5lci5wdXNoX3RvX2h1Yigp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.create_model_card()
<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.push_to_hub()`}}),Ta=new Cs({}),_a=new w({props:{code:"ZXhhbXBsZSUyMCUzRCUyMGRhdGFzZXQlNUIlMjJ0ZXN0JTIyJTVEJTVCMiU1RCUwQXF1ZXN0aW9uJTIwJTNEJTIwZXhhbXBsZSU1QiUyMnF1ZXJ5JTIyJTVEJTVCJTIyZW4lMjIlNUQlMEFpbWFnZSUyMCUzRCUyMGV4YW1wbGUlNUIlMjJpbWFnZSUyMiU1RCUwQXByaW50KHF1ZXN0aW9uKSUwQXByaW50KGV4YW1wbGUlNUIlMjJhbnN3ZXJzJTIyJTVEKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>example = dataset[<span class="hljs-string">&quot;test&quot;</span>][<span class="hljs-number">2</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>question = example[<span class="hljs-string">&quot;query&quot;</span>][<span class="hljs-string">&quot;en&quot;</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>image = example[<span class="hljs-string">&quot;image&quot;</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(question)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(example[<span class="hljs-string">&quot;answers&quot;</span>])
<span class="hljs-string">&#x27;Who is \u2018presiding\u2019 TRRF GENERAL SESSION (PART 1)?&#x27;</span>
[<span class="hljs-string">&#x27;TRRF Vice President&#x27;</span>, <span class="hljs-string">&#x27;lee a. waller&#x27;</span>]`}}),va=new w({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBcWFfcGlwZWxpbmUlMjAlM0QlMjBwaXBlbGluZSglMjJkb2N1bWVudC1xdWVzdGlvbi1hbnN3ZXJpbmclMjIlMkMlMjBtb2RlbCUzRCUyMk1hcmlhSyUyRmxheW91dGxtdjItYmFzZS11bmNhc2VkX2ZpbmV0dW5lZF9kb2N2cWElMjIpJTBBcWFfcGlwZWxpbmUoaW1hZ2UlMkMlMjBxdWVzdGlvbik=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

<span class="hljs-meta">&gt;&gt;&gt; </span>qa_pipeline = pipeline(<span class="hljs-string">&quot;document-question-answering&quot;</span>, model=<span class="hljs-string">&quot;MariaK/layoutlmv2-base-uncased_finetuned_docvqa&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>qa_pipeline(image, question)
[{<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.9949808120727539</span>,
  <span class="hljs-string">&#x27;answer&#x27;</span>: <span class="hljs-string">&#x27;Lee A. Waller&#x27;</span>,
  <span class="hljs-string">&#x27;start&#x27;</span>: <span class="hljs-number">55</span>,
  <span class="hljs-string">&#x27;end&#x27;</span>: <span class="hljs-number">57</span>}]`}}),Ia=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Byb2Nlc3NvciUwQWZyb20lMjB0cmFuc2Zvcm1lcnMlMjBpbXBvcnQlMjBBdXRvTW9kZWxGb3JEb2N1bWVudFF1ZXN0aW9uQW5zd2VyaW5nJTBBJTBBcHJvY2Vzc29yJTIwJTNEJTIwQXV0b1Byb2Nlc3Nvci5mcm9tX3ByZXRyYWluZWQoJTIyTWFyaWFLJTJGbGF5b3V0bG12Mi1iYXNlLXVuY2FzZWRfZmluZXR1bmVkX2RvY3ZxYSUyMiklMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvckRvY3VtZW50UXVlc3Rpb25BbnN3ZXJpbmcuZnJvbV9wcmV0cmFpbmVkKCUyMk1hcmlhSyUyRmxheW91dGxtdjItYmFzZS11bmNhc2VkX2ZpbmV0dW5lZF9kb2N2cWElMjIpJTBBJTBBd2l0aCUyMHRvcmNoLm5vX2dyYWQoKSUzQSUwQSUyMCUyMCUyMCUyMGVuY29kaW5nJTIwJTNEJTIwcHJvY2Vzc29yKGltYWdlLmNvbnZlcnQoJTIyUkdCJTIyKSUyQyUyMHF1ZXN0aW9uJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMiklMEElMjAlMjAlMjAlMjBvdXRwdXRzJTIwJTNEJTIwbW9kZWwoKiplbmNvZGluZyklMEElMjAlMjAlMjAlMjBzdGFydF9sb2dpdHMlMjAlM0QlMjBvdXRwdXRzLnN0YXJ0X2xvZ2l0cyUwQSUyMCUyMCUyMCUyMGVuZF9sb2dpdHMlMjAlM0QlMjBvdXRwdXRzLmVuZF9sb2dpdHMlMEElMjAlMjAlMjAlMjBwcmVkaWN0ZWRfc3RhcnRfaWR4JTIwJTNEJTIwc3RhcnRfbG9naXRzLmFyZ21heCgtMSkuaXRlbSgpJTBBJTIwJTIwJTIwJTIwcHJlZGljdGVkX2VuZF9pZHglMjAlM0QlMjBlbmRfbG9naXRzLmFyZ21heCgtMSkuaXRlbSgpJTBBJTBBcHJvY2Vzc29yLnRva2VuaXplci5kZWNvZGUoZW5jb2RpbmcuaW5wdXRfaWRzLnNxdWVlemUoKSU1QnByZWRpY3RlZF9zdGFydF9pZHglMjAlM0ElMjBwcmVkaWN0ZWRfZW5kX2lkeCUyMCUyQiUyMDElNUQp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoProcessor
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForDocumentQuestionAnswering

<span class="hljs-meta">&gt;&gt;&gt; </span>processor = AutoProcessor.from_pretrained(<span class="hljs-string">&quot;MariaK/layoutlmv2-base-uncased_finetuned_docvqa&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForDocumentQuestionAnswering.from_pretrained(<span class="hljs-string">&quot;MariaK/layoutlmv2-base-uncased_finetuned_docvqa&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">with</span> torch.no_grad():
<span class="hljs-meta">... </span>    encoding = processor(image.convert(<span class="hljs-string">&quot;RGB&quot;</span>), question, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)
<span class="hljs-meta">... </span>    outputs = model(**encoding)
<span class="hljs-meta">... </span>    start_logits = outputs.start_logits
<span class="hljs-meta">... </span>    end_logits = outputs.end_logits
<span class="hljs-meta">... </span>    predicted_start_idx = start_logits.argmax(-<span class="hljs-number">1</span>).item()
<span class="hljs-meta">... </span>    predicted_end_idx = end_logits.argmax(-<span class="hljs-number">1</span>).item()

<span class="hljs-meta">&gt;&gt;&gt; </span>processor.tokenizer.decode(encoding.input_ids.squeeze()[predicted_start_idx : predicted_end_idx + <span class="hljs-number">1</span>])
<span class="hljs-string">&#x27;lee a. waller&#x27;</span>`}}),{c(){U=o("meta"),is=c(),I=o("h1"),x=o("a"),Z=o("span"),u(N.$$.fragment),Es=c(),R=o("span"),As=t("Document Question Answering"),ps=c(),u(k.$$.fragment),rs=c(),b=o("p"),F=t(`Document Question Answering, also referred to as Document Visual Question Answering, is a task that involves providing
answers to questions posed about document images. The input to models supporting this task is typically a combination of an image and
a question, and the output is an answer expressed in natural language. These models utilize multiple modalities, including
text, the positions of words (bounding boxes), and the image itself.`),V=c(),D=o("p"),Ca=t("This guide illustrates how to:"),Zs=c(),cs=o("ul"),P=o("li"),Tn=t("Fine-tune "),Ea=o("a"),_n=t("LayoutLMv2"),vn=t(" on the "),Rs=o("a"),In=t("DocVQA dataset"),kn=t("."),Cn=c(),Je=o("li"),En=t("Use your fine-tuned model for inference."),Zt=c(),u(hs.$$.fragment),Rt=c(),Aa=o("p"),An=t(`LayoutLMv2 solves the document question-answering task by adding a question-answering head on top of the final hidden
states of the tokens, to predict the positions of the start and end tokens of the
answer. In other words, the problem is treated as extractive question answering: given the context, extract which piece
of information answers the question. The context comes from the output of an OCR engine, here it is Google\u2019s Tesseract.`),Xt=c(),Za=o("p"),Zn=t("Before you begin, make sure you have all the necessary libraries installed. LayoutLMv2 depends on detectron2, torchvision and tesseract."),$t=c(),u(Xs.$$.fragment),Gt=c(),u($s.$$.fragment),Bt=c(),u(Gs.$$.fragment),Wt=c(),Ra=o("p"),Rn=t("Once you have installed all of the dependencies, restart your runtime."),Nt=c(),Xa=o("p"),Xn=t(`We encourage you to share your model with the community. Log in to your Hugging Face account to upload it to the \u{1F917} Hub.
When prompted, enter your token to log in:`),Vt=c(),u(Bs.$$.fragment),Ft=c(),$a=o("p"),$n=t("Let\u2019s define some global variables."),Qt=c(),u(Ws.$$.fragment),qt=c(),O=o("h2"),ds=o("a"),Ue=o("span"),u(Ns.$$.fragment),Gn=c(),Te=o("span"),Bn=t("Load the data"),zt=c(),Q=o("p"),Wn=t(`In this guide we use a small sample of preprocessed DocVQA that you can find on \u{1F917} Hub. If you\u2019d like to use the full
DocVQA dataset, you can register and download it on `),Vs=o("a"),Nn=t("DocVQA homepage"),Vn=t(`. If you do so, to
proceed with this guide check out `),Fs=o("a"),Fn=t("how to load files into a \u{1F917} dataset"),Qn=t("."),Yt=c(),u(Qs.$$.fragment),St=c(),Ga=o("p"),qn=t(`As you can see, the dataset is split into train and test sets already. Take a look at a random example to familiarize
yourself with the features.`),Ht=c(),u(qs.$$.fragment),Lt=c(),Ba=o("p"),zn=t("Here\u2019s what the individual fields represent:"),Dt=c(),T=o("ul"),Wa=o("li"),_e=o("code"),Yn=t("id"),Sn=t(": the example\u2019s id"),Hn=c(),Na=o("li"),ve=o("code"),Ln=t("image"),Dn=t(": a PIL.Image.Image object containing the document image"),Pn=c(),Va=o("li"),Ie=o("code"),On=t("query"),Kn=t(": the question string - natural language asked question, in several languages"),so=c(),Fa=o("li"),ke=o("code"),ao=t("answers"),eo=t(": a list of correct answers provided by human annotators"),to=c(),us=o("li"),Ce=o("code"),lo=t("words"),no=t(" and "),Ee=o("code"),oo=t("bounding_boxes"),io=t(": the results of OCR, which we will not use here"),po=c(),Qa=o("li"),Ae=o("code"),ro=t("answer"),co=t(": an answer matched by a different model which we will not use here"),Pt=c(),ms=o("p"),ho=t("Let\u2019s leave only English questions, and drop the "),Ze=o("code"),uo=t("answer"),mo=t(` feature which appears to contain predictions by another model.
We\u2019ll also take the first of the answers from the set provided by the annotators. Alternatively, you can randomly sample it.`),Ot=c(),u(zs.$$.fragment),Kt=c(),X=o("p"),jo=t("Note that the LayoutLMv2 checkpoint that we use in this guide has been trained with "),Re=o("code"),fo=t("max_position_embeddings = 512"),Mo=t(` (you can
find this information in the `),K=o("a"),go=t("checkpoint\u2019s "),Xe=o("code"),wo=t("config.json"),yo=t(" file"),xo=t(`).
We can truncate the examples but to avoid the situation where the answer might be at the end of a large document and end up truncated,
here we\u2019ll remove the few examples where the embedding is likely to end up longer than 512.
If most of the documents in your dataset are long, you can implement a sliding window strategy - check out `),Ys=o("a"),bo=t("this notebook"),Jo=t(" for details."),sl=c(),u(Ss.$$.fragment),al=c(),q=o("p"),Uo=t(`At this point let\u2019s also remove the OCR features from this dataset. These are a result of OCR for fine-tuning a different
model. They would still require some processing if we wanted to use them, as they do not match the input requirements
of the model we use in this guide. Instead, we can use the `),qa=o("a"),To=t("LayoutLMv2Processor"),_o=t(` on the original data for both OCR and
tokenization. This way we\u2019ll get the inputs that match model\u2019s expected input. If you want to process images manually,
check out the `),Hs=o("a"),$e=o("code"),vo=t("LayoutLMv2"),Io=t(" model documentation"),ko=t(" to learn what input format the model expects."),el=c(),u(Ls.$$.fragment),tl=c(),za=o("p"),Co=t("Finally, the data exploration won\u2019t be complete if we don\u2019t peek at an image example."),ll=c(),u(Ds.$$.fragment),nl=c(),Ps=o("div"),Ya=o("img"),ol=c(),ss=o("h2"),js=o("a"),Ge=o("span"),u(Os.$$.fragment),Eo=c(),Be=o("span"),Ao=t("Preprocess the data"),il=c(),fs=o("p"),Zo=t(`The Document Question Answering task is a multimodal task, and you need to make sure that the inputs from each modality
are preprocessed according to the model\u2019s expectations. Let\u2019s start by loading the `),Sa=o("a"),Ro=t("LayoutLMv2Processor"),Xo=t(", which internally combines an image processor that can handle image data and a tokenizer that can encode text data."),pl=c(),u(Ks.$$.fragment),rl=c(),as=o("h3"),Ms=o("a"),We=o("span"),u(sa.$$.fragment),$o=c(),Ne=o("span"),Go=t("Preprocessing document images"),cl=c(),gs=o("p"),Bo=t("First, let\u2019s prepare the document images for the model with the help of the "),Ve=o("code"),Wo=t("image_processor"),No=t(` from the processor.
By default, image processor resizes the images to 224x224, makes sure they have the correct order of color channels,
applies OCR with tesseract to get words and normalized bounding boxes. In this tutorial, all of these defaults are exactly what we need.
Write a function that applies the default image processing to a batch of images and returns the results of OCR.`),hl=c(),u(aa.$$.fragment),dl=c(),ws=o("p"),Vo=t("To apply this preprocessing to the entire dataset in a fast way, use "),ea=o("a"),Fo=t("map"),Qo=t("."),ul=c(),u(ta.$$.fragment),ml=c(),es=o("h3"),ys=o("a"),Fe=o("span"),u(la.$$.fragment),qo=c(),Qe=o("span"),zo=t("Preprocessing text data"),jl=c(),_=o("p"),Yo=t(`Once we have applied OCR to the images, we need to encode the text part of the dataset to prepare it for the model.
This involves converting the words and boxes that we got in the previous step to token-level `),qe=o("code"),So=t("input_ids"),Ho=t(", "),ze=o("code"),Lo=t("attention_mask"),Do=t(`,
`),Ye=o("code"),Po=t("token_type_ids"),Oo=t(" and "),Se=o("code"),Ko=t("bbox"),si=t(". For preprocessing text, we\u2019ll need the "),He=o("code"),ai=t("tokenizer"),ei=t(" from the processor."),fl=c(),u(na.$$.fragment),Ml=c(),$=o("p"),ti=t("On top of the preprocessing mentioned above, we also need to add the labels for the model. For "),Le=o("code"),li=t("xxxForQuestionAnswering"),ni=t(` models
in \u{1F917} Transformers, the labels consist of the `),De=o("code"),oi=t("start_positions"),ii=t(" and "),Pe=o("code"),pi=t("end_positions"),ri=t(`, indicating which token is at the
start and which token is at the end of the answer.`),gl=c(),Ha=o("p"),ci=t("Let\u2019s start with that. Define a helper function that can find a sublist (the answer split into words) in a larger list (the words list)."),wl=c(),y=o("p"),hi=t("This function will take two lists as input, "),Oe=o("code"),di=t("words_list"),ui=t(" and "),Ke=o("code"),mi=t("answer_list"),ji=t(". It will then iterate over the "),st=o("code"),fi=t("words_list"),Mi=t(` and check
if the current word in the `),at=o("code"),gi=t("words_list"),wi=t(` (words_list[i]) is equal to the first word of answer_list (answer_list[0]) and if
the sublist of `),et=o("code"),yi=t("words_list"),xi=t(" starting from the current word and of the same length as "),tt=o("code"),bi=t("answer_list"),Ji=t(" is equal "),lt=o("code"),Ui=t("to answer_list"),Ti=t(`.
If this condition is true, it means that a match has been found, and the function will record the match, its starting index (idx),
and its ending index (idx + len(answer_list) - 1). If more than one match was found, the function will return only the first one.
If no match is found, the function returns (`),nt=o("code"),_i=t("None"),vi=t(", 0, and 0)."),yl=c(),u(oa.$$.fragment),xl=c(),La=o("p"),Ii=t("To illustrate how this function finds the position of the answer, let\u2019s use it on an example:"),bl=c(),u(ia.$$.fragment),Jl=c(),Da=o("p"),ki=t("Once examples are encoded, however, they will look like this:"),Ul=c(),u(pa.$$.fragment),Tl=c(),Pa=o("p"),Ci=t("We\u2019ll need to find the position of the answer in the encoded input."),_l=c(),z=o("ul"),Oa=o("li"),ot=o("code"),Ei=t("token_type_ids"),Ai=t(" tells us which tokens are part of the question, and which ones are part of the document\u2019s words."),Zi=c(),Ka=o("li"),it=o("code"),Ri=t("tokenizer.cls_token_id"),Xi=t(" will help find the special token at the beginning of the input."),$i=c(),xs=o("li"),pt=o("code"),Gi=t("word_ids"),Bi=t(" will help match the answer found in the original "),rt=o("code"),Wi=t("words"),Ni=t(` to the same answer in the full encoded input and determine
the start/end position of the answer in the encoded input.`),vl=c(),se=o("p"),Vi=t("With that in mind, let\u2019s create a function to encode a batch of examples in the dataset:"),Il=c(),u(ra.$$.fragment),kl=c(),ae=o("p"),Fi=t("Now that we have this preprocessing function, we can encode the entire dataset:"),Cl=c(),u(ca.$$.fragment),El=c(),ee=o("p"),Qi=t("Let\u2019s check what the features of the encoded dataset look like:"),Al=c(),u(ha.$$.fragment),Zl=c(),ts=o("h2"),bs=o("a"),ct=o("span"),u(da.$$.fragment),qi=c(),ht=o("span"),zi=t("Evaluation"),Rl=c(),Y=o("p"),Yi=t(`Evaluation for document question answering requires a significant amount of postprocessing. To avoid taking up too much
of your time, this guide skips the evaluation step. The `),te=o("a"),Si=t("Trainer"),Hi=t(` still calculates the evaluation loss during training so
you\u2019re not completely in the dark about your model\u2019s performance. Extractive question answering is typically evaluated using F1/exact match.
If you\u2019d like to implement it yourself, check out the `),ua=o("a"),Li=t("Question Answering chapter"),Di=t(`
of the Hugging Face course for inspiration.`),Xl=c(),ls=o("h2"),Js=o("a"),dt=o("span"),u(ma.$$.fragment),Pi=c(),ut=o("span"),Oi=t("Train"),$l=c(),le=o("p"),Ki=t(`Congratulations! You\u2019ve successfully navigated the toughest part of this guide and now you are ready to train your own model.
Training involves the following steps:`),Gl=c(),C=o("ul"),ja=o("li"),sp=t("Load the model with "),ne=o("a"),ap=t("AutoModelForDocumentQuestionAnswering"),ep=t(" using the same checkpoint as in the preprocessing."),tp=c(),fa=o("li"),lp=t("Define your training hyperparameters in "),oe=o("a"),np=t("TrainingArguments"),op=t("."),ip=c(),Ma=o("li"),pp=t("Define a function to batch examples together, here the "),ie=o("a"),rp=t("DefaultDataCollator"),cp=t(" will do just fine"),hp=c(),ga=o("li"),dp=t("Pass the training arguments to "),pe=o("a"),up=t("Trainer"),mp=t(" along with the model, dataset, and data collator."),jp=c(),wa=o("li"),fp=t("Call "),re=o("a"),Mp=t("train()"),gp=t(" to finetune your model."),Bl=c(),u(ya.$$.fragment),Wl=c(),v=o("p"),wp=t("In the "),ce=o("a"),yp=t("TrainingArguments"),xp=t(" use "),mt=o("code"),bp=t("output_dir"),Jp=t(` to specify where to save your model, and configure hyperparameters as you see fit.
If you wish to share your model with the community, set `),jt=o("code"),Up=t("push_to_hub"),Tp=t(" to "),ft=o("code"),_p=t("True"),vp=t(` (you must be signed in to Hugging Face to upload your model).
In this case the `),Mt=o("code"),Ip=t("output_dir"),kp=t(" will also be the name of the repo where your model checkpoint will be pushed."),Nl=c(),u(xa.$$.fragment),Vl=c(),he=o("p"),Cp=t("Define a simple data collator to batch examples together."),Fl=c(),u(ba.$$.fragment),Ql=c(),Us=o("p"),Ep=t("Finally, bring everything together, and call "),de=o("a"),Ap=t("train()"),Zp=t(":"),ql=c(),u(Ja.$$.fragment),zl=c(),Ts=o("p"),Rp=t("To add the final model to \u{1F917} Hub, create a model card and call "),gt=o("code"),Xp=t("push_to_hub"),$p=t(":"),Yl=c(),u(Ua.$$.fragment),Sl=c(),ns=o("h2"),_s=o("a"),wt=o("span"),u(Ta.$$.fragment),Gp=c(),yt=o("span"),Bp=t("Inference"),Hl=c(),vs=o("p"),Wp=t(`Now that you have finetuned a LayoutLMv2 model, and uploaded it to the \u{1F917} Hub, you can use it for inference. The simplest
way to try out your finetuned model for inference is to use it in a `),ue=o("a"),Np=t("Pipeline"),Vp=t("."),Ll=c(),me=o("p"),Fp=t("Let\u2019s take an example:"),Dl=c(),u(_a.$$.fragment),Pl=c(),je=o("p"),Qp=t(`Next, instantiate a pipeline for
document question answering with your model, and pass the image + question combination to it.`),Ol=c(),u(va.$$.fragment),Kl=c(),fe=o("p"),qp=t("You can also manually replicate the results of the pipeline if you\u2019d like:"),sn=c(),E=o("ol"),xt=o("li"),zp=t("Take an image and a question, prepare them for the model using the processor from your model."),Yp=c(),bt=o("li"),Sp=t("Forward the result or preprocessing through the model."),Hp=c(),os=o("li"),Lp=t("The model returns "),Jt=o("code"),Dp=t("start_logits"),Pp=t(" and "),Ut=o("code"),Op=t("end_logits"),Kp=t(`, which indicate which token is at the start of the answer and
which token is at the end of the answer. Both have shape (batch_size, sequence_length).`),sr=c(),A=o("li"),ar=t("Take an argmax on the last dimension of both the "),Tt=o("code"),er=t("start_logits"),tr=t(" and "),_t=o("code"),lr=t("end_logits"),nr=t(" to get the predicted "),vt=o("code"),or=t("start_idx"),ir=t(" and "),It=o("code"),pr=t("end_idx"),rr=t("."),cr=c(),kt=o("li"),hr=t("Decode the answer with the tokenizer."),an=c(),u(Ia.$$.fragment),this.h()},l(s){const n=Ih('[data-svelte="svelte-1phssyn"]',document.head);U=i(n,"META",{name:!0,content:!0}),n.forEach(e),is=h(s),I=i(s,"H1",{class:!0});var ka=p(I);x=i(ka,"A",{id:!0,class:!0,href:!0});var xr=p(x);Z=i(xr,"SPAN",{});var br=p(Z);m(N.$$.fragment,br),br.forEach(e),xr.forEach(e),Es=h(ka),R=i(ka,"SPAN",{});var Jr=p(R);As=l(Jr,"Document Question Answering"),Jr.forEach(e),ka.forEach(e),ps=h(s),m(k.$$.fragment,s),rs=h(s),b=i(s,"P",{});var Ur=p(b);F=l(Ur,`Document Question Answering, also referred to as Document Visual Question Answering, is a task that involves providing
answers to questions posed about document images. The input to models supporting this task is typically a combination of an image and
a question, and the output is an answer expressed in natural language. These models utilize multiple modalities, including
text, the positions of words (bounding boxes), and the image itself.`),Ur.forEach(e),V=h(s),D=i(s,"P",{});var Tr=p(D);Ca=l(Tr,"This guide illustrates how to:"),Tr.forEach(e),Zs=h(s),cs=i(s,"UL",{});var tn=p(cs);P=i(tn,"LI",{});var Me=p(P);Tn=l(Me,"Fine-tune "),Ea=i(Me,"A",{href:!0});var _r=p(Ea);_n=l(_r,"LayoutLMv2"),_r.forEach(e),vn=l(Me," on the "),Rs=i(Me,"A",{href:!0,rel:!0});var vr=p(Rs);In=l(vr,"DocVQA dataset"),vr.forEach(e),kn=l(Me,"."),Me.forEach(e),Cn=h(tn),Je=i(tn,"LI",{});var Ir=p(Je);En=l(Ir,"Use your fine-tuned model for inference."),Ir.forEach(e),tn.forEach(e),Zt=h(s),m(hs.$$.fragment,s),Rt=h(s),Aa=i(s,"P",{});var kr=p(Aa);An=l(kr,`LayoutLMv2 solves the document question-answering task by adding a question-answering head on top of the final hidden
states of the tokens, to predict the positions of the start and end tokens of the
answer. In other words, the problem is treated as extractive question answering: given the context, extract which piece
of information answers the question. The context comes from the output of an OCR engine, here it is Google\u2019s Tesseract.`),kr.forEach(e),Xt=h(s),Za=i(s,"P",{});var Cr=p(Za);Zn=l(Cr,"Before you begin, make sure you have all the necessary libraries installed. LayoutLMv2 depends on detectron2, torchvision and tesseract."),Cr.forEach(e),$t=h(s),m(Xs.$$.fragment,s),Gt=h(s),m($s.$$.fragment,s),Bt=h(s),m(Gs.$$.fragment,s),Wt=h(s),Ra=i(s,"P",{});var Er=p(Ra);Rn=l(Er,"Once you have installed all of the dependencies, restart your runtime."),Er.forEach(e),Nt=h(s),Xa=i(s,"P",{});var Ar=p(Xa);Xn=l(Ar,`We encourage you to share your model with the community. Log in to your Hugging Face account to upload it to the \u{1F917} Hub.
When prompted, enter your token to log in:`),Ar.forEach(e),Vt=h(s),m(Bs.$$.fragment,s),Ft=h(s),$a=i(s,"P",{});var Zr=p($a);$n=l(Zr,"Let\u2019s define some global variables."),Zr.forEach(e),Qt=h(s),m(Ws.$$.fragment,s),qt=h(s),O=i(s,"H2",{class:!0});var ln=p(O);ds=i(ln,"A",{id:!0,class:!0,href:!0});var Rr=p(ds);Ue=i(Rr,"SPAN",{});var Xr=p(Ue);m(Ns.$$.fragment,Xr),Xr.forEach(e),Rr.forEach(e),Gn=h(ln),Te=i(ln,"SPAN",{});var $r=p(Te);Bn=l($r,"Load the data"),$r.forEach(e),ln.forEach(e),zt=h(s),Q=i(s,"P",{});var ge=p(Q);Wn=l(ge,`In this guide we use a small sample of preprocessed DocVQA that you can find on \u{1F917} Hub. If you\u2019d like to use the full
DocVQA dataset, you can register and download it on `),Vs=i(ge,"A",{href:!0,rel:!0});var Gr=p(Vs);Nn=l(Gr,"DocVQA homepage"),Gr.forEach(e),Vn=l(ge,`. If you do so, to
proceed with this guide check out `),Fs=i(ge,"A",{href:!0,rel:!0});var Br=p(Fs);Fn=l(Br,"how to load files into a \u{1F917} dataset"),Br.forEach(e),Qn=l(ge,"."),ge.forEach(e),Yt=h(s),m(Qs.$$.fragment,s),St=h(s),Ga=i(s,"P",{});var Wr=p(Ga);qn=l(Wr,`As you can see, the dataset is split into train and test sets already. Take a look at a random example to familiarize
yourself with the features.`),Wr.forEach(e),Ht=h(s),m(qs.$$.fragment,s),Lt=h(s),Ba=i(s,"P",{});var Nr=p(Ba);zn=l(Nr,"Here\u2019s what the individual fields represent:"),Nr.forEach(e),Dt=h(s),T=i(s,"UL",{});var G=p(T);Wa=i(G,"LI",{});var dr=p(Wa);_e=i(dr,"CODE",{});var Vr=p(_e);Yn=l(Vr,"id"),Vr.forEach(e),Sn=l(dr,": the example\u2019s id"),dr.forEach(e),Hn=h(G),Na=i(G,"LI",{});var ur=p(Na);ve=i(ur,"CODE",{});var Fr=p(ve);Ln=l(Fr,"image"),Fr.forEach(e),Dn=l(ur,": a PIL.Image.Image object containing the document image"),ur.forEach(e),Pn=h(G),Va=i(G,"LI",{});var mr=p(Va);Ie=i(mr,"CODE",{});var Qr=p(Ie);On=l(Qr,"query"),Qr.forEach(e),Kn=l(mr,": the question string - natural language asked question, in several languages"),mr.forEach(e),so=h(G),Fa=i(G,"LI",{});var jr=p(Fa);ke=i(jr,"CODE",{});var qr=p(ke);ao=l(qr,"answers"),qr.forEach(e),eo=l(jr,": a list of correct answers provided by human annotators"),jr.forEach(e),to=h(G),us=i(G,"LI",{});var Ct=p(us);Ce=i(Ct,"CODE",{});var zr=p(Ce);lo=l(zr,"words"),zr.forEach(e),no=l(Ct," and "),Ee=i(Ct,"CODE",{});var Yr=p(Ee);oo=l(Yr,"bounding_boxes"),Yr.forEach(e),io=l(Ct,": the results of OCR, which we will not use here"),Ct.forEach(e),po=h(G),Qa=i(G,"LI",{});var fr=p(Qa);Ae=i(fr,"CODE",{});var Sr=p(Ae);ro=l(Sr,"answer"),Sr.forEach(e),co=l(fr,": an answer matched by a different model which we will not use here"),fr.forEach(e),G.forEach(e),Pt=h(s),ms=i(s,"P",{});var nn=p(ms);ho=l(nn,"Let\u2019s leave only English questions, and drop the "),Ze=i(nn,"CODE",{});var Hr=p(Ze);uo=l(Hr,"answer"),Hr.forEach(e),mo=l(nn,` feature which appears to contain predictions by another model.
We\u2019ll also take the first of the answers from the set provided by the annotators. Alternatively, you can randomly sample it.`),nn.forEach(e),Ot=h(s),m(zs.$$.fragment,s),Kt=h(s),X=i(s,"P",{});var Is=p(X);jo=l(Is,"Note that the LayoutLMv2 checkpoint that we use in this guide has been trained with "),Re=i(Is,"CODE",{});var Lr=p(Re);fo=l(Lr,"max_position_embeddings = 512"),Lr.forEach(e),Mo=l(Is,` (you can
find this information in the `),K=i(Is,"A",{href:!0,rel:!0});var on=p(K);go=l(on,"checkpoint\u2019s "),Xe=i(on,"CODE",{});var Dr=p(Xe);wo=l(Dr,"config.json"),Dr.forEach(e),yo=l(on," file"),on.forEach(e),xo=l(Is,`).
We can truncate the examples but to avoid the situation where the answer might be at the end of a large document and end up truncated,
here we\u2019ll remove the few examples where the embedding is likely to end up longer than 512.
If most of the documents in your dataset are long, you can implement a sliding window strategy - check out `),Ys=i(Is,"A",{href:!0,rel:!0});var Pr=p(Ys);bo=l(Pr,"this notebook"),Pr.forEach(e),Jo=l(Is," for details."),Is.forEach(e),sl=h(s),m(Ss.$$.fragment,s),al=h(s),q=i(s,"P",{});var we=p(q);Uo=l(we,`At this point let\u2019s also remove the OCR features from this dataset. These are a result of OCR for fine-tuning a different
model. They would still require some processing if we wanted to use them, as they do not match the input requirements
of the model we use in this guide. Instead, we can use the `),qa=i(we,"A",{href:!0});var Or=p(qa);To=l(Or,"LayoutLMv2Processor"),Or.forEach(e),_o=l(we,` on the original data for both OCR and
tokenization. This way we\u2019ll get the inputs that match model\u2019s expected input. If you want to process images manually,
check out the `),Hs=i(we,"A",{href:!0});var Mr=p(Hs);$e=i(Mr,"CODE",{});var Kr=p($e);vo=l(Kr,"LayoutLMv2"),Kr.forEach(e),Io=l(Mr," model documentation"),Mr.forEach(e),ko=l(we," to learn what input format the model expects."),we.forEach(e),el=h(s),m(Ls.$$.fragment,s),tl=h(s),za=i(s,"P",{});var sc=p(za);Co=l(sc,"Finally, the data exploration won\u2019t be complete if we don\u2019t peek at an image example."),sc.forEach(e),ll=h(s),m(Ds.$$.fragment,s),nl=h(s),Ps=i(s,"DIV",{class:!0});var ac=p(Ps);Ya=i(ac,"IMG",{src:!0,alt:!0}),ac.forEach(e),ol=h(s),ss=i(s,"H2",{class:!0});var pn=p(ss);js=i(pn,"A",{id:!0,class:!0,href:!0});var ec=p(js);Ge=i(ec,"SPAN",{});var tc=p(Ge);m(Os.$$.fragment,tc),tc.forEach(e),ec.forEach(e),Eo=h(pn),Be=i(pn,"SPAN",{});var lc=p(Be);Ao=l(lc,"Preprocess the data"),lc.forEach(e),pn.forEach(e),il=h(s),fs=i(s,"P",{});var rn=p(fs);Zo=l(rn,`The Document Question Answering task is a multimodal task, and you need to make sure that the inputs from each modality
are preprocessed according to the model\u2019s expectations. Let\u2019s start by loading the `),Sa=i(rn,"A",{href:!0});var nc=p(Sa);Ro=l(nc,"LayoutLMv2Processor"),nc.forEach(e),Xo=l(rn,", which internally combines an image processor that can handle image data and a tokenizer that can encode text data."),rn.forEach(e),pl=h(s),m(Ks.$$.fragment,s),rl=h(s),as=i(s,"H3",{class:!0});var cn=p(as);Ms=i(cn,"A",{id:!0,class:!0,href:!0});var oc=p(Ms);We=i(oc,"SPAN",{});var ic=p(We);m(sa.$$.fragment,ic),ic.forEach(e),oc.forEach(e),$o=h(cn),Ne=i(cn,"SPAN",{});var pc=p(Ne);Go=l(pc,"Preprocessing document images"),pc.forEach(e),cn.forEach(e),cl=h(s),gs=i(s,"P",{});var hn=p(gs);Bo=l(hn,"First, let\u2019s prepare the document images for the model with the help of the "),Ve=i(hn,"CODE",{});var rc=p(Ve);Wo=l(rc,"image_processor"),rc.forEach(e),No=l(hn,` from the processor.
By default, image processor resizes the images to 224x224, makes sure they have the correct order of color channels,
applies OCR with tesseract to get words and normalized bounding boxes. In this tutorial, all of these defaults are exactly what we need.
Write a function that applies the default image processing to a batch of images and returns the results of OCR.`),hn.forEach(e),hl=h(s),m(aa.$$.fragment,s),dl=h(s),ws=i(s,"P",{});var dn=p(ws);Vo=l(dn,"To apply this preprocessing to the entire dataset in a fast way, use "),ea=i(dn,"A",{href:!0,rel:!0});var cc=p(ea);Fo=l(cc,"map"),cc.forEach(e),Qo=l(dn,"."),dn.forEach(e),ul=h(s),m(ta.$$.fragment,s),ml=h(s),es=i(s,"H3",{class:!0});var un=p(es);ys=i(un,"A",{id:!0,class:!0,href:!0});var hc=p(ys);Fe=i(hc,"SPAN",{});var dc=p(Fe);m(la.$$.fragment,dc),dc.forEach(e),hc.forEach(e),qo=h(un),Qe=i(un,"SPAN",{});var uc=p(Qe);zo=l(uc,"Preprocessing text data"),uc.forEach(e),un.forEach(e),jl=h(s),_=i(s,"P",{});var B=p(_);Yo=l(B,`Once we have applied OCR to the images, we need to encode the text part of the dataset to prepare it for the model.
This involves converting the words and boxes that we got in the previous step to token-level `),qe=i(B,"CODE",{});var mc=p(qe);So=l(mc,"input_ids"),mc.forEach(e),Ho=l(B,", "),ze=i(B,"CODE",{});var jc=p(ze);Lo=l(jc,"attention_mask"),jc.forEach(e),Do=l(B,`,
`),Ye=i(B,"CODE",{});var fc=p(Ye);Po=l(fc,"token_type_ids"),fc.forEach(e),Oo=l(B," and "),Se=i(B,"CODE",{});var Mc=p(Se);Ko=l(Mc,"bbox"),Mc.forEach(e),si=l(B,". For preprocessing text, we\u2019ll need the "),He=i(B,"CODE",{});var gc=p(He);ai=l(gc,"tokenizer"),gc.forEach(e),ei=l(B," from the processor."),B.forEach(e),fl=h(s),m(na.$$.fragment,s),Ml=h(s),$=i(s,"P",{});var ks=p($);ti=l(ks,"On top of the preprocessing mentioned above, we also need to add the labels for the model. For "),Le=i(ks,"CODE",{});var wc=p(Le);li=l(wc,"xxxForQuestionAnswering"),wc.forEach(e),ni=l(ks,` models
in \u{1F917} Transformers, the labels consist of the `),De=i(ks,"CODE",{});var yc=p(De);oi=l(yc,"start_positions"),yc.forEach(e),ii=l(ks," and "),Pe=i(ks,"CODE",{});var xc=p(Pe);pi=l(xc,"end_positions"),xc.forEach(e),ri=l(ks,`, indicating which token is at the
start and which token is at the end of the answer.`),ks.forEach(e),gl=h(s),Ha=i(s,"P",{});var bc=p(Ha);ci=l(bc,"Let\u2019s start with that. Define a helper function that can find a sublist (the answer split into words) in a larger list (the words list)."),bc.forEach(e),wl=h(s),y=i(s,"P",{});var J=p(y);hi=l(J,"This function will take two lists as input, "),Oe=i(J,"CODE",{});var Jc=p(Oe);di=l(Jc,"words_list"),Jc.forEach(e),ui=l(J," and "),Ke=i(J,"CODE",{});var Uc=p(Ke);mi=l(Uc,"answer_list"),Uc.forEach(e),ji=l(J,". It will then iterate over the "),st=i(J,"CODE",{});var Tc=p(st);fi=l(Tc,"words_list"),Tc.forEach(e),Mi=l(J,` and check
if the current word in the `),at=i(J,"CODE",{});var _c=p(at);gi=l(_c,"words_list"),_c.forEach(e),wi=l(J,` (words_list[i]) is equal to the first word of answer_list (answer_list[0]) and if
the sublist of `),et=i(J,"CODE",{});var vc=p(et);yi=l(vc,"words_list"),vc.forEach(e),xi=l(J," starting from the current word and of the same length as "),tt=i(J,"CODE",{});var Ic=p(tt);bi=l(Ic,"answer_list"),Ic.forEach(e),Ji=l(J," is equal "),lt=i(J,"CODE",{});var kc=p(lt);Ui=l(kc,"to answer_list"),kc.forEach(e),Ti=l(J,`.
If this condition is true, it means that a match has been found, and the function will record the match, its starting index (idx),
and its ending index (idx + len(answer_list) - 1). If more than one match was found, the function will return only the first one.
If no match is found, the function returns (`),nt=i(J,"CODE",{});var Cc=p(nt);_i=l(Cc,"None"),Cc.forEach(e),vi=l(J,", 0, and 0)."),J.forEach(e),yl=h(s),m(oa.$$.fragment,s),xl=h(s),La=i(s,"P",{});var Ec=p(La);Ii=l(Ec,"To illustrate how this function finds the position of the answer, let\u2019s use it on an example:"),Ec.forEach(e),bl=h(s),m(ia.$$.fragment,s),Jl=h(s),Da=i(s,"P",{});var Ac=p(Da);ki=l(Ac,"Once examples are encoded, however, they will look like this:"),Ac.forEach(e),Ul=h(s),m(pa.$$.fragment,s),Tl=h(s),Pa=i(s,"P",{});var Zc=p(Pa);Ci=l(Zc,"We\u2019ll need to find the position of the answer in the encoded input."),Zc.forEach(e),_l=h(s),z=i(s,"UL",{});var ye=p(z);Oa=i(ye,"LI",{});var gr=p(Oa);ot=i(gr,"CODE",{});var Rc=p(ot);Ei=l(Rc,"token_type_ids"),Rc.forEach(e),Ai=l(gr," tells us which tokens are part of the question, and which ones are part of the document\u2019s words."),gr.forEach(e),Zi=h(ye),Ka=i(ye,"LI",{});var wr=p(Ka);it=i(wr,"CODE",{});var Xc=p(it);Ri=l(Xc,"tokenizer.cls_token_id"),Xc.forEach(e),Xi=l(wr," will help find the special token at the beginning of the input."),wr.forEach(e),$i=h(ye),xs=i(ye,"LI",{});var Et=p(xs);pt=i(Et,"CODE",{});var $c=p(pt);Gi=l($c,"word_ids"),$c.forEach(e),Bi=l(Et," will help match the answer found in the original "),rt=i(Et,"CODE",{});var Gc=p(rt);Wi=l(Gc,"words"),Gc.forEach(e),Ni=l(Et,` to the same answer in the full encoded input and determine
the start/end position of the answer in the encoded input.`),Et.forEach(e),ye.forEach(e),vl=h(s),se=i(s,"P",{});var Bc=p(se);Vi=l(Bc,"With that in mind, let\u2019s create a function to encode a batch of examples in the dataset:"),Bc.forEach(e),Il=h(s),m(ra.$$.fragment,s),kl=h(s),ae=i(s,"P",{});var Wc=p(ae);Fi=l(Wc,"Now that we have this preprocessing function, we can encode the entire dataset:"),Wc.forEach(e),Cl=h(s),m(ca.$$.fragment,s),El=h(s),ee=i(s,"P",{});var Nc=p(ee);Qi=l(Nc,"Let\u2019s check what the features of the encoded dataset look like:"),Nc.forEach(e),Al=h(s),m(ha.$$.fragment,s),Zl=h(s),ts=i(s,"H2",{class:!0});var mn=p(ts);bs=i(mn,"A",{id:!0,class:!0,href:!0});var Vc=p(bs);ct=i(Vc,"SPAN",{});var Fc=p(ct);m(da.$$.fragment,Fc),Fc.forEach(e),Vc.forEach(e),qi=h(mn),ht=i(mn,"SPAN",{});var Qc=p(ht);zi=l(Qc,"Evaluation"),Qc.forEach(e),mn.forEach(e),Rl=h(s),Y=i(s,"P",{});var xe=p(Y);Yi=l(xe,`Evaluation for document question answering requires a significant amount of postprocessing. To avoid taking up too much
of your time, this guide skips the evaluation step. The `),te=i(xe,"A",{href:!0});var qc=p(te);Si=l(qc,"Trainer"),qc.forEach(e),Hi=l(xe,` still calculates the evaluation loss during training so
you\u2019re not completely in the dark about your model\u2019s performance. Extractive question answering is typically evaluated using F1/exact match.
If you\u2019d like to implement it yourself, check out the `),ua=i(xe,"A",{href:!0,rel:!0});var zc=p(ua);Li=l(zc,"Question Answering chapter"),zc.forEach(e),Di=l(xe,`
of the Hugging Face course for inspiration.`),xe.forEach(e),Xl=h(s),ls=i(s,"H2",{class:!0});var jn=p(ls);Js=i(jn,"A",{id:!0,class:!0,href:!0});var Yc=p(Js);dt=i(Yc,"SPAN",{});var Sc=p(dt);m(ma.$$.fragment,Sc),Sc.forEach(e),Yc.forEach(e),Pi=h(jn),ut=i(jn,"SPAN",{});var Hc=p(ut);Oi=l(Hc,"Train"),Hc.forEach(e),jn.forEach(e),$l=h(s),le=i(s,"P",{});var Lc=p(le);Ki=l(Lc,`Congratulations! You\u2019ve successfully navigated the toughest part of this guide and now you are ready to train your own model.
Training involves the following steps:`),Lc.forEach(e),Gl=h(s),C=i(s,"UL",{});var S=p(C);ja=i(S,"LI",{});var fn=p(ja);sp=l(fn,"Load the model with "),ne=i(fn,"A",{href:!0});var Dc=p(ne);ap=l(Dc,"AutoModelForDocumentQuestionAnswering"),Dc.forEach(e),ep=l(fn," using the same checkpoint as in the preprocessing."),fn.forEach(e),tp=h(S),fa=i(S,"LI",{});var Mn=p(fa);lp=l(Mn,"Define your training hyperparameters in "),oe=i(Mn,"A",{href:!0});var Pc=p(oe);np=l(Pc,"TrainingArguments"),Pc.forEach(e),op=l(Mn,"."),Mn.forEach(e),ip=h(S),Ma=i(S,"LI",{});var gn=p(Ma);pp=l(gn,"Define a function to batch examples together, here the "),ie=i(gn,"A",{href:!0});var Oc=p(ie);rp=l(Oc,"DefaultDataCollator"),Oc.forEach(e),cp=l(gn," will do just fine"),gn.forEach(e),hp=h(S),ga=i(S,"LI",{});var wn=p(ga);dp=l(wn,"Pass the training arguments to "),pe=i(wn,"A",{href:!0});var Kc=p(pe);up=l(Kc,"Trainer"),Kc.forEach(e),mp=l(wn," along with the model, dataset, and data collator."),wn.forEach(e),jp=h(S),wa=i(S,"LI",{});var yn=p(wa);fp=l(yn,"Call "),re=i(yn,"A",{href:!0});var sh=p(re);Mp=l(sh,"train()"),sh.forEach(e),gp=l(yn," to finetune your model."),yn.forEach(e),S.forEach(e),Bl=h(s),m(ya.$$.fragment,s),Wl=h(s),v=i(s,"P",{});var W=p(v);wp=l(W,"In the "),ce=i(W,"A",{href:!0});var ah=p(ce);yp=l(ah,"TrainingArguments"),ah.forEach(e),xp=l(W," use "),mt=i(W,"CODE",{});var eh=p(mt);bp=l(eh,"output_dir"),eh.forEach(e),Jp=l(W,` to specify where to save your model, and configure hyperparameters as you see fit.
If you wish to share your model with the community, set `),jt=i(W,"CODE",{});var th=p(jt);Up=l(th,"push_to_hub"),th.forEach(e),Tp=l(W," to "),ft=i(W,"CODE",{});var lh=p(ft);_p=l(lh,"True"),lh.forEach(e),vp=l(W,` (you must be signed in to Hugging Face to upload your model).
In this case the `),Mt=i(W,"CODE",{});var nh=p(Mt);Ip=l(nh,"output_dir"),nh.forEach(e),kp=l(W," will also be the name of the repo where your model checkpoint will be pushed."),W.forEach(e),Nl=h(s),m(xa.$$.fragment,s),Vl=h(s),he=i(s,"P",{});var oh=p(he);Cp=l(oh,"Define a simple data collator to batch examples together."),oh.forEach(e),Fl=h(s),m(ba.$$.fragment,s),Ql=h(s),Us=i(s,"P",{});var xn=p(Us);Ep=l(xn,"Finally, bring everything together, and call "),de=i(xn,"A",{href:!0});var ih=p(de);Ap=l(ih,"train()"),ih.forEach(e),Zp=l(xn,":"),xn.forEach(e),ql=h(s),m(Ja.$$.fragment,s),zl=h(s),Ts=i(s,"P",{});var bn=p(Ts);Rp=l(bn,"To add the final model to \u{1F917} Hub, create a model card and call "),gt=i(bn,"CODE",{});var ph=p(gt);Xp=l(ph,"push_to_hub"),ph.forEach(e),$p=l(bn,":"),bn.forEach(e),Yl=h(s),m(Ua.$$.fragment,s),Sl=h(s),ns=i(s,"H2",{class:!0});var Jn=p(ns);_s=i(Jn,"A",{id:!0,class:!0,href:!0});var rh=p(_s);wt=i(rh,"SPAN",{});var ch=p(wt);m(Ta.$$.fragment,ch),ch.forEach(e),rh.forEach(e),Gp=h(Jn),yt=i(Jn,"SPAN",{});var hh=p(yt);Bp=l(hh,"Inference"),hh.forEach(e),Jn.forEach(e),Hl=h(s),vs=i(s,"P",{});var Un=p(vs);Wp=l(Un,`Now that you have finetuned a LayoutLMv2 model, and uploaded it to the \u{1F917} Hub, you can use it for inference. The simplest
way to try out your finetuned model for inference is to use it in a `),ue=i(Un,"A",{href:!0});var dh=p(ue);Np=l(dh,"Pipeline"),dh.forEach(e),Vp=l(Un,"."),Un.forEach(e),Ll=h(s),me=i(s,"P",{});var uh=p(me);Fp=l(uh,"Let\u2019s take an example:"),uh.forEach(e),Dl=h(s),m(_a.$$.fragment,s),Pl=h(s),je=i(s,"P",{});var mh=p(je);Qp=l(mh,`Next, instantiate a pipeline for
document question answering with your model, and pass the image + question combination to it.`),mh.forEach(e),Ol=h(s),m(va.$$.fragment,s),Kl=h(s),fe=i(s,"P",{});var jh=p(fe);qp=l(jh,"You can also manually replicate the results of the pipeline if you\u2019d like:"),jh.forEach(e),sn=h(s),E=i(s,"OL",{});var H=p(E);xt=i(H,"LI",{});var fh=p(xt);zp=l(fh,"Take an image and a question, prepare them for the model using the processor from your model."),fh.forEach(e),Yp=h(H),bt=i(H,"LI",{});var Mh=p(bt);Sp=l(Mh,"Forward the result or preprocessing through the model."),Mh.forEach(e),Hp=h(H),os=i(H,"LI",{});var be=p(os);Lp=l(be,"The model returns "),Jt=i(be,"CODE",{});var gh=p(Jt);Dp=l(gh,"start_logits"),gh.forEach(e),Pp=l(be," and "),Ut=i(be,"CODE",{});var wh=p(Ut);Op=l(wh,"end_logits"),wh.forEach(e),Kp=l(be,`, which indicate which token is at the start of the answer and
which token is at the end of the answer. Both have shape (batch_size, sequence_length).`),be.forEach(e),sr=h(H),A=i(H,"LI",{});var L=p(A);ar=l(L,"Take an argmax on the last dimension of both the "),Tt=i(L,"CODE",{});var yh=p(Tt);er=l(yh,"start_logits"),yh.forEach(e),tr=l(L," and "),_t=i(L,"CODE",{});var xh=p(_t);lr=l(xh,"end_logits"),xh.forEach(e),nr=l(L," to get the predicted "),vt=i(L,"CODE",{});var bh=p(vt);or=l(bh,"start_idx"),bh.forEach(e),ir=l(L," and "),It=i(L,"CODE",{});var Jh=p(It);pr=l(Jh,"end_idx"),Jh.forEach(e),rr=l(L,"."),L.forEach(e),cr=h(H),kt=i(H,"LI",{});var Uh=p(kt);hr=l(Uh,"Decode the answer with the tokenizer."),Uh.forEach(e),H.forEach(e),an=h(s),m(Ia.$$.fragment,s),this.h()},h(){d(U,"name","hf:doc:metadata"),d(U,"content",JSON.stringify(Xh)),d(x,"id","document-question-answering"),d(x,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),d(x,"href","#document-question-answering"),d(I,"class","relative group"),d(Ea,"href","../model_doc/layoutlmv2"),d(Rs,"href","https://huggingface.co/datasets/nielsr/docvqa_1200_examples_donut"),d(Rs,"rel","nofollow"),d(ds,"id","load-the-data"),d(ds,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),d(ds,"href","#load-the-data"),d(O,"class","relative group"),d(Vs,"href","https://rrc.cvc.uab.es/?ch=17"),d(Vs,"rel","nofollow"),d(Fs,"href","https://huggingface.co/docs/datasets/loading#local-and-remote-files"),d(Fs,"rel","nofollow"),d(K,"href","https://huggingface.co/microsoft/layoutlmv2-base-uncased/blob/main/config.json#L18"),d(K,"rel","nofollow"),d(Ys,"href","https://github.com/huggingface/notebooks/blob/main/examples/question_answering.ipynb"),d(Ys,"rel","nofollow"),d(qa,"href","/docs/transformers/v4.33.0/en/model_doc/layoutlmv2#transformers.LayoutLMv2Processor"),d(Hs,"href","../model_doc/layoutlmv2"),kh(Ya.src,yr="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/docvqa_example.jpg")||d(Ya,"src",yr),d(Ya,"alt","DocVQA Image Example"),d(Ps,"class","flex justify-center"),d(js,"id","preprocess-the-data"),d(js,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),d(js,"href","#preprocess-the-data"),d(ss,"class","relative group"),d(Sa,"href","/docs/transformers/v4.33.0/en/model_doc/layoutlmv2#transformers.LayoutLMv2Processor"),d(Ms,"id","preprocessing-document-images"),d(Ms,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),d(Ms,"href","#preprocessing-document-images"),d(as,"class","relative group"),d(ea,"href","https://huggingface.co/docs/datasets/v2.14.4/en/package_reference/main_classes#datasets.Dataset.map"),d(ea,"rel","nofollow"),d(ys,"id","preprocessing-text-data"),d(ys,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),d(ys,"href","#preprocessing-text-data"),d(es,"class","relative group"),d(bs,"id","evaluation"),d(bs,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),d(bs,"href","#evaluation"),d(ts,"class","relative group"),d(te,"href","/docs/transformers/v4.33.0/en/main_classes/trainer#transformers.Trainer"),d(ua,"href","https://huggingface.co/course/chapter7/7?fw=pt#postprocessing"),d(ua,"rel","nofollow"),d(Js,"id","train"),d(Js,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),d(Js,"href","#train"),d(ls,"class","relative group"),d(ne,"href","/docs/transformers/v4.33.0/en/model_doc/auto#transformers.AutoModelForDocumentQuestionAnswering"),d(oe,"href","/docs/transformers/v4.33.0/en/main_classes/trainer#transformers.TrainingArguments"),d(ie,"href","/docs/transformers/v4.33.0/en/main_classes/data_collator#transformers.DefaultDataCollator"),d(pe,"href","/docs/transformers/v4.33.0/en/main_classes/trainer#transformers.Trainer"),d(re,"href","/docs/transformers/v4.33.0/en/main_classes/trainer#transformers.Trainer.train"),d(ce,"href","/docs/transformers/v4.33.0/en/main_classes/trainer#transformers.TrainingArguments"),d(de,"href","/docs/transformers/v4.33.0/en/main_classes/trainer#transformers.Trainer.train"),d(_s,"id","inference"),d(_s,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),d(_s,"href","#inference"),d(ns,"class","relative group"),d(ue,"href","/docs/transformers/v4.33.0/en/main_classes/pipelines#transformers.Pipeline")},m(s,n){a(document.head,U),r(s,is,n),r(s,I,n),a(I,x),a(x,Z),j(N,Z,null),a(I,Es),a(I,R),a(R,As),r(s,ps,n),j(k,s,n),r(s,rs,n),r(s,b,n),a(b,F),r(s,V,n),r(s,D,n),a(D,Ca),r(s,Zs,n),r(s,cs,n),a(cs,P),a(P,Tn),a(P,Ea),a(Ea,_n),a(P,vn),a(P,Rs),a(Rs,In),a(P,kn),a(cs,Cn),a(cs,Je),a(Je,En),r(s,Zt,n),j(hs,s,n),r(s,Rt,n),r(s,Aa,n),a(Aa,An),r(s,Xt,n),r(s,Za,n),a(Za,Zn),r(s,$t,n),j(Xs,s,n),r(s,Gt,n),j($s,s,n),r(s,Bt,n),j(Gs,s,n),r(s,Wt,n),r(s,Ra,n),a(Ra,Rn),r(s,Nt,n),r(s,Xa,n),a(Xa,Xn),r(s,Vt,n),j(Bs,s,n),r(s,Ft,n),r(s,$a,n),a($a,$n),r(s,Qt,n),j(Ws,s,n),r(s,qt,n),r(s,O,n),a(O,ds),a(ds,Ue),j(Ns,Ue,null),a(O,Gn),a(O,Te),a(Te,Bn),r(s,zt,n),r(s,Q,n),a(Q,Wn),a(Q,Vs),a(Vs,Nn),a(Q,Vn),a(Q,Fs),a(Fs,Fn),a(Q,Qn),r(s,Yt,n),j(Qs,s,n),r(s,St,n),r(s,Ga,n),a(Ga,qn),r(s,Ht,n),j(qs,s,n),r(s,Lt,n),r(s,Ba,n),a(Ba,zn),r(s,Dt,n),r(s,T,n),a(T,Wa),a(Wa,_e),a(_e,Yn),a(Wa,Sn),a(T,Hn),a(T,Na),a(Na,ve),a(ve,Ln),a(Na,Dn),a(T,Pn),a(T,Va),a(Va,Ie),a(Ie,On),a(Va,Kn),a(T,so),a(T,Fa),a(Fa,ke),a(ke,ao),a(Fa,eo),a(T,to),a(T,us),a(us,Ce),a(Ce,lo),a(us,no),a(us,Ee),a(Ee,oo),a(us,io),a(T,po),a(T,Qa),a(Qa,Ae),a(Ae,ro),a(Qa,co),r(s,Pt,n),r(s,ms,n),a(ms,ho),a(ms,Ze),a(Ze,uo),a(ms,mo),r(s,Ot,n),j(zs,s,n),r(s,Kt,n),r(s,X,n),a(X,jo),a(X,Re),a(Re,fo),a(X,Mo),a(X,K),a(K,go),a(K,Xe),a(Xe,wo),a(K,yo),a(X,xo),a(X,Ys),a(Ys,bo),a(X,Jo),r(s,sl,n),j(Ss,s,n),r(s,al,n),r(s,q,n),a(q,Uo),a(q,qa),a(qa,To),a(q,_o),a(q,Hs),a(Hs,$e),a($e,vo),a(Hs,Io),a(q,ko),r(s,el,n),j(Ls,s,n),r(s,tl,n),r(s,za,n),a(za,Co),r(s,ll,n),j(Ds,s,n),r(s,nl,n),r(s,Ps,n),a(Ps,Ya),r(s,ol,n),r(s,ss,n),a(ss,js),a(js,Ge),j(Os,Ge,null),a(ss,Eo),a(ss,Be),a(Be,Ao),r(s,il,n),r(s,fs,n),a(fs,Zo),a(fs,Sa),a(Sa,Ro),a(fs,Xo),r(s,pl,n),j(Ks,s,n),r(s,rl,n),r(s,as,n),a(as,Ms),a(Ms,We),j(sa,We,null),a(as,$o),a(as,Ne),a(Ne,Go),r(s,cl,n),r(s,gs,n),a(gs,Bo),a(gs,Ve),a(Ve,Wo),a(gs,No),r(s,hl,n),j(aa,s,n),r(s,dl,n),r(s,ws,n),a(ws,Vo),a(ws,ea),a(ea,Fo),a(ws,Qo),r(s,ul,n),j(ta,s,n),r(s,ml,n),r(s,es,n),a(es,ys),a(ys,Fe),j(la,Fe,null),a(es,qo),a(es,Qe),a(Qe,zo),r(s,jl,n),r(s,_,n),a(_,Yo),a(_,qe),a(qe,So),a(_,Ho),a(_,ze),a(ze,Lo),a(_,Do),a(_,Ye),a(Ye,Po),a(_,Oo),a(_,Se),a(Se,Ko),a(_,si),a(_,He),a(He,ai),a(_,ei),r(s,fl,n),j(na,s,n),r(s,Ml,n),r(s,$,n),a($,ti),a($,Le),a(Le,li),a($,ni),a($,De),a(De,oi),a($,ii),a($,Pe),a(Pe,pi),a($,ri),r(s,gl,n),r(s,Ha,n),a(Ha,ci),r(s,wl,n),r(s,y,n),a(y,hi),a(y,Oe),a(Oe,di),a(y,ui),a(y,Ke),a(Ke,mi),a(y,ji),a(y,st),a(st,fi),a(y,Mi),a(y,at),a(at,gi),a(y,wi),a(y,et),a(et,yi),a(y,xi),a(y,tt),a(tt,bi),a(y,Ji),a(y,lt),a(lt,Ui),a(y,Ti),a(y,nt),a(nt,_i),a(y,vi),r(s,yl,n),j(oa,s,n),r(s,xl,n),r(s,La,n),a(La,Ii),r(s,bl,n),j(ia,s,n),r(s,Jl,n),r(s,Da,n),a(Da,ki),r(s,Ul,n),j(pa,s,n),r(s,Tl,n),r(s,Pa,n),a(Pa,Ci),r(s,_l,n),r(s,z,n),a(z,Oa),a(Oa,ot),a(ot,Ei),a(Oa,Ai),a(z,Zi),a(z,Ka),a(Ka,it),a(it,Ri),a(Ka,Xi),a(z,$i),a(z,xs),a(xs,pt),a(pt,Gi),a(xs,Bi),a(xs,rt),a(rt,Wi),a(xs,Ni),r(s,vl,n),r(s,se,n),a(se,Vi),r(s,Il,n),j(ra,s,n),r(s,kl,n),r(s,ae,n),a(ae,Fi),r(s,Cl,n),j(ca,s,n),r(s,El,n),r(s,ee,n),a(ee,Qi),r(s,Al,n),j(ha,s,n),r(s,Zl,n),r(s,ts,n),a(ts,bs),a(bs,ct),j(da,ct,null),a(ts,qi),a(ts,ht),a(ht,zi),r(s,Rl,n),r(s,Y,n),a(Y,Yi),a(Y,te),a(te,Si),a(Y,Hi),a(Y,ua),a(ua,Li),a(Y,Di),r(s,Xl,n),r(s,ls,n),a(ls,Js),a(Js,dt),j(ma,dt,null),a(ls,Pi),a(ls,ut),a(ut,Oi),r(s,$l,n),r(s,le,n),a(le,Ki),r(s,Gl,n),r(s,C,n),a(C,ja),a(ja,sp),a(ja,ne),a(ne,ap),a(ja,ep),a(C,tp),a(C,fa),a(fa,lp),a(fa,oe),a(oe,np),a(fa,op),a(C,ip),a(C,Ma),a(Ma,pp),a(Ma,ie),a(ie,rp),a(Ma,cp),a(C,hp),a(C,ga),a(ga,dp),a(ga,pe),a(pe,up),a(ga,mp),a(C,jp),a(C,wa),a(wa,fp),a(wa,re),a(re,Mp),a(wa,gp),r(s,Bl,n),j(ya,s,n),r(s,Wl,n),r(s,v,n),a(v,wp),a(v,ce),a(ce,yp),a(v,xp),a(v,mt),a(mt,bp),a(v,Jp),a(v,jt),a(jt,Up),a(v,Tp),a(v,ft),a(ft,_p),a(v,vp),a(v,Mt),a(Mt,Ip),a(v,kp),r(s,Nl,n),j(xa,s,n),r(s,Vl,n),r(s,he,n),a(he,Cp),r(s,Fl,n),j(ba,s,n),r(s,Ql,n),r(s,Us,n),a(Us,Ep),a(Us,de),a(de,Ap),a(Us,Zp),r(s,ql,n),j(Ja,s,n),r(s,zl,n),r(s,Ts,n),a(Ts,Rp),a(Ts,gt),a(gt,Xp),a(Ts,$p),r(s,Yl,n),j(Ua,s,n),r(s,Sl,n),r(s,ns,n),a(ns,_s),a(_s,wt),j(Ta,wt,null),a(ns,Gp),a(ns,yt),a(yt,Bp),r(s,Hl,n),r(s,vs,n),a(vs,Wp),a(vs,ue),a(ue,Np),a(vs,Vp),r(s,Ll,n),r(s,me,n),a(me,Fp),r(s,Dl,n),j(_a,s,n),r(s,Pl,n),r(s,je,n),a(je,Qp),r(s,Ol,n),j(va,s,n),r(s,Kl,n),r(s,fe,n),a(fe,qp),r(s,sn,n),r(s,E,n),a(E,xt),a(xt,zp),a(E,Yp),a(E,bt),a(bt,Sp),a(E,Hp),a(E,os),a(os,Lp),a(os,Jt),a(Jt,Dp),a(os,Pp),a(os,Ut),a(Ut,Op),a(os,Kp),a(E,sr),a(E,A),a(A,ar),a(A,Tt),a(Tt,er),a(A,tr),a(A,_t),a(_t,lr),a(A,nr),a(A,vt),a(vt,or),a(A,ir),a(A,It),a(It,pr),a(A,rr),a(E,cr),a(E,kt),a(kt,hr),r(s,an,n),j(Ia,s,n),en=!0},p(s,[n]){const ka={};n&2&&(ka.$$scope={dirty:n,ctx:s}),hs.$set(ka)},i(s){en||(f(N.$$.fragment,s),f(k.$$.fragment,s),f(hs.$$.fragment,s),f(Xs.$$.fragment,s),f($s.$$.fragment,s),f(Gs.$$.fragment,s),f(Bs.$$.fragment,s),f(Ws.$$.fragment,s),f(Ns.$$.fragment,s),f(Qs.$$.fragment,s),f(qs.$$.fragment,s),f(zs.$$.fragment,s),f(Ss.$$.fragment,s),f(Ls.$$.fragment,s),f(Ds.$$.fragment,s),f(Os.$$.fragment,s),f(Ks.$$.fragment,s),f(sa.$$.fragment,s),f(aa.$$.fragment,s),f(ta.$$.fragment,s),f(la.$$.fragment,s),f(na.$$.fragment,s),f(oa.$$.fragment,s),f(ia.$$.fragment,s),f(pa.$$.fragment,s),f(ra.$$.fragment,s),f(ca.$$.fragment,s),f(ha.$$.fragment,s),f(da.$$.fragment,s),f(ma.$$.fragment,s),f(ya.$$.fragment,s),f(xa.$$.fragment,s),f(ba.$$.fragment,s),f(Ja.$$.fragment,s),f(Ua.$$.fragment,s),f(Ta.$$.fragment,s),f(_a.$$.fragment,s),f(va.$$.fragment,s),f(Ia.$$.fragment,s),en=!0)},o(s){M(N.$$.fragment,s),M(k.$$.fragment,s),M(hs.$$.fragment,s),M(Xs.$$.fragment,s),M($s.$$.fragment,s),M(Gs.$$.fragment,s),M(Bs.$$.fragment,s),M(Ws.$$.fragment,s),M(Ns.$$.fragment,s),M(Qs.$$.fragment,s),M(qs.$$.fragment,s),M(zs.$$.fragment,s),M(Ss.$$.fragment,s),M(Ls.$$.fragment,s),M(Ds.$$.fragment,s),M(Os.$$.fragment,s),M(Ks.$$.fragment,s),M(sa.$$.fragment,s),M(aa.$$.fragment,s),M(ta.$$.fragment,s),M(la.$$.fragment,s),M(na.$$.fragment,s),M(oa.$$.fragment,s),M(ia.$$.fragment,s),M(pa.$$.fragment,s),M(ra.$$.fragment,s),M(ca.$$.fragment,s),M(ha.$$.fragment,s),M(da.$$.fragment,s),M(ma.$$.fragment,s),M(ya.$$.fragment,s),M(xa.$$.fragment,s),M(ba.$$.fragment,s),M(Ja.$$.fragment,s),M(Ua.$$.fragment,s),M(Ta.$$.fragment,s),M(_a.$$.fragment,s),M(va.$$.fragment,s),M(Ia.$$.fragment,s),en=!1},d(s){e(U),s&&e(is),s&&e(I),g(N),s&&e(ps),g(k,s),s&&e(rs),s&&e(b),s&&e(V),s&&e(D),s&&e(Zs),s&&e(cs),s&&e(Zt),g(hs,s),s&&e(Rt),s&&e(Aa),s&&e(Xt),s&&e(Za),s&&e($t),g(Xs,s),s&&e(Gt),g($s,s),s&&e(Bt),g(Gs,s),s&&e(Wt),s&&e(Ra),s&&e(Nt),s&&e(Xa),s&&e(Vt),g(Bs,s),s&&e(Ft),s&&e($a),s&&e(Qt),g(Ws,s),s&&e(qt),s&&e(O),g(Ns),s&&e(zt),s&&e(Q),s&&e(Yt),g(Qs,s),s&&e(St),s&&e(Ga),s&&e(Ht),g(qs,s),s&&e(Lt),s&&e(Ba),s&&e(Dt),s&&e(T),s&&e(Pt),s&&e(ms),s&&e(Ot),g(zs,s),s&&e(Kt),s&&e(X),s&&e(sl),g(Ss,s),s&&e(al),s&&e(q),s&&e(el),g(Ls,s),s&&e(tl),s&&e(za),s&&e(ll),g(Ds,s),s&&e(nl),s&&e(Ps),s&&e(ol),s&&e(ss),g(Os),s&&e(il),s&&e(fs),s&&e(pl),g(Ks,s),s&&e(rl),s&&e(as),g(sa),s&&e(cl),s&&e(gs),s&&e(hl),g(aa,s),s&&e(dl),s&&e(ws),s&&e(ul),g(ta,s),s&&e(ml),s&&e(es),g(la),s&&e(jl),s&&e(_),s&&e(fl),g(na,s),s&&e(Ml),s&&e($),s&&e(gl),s&&e(Ha),s&&e(wl),s&&e(y),s&&e(yl),g(oa,s),s&&e(xl),s&&e(La),s&&e(bl),g(ia,s),s&&e(Jl),s&&e(Da),s&&e(Ul),g(pa,s),s&&e(Tl),s&&e(Pa),s&&e(_l),s&&e(z),s&&e(vl),s&&e(se),s&&e(Il),g(ra,s),s&&e(kl),s&&e(ae),s&&e(Cl),g(ca,s),s&&e(El),s&&e(ee),s&&e(Al),g(ha,s),s&&e(Zl),s&&e(ts),g(da),s&&e(Rl),s&&e(Y),s&&e(Xl),s&&e(ls),g(ma),s&&e($l),s&&e(le),s&&e(Gl),s&&e(C),s&&e(Bl),g(ya,s),s&&e(Wl),s&&e(v),s&&e(Nl),g(xa,s),s&&e(Vl),s&&e(he),s&&e(Fl),g(ba,s),s&&e(Ql),s&&e(Us),s&&e(ql),g(Ja,s),s&&e(zl),s&&e(Ts),s&&e(Yl),g(Ua,s),s&&e(Sl),s&&e(ns),g(Ta),s&&e(Hl),s&&e(vs),s&&e(Ll),s&&e(me),s&&e(Dl),g(_a,s),s&&e(Pl),s&&e(je),s&&e(Ol),g(va,s),s&&e(Kl),s&&e(fe),s&&e(sn),s&&e(E),s&&e(an),g(Ia,s)}}}const Xh={local:"document-question-answering",sections:[{local:"load-the-data",title:"Load the data"},{local:"preprocess-the-data",sections:[{local:"preprocessing-document-images",title:"Preprocessing document images"},{local:"preprocessing-text-data",title:"Preprocessing text data"}],title:"Preprocess the data"},{local:"evaluation",title:"Evaluation"},{local:"train",title:"Train"},{local:"inference",title:"Inference"}],title:"Document Question Answering"};function $h(At){return Ch(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Fh extends Th{constructor(U){super();_h(this,U,$h,Rh,vh,{})}}export{Fh as default,Xh as metadata};
