import{S as ai,i as oi,s as ni,e as o,k as c,w as f,t as l,M as ii,c as n,d as t,m as d,a as i,x as w,h as a,b as h,G as s,g as r,y as M,L as pi,q as J,o as T,B as j,v as ri}from"../chunks/vendor-hf-doc-builder.js";import{I as Dt}from"../chunks/IconCopyLink-hf-doc-builder.js";import{C as X}from"../chunks/CodeBlock-hf-doc-builder.js";function ci(Vo){let R,Os,V,Y,Ge,ce,Ot,Ne,Lt,Ls,S,Kt,Ie,el,sl,Ks,B,tl,Xe,ll,al,Re,ol,nl,et,b,il,Ve,pl,rl,We,cl,dl,qe,hl,ul,st,u,ml,ze,yl,fl,$e,wl,Ml,He,Jl,Tl,Ye,jl,bl,Se,_l,Ul,tt,de,lt,ke,El,at,W,Fe,Il,kl,Qe,Cl,Bl,ot,k,Pe,vl,Zl,xe,Al,gl,De,Gl,Nl,nt,q,Oe,Xl,Rl,Le,Vl,Wl,it,C,Ke,ql,zl,es,$l,Hl,ss,Yl,Sl,pt,m,Fl,ts,Ql,Pl,ls,xl,Dl,as,Ol,Ll,os,Kl,ea,rt,F,sa,ns,ta,la,ct,he,dt,_,aa,is,oa,na,ps,ia,pa,rs,ra,ca,ht,ue,ut,Ce,da,mt,z,Q,cs,me,ha,ds,ua,yt,v,ma,hs,ya,fa,us,wa,Ma,ft,ye,wt,P,Ja,ms,Ta,ja,Mt,fe,Jt,$,x,ys,we,ba,fs,_a,Tt,D,Ua,ws,Ea,Ia,jt,Me,bt,O,ka,Ms,Ca,Ba,_t,Je,Ut,L,va,Js,Za,Aa,Et,Te,It,Z,ga,Ts,Ga,Na,js,Xa,Ra,kt,je,Ct,y,Va,bs,Wa,qa,_s,za,$a,Us,Ha,Ya,Es,Sa,Fa,Bt,be,vt,H,K,Is,_e,Qa,ks,Pa,Zt,A,xa,Cs,Da,Oa,Bs,La,Ka,At,ee,eo,vs,so,to,gt,U,lo,Zs,ao,oo,As,no,io,gs,po,ro,Gt,g,co,Gs,ho,uo,Ns,mo,yo,Nt,se,fo,Xs,wo,Mo,Xt,E,te,Rs,Jo,To,Vs,jo,bo,_o,le,Ws,Uo,Eo,qs,Io,ko,Co,ae,zs,Bo,vo,$s,Zo,Ao,go,oe,Hs,Go,No,Ys,Xo,Ro,Rt;return ce=new Dt({}),de=new X({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFBpcGVsaW5lJTBBJTBBJTBBY2xhc3MlMjBNeVBpcGVsaW5lKFBpcGVsaW5lKSUzQSUwQSUyMCUyMCUyMCUyMGRlZiUyMF9zYW5pdGl6ZV9wYXJhbWV0ZXJzKHNlbGYlMkMlMjAqKmt3YXJncyklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcmVwcm9jZXNzX2t3YXJncyUyMCUzRCUyMCU3QiU3RCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGlmJTIwJTIybWF5YmVfYXJnJTIyJTIwaW4lMjBrd2FyZ3MlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcmVwcm9jZXNzX2t3YXJncyU1QiUyMm1heWJlX2FyZyUyMiU1RCUyMCUzRCUyMGt3YXJncyU1QiUyMm1heWJlX2FyZyUyMiU1RCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJldHVybiUyMHByZXByb2Nlc3Nfa3dhcmdzJTJDJTIwJTdCJTdEJTJDJTIwJTdCJTdEJTBBJTBBJTIwJTIwJTIwJTIwZGVmJTIwcHJlcHJvY2VzcyhzZWxmJTJDJTIwaW5wdXRzJTJDJTIwbWF5YmVfYXJnJTNEMiklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBtb2RlbF9pbnB1dCUyMCUzRCUyMFRlbnNvcihpbnB1dHMlNUIlMjJpbnB1dF9pZHMlMjIlNUQpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTdCJTIybW9kZWxfaW5wdXQlMjIlM0ElMjBtb2RlbF9pbnB1dCU3RCUwQSUwQSUyMCUyMCUyMCUyMGRlZiUyMF9mb3J3YXJkKHNlbGYlMkMlMjBtb2RlbF9pbnB1dHMpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIzJTIwbW9kZWxfaW5wdXRzJTIwJTNEJTNEJTIwJTdCJTIybW9kZWxfaW5wdXQlMjIlM0ElMjBtb2RlbF9pbnB1dCU3RCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dHMlMjAlM0QlMjBzZWxmLm1vZGVsKCoqbW9kZWxfaW5wdXRzKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMyUyME1heWJlJTIwJTdCJTIybG9naXRzJTIyJTNBJTIwVGVuc29yKC4uLiklN0QlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm4lMjBvdXRwdXRzJTBBJTBBJTIwJTIwJTIwJTIwZGVmJTIwcG9zdHByb2Nlc3Moc2VsZiUyQyUyMG1vZGVsX291dHB1dHMpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYmVzdF9jbGFzcyUyMCUzRCUyMG1vZGVsX291dHB1dHMlNUIlMjJsb2dpdHMlMjIlNUQuc29mdG1heCgtMSklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm4lMjBiZXN0X2NsYXNz",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Pipeline


<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyPipeline</span>(<span class="hljs-title class_ inherited__">Pipeline</span>):
    <span class="hljs-keyword">def</span> <span class="hljs-title function_">_sanitize_parameters</span>(<span class="hljs-params">self, **kwargs</span>):
        preprocess_kwargs = {}
        <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;maybe_arg&quot;</span> <span class="hljs-keyword">in</span> kwargs:
            preprocess_kwargs[<span class="hljs-string">&quot;maybe_arg&quot;</span>] = kwargs[<span class="hljs-string">&quot;maybe_arg&quot;</span>]
        <span class="hljs-keyword">return</span> preprocess_kwargs, {}, {}

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess</span>(<span class="hljs-params">self, inputs, maybe_arg=<span class="hljs-number">2</span></span>):
        model_input = Tensor(inputs[<span class="hljs-string">&quot;input_ids&quot;</span>])
        <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;model_input&quot;</span>: model_input}

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">_forward</span>(<span class="hljs-params">self, model_inputs</span>):
        <span class="hljs-comment"># model_inputs == {&quot;model_input&quot;: model_input}</span>
        outputs = self.model(**model_inputs)
        <span class="hljs-comment"># Maybe {&quot;logits&quot;: Tensor(...)}</span>
        <span class="hljs-keyword">return</span> outputs

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">postprocess</span>(<span class="hljs-params">self, model_outputs</span>):
        best_class = model_outputs[<span class="hljs-string">&quot;logits&quot;</span>].softmax(-<span class="hljs-number">1</span>)
        <span class="hljs-keyword">return</span> best_class`}}),he=new X({props:{code:"cGlwZSUyMCUzRCUyMHBpcGVsaW5lKCUyMm15LW5ldy10YXNrJTIyKSUwQXBpcGUoJTIyVGhpcyUyMGlzJTIwYSUyMHRlc3QlMjIpJTBBJTBBcGlwZSglMjJUaGlzJTIwaXMlMjBhJTIwdGVzdCUyMiUyQyUyMHRvcF9rJTNEMik=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>pipe = pipeline(<span class="hljs-string">&quot;my-new-task&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>pipe(<span class="hljs-string">&quot;This is a test&quot;</span>)
[{<span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;1-star&quot;</span>, <span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.8</span>}, {<span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;2-star&quot;</span>, <span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.1</span>}, {<span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;3-star&quot;</span>, <span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.05</span>}
{<span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;4-star&quot;</span>, <span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.025</span>}, {<span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;5-star&quot;</span>, <span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.025</span>}]

<span class="hljs-meta">&gt;&gt;&gt; </span>pipe(<span class="hljs-string">&quot;This is a test&quot;</span>, top_k=<span class="hljs-number">2</span>)
[{<span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;1-star&quot;</span>, <span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.8</span>}, {<span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;2-star&quot;</span>, <span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.1</span>}]`}}),ue=new X({props:{code:"ZGVmJTIwcG9zdHByb2Nlc3Moc2VsZiUyQyUyMG1vZGVsX291dHB1dHMlMkMlMjB0b3BfayUzRDUpJTNBJTBBJTIwJTIwJTIwJTIwYmVzdF9jbGFzcyUyMCUzRCUyMG1vZGVsX291dHB1dHMlNUIlMjJsb2dpdHMlMjIlNUQuc29mdG1heCgtMSklMEElMjAlMjAlMjAlMjAlMjMlMjBBZGQlMjBsb2dpYyUyMHRvJTIwaGFuZGxlJTIwdG9wX2slMEElMjAlMjAlMjAlMjByZXR1cm4lMjBiZXN0X2NsYXNzJTBBJTBBJTBBZGVmJTIwX3Nhbml0aXplX3BhcmFtZXRlcnMoc2VsZiUyQyUyMCoqa3dhcmdzKSUzQSUwQSUyMCUyMCUyMCUyMHByZXByb2Nlc3Nfa3dhcmdzJTIwJTNEJTIwJTdCJTdEJTBBJTIwJTIwJTIwJTIwaWYlMjAlMjJtYXliZV9hcmclMjIlMjBpbiUyMGt3YXJncyUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByZXByb2Nlc3Nfa3dhcmdzJTVCJTIybWF5YmVfYXJnJTIyJTVEJTIwJTNEJTIwa3dhcmdzJTVCJTIybWF5YmVfYXJnJTIyJTVEJTBBJTBBJTIwJTIwJTIwJTIwcG9zdHByb2Nlc3Nfa3dhcmdzJTIwJTNEJTIwJTdCJTdEJTBBJTIwJTIwJTIwJTIwaWYlMjAlMjJ0b3BfayUyMiUyMGluJTIwa3dhcmdzJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcG9zdHByb2Nlc3Nfa3dhcmdzJTVCJTIydG9wX2slMjIlNUQlMjAlM0QlMjBrd2FyZ3MlNUIlMjJ0b3BfayUyMiU1RCUwQSUyMCUyMCUyMCUyMHJldHVybiUyMHByZXByb2Nlc3Nfa3dhcmdzJTJDJTIwJTdCJTdEJTJDJTIwcG9zdHByb2Nlc3Nfa3dhcmdz",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">postprocess</span>(<span class="hljs-params">self, model_outputs, top_k=<span class="hljs-number">5</span></span>):
    best_class = model_outputs[<span class="hljs-string">&quot;logits&quot;</span>].softmax(-<span class="hljs-number">1</span>)
    <span class="hljs-comment"># Add logic to handle top_k</span>
    <span class="hljs-keyword">return</span> best_class


<span class="hljs-keyword">def</span> <span class="hljs-title function_">_sanitize_parameters</span>(<span class="hljs-params">self, **kwargs</span>):
    preprocess_kwargs = {}
    <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;maybe_arg&quot;</span> <span class="hljs-keyword">in</span> kwargs:
        preprocess_kwargs[<span class="hljs-string">&quot;maybe_arg&quot;</span>] = kwargs[<span class="hljs-string">&quot;maybe_arg&quot;</span>]

    postprocess_kwargs = {}
    <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;top_k&quot;</span> <span class="hljs-keyword">in</span> kwargs:
        postprocess_kwargs[<span class="hljs-string">&quot;top_k&quot;</span>] = kwargs[<span class="hljs-string">&quot;top_k&quot;</span>]
    <span class="hljs-keyword">return</span> preprocess_kwargs, {}, postprocess_kwargs`}}),me=new Dt({}),ye=new X({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycy5waXBlbGluZXMlMjBpbXBvcnQlMjBQSVBFTElORV9SRUdJU1RSWSUwQSUwQVBJUEVMSU5FX1JFR0lTVFJZLnJlZ2lzdGVyX3BpcGVsaW5lKCUwQSUyMCUyMCUyMCUyMCUyMm5ldy10YXNrJTIyJTJDJTBBJTIwJTIwJTIwJTIwcGlwZWxpbmVfY2xhc3MlM0RNeVBpcGVsaW5lJTJDJTBBJTIwJTIwJTIwJTIwcHRfbW9kZWwlM0RBdXRvTW9kZWxGb3JTZXF1ZW5jZUNsYXNzaWZpY2F0aW9uJTJDJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers.pipelines <span class="hljs-keyword">import</span> PIPELINE_REGISTRY

PIPELINE_REGISTRY.register_pipeline(
    <span class="hljs-string">&quot;new-task&quot;</span>,
    pipeline_class=MyPipeline,
    pt_model=AutoModelForSequenceClassification,
)`}}),fe=new X({props:{code:"UElQRUxJTkVfUkVHSVNUUlkucmVnaXN0ZXJfcGlwZWxpbmUoJTBBJTIwJTIwJTIwJTIwJTIybmV3LXRhc2slMjIlMkMlMEElMjAlMjAlMjAlMjBwaXBlbGluZV9jbGFzcyUzRE15UGlwZWxpbmUlMkMlMEElMjAlMjAlMjAlMjBwdF9tb2RlbCUzREF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMkMlMEElMjAlMjAlMjAlMjBkZWZhdWx0JTNEJTdCJTIycHQlMjIlM0ElMjAoJTIydXNlciUyRmF3ZXNvbWVfbW9kZWwlMjIlMkMlMjAlMjJhYmNkZWYlMjIpJTdEJTJDJTBBJTIwJTIwJTIwJTIwdHlwZSUzRCUyMnRleHQlMjIlMkMlMjAlMjAlMjMlMjBjdXJyZW50JTIwc3VwcG9ydCUyMHR5cGUlM0ElMjB0ZXh0JTJDJTIwYXVkaW8lMkMlMjBpbWFnZSUyQyUyMG11bHRpbW9kYWwlMEEp",highlighted:`PIPELINE_REGISTRY.register_pipeline(
    <span class="hljs-string">&quot;new-task&quot;</span>,
    pipeline_class=MyPipeline,
    pt_model=AutoModelForSequenceClassification,
    default={<span class="hljs-string">&quot;pt&quot;</span>: (<span class="hljs-string">&quot;user/awesome_model&quot;</span>, <span class="hljs-string">&quot;abcdef&quot;</span>)},
    <span class="hljs-built_in">type</span>=<span class="hljs-string">&quot;text&quot;</span>,  <span class="hljs-comment"># current support type: text, audio, image, multimodal</span>
)`}}),we=new Dt({}),Me=new X({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFBpcGVsaW5lJTBBJTBBJTBBZGVmJTIwc29mdG1heChvdXRwdXRzKSUzQSUwQSUyMCUyMCUyMCUyMG1heGVzJTIwJTNEJTIwbnAubWF4KG91dHB1dHMlMkMlMjBheGlzJTNELTElMkMlMjBrZWVwZGltcyUzRFRydWUpJTBBJTIwJTIwJTIwJTIwc2hpZnRlZF9leHAlMjAlM0QlMjBucC5leHAob3V0cHV0cyUyMC0lMjBtYXhlcyklMEElMjAlMjAlMjAlMjByZXR1cm4lMjBzaGlmdGVkX2V4cCUyMCUyRiUyMHNoaWZ0ZWRfZXhwLnN1bShheGlzJTNELTElMkMlMjBrZWVwZGltcyUzRFRydWUpJTBBJTBBJTBBY2xhc3MlMjBQYWlyQ2xhc3NpZmljYXRpb25QaXBlbGluZShQaXBlbGluZSklM0ElMEElMjAlMjAlMjAlMjBkZWYlMjBfc2FuaXRpemVfcGFyYW1ldGVycyhzZWxmJTJDJTIwKiprd2FyZ3MpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJlcHJvY2Vzc19rd2FyZ3MlMjAlM0QlMjAlN0IlN0QlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpZiUyMCUyMnNlY29uZF90ZXh0JTIyJTIwaW4lMjBrd2FyZ3MlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcmVwcm9jZXNzX2t3YXJncyU1QiUyMnNlY29uZF90ZXh0JTIyJTVEJTIwJTNEJTIwa3dhcmdzJTVCJTIyc2Vjb25kX3RleHQlMjIlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm4lMjBwcmVwcm9jZXNzX2t3YXJncyUyQyUyMCU3QiU3RCUyQyUyMCU3QiU3RCUwQSUwQSUyMCUyMCUyMCUyMGRlZiUyMHByZXByb2Nlc3Moc2VsZiUyQyUyMHRleHQlMkMlMjBzZWNvbmRfdGV4dCUzRE5vbmUpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwc2VsZi50b2tlbml6ZXIodGV4dCUyQyUyMHRleHRfcGFpciUzRHNlY29uZF90ZXh0JTJDJTIwcmV0dXJuX3RlbnNvcnMlM0RzZWxmLmZyYW1ld29yayklMEElMEElMjAlMjAlMjAlMjBkZWYlMjBfZm9yd2FyZChzZWxmJTJDJTIwbW9kZWxfaW5wdXRzKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJldHVybiUyMHNlbGYubW9kZWwoKiptb2RlbF9pbnB1dHMpJTBBJTBBJTIwJTIwJTIwJTIwZGVmJTIwcG9zdHByb2Nlc3Moc2VsZiUyQyUyMG1vZGVsX291dHB1dHMpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbG9naXRzJTIwJTNEJTIwbW9kZWxfb3V0cHV0cy5sb2dpdHMlNUIwJTVELm51bXB5KCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9iYWJpbGl0aWVzJTIwJTNEJTIwc29mdG1heChsb2dpdHMpJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYmVzdF9jbGFzcyUyMCUzRCUyMG5wLmFyZ21heChwcm9iYWJpbGl0aWVzKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxhYmVsJTIwJTNEJTIwc2VsZi5tb2RlbC5jb25maWcuaWQybGFiZWwlNUJiZXN0X2NsYXNzJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2NvcmUlMjAlM0QlMjBwcm9iYWJpbGl0aWVzJTVCYmVzdF9jbGFzcyU1RC5pdGVtKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb2dpdHMlMjAlM0QlMjBsb2dpdHMudG9saXN0KCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm4lMjAlN0IlMjJsYWJlbCUyMiUzQSUyMGxhYmVsJTJDJTIwJTIyc2NvcmUlMjIlM0ElMjBzY29yZSUyQyUyMCUyMmxvZ2l0cyUyMiUzQSUyMGxvZ2l0cyU3RA==",highlighted:`<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Pipeline


<span class="hljs-keyword">def</span> <span class="hljs-title function_">softmax</span>(<span class="hljs-params">outputs</span>):
    maxes = np.<span class="hljs-built_in">max</span>(outputs, axis=-<span class="hljs-number">1</span>, keepdims=<span class="hljs-literal">True</span>)
    shifted_exp = np.exp(outputs - maxes)
    <span class="hljs-keyword">return</span> shifted_exp / shifted_exp.<span class="hljs-built_in">sum</span>(axis=-<span class="hljs-number">1</span>, keepdims=<span class="hljs-literal">True</span>)


<span class="hljs-keyword">class</span> <span class="hljs-title class_">PairClassificationPipeline</span>(<span class="hljs-title class_ inherited__">Pipeline</span>):
    <span class="hljs-keyword">def</span> <span class="hljs-title function_">_sanitize_parameters</span>(<span class="hljs-params">self, **kwargs</span>):
        preprocess_kwargs = {}
        <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;second_text&quot;</span> <span class="hljs-keyword">in</span> kwargs:
            preprocess_kwargs[<span class="hljs-string">&quot;second_text&quot;</span>] = kwargs[<span class="hljs-string">&quot;second_text&quot;</span>]
        <span class="hljs-keyword">return</span> preprocess_kwargs, {}, {}

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess</span>(<span class="hljs-params">self, text, second_text=<span class="hljs-literal">None</span></span>):
        <span class="hljs-keyword">return</span> self.tokenizer(text, text_pair=second_text, return_tensors=self.framework)

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">_forward</span>(<span class="hljs-params">self, model_inputs</span>):
        <span class="hljs-keyword">return</span> self.model(**model_inputs)

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">postprocess</span>(<span class="hljs-params">self, model_outputs</span>):
        logits = model_outputs.logits[<span class="hljs-number">0</span>].numpy()
        probabilities = softmax(logits)

        best_class = np.argmax(probabilities)
        label = self.model.config.id2label[best_class]
        score = probabilities[best_class].item()
        logits = logits.tolist()
        <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;label&quot;</span>: label, <span class="hljs-string">&quot;score&quot;</span>: score, <span class="hljs-string">&quot;logits&quot;</span>: logits}`}}),Je=new X({props:{code:"ZnJvbSUyMHBhaXJfY2xhc3NpZmljYXRpb24lMjBpbXBvcnQlMjBQYWlyQ2xhc3NpZmljYXRpb25QaXBlbGluZSUwQWZyb20lMjB0cmFuc2Zvcm1lcnMucGlwZWxpbmVzJTIwaW1wb3J0JTIwUElQRUxJTkVfUkVHSVNUUlklMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbiUyQyUyMFRGQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbiUwQSUwQVBJUEVMSU5FX1JFR0lTVFJZLnJlZ2lzdGVyX3BpcGVsaW5lKCUwQSUyMCUyMCUyMCUyMCUyMnBhaXItY2xhc3NpZmljYXRpb24lMjIlMkMlMEElMjAlMjAlMjAlMjBwaXBlbGluZV9jbGFzcyUzRFBhaXJDbGFzc2lmaWNhdGlvblBpcGVsaW5lJTJDJTBBJTIwJTIwJTIwJTIwcHRfbW9kZWwlM0RBdXRvTW9kZWxGb3JTZXF1ZW5jZUNsYXNzaWZpY2F0aW9uJTJDJTBBJTIwJTIwJTIwJTIwdGZfbW9kZWwlM0RURkF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> pair_classification <span class="hljs-keyword">import</span> PairClassificationPipeline
<span class="hljs-keyword">from</span> transformers.pipelines <span class="hljs-keyword">import</span> PIPELINE_REGISTRY
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification, TFAutoModelForSequenceClassification

PIPELINE_REGISTRY.register_pipeline(
    <span class="hljs-string">&quot;pair-classification&quot;</span>,
    pipeline_class=PairClassificationPipeline,
    pt_model=AutoModelForSequenceClassification,
    tf_model=TFAutoModelForSequenceClassification,
)`}}),Te=new X({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBY2xhc3NpZmllciUyMCUzRCUyMHBpcGVsaW5lKCUyMnBhaXItY2xhc3NpZmljYXRpb24lMjIlMkMlMjBtb2RlbCUzRCUyMnNndWdnZXIlMkZmaW5ldHVuZWQtYmVydC1tcnBjJTIyKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

classifier = pipeline(<span class="hljs-string">&quot;pair-classification&quot;</span>, model=<span class="hljs-string">&quot;sgugger/finetuned-bert-mrpc&quot;</span>)`}}),je=new X({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMFJlcG9zaXRvcnklMEElMEFyZXBvJTIwJTNEJTIwUmVwb3NpdG9yeSglMjJ0ZXN0LWR5bmFtaWMtcGlwZWxpbmUlMjIlMkMlMjBjbG9uZV9mcm9tJTNEJTIyJTdCeW91cl91c2VybmFtZSU3RCUyRnRlc3QtZHluYW1pYy1waXBlbGluZSUyMiklMEFjbGFzc2lmaWVyLnNhdmVfcHJldHJhaW5lZCglMjJ0ZXN0LWR5bmFtaWMtcGlwZWxpbmUlMjIpJTBBcmVwby5wdXNoX3RvX2h1Yigp",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository

repo = Repository(<span class="hljs-string">&quot;test-dynamic-pipeline&quot;</span>, clone_from=<span class="hljs-string">&quot;{your_username}/test-dynamic-pipeline&quot;</span>)
classifier.save_pretrained(<span class="hljs-string">&quot;test-dynamic-pipeline&quot;</span>)
repo.push_to_hub()`}}),be=new X({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBY2xhc3NpZmllciUyMCUzRCUyMHBpcGVsaW5lKG1vZGVsJTNEJTIyJTdCeW91cl91c2VybmFtZSU3RCUyRnRlc3QtZHluYW1pYy1waXBlbGluZSUyMiUyQyUyMHRydXN0X3JlbW90ZV9jb2RlJTNEVHJ1ZSk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

classifier = pipeline(model=<span class="hljs-string">&quot;{your_username}/test-dynamic-pipeline&quot;</span>, trust_remote_code=<span class="hljs-literal">True</span>)`}}),_e=new Dt({}),{c(){R=o("meta"),Os=c(),V=o("h1"),Y=o("a"),Ge=o("span"),f(ce.$$.fragment),Ot=c(),Ne=o("span"),Lt=l("How to create a custom pipeline?"),Ls=c(),S=o("p"),Kt=l("In this guide, we will see how to create a custom pipeline and share it on the "),Ie=o("a"),el=l("Hub"),sl=l(` or add it to the
\u{1F917} Transformers library.`),Ks=c(),B=o("p"),tl=l(`First and foremost, you need to decide the raw entries the pipeline will be able to take. It can be strings, raw bytes,
dictionaries or whatever seems to be the most likely desired input. Try to keep these inputs as pure Python as possible
as it makes compatibility easier (even through other languages via JSON). Those will be the `),Xe=o("code"),ll=l("inputs"),al=l(` of the
pipeline (`),Re=o("code"),ol=l("preprocess"),nl=l(")."),et=c(),b=o("p"),il=l("Then define the "),Ve=o("code"),pl=l("outputs"),rl=l(". Same policy as the "),We=o("code"),cl=l("inputs"),dl=l(`. The simpler, the better. Those will be the outputs of
`),qe=o("code"),hl=l("postprocess"),ul=l(" method."),st=c(),u=o("p"),ml=l("Start by inheriting the base class "),ze=o("code"),yl=l("Pipeline"),fl=l(" with the 4 methods needed to implement "),$e=o("code"),wl=l("preprocess"),Ml=l(`,
`),He=o("code"),Jl=l("_forward"),Tl=l(", "),Ye=o("code"),jl=l("postprocess"),bl=l(", and "),Se=o("code"),_l=l("_sanitize_parameters"),Ul=l("."),tt=c(),f(de.$$.fragment),lt=c(),ke=o("p"),El=l(`The structure of this breakdown is to support relatively seamless support for CPU/GPU, while supporting doing
pre/postprocessing on the CPU on different threads`),at=c(),W=o("p"),Fe=o("code"),Il=l("preprocess"),kl=l(` will take the originally defined inputs, and turn them into something feedable to the model. It might
contain more information and is usually a `),Qe=o("code"),Cl=l("Dict"),Bl=l("."),ot=c(),k=o("p"),Pe=o("code"),vl=l("_forward"),Zl=l(" is the implementation detail and is not meant to be called directly. "),xe=o("code"),Al=l("forward"),gl=l(` is the preferred
called method as it contains safeguards to make sure everything is working on the expected device. If anything is
linked to a real model it belongs in the `),De=o("code"),Gl=l("_forward"),Nl=l(" method, anything else is in the preprocess/postprocess."),nt=c(),q=o("p"),Oe=o("code"),Xl=l("postprocess"),Rl=l(" methods will take the output of "),Le=o("code"),Vl=l("_forward"),Wl=l(` and turn it into the final output that was decided
earlier.`),it=c(),C=o("p"),Ke=o("code"),ql=l("_sanitize_parameters"),zl=l(` exists to allow users to pass any parameters whenever they wish, be it at initialization
time `),es=o("code"),$l=l("pipeline(...., maybe_arg=4)"),Hl=l(" or at call time "),ss=o("code"),Yl=l("pipe = pipeline(...); output = pipe(...., maybe_arg=4)"),Sl=l("."),pt=c(),m=o("p"),Fl=l("The returns of "),ts=o("code"),Ql=l("_sanitize_parameters"),Pl=l(" are the 3 dicts of kwargs that will be passed directly to "),ls=o("code"),xl=l("preprocess"),Dl=l(`,
`),as=o("code"),Ol=l("_forward"),Ll=l(", and "),os=o("code"),Kl=l("postprocess"),ea=l(`. Don\u2019t fill anything if the caller didn\u2019t call with any extra parameter. That
allows to keep the default arguments in the function definition which is always more \u201Cnatural\u201D.`),rt=c(),F=o("p"),sa=l("A classic example would be a "),ns=o("code"),ta=l("top_k"),la=l(" argument in the post processing in classification tasks."),ct=c(),f(he.$$.fragment),dt=c(),_=o("p"),aa=l("In order to achieve that, we\u2019ll update our "),is=o("code"),oa=l("postprocess"),na=l(" method with a default parameter to "),ps=o("code"),ia=l("5"),pa=l(`. and edit
`),rs=o("code"),ra=l("_sanitize_parameters"),ca=l(" to allow this new parameter."),ht=c(),f(ue.$$.fragment),ut=c(),Ce=o("p"),da=l(`Try to keep the inputs/outputs very simple and ideally JSON-serializable as it makes the pipeline usage very easy
without requiring users to understand new kind of objects. It\u2019s also relatively common to support many different types
of arguments for ease of use (audio files, can be filenames, URLs or pure bytes)`),mt=c(),z=o("h2"),Q=o("a"),cs=o("span"),f(me.$$.fragment),ha=c(),ds=o("span"),ua=l("Adding it to the list of supported tasks"),yt=c(),v=o("p"),ma=l("To register your "),hs=o("code"),ya=l("new-task"),fa=l(" to the list of supported tasks, you have to add it to the "),us=o("code"),wa=l("PIPELINE_REGISTRY"),Ma=l(":"),ft=c(),f(ye.$$.fragment),wt=c(),P=o("p"),Ja=l("You can specify a default model if you want, in which case it should come with a specific revision (which can be the name of a branch or a commit hash, here we took "),ms=o("code"),Ta=l('"abcdef"'),ja=l(") as well as the type:"),Mt=c(),f(fe.$$.fragment),Jt=c(),$=o("h2"),x=o("a"),ys=o("span"),f(we.$$.fragment),ba=c(),fs=o("span"),_a=l("Share your pipeline on the Hub"),Tt=c(),D=o("p"),Ua=l("To share your custom pipeline on the Hub, you just have to save the custom code of your "),ws=o("code"),Ea=l("Pipeline"),Ia=l(` subclass in a
python file. For instance, let\u2019s say we want to use a custom pipeline for sentence pair classification like this:`),jt=c(),f(Me.$$.fragment),bt=c(),O=o("p"),ka=l(`The implementation is framework agnostic, and will work for PyTorch and TensorFlow models. If we have saved this in
a file named `),Ms=o("code"),Ca=l("pair_classification.py"),Ba=l(", we can then import it and register it like this:"),_t=c(),f(Je.$$.fragment),Ut=c(),L=o("p"),va=l("Once this is done, we can use it with a pretrained model. For instance "),Js=o("code"),Za=l("sgugger/finetuned-bert-mrpc"),Aa=l(` has been
fine-tuned on the MRPC dataset, which classifies pairs of sentences as paraphrases or not.`),Et=c(),f(Te.$$.fragment),It=c(),Z=o("p"),ga=l("Then we can share it on the Hub by using the "),Ts=o("code"),Ga=l("save_pretrained"),Na=l(" method in a "),js=o("code"),Xa=l("Repository"),Ra=l(":"),kt=c(),f(je.$$.fragment),Ct=c(),y=o("p"),Va=l("This will copy the file where you defined "),bs=o("code"),Wa=l("PairClassificationPipeline"),qa=l(" inside the folder "),_s=o("code"),za=l('"test-dynamic-pipeline"'),$a=l(`,
along with saving the model and tokenizer of the pipeline, before pushing everything in the repository
`),Us=o("code"),Ha=l("{your_username}/test-dynamic-pipeline"),Ya=l(`. After that anyone can use it as long as they provide the option
`),Es=o("code"),Sa=l("trust_remote_code=True"),Fa=l(":"),Bt=c(),f(be.$$.fragment),vt=c(),H=o("h2"),K=o("a"),Is=o("span"),f(_e.$$.fragment),Qa=c(),ks=o("span"),Pa=l("Add the pipeline to \u{1F917} Transformers"),Zt=c(),A=o("p"),xa=l("If you want to contribute your pipeline to \u{1F917} Transformers, you will need to add a new module in the "),Cs=o("code"),Da=l("pipelines"),Oa=l(` submodule
with the code of your pipeline, then add it in the list of tasks defined in `),Bs=o("code"),La=l("pipelines/__init__.py"),Ka=l("."),At=c(),ee=o("p"),eo=l("Then you will need to add tests. Create a new file "),vs=o("code"),so=l("tests/test_pipelines_MY_PIPELINE.py"),to=l(" with example with the other tests."),gt=c(),U=o("p"),lo=l("The "),Zs=o("code"),ao=l("run_pipeline_test"),oo=l(` function will be very generic and run on small random models on every possible
architecture as defined by `),As=o("code"),no=l("model_mapping"),io=l(" and "),gs=o("code"),po=l("tf_model_mapping"),ro=l("."),Gt=c(),g=o("p"),co=l(`This is very important to test future compatibility, meaning if someone adds a new model for
`),Gs=o("code"),ho=l("XXXForQuestionAnswering"),uo=l(` then the pipeline test will attempt to run on it. Because the models are random it\u2019s
impossible to check for actual values, that\u2019s why there is a helper `),Ns=o("code"),mo=l("ANY"),yo=l(` that will simply attempt to match the
output of the pipeline TYPE.`),Nt=c(),se=o("p"),fo=l("You also "),Xs=o("em"),wo=l("need"),Mo=l(" to implement 2 (ideally 4) tests."),Xt=c(),E=o("ul"),te=o("li"),Rs=o("code"),Jo=l("test_small_model_pt"),To=l(` : Define 1 small model for this pipeline (doesn\u2019t matter if the results don\u2019t make sense)
and test the pipeline outputs. The results should be the same as `),Vs=o("code"),jo=l("test_small_model_tf"),bo=l("."),_o=c(),le=o("li"),Ws=o("code"),Uo=l("test_small_model_tf"),Eo=l(` : Define 1 small model for this pipeline (doesn\u2019t matter if the results don\u2019t make sense)
and test the pipeline outputs. The results should be the same as `),qs=o("code"),Io=l("test_small_model_pt"),ko=l("."),Co=c(),ae=o("li"),zs=o("code"),Bo=l("test_large_model_pt"),vo=l(" ("),$s=o("code"),Zo=l("optional"),Ao=l(`): Tests the pipeline on a real pipeline where the results are supposed to
make sense. These tests are slow and should be marked as such. Here the goal is to showcase the pipeline and to make
sure there is no drift in future releases.`),go=c(),oe=o("li"),Hs=o("code"),Go=l("test_large_model_tf"),No=l(" ("),Ys=o("code"),Xo=l("optional"),Ro=l(`): Tests the pipeline on a real pipeline where the results are supposed to
make sense. These tests are slow and should be marked as such. Here the goal is to showcase the pipeline and to make
sure there is no drift in future releases.`),this.h()},l(e){const p=ii('[data-svelte="svelte-1phssyn"]',document.head);R=n(p,"META",{name:!0,content:!0}),p.forEach(t),Os=d(e),V=n(e,"H1",{class:!0});var Vt=i(V);Y=n(Vt,"A",{id:!0,class:!0,href:!0});var Wo=i(Y);Ge=n(Wo,"SPAN",{});var qo=i(Ge);w(ce.$$.fragment,qo),qo.forEach(t),Wo.forEach(t),Ot=d(Vt),Ne=n(Vt,"SPAN",{});var zo=i(Ne);Lt=a(zo,"How to create a custom pipeline?"),zo.forEach(t),Vt.forEach(t),Ls=d(e),S=n(e,"P",{});var Wt=i(S);Kt=a(Wt,"In this guide, we will see how to create a custom pipeline and share it on the "),Ie=n(Wt,"A",{href:!0});var $o=i(Ie);el=a($o,"Hub"),$o.forEach(t),sl=a(Wt,` or add it to the
\u{1F917} Transformers library.`),Wt.forEach(t),Ks=d(e),B=n(e,"P",{});var Be=i(B);tl=a(Be,`First and foremost, you need to decide the raw entries the pipeline will be able to take. It can be strings, raw bytes,
dictionaries or whatever seems to be the most likely desired input. Try to keep these inputs as pure Python as possible
as it makes compatibility easier (even through other languages via JSON). Those will be the `),Xe=n(Be,"CODE",{});var Ho=i(Xe);ll=a(Ho,"inputs"),Ho.forEach(t),al=a(Be,` of the
pipeline (`),Re=n(Be,"CODE",{});var Yo=i(Re);ol=a(Yo,"preprocess"),Yo.forEach(t),nl=a(Be,")."),Be.forEach(t),et=d(e),b=n(e,"P",{});var ne=i(b);il=a(ne,"Then define the "),Ve=n(ne,"CODE",{});var So=i(Ve);pl=a(So,"outputs"),So.forEach(t),rl=a(ne,". Same policy as the "),We=n(ne,"CODE",{});var Fo=i(We);cl=a(Fo,"inputs"),Fo.forEach(t),dl=a(ne,`. The simpler, the better. Those will be the outputs of
`),qe=n(ne,"CODE",{});var Qo=i(qe);hl=a(Qo,"postprocess"),Qo.forEach(t),ul=a(ne," method."),ne.forEach(t),st=d(e),u=n(e,"P",{});var I=i(u);ml=a(I,"Start by inheriting the base class "),ze=n(I,"CODE",{});var Po=i(ze);yl=a(Po,"Pipeline"),Po.forEach(t),fl=a(I," with the 4 methods needed to implement "),$e=n(I,"CODE",{});var xo=i($e);wl=a(xo,"preprocess"),xo.forEach(t),Ml=a(I,`,
`),He=n(I,"CODE",{});var Do=i(He);Jl=a(Do,"_forward"),Do.forEach(t),Tl=a(I,", "),Ye=n(I,"CODE",{});var Oo=i(Ye);jl=a(Oo,"postprocess"),Oo.forEach(t),bl=a(I,", and "),Se=n(I,"CODE",{});var Lo=i(Se);_l=a(Lo,"_sanitize_parameters"),Lo.forEach(t),Ul=a(I,"."),I.forEach(t),tt=d(e),w(de.$$.fragment,e),lt=d(e),ke=n(e,"P",{});var Ko=i(ke);El=a(Ko,`The structure of this breakdown is to support relatively seamless support for CPU/GPU, while supporting doing
pre/postprocessing on the CPU on different threads`),Ko.forEach(t),at=d(e),W=n(e,"P",{});var Ss=i(W);Fe=n(Ss,"CODE",{});var en=i(Fe);Il=a(en,"preprocess"),en.forEach(t),kl=a(Ss,` will take the originally defined inputs, and turn them into something feedable to the model. It might
contain more information and is usually a `),Qe=n(Ss,"CODE",{});var sn=i(Qe);Cl=a(sn,"Dict"),sn.forEach(t),Bl=a(Ss,"."),Ss.forEach(t),ot=d(e),k=n(e,"P",{});var Ue=i(k);Pe=n(Ue,"CODE",{});var tn=i(Pe);vl=a(tn,"_forward"),tn.forEach(t),Zl=a(Ue," is the implementation detail and is not meant to be called directly. "),xe=n(Ue,"CODE",{});var ln=i(xe);Al=a(ln,"forward"),ln.forEach(t),gl=a(Ue,` is the preferred
called method as it contains safeguards to make sure everything is working on the expected device. If anything is
linked to a real model it belongs in the `),De=n(Ue,"CODE",{});var an=i(De);Gl=a(an,"_forward"),an.forEach(t),Nl=a(Ue," method, anything else is in the preprocess/postprocess."),Ue.forEach(t),nt=d(e),q=n(e,"P",{});var Fs=i(q);Oe=n(Fs,"CODE",{});var on=i(Oe);Xl=a(on,"postprocess"),on.forEach(t),Rl=a(Fs," methods will take the output of "),Le=n(Fs,"CODE",{});var nn=i(Le);Vl=a(nn,"_forward"),nn.forEach(t),Wl=a(Fs,` and turn it into the final output that was decided
earlier.`),Fs.forEach(t),it=d(e),C=n(e,"P",{});var Ee=i(C);Ke=n(Ee,"CODE",{});var pn=i(Ke);ql=a(pn,"_sanitize_parameters"),pn.forEach(t),zl=a(Ee,` exists to allow users to pass any parameters whenever they wish, be it at initialization
time `),es=n(Ee,"CODE",{});var rn=i(es);$l=a(rn,"pipeline(...., maybe_arg=4)"),rn.forEach(t),Hl=a(Ee," or at call time "),ss=n(Ee,"CODE",{});var cn=i(ss);Yl=a(cn,"pipe = pipeline(...); output = pipe(...., maybe_arg=4)"),cn.forEach(t),Sl=a(Ee,"."),Ee.forEach(t),pt=d(e),m=n(e,"P",{});var G=i(m);Fl=a(G,"The returns of "),ts=n(G,"CODE",{});var dn=i(ts);Ql=a(dn,"_sanitize_parameters"),dn.forEach(t),Pl=a(G," are the 3 dicts of kwargs that will be passed directly to "),ls=n(G,"CODE",{});var hn=i(ls);xl=a(hn,"preprocess"),hn.forEach(t),Dl=a(G,`,
`),as=n(G,"CODE",{});var un=i(as);Ol=a(un,"_forward"),un.forEach(t),Ll=a(G,", and "),os=n(G,"CODE",{});var mn=i(os);Kl=a(mn,"postprocess"),mn.forEach(t),ea=a(G,`. Don\u2019t fill anything if the caller didn\u2019t call with any extra parameter. That
allows to keep the default arguments in the function definition which is always more \u201Cnatural\u201D.`),G.forEach(t),rt=d(e),F=n(e,"P",{});var qt=i(F);sa=a(qt,"A classic example would be a "),ns=n(qt,"CODE",{});var yn=i(ns);ta=a(yn,"top_k"),yn.forEach(t),la=a(qt," argument in the post processing in classification tasks."),qt.forEach(t),ct=d(e),w(he.$$.fragment,e),dt=d(e),_=n(e,"P",{});var ie=i(_);aa=a(ie,"In order to achieve that, we\u2019ll update our "),is=n(ie,"CODE",{});var fn=i(is);oa=a(fn,"postprocess"),fn.forEach(t),na=a(ie," method with a default parameter to "),ps=n(ie,"CODE",{});var wn=i(ps);ia=a(wn,"5"),wn.forEach(t),pa=a(ie,`. and edit
`),rs=n(ie,"CODE",{});var Mn=i(rs);ra=a(Mn,"_sanitize_parameters"),Mn.forEach(t),ca=a(ie," to allow this new parameter."),ie.forEach(t),ht=d(e),w(ue.$$.fragment,e),ut=d(e),Ce=n(e,"P",{});var Jn=i(Ce);da=a(Jn,`Try to keep the inputs/outputs very simple and ideally JSON-serializable as it makes the pipeline usage very easy
without requiring users to understand new kind of objects. It\u2019s also relatively common to support many different types
of arguments for ease of use (audio files, can be filenames, URLs or pure bytes)`),Jn.forEach(t),mt=d(e),z=n(e,"H2",{class:!0});var zt=i(z);Q=n(zt,"A",{id:!0,class:!0,href:!0});var Tn=i(Q);cs=n(Tn,"SPAN",{});var jn=i(cs);w(me.$$.fragment,jn),jn.forEach(t),Tn.forEach(t),ha=d(zt),ds=n(zt,"SPAN",{});var bn=i(ds);ua=a(bn,"Adding it to the list of supported tasks"),bn.forEach(t),zt.forEach(t),yt=d(e),v=n(e,"P",{});var ve=i(v);ma=a(ve,"To register your "),hs=n(ve,"CODE",{});var _n=i(hs);ya=a(_n,"new-task"),_n.forEach(t),fa=a(ve," to the list of supported tasks, you have to add it to the "),us=n(ve,"CODE",{});var Un=i(us);wa=a(Un,"PIPELINE_REGISTRY"),Un.forEach(t),Ma=a(ve,":"),ve.forEach(t),ft=d(e),w(ye.$$.fragment,e),wt=d(e),P=n(e,"P",{});var $t=i(P);Ja=a($t,"You can specify a default model if you want, in which case it should come with a specific revision (which can be the name of a branch or a commit hash, here we took "),ms=n($t,"CODE",{});var En=i(ms);Ta=a(En,'"abcdef"'),En.forEach(t),ja=a($t,") as well as the type:"),$t.forEach(t),Mt=d(e),w(fe.$$.fragment,e),Jt=d(e),$=n(e,"H2",{class:!0});var Ht=i($);x=n(Ht,"A",{id:!0,class:!0,href:!0});var In=i(x);ys=n(In,"SPAN",{});var kn=i(ys);w(we.$$.fragment,kn),kn.forEach(t),In.forEach(t),ba=d(Ht),fs=n(Ht,"SPAN",{});var Cn=i(fs);_a=a(Cn,"Share your pipeline on the Hub"),Cn.forEach(t),Ht.forEach(t),Tt=d(e),D=n(e,"P",{});var Yt=i(D);Ua=a(Yt,"To share your custom pipeline on the Hub, you just have to save the custom code of your "),ws=n(Yt,"CODE",{});var Bn=i(ws);Ea=a(Bn,"Pipeline"),Bn.forEach(t),Ia=a(Yt,` subclass in a
python file. For instance, let\u2019s say we want to use a custom pipeline for sentence pair classification like this:`),Yt.forEach(t),jt=d(e),w(Me.$$.fragment,e),bt=d(e),O=n(e,"P",{});var St=i(O);ka=a(St,`The implementation is framework agnostic, and will work for PyTorch and TensorFlow models. If we have saved this in
a file named `),Ms=n(St,"CODE",{});var vn=i(Ms);Ca=a(vn,"pair_classification.py"),vn.forEach(t),Ba=a(St,", we can then import it and register it like this:"),St.forEach(t),_t=d(e),w(Je.$$.fragment,e),Ut=d(e),L=n(e,"P",{});var Ft=i(L);va=a(Ft,"Once this is done, we can use it with a pretrained model. For instance "),Js=n(Ft,"CODE",{});var Zn=i(Js);Za=a(Zn,"sgugger/finetuned-bert-mrpc"),Zn.forEach(t),Aa=a(Ft,` has been
fine-tuned on the MRPC dataset, which classifies pairs of sentences as paraphrases or not.`),Ft.forEach(t),Et=d(e),w(Te.$$.fragment,e),It=d(e),Z=n(e,"P",{});var Ze=i(Z);ga=a(Ze,"Then we can share it on the Hub by using the "),Ts=n(Ze,"CODE",{});var An=i(Ts);Ga=a(An,"save_pretrained"),An.forEach(t),Na=a(Ze," method in a "),js=n(Ze,"CODE",{});var gn=i(js);Xa=a(gn,"Repository"),gn.forEach(t),Ra=a(Ze,":"),Ze.forEach(t),kt=d(e),w(je.$$.fragment,e),Ct=d(e),y=n(e,"P",{});var N=i(y);Va=a(N,"This will copy the file where you defined "),bs=n(N,"CODE",{});var Gn=i(bs);Wa=a(Gn,"PairClassificationPipeline"),Gn.forEach(t),qa=a(N," inside the folder "),_s=n(N,"CODE",{});var Nn=i(_s);za=a(Nn,'"test-dynamic-pipeline"'),Nn.forEach(t),$a=a(N,`,
along with saving the model and tokenizer of the pipeline, before pushing everything in the repository
`),Us=n(N,"CODE",{});var Xn=i(Us);Ha=a(Xn,"{your_username}/test-dynamic-pipeline"),Xn.forEach(t),Ya=a(N,`. After that anyone can use it as long as they provide the option
`),Es=n(N,"CODE",{});var Rn=i(Es);Sa=a(Rn,"trust_remote_code=True"),Rn.forEach(t),Fa=a(N,":"),N.forEach(t),Bt=d(e),w(be.$$.fragment,e),vt=d(e),H=n(e,"H2",{class:!0});var Qt=i(H);K=n(Qt,"A",{id:!0,class:!0,href:!0});var Vn=i(K);Is=n(Vn,"SPAN",{});var Wn=i(Is);w(_e.$$.fragment,Wn),Wn.forEach(t),Vn.forEach(t),Qa=d(Qt),ks=n(Qt,"SPAN",{});var qn=i(ks);Pa=a(qn,"Add the pipeline to \u{1F917} Transformers"),qn.forEach(t),Qt.forEach(t),Zt=d(e),A=n(e,"P",{});var Ae=i(A);xa=a(Ae,"If you want to contribute your pipeline to \u{1F917} Transformers, you will need to add a new module in the "),Cs=n(Ae,"CODE",{});var zn=i(Cs);Da=a(zn,"pipelines"),zn.forEach(t),Oa=a(Ae,` submodule
with the code of your pipeline, then add it in the list of tasks defined in `),Bs=n(Ae,"CODE",{});var $n=i(Bs);La=a($n,"pipelines/__init__.py"),$n.forEach(t),Ka=a(Ae,"."),Ae.forEach(t),At=d(e),ee=n(e,"P",{});var Pt=i(ee);eo=a(Pt,"Then you will need to add tests. Create a new file "),vs=n(Pt,"CODE",{});var Hn=i(vs);so=a(Hn,"tests/test_pipelines_MY_PIPELINE.py"),Hn.forEach(t),to=a(Pt," with example with the other tests."),Pt.forEach(t),gt=d(e),U=n(e,"P",{});var pe=i(U);lo=a(pe,"The "),Zs=n(pe,"CODE",{});var Yn=i(Zs);ao=a(Yn,"run_pipeline_test"),Yn.forEach(t),oo=a(pe,` function will be very generic and run on small random models on every possible
architecture as defined by `),As=n(pe,"CODE",{});var Sn=i(As);no=a(Sn,"model_mapping"),Sn.forEach(t),io=a(pe," and "),gs=n(pe,"CODE",{});var Fn=i(gs);po=a(Fn,"tf_model_mapping"),Fn.forEach(t),ro=a(pe,"."),pe.forEach(t),Gt=d(e),g=n(e,"P",{});var ge=i(g);co=a(ge,`This is very important to test future compatibility, meaning if someone adds a new model for
`),Gs=n(ge,"CODE",{});var Qn=i(Gs);ho=a(Qn,"XXXForQuestionAnswering"),Qn.forEach(t),uo=a(ge,` then the pipeline test will attempt to run on it. Because the models are random it\u2019s
impossible to check for actual values, that\u2019s why there is a helper `),Ns=n(ge,"CODE",{});var Pn=i(Ns);mo=a(Pn,"ANY"),Pn.forEach(t),yo=a(ge,` that will simply attempt to match the
output of the pipeline TYPE.`),ge.forEach(t),Nt=d(e),se=n(e,"P",{});var xt=i(se);fo=a(xt,"You also "),Xs=n(xt,"EM",{});var xn=i(Xs);wo=a(xn,"need"),xn.forEach(t),Mo=a(xt," to implement 2 (ideally 4) tests."),xt.forEach(t),Xt=d(e),E=n(e,"UL",{});var re=i(E);te=n(re,"LI",{});var Qs=i(te);Rs=n(Qs,"CODE",{});var Dn=i(Rs);Jo=a(Dn,"test_small_model_pt"),Dn.forEach(t),To=a(Qs,` : Define 1 small model for this pipeline (doesn\u2019t matter if the results don\u2019t make sense)
and test the pipeline outputs. The results should be the same as `),Vs=n(Qs,"CODE",{});var On=i(Vs);jo=a(On,"test_small_model_tf"),On.forEach(t),bo=a(Qs,"."),Qs.forEach(t),_o=d(re),le=n(re,"LI",{});var Ps=i(le);Ws=n(Ps,"CODE",{});var Ln=i(Ws);Uo=a(Ln,"test_small_model_tf"),Ln.forEach(t),Eo=a(Ps,` : Define 1 small model for this pipeline (doesn\u2019t matter if the results don\u2019t make sense)
and test the pipeline outputs. The results should be the same as `),qs=n(Ps,"CODE",{});var Kn=i(qs);Io=a(Kn,"test_small_model_pt"),Kn.forEach(t),ko=a(Ps,"."),Ps.forEach(t),Co=d(re),ae=n(re,"LI",{});var xs=i(ae);zs=n(xs,"CODE",{});var ei=i(zs);Bo=a(ei,"test_large_model_pt"),ei.forEach(t),vo=a(xs," ("),$s=n(xs,"CODE",{});var si=i($s);Zo=a(si,"optional"),si.forEach(t),Ao=a(xs,`): Tests the pipeline on a real pipeline where the results are supposed to
make sense. These tests are slow and should be marked as such. Here the goal is to showcase the pipeline and to make
sure there is no drift in future releases.`),xs.forEach(t),go=d(re),oe=n(re,"LI",{});var Ds=i(oe);Hs=n(Ds,"CODE",{});var ti=i(Hs);Go=a(ti,"test_large_model_tf"),ti.forEach(t),No=a(Ds," ("),Ys=n(Ds,"CODE",{});var li=i(Ys);Xo=a(li,"optional"),li.forEach(t),Ro=a(Ds,`): Tests the pipeline on a real pipeline where the results are supposed to
make sense. These tests are slow and should be marked as such. Here the goal is to showcase the pipeline and to make
sure there is no drift in future releases.`),Ds.forEach(t),re.forEach(t),this.h()},h(){h(R,"name","hf:doc:metadata"),h(R,"content",JSON.stringify(di)),h(Y,"id","how-to-create-a-custom-pipeline"),h(Y,"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"),h(Y,"href","#how-to-create-a-custom-pipeline"),h(V,"class","relative group"),h(Ie,"href","hf.co/models"),h(Q,"id","adding-it-to-the-list-of-supported-tasks"),h(Q,"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"),h(Q,"href","#adding-it-to-the-list-of-supported-tasks"),h(z,"class","relative group"),h(x,"id","share-your-pipeline-on-the-hub"),h(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"),h(x,"href","#share-your-pipeline-on-the-hub"),h($,"class","relative group"),h(K,"id","add-the-pipeline-to-transformers"),h(K,"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"),h(K,"href","#add-the-pipeline-to-transformers"),h(H,"class","relative group")},m(e,p){s(document.head,R),r(e,Os,p),r(e,V,p),s(V,Y),s(Y,Ge),M(ce,Ge,null),s(V,Ot),s(V,Ne),s(Ne,Lt),r(e,Ls,p),r(e,S,p),s(S,Kt),s(S,Ie),s(Ie,el),s(S,sl),r(e,Ks,p),r(e,B,p),s(B,tl),s(B,Xe),s(Xe,ll),s(B,al),s(B,Re),s(Re,ol),s(B,nl),r(e,et,p),r(e,b,p),s(b,il),s(b,Ve),s(Ve,pl),s(b,rl),s(b,We),s(We,cl),s(b,dl),s(b,qe),s(qe,hl),s(b,ul),r(e,st,p),r(e,u,p),s(u,ml),s(u,ze),s(ze,yl),s(u,fl),s(u,$e),s($e,wl),s(u,Ml),s(u,He),s(He,Jl),s(u,Tl),s(u,Ye),s(Ye,jl),s(u,bl),s(u,Se),s(Se,_l),s(u,Ul),r(e,tt,p),M(de,e,p),r(e,lt,p),r(e,ke,p),s(ke,El),r(e,at,p),r(e,W,p),s(W,Fe),s(Fe,Il),s(W,kl),s(W,Qe),s(Qe,Cl),s(W,Bl),r(e,ot,p),r(e,k,p),s(k,Pe),s(Pe,vl),s(k,Zl),s(k,xe),s(xe,Al),s(k,gl),s(k,De),s(De,Gl),s(k,Nl),r(e,nt,p),r(e,q,p),s(q,Oe),s(Oe,Xl),s(q,Rl),s(q,Le),s(Le,Vl),s(q,Wl),r(e,it,p),r(e,C,p),s(C,Ke),s(Ke,ql),s(C,zl),s(C,es),s(es,$l),s(C,Hl),s(C,ss),s(ss,Yl),s(C,Sl),r(e,pt,p),r(e,m,p),s(m,Fl),s(m,ts),s(ts,Ql),s(m,Pl),s(m,ls),s(ls,xl),s(m,Dl),s(m,as),s(as,Ol),s(m,Ll),s(m,os),s(os,Kl),s(m,ea),r(e,rt,p),r(e,F,p),s(F,sa),s(F,ns),s(ns,ta),s(F,la),r(e,ct,p),M(he,e,p),r(e,dt,p),r(e,_,p),s(_,aa),s(_,is),s(is,oa),s(_,na),s(_,ps),s(ps,ia),s(_,pa),s(_,rs),s(rs,ra),s(_,ca),r(e,ht,p),M(ue,e,p),r(e,ut,p),r(e,Ce,p),s(Ce,da),r(e,mt,p),r(e,z,p),s(z,Q),s(Q,cs),M(me,cs,null),s(z,ha),s(z,ds),s(ds,ua),r(e,yt,p),r(e,v,p),s(v,ma),s(v,hs),s(hs,ya),s(v,fa),s(v,us),s(us,wa),s(v,Ma),r(e,ft,p),M(ye,e,p),r(e,wt,p),r(e,P,p),s(P,Ja),s(P,ms),s(ms,Ta),s(P,ja),r(e,Mt,p),M(fe,e,p),r(e,Jt,p),r(e,$,p),s($,x),s(x,ys),M(we,ys,null),s($,ba),s($,fs),s(fs,_a),r(e,Tt,p),r(e,D,p),s(D,Ua),s(D,ws),s(ws,Ea),s(D,Ia),r(e,jt,p),M(Me,e,p),r(e,bt,p),r(e,O,p),s(O,ka),s(O,Ms),s(Ms,Ca),s(O,Ba),r(e,_t,p),M(Je,e,p),r(e,Ut,p),r(e,L,p),s(L,va),s(L,Js),s(Js,Za),s(L,Aa),r(e,Et,p),M(Te,e,p),r(e,It,p),r(e,Z,p),s(Z,ga),s(Z,Ts),s(Ts,Ga),s(Z,Na),s(Z,js),s(js,Xa),s(Z,Ra),r(e,kt,p),M(je,e,p),r(e,Ct,p),r(e,y,p),s(y,Va),s(y,bs),s(bs,Wa),s(y,qa),s(y,_s),s(_s,za),s(y,$a),s(y,Us),s(Us,Ha),s(y,Ya),s(y,Es),s(Es,Sa),s(y,Fa),r(e,Bt,p),M(be,e,p),r(e,vt,p),r(e,H,p),s(H,K),s(K,Is),M(_e,Is,null),s(H,Qa),s(H,ks),s(ks,Pa),r(e,Zt,p),r(e,A,p),s(A,xa),s(A,Cs),s(Cs,Da),s(A,Oa),s(A,Bs),s(Bs,La),s(A,Ka),r(e,At,p),r(e,ee,p),s(ee,eo),s(ee,vs),s(vs,so),s(ee,to),r(e,gt,p),r(e,U,p),s(U,lo),s(U,Zs),s(Zs,ao),s(U,oo),s(U,As),s(As,no),s(U,io),s(U,gs),s(gs,po),s(U,ro),r(e,Gt,p),r(e,g,p),s(g,co),s(g,Gs),s(Gs,ho),s(g,uo),s(g,Ns),s(Ns,mo),s(g,yo),r(e,Nt,p),r(e,se,p),s(se,fo),s(se,Xs),s(Xs,wo),s(se,Mo),r(e,Xt,p),r(e,E,p),s(E,te),s(te,Rs),s(Rs,Jo),s(te,To),s(te,Vs),s(Vs,jo),s(te,bo),s(E,_o),s(E,le),s(le,Ws),s(Ws,Uo),s(le,Eo),s(le,qs),s(qs,Io),s(le,ko),s(E,Co),s(E,ae),s(ae,zs),s(zs,Bo),s(ae,vo),s(ae,$s),s($s,Zo),s(ae,Ao),s(E,go),s(E,oe),s(oe,Hs),s(Hs,Go),s(oe,No),s(oe,Ys),s(Ys,Xo),s(oe,Ro),Rt=!0},p:pi,i(e){Rt||(J(ce.$$.fragment,e),J(de.$$.fragment,e),J(he.$$.fragment,e),J(ue.$$.fragment,e),J(me.$$.fragment,e),J(ye.$$.fragment,e),J(fe.$$.fragment,e),J(we.$$.fragment,e),J(Me.$$.fragment,e),J(Je.$$.fragment,e),J(Te.$$.fragment,e),J(je.$$.fragment,e),J(be.$$.fragment,e),J(_e.$$.fragment,e),Rt=!0)},o(e){T(ce.$$.fragment,e),T(de.$$.fragment,e),T(he.$$.fragment,e),T(ue.$$.fragment,e),T(me.$$.fragment,e),T(ye.$$.fragment,e),T(fe.$$.fragment,e),T(we.$$.fragment,e),T(Me.$$.fragment,e),T(Je.$$.fragment,e),T(Te.$$.fragment,e),T(je.$$.fragment,e),T(be.$$.fragment,e),T(_e.$$.fragment,e),Rt=!1},d(e){t(R),e&&t(Os),e&&t(V),j(ce),e&&t(Ls),e&&t(S),e&&t(Ks),e&&t(B),e&&t(et),e&&t(b),e&&t(st),e&&t(u),e&&t(tt),j(de,e),e&&t(lt),e&&t(ke),e&&t(at),e&&t(W),e&&t(ot),e&&t(k),e&&t(nt),e&&t(q),e&&t(it),e&&t(C),e&&t(pt),e&&t(m),e&&t(rt),e&&t(F),e&&t(ct),j(he,e),e&&t(dt),e&&t(_),e&&t(ht),j(ue,e),e&&t(ut),e&&t(Ce),e&&t(mt),e&&t(z),j(me),e&&t(yt),e&&t(v),e&&t(ft),j(ye,e),e&&t(wt),e&&t(P),e&&t(Mt),j(fe,e),e&&t(Jt),e&&t($),j(we),e&&t(Tt),e&&t(D),e&&t(jt),j(Me,e),e&&t(bt),e&&t(O),e&&t(_t),j(Je,e),e&&t(Ut),e&&t(L),e&&t(Et),j(Te,e),e&&t(It),e&&t(Z),e&&t(kt),j(je,e),e&&t(Ct),e&&t(y),e&&t(Bt),j(be,e),e&&t(vt),e&&t(H),j(_e),e&&t(Zt),e&&t(A),e&&t(At),e&&t(ee),e&&t(gt),e&&t(U),e&&t(Gt),e&&t(g),e&&t(Nt),e&&t(se),e&&t(Xt),e&&t(E)}}}const di={local:"how-to-create-a-custom-pipeline",sections:[{local:"adding-it-to-the-list-of-supported-tasks",title:"Adding it to the list of supported tasks"},{local:"share-your-pipeline-on-the-hub",title:"Share your pipeline on the Hub"},{local:"add-the-pipeline-to-transformers",title:"Add the pipeline to \u{1F917} Transformers"}],title:"How to create a custom pipeline?"};function hi(Vo){return ri(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class fi extends ai{constructor(R){super();oi(this,R,hi,ci,ni,{})}}export{fi as default,di as metadata};
