import{s as bl,n as Bl,o as Cl}from"../chunks/scheduler.9991993c.js";import{S as _l,i as Zl,g as n,s as a,r as i,A as kl,h as M,f as e,c as p,j as Il,u as o,x as c,k as hl,y as Gl,a as t,v as r,d as y,t as J,w as T}from"../chunks/index.7fc9a5e7.js";import{C as d}from"../chunks/CodeBlock.e11cba92.js";import{H as Ss}from"../chunks/Heading.e3de321f.js";function Hl(Ls){let j,ps,ts,ns,w,Ms,m,Ps='在本指南中，我们将演示如何创建一个自定义流水线并分享到 <a href="https://hf.co/models" rel="nofollow">Hub</a>，或将其添加到 🤗 Transformers 库中。',cs,u,Ks=`首先，你需要决定流水线将能够接受的原始条目。它可以是字符串、原始字节、字典或任何看起来最可能是期望的输入。
尽量保持输入为纯 Python 语言，因为这样可以更容易地实现兼容性（甚至通过 JSON 在其他语言之间）。
这些将是流水线 (<code>preprocess</code>) 的 <code>inputs</code>。`,is,U,Ds="然后定义 <code>outputs</code>。与 <code>inputs</code> 相同的策略。越简单越好。这些将是 <code>postprocess</code> 方法的输出。",os,f,Os="首先继承基类 <code>Pipeline</code>，其中包含实现 <code>preprocess</code>、<code>_forward</code>、<code>postprocess</code> 和 <code>_sanitize_parameters</code> 所需的 4 个方法。",rs,I,ys,h,sl="这种分解的结构旨在为 CPU/GPU 提供相对无缝的支持，同时支持在不同线程上对 CPU 进行预处理/后处理。",Js,b,ll="<code>preprocess</code> 将接受最初定义的输入，并将其转换为可供模型输入的内容。它可能包含更多信息，通常是一个 <code>Dict</code>。",Ts,B,el=`<code>_forward</code> 是实现细节，不应直接调用。<code>forward</code> 是首选的调用方法，因为它包含保障措施，以确保一切都在预期的设备上运作。
如果任何内容与实际模型相关，它应该属于 <code>_forward</code> 方法，其他内容应该在 preprocess/postprocess 中。`,ds,C,tl="<code>postprocess</code> 方法将接受 <code>_forward</code> 的输出，并将其转换为之前确定的最终输出。",js,_,al=`<code>_sanitize_parameters</code> 存在是为了允许用户在任何时候传递任何参数，无论是在初始化时 <code>pipeline(...., maybe_arg=4)</code>
还是在调用时 <code>pipe = pipeline(...); output = pipe(...., maybe_arg=4)</code>。`,ws,Z,pl=`<code>_sanitize_parameters</code> 的返回值是将直接传递给 <code>preprocess</code>、<code>_forward</code> 和 <code>postprocess</code> 的 3 个关键字参数字典。
如果调用方没有使用任何额外参数调用，则不要填写任何内容。这样可以保留函数定义中的默认参数，这总是更”自然”的。`,ms,k,nl="在分类任务中，一个经典的例子是在后处理中使用 <code>top_k</code> 参数。",us,G,Us,H,Ml=`为了实现这一点，我们将更新我们的 <code>postprocess</code> 方法，将默认参数设置为 <code>5</code>，
并编辑 <code>_sanitize_parameters</code> 方法，以允许这个新参数。`,fs,V,Is,X,cl=`尽量保持简单输入/输出，最好是可 JSON 序列化的，因为这样可以使流水线的使用非常简单，而不需要用户了解新的对象类型。
通常也相对常见地支持许多不同类型的参数以便使用（例如音频文件，可以是文件名、URL 或纯字节）。`,hs,W,bs,A,il="要将你的 <code>new-task</code> 注册到支持的任务列表中，你需要将其添加到 <code>PIPELINE_REGISTRY</code> 中：",Bs,R,Cs,N,ol="如果需要，你可以指定一个默认模型，此时它应该带有一个特定的修订版本（可以是分支名称或提交哈希，这里我们使用了 <code>&quot;abcdef&quot;</code>），以及类型：",_s,g,Zs,q,ks,E,rl=`要在 Hub 上分享你的自定义流水线，你只需要将 <code>Pipeline</code> 子类的自定义代码保存在一个 Python 文件中。
例如，假设我们想使用一个自定义流水线进行句对分类，如下所示：`,Gs,z,Hs,$,yl=`这个实现与框架无关，适用于 PyTorch 和 TensorFlow 模型。如果我们将其保存在一个名为
<code>pair_classification.py</code> 的文件中，然后我们可以像这样导入并注册它：`,Vs,v,Xs,x,Jl=`完成这些步骤后，我们可以将其与预训练模型一起使用。例如，<code>sgugger/finetuned-bert-mrpc</code>
已经在 MRPC 数据集上进行了微调，用于将句子对分类为是释义或不是释义。`,Ws,Y,As,Q,Tl="然后，我们可以通过在 <code>Repository</code> 中使用 <code>save_pretrained</code> 方法将其分享到 Hub 上：",Rs,F,Ns,S,dl=`这将会复制包含你定义的 <code>PairClassificationPipeline</code> 的文件到文件夹 <code>&quot;test-dynamic-pipeline&quot;</code> 中，
同时保存流水线的模型和分词器，然后将所有内容推送到仓库 <code>{your_username}/test-dynamic-pipeline</code> 中。
之后，只要提供选项 <code>trust_remote_code=True</code>，任何人都可以使用它：`,gs,L,qs,P,Es,K,jl=`如果你想将你的流水线贡献给 🤗 Transformers，你需要在 <code>pipelines</code> 子模块中添加一个新模块，
其中包含你的流水线的代码，然后将其添加到 <code>pipelines/__init__.py</code> 中定义的任务列表中。`,zs,D,wl="然后，你需要添加测试。创建一个新文件 <code>tests/test_pipelines_MY_PIPELINE.py</code>，其中包含其他测试的示例。",$s,O,ml="<code>run_pipeline_test</code> 函数将非常通用，并在每种可能的架构上运行小型随机模型，如 <code>model_mapping</code> 和 <code>tf_model_mapping</code> 所定义。",vs,ss,ul=`这对于测试未来的兼容性非常重要，这意味着如果有人为 <code>XXXForQuestionAnswering</code> 添加了一个新模型，
流水线测试将尝试在其上运行。由于模型是随机的，所以不可能检查实际值，这就是为什么有一个帮助函数 <code>ANY</code>，它只是尝试匹配流水线的输出类型。`,xs,ls,Ul="你还 <strong>需要</strong> 实现 2（最好是 4）个测试。",Ys,es,fl=`<li><code>test_small_model_pt</code>：为这个流水线定义一个小型模型（结果是否合理并不重要），并测试流水线的输出。
结果应该与 <code>test_small_model_tf</code> 的结果相同。</li> <li><code>test_small_model_tf</code>：为这个流水线定义一个小型模型（结果是否合理并不重要），并测试流水线的输出。
结果应该与 <code>test_small_model_pt</code> 的结果相同。</li> <li><code>test_large_model_pt</code>（可选）：在一个真实的流水线上测试流水线，结果应该是有意义的。
这些测试速度较慢，应该被如此标记。这里的目标是展示流水线，并确保在未来的发布中没有漂移。</li> <li><code>test_large_model_tf</code>（可选）：在一个真实的流水线上测试流水线，结果应该是有意义的。
这些测试速度较慢，应该被如此标记。这里的目标是展示流水线，并确保在未来的发布中没有漂移。</li>`,Qs,as,Fs;return w=new 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F9pbnB1dHMpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIzJTIwbW9kZWxfaW5wdXRzJTIwJTNEJTNEJTIwJTdCJTIybW9kZWxfaW5wdXQlMjIlM0ElMjBtb2RlbF9pbnB1dCU3RCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dHMlMjAlM0QlMjBzZWxmLm1vZGVsKCoqbW9kZWxfaW5wdXRzKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMyUyME1heWJlJTIwJTdCJTIybG9naXRzJTIyJTNBJTIwVGVuc29yKC4uLiklN0QlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm4lMjBvdXRwdXRzJTBBJTBBJTIwJTIwJTIwJTIwZGVmJTIwcG9zdHByb2Nlc3Moc2VsZiUyQyUyMG1vZGVsX291dHB1dHMpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYmVzdF9jbGFzcyUyMCUzRCUyMG1vZGVsX291dHB1dHMlNUIlMjJsb2dpdHMlMjIlNUQuc29mdG1heCgtMSklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm4lMjBiZXN0X2NsYXNz",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`,wrap:!1}}),G=new d({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>}]`,wrap:!1}}),V=new d({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`,wrap:!1}}),W=new Ss({props:{title:"将其添加到支持的任务列表中",local:"将其添加到支持的任务列表中",headingTag:"h2"}}),R=new d({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,
)`,wrap:!1}}),g=new d({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>
)`,wrap:!1}}),q=new Ss({props:{title:"在 Hub 上分享你的流水线",local:"在-hub-上分享你的流水线",headingTag:"h2"}}),z=new d({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFBpcGVsaW5lJTBBJTBBJTBBZGVmJTIwc29mdG1heChvdXRwdXRzKSUzQSUwQSUyMCUyMCUyMCUyMG1heGVzJTIwJTNEJTIwbnAubWF4KG91dHB1dHMlMkMlMjBheGlzJTNELTElMkMlMjBrZWVwZGltcyUzRFRydWUpJTBBJTIwJTIwJTIwJTIwc2hpZnRlZF9leHAlMjAlM0QlMjBucC5leHAob3V0cHV0cyUyMC0lMjBtYXhlcyklMEElMjAlMjAlMjAlMjByZXR1cm4lMjBzaGlmdGVkX2V4cCUyMCUyRiUyMHNoaWZ0ZWRfZXhwLnN1bShheGlzJTNELTElMkMlMjBrZWVwZGltcyUzRFRydWUpJTBBJTBBJTBBY2xhc3MlMjBQYWlyQ2xhc3NpZmljYXRpb25QaXBlbGluZShQaXBlbGluZSklM0ElMEElMjAlMjAlMjAlMjBkZWYlMjBfc2FuaXRpemVfcGFyYW1ldGVycyhzZWxmJTJDJTIwKiprd2FyZ3MpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJlcHJvY2Vzc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29mdG1heChsb2dpdHMpJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYmVzdF9jbGFzcyUyMCUzRCUyMG5wLmFyZ21heChwcm9iYWJpbGl0aWVzKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxhYmVsJTIwJTNEJTIwc2VsZi5tb2RlbC5jb25maWcuaWQybGFiZWwlNUJiZXN0X2NsYXNzJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2NvcmUlMjAlM0QlMjBwcm9iYWJpbGl0aWVzJTVCYmVzdF9jbGFzcyU1RC5pdGVtKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb2dpdHMlMjAlM0QlMjBsb2dpdHMudG9saXN0KCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm4lMjAlN0IlMjJsYWJlbCUyMiUzQSUyMGxhYmVsJTJDJTIwJTIyc2NvcmUlMjIlM0ElMjBzY29yZSUyQyUyMCUyMmxvZ2l0cyUyMiUzQSUyMGxvZ2l0cyU3RA==",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}`,wrap:!1}}),v=new d({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,
)`,wrap:!1}}),Y=new d({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>)`,wrap:!1}}),F=new d({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()`,wrap:!1}}),L=new d({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>)`,wrap:!1}}),P=new Ss({props:{title:"将流水线添加到 🤗 Transformers",local:"将流水线添加到--transformers",headingTag:"h2"}}),{c(){j=n("meta"),ps=a(),ts=n("p"),ns=a(),i(w.$$.fragment),Ms=a(),m=n("p"),m.innerHTML=Ps,cs=a(),u=n("p"),u.innerHTML=Ks,is=a(),U=n("p"),U.innerHTML=Ds,os=a(),f=n("p"),f.innerHTML=Os,rs=a(),i(I.$$.fragment),ys=a(),h=n("p"),h.textContent=sl,Js=a(),b=n("p"),b.innerHTML=ll,Ts=a(),B=n("p"),B.innerHTML=el,ds=a(),C=n("p"),C.innerHTML=tl,js=a(),_=n("p"),_.innerHTML=al,ws=a(),Z=n("p"),Z.innerHTML=pl,ms=a(),k=n("p"),k.innerHTML=nl,us=a(),i(G.$$.fragment),Us=a(),H=n("p"),H.innerHTML=Ml,fs=a(),i(V.$$.fragment),Is=a(),X=n("p"),X.textContent=cl,hs=a(),i(W.$$.fragment),bs=a(),A=n("p"),A.innerHTML=il,Bs=a(),i(R.$$.fragment),Cs=a(),N=n("p"),N.innerHTML=ol,_s=a(),i(g.$$.fragment),Zs=a(),i(q.$$.fragment),ks=a(),E=n("p"),E.innerHTML=rl,Gs=a(),i(z.$$.fragment),Hs=a(),$=n("p"),$.innerHTML=yl,Vs=a(),i(v.$$.fragment),Xs=a(),x=n("p"),x.innerHTML=Jl,Ws=a(),i(Y.$$.fragment),As=a(),Q=n("p"),Q.innerHTML=Tl,Rs=a(),i(F.$$.fragment),Ns=a(),S=n("p"),S.innerHTML=dl,gs=a(),i(L.$$.fragment),qs=a(),i(P.$$.fragment),Es=a(),K=n("p"),K.innerHTML=jl,zs=a(),D=n("p"),D.innerHTML=wl,$s=a(),O=n("p"),O.innerHTML=ml,vs=a(),ss=n("p"),ss.innerHTML=ul,xs=a(),ls=n("p"),ls.innerHTML=Ul,Ys=a(),es=n("ul"),es.innerHTML=fl,Qs=a(),as=n("p"),this.h()},l(s){const l=kl("svelte-u9bgzb",document.head);j=M(l,"META",{name:!0,content:!0}),l.forEach(e),ps=p(s),ts=M(s,"P",{}),Il(ts).forEach(e),ns=p(s),o(w.$$.fragment,s),Ms=p(s),m=M(s,"P",{"data-svelte-h":!0}),c(m)!=="svelte-ia2fek"&&(m.innerHTML=Ps),cs=p(s),u=M(s,"P",{"data-svelte-h":!0}),c(u)!=="svelte-rqfhkm"&&(u.innerHTML=Ks),is=p(s),U=M(s,"P",{"data-svelte-h":!0}),c(U)!=="svelte-11li02j"&&(U.innerHTML=Ds),os=p(s),f=M(s,"P",{"data-svelte-h":!0}),c(f)!=="svelte-1uqe9lz"&&(f.innerHTML=Os),rs=p(s),o(I.$$.fragment,s),ys=p(s),h=M(s,"P",{"data-svelte-h":!0}),c(h)!=="svelte-1k9lsn2"&&(h.textContent=sl),Js=p(s),b=M(s,"P",{"data-svelte-h":!0}),c(b)!=="svelte-gzvxzk"&&(b.innerHTML=ll),Ts=p(s),B=M(s,"P",{"data-svelte-h":!0}),c(B)!=="svelte-1cdihds"&&(B.innerHTML=el),ds=p(s),C=M(s,"P",{"data-svelte-h":!0}),c(C)!=="svelte-1x5gtvf"&&(C.innerHTML=tl),js=p(s),_=M(s,"P",{"data-svelte-h":!0}),c(_)!=="svelte-d4t2rm"&&(_.innerHTML=al),ws=p(s),Z=M(s,"P",{"data-svelte-h":!0}),c(Z)!=="svelte-1i0ixpi"&&(Z.innerHTML=pl),ms=p(s),k=M(s,"P",{"data-svelte-h":!0}),c(k)!=="svelte-oow5v7"&&(k.innerHTML=nl),us=p(s),o(G.$$.fragment,s),Us=p(s),H=M(s,"P",{"data-svelte-h":!0}),c(H)!=="svelte-15x8cpa"&&(H.innerHTML=Ml),fs=p(s),o(V.$$.fragment,s),Is=p(s),X=M(s,"P",{"data-svelte-h":!0}),c(X)!=="svelte-1axfvzi"&&(X.textContent=cl),hs=p(s),o(W.$$.fragment,s),bs=p(s),A=M(s,"P",{"data-svelte-h":!0}),c(A)!=="svelte-pd1e4e"&&(A.innerHTML=il),Bs=p(s),o(R.$$.fragment,s),Cs=p(s),N=M(s,"P",{"data-svelte-h":!0}),c(N)!=="svelte-1pcnt1i"&&(N.innerHTML=ol),_s=p(s),o(g.$$.fragment,s),Zs=p(s),o(q.$$.fragment,s),ks=p(s),E=M(s,"P",{"data-svelte-h":!0}),c(E)!=="svelte-12so9yg"&&(E.innerHTML=rl),Gs=p(s),o(z.$$.fragment,s),Hs=p(s),$=M(s,"P",{"data-svelte-h":!0}),c($)!=="svelte-jvth30"&&($.innerHTML=yl),Vs=p(s),o(v.$$.fragment,s),Xs=p(s),x=M(s,"P",{"data-svelte-h":!0}),c(x)!=="svelte-1hij24v"&&(x.innerHTML=Jl),Ws=p(s),o(Y.$$.fragment,s),As=p(s),Q=M(s,"P",{"data-svelte-h":!0}),c(Q)!=="svelte-5v42jo"&&(Q.innerHTML=Tl),Rs=p(s),o(F.$$.fragment,s),Ns=p(s),S=M(s,"P",{"data-svelte-h":!0}),c(S)!=="svelte-792yxj"&&(S.innerHTML=dl),gs=p(s),o(L.$$.fragment,s),qs=p(s),o(P.$$.fragment,s),Es=p(s),K=M(s,"P",{"data-svelte-h":!0}),c(K)!=="svelte-1kt4zz1"&&(K.innerHTML=jl),zs=p(s),D=M(s,"P",{"data-svelte-h":!0}),c(D)!=="svelte-g2mdjg"&&(D.innerHTML=wl),$s=p(s),O=M(s,"P",{"data-svelte-h":!0}),c(O)!=="svelte-1bz25ei"&&(O.innerHTML=ml),vs=p(s),ss=M(s,"P",{"data-svelte-h":!0}),c(ss)!=="svelte-a4o9sd"&&(ss.innerHTML=ul),xs=p(s),ls=M(s,"P",{"data-svelte-h":!0}),c(ls)!=="svelte-1f45ohx"&&(ls.innerHTML=Ul),Ys=p(s),es=M(s,"UL",{"data-svelte-h":!0}),c(es)!=="svelte-1na82ye"&&(es.innerHTML=fl),Qs=p(s),as=M(s,"P",{}),Il(as).forEach(e),this.h()},h(){hl(j,"name","hf:doc:metadata"),hl(j,"content",Vl)},m(s,l){Gl(document.head,j),t(s,ps,l),t(s,ts,l),t(s,ns,l),r(w,s,l),t(s,Ms,l),t(s,m,l),t(s,cs,l),t(s,u,l),t(s,is,l),t(s,U,l),t(s,os,l),t(s,f,l),t(s,rs,l),r(I,s,l),t(s,ys,l),t(s,h,l),t(s,Js,l),t(s,b,l),t(s,Ts,l),t(s,B,l),t(s,ds,l),t(s,C,l),t(s,js,l),t(s,_,l),t(s,ws,l),t(s,Z,l),t(s,ms,l),t(s,k,l),t(s,us,l),r(G,s,l),t(s,Us,l),t(s,H,l),t(s,fs,l),r(V,s,l),t(s,Is,l),t(s,X,l),t(s,hs,l),r(W,s,l),t(s,bs,l),t(s,A,l),t(s,Bs,l),r(R,s,l),t(s,Cs,l),t(s,N,l),t(s,_s,l),r(g,s,l),t(s,Zs,l),r(q,s,l),t(s,ks,l),t(s,E,l),t(s,Gs,l),r(z,s,l),t(s,Hs,l),t(s,$,l),t(s,Vs,l),r(v,s,l),t(s,Xs,l),t(s,x,l),t(s,Ws,l),r(Y,s,l),t(s,As,l),t(s,Q,l),t(s,Rs,l),r(F,s,l),t(s,Ns,l),t(s,S,l),t(s,gs,l),r(L,s,l),t(s,qs,l),r(P,s,l),t(s,Es,l),t(s,K,l),t(s,zs,l),t(s,D,l),t(s,$s,l),t(s,O,l),t(s,vs,l),t(s,ss,l),t(s,xs,l),t(s,ls,l),t(s,Ys,l),t(s,es,l),t(s,Qs,l),t(s,as,l),Fs=!0},p:Bl,i(s){Fs||(y(w.$$.fragment,s),y(I.$$.fragment,s),y(G.$$.fragment,s),y(V.$$.fragment,s),y(W.$$.fragment,s),y(R.$$.fragment,s),y(g.$$.fragment,s),y(q.$$.fragment,s),y(z.$$.fragment,s),y(v.$$.fragment,s),y(Y.$$.fragment,s),y(F.$$.fragment,s),y(L.$$.fragment,s),y(P.$$.fragment,s),Fs=!0)},o(s){J(w.$$.fragment,s),J(I.$$.fragment,s),J(G.$$.fragment,s),J(V.$$.fragment,s),J(W.$$.fragment,s),J(R.$$.fragment,s),J(g.$$.fragment,s),J(q.$$.fragment,s),J(z.$$.fragment,s),J(v.$$.fragment,s),J(Y.$$.fragment,s),J(F.$$.fragment,s),J(L.$$.fragment,s),J(P.$$.fragment,s),Fs=!1},d(s){s&&(e(ps),e(ts),e(ns),e(Ms),e(m),e(cs),e(u),e(is),e(U),e(os),e(f),e(rs),e(ys),e(h),e(Js),e(b),e(Ts),e(B),e(ds),e(C),e(js),e(_),e(ws),e(Z),e(ms),e(k),e(us),e(Us),e(H),e(fs),e(Is),e(X),e(hs),e(bs),e(A),e(Bs),e(Cs),e(N),e(_s),e(Zs),e(ks),e(E),e(Gs),e(Hs),e($),e(Vs),e(Xs),e(x),e(Ws),e(As),e(Q),e(Rs),e(Ns),e(S),e(gs),e(qs),e(Es),e(K),e(zs),e(D),e($s),e(O),e(vs),e(ss),e(xs),e(ls),e(Ys),e(es),e(Qs),e(as)),e(j),T(w,s),T(I,s),T(G,s),T(V,s),T(W,s),T(R,s),T(g,s),T(q,s),T(z,s),T(v,s),T(Y,s),T(F,s),T(L,s),T(P,s)}}}const Vl='{"title":"如何创建自定义流水线？","local":"如何创建自定义流水线","sections":[{"title":"将其添加到支持的任务列表中","local":"将其添加到支持的任务列表中","sections":[],"depth":2},{"title":"在 Hub 上分享你的流水线","local":"在-hub-上分享你的流水线","sections":[],"depth":2},{"title":"将流水线添加到 🤗 Transformers","local":"将流水线添加到--transformers","sections":[],"depth":2}],"depth":1}';function Xl(Ls){return Cl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class gl extends _l{constructor(j){super(),Zl(this,j,Xl,Hl,bl,{})}}export{gl as component};
