import{s as it,f as We,n as rt,o as mt}from"../chunks/scheduler.37c15a92.js";import{S as ct,i as ut,g as p,s as a,r as d,A as dt,h as o,f as l,c as n,j as O,u as y,x as r,k as i,y as yt,a as s,v as f,d as w,t as h,w as M}from"../chunks/index.2bf4358c.js";import{C as A}from"../chunks/CodeBlock.4f5fc1ad.js";import{D as ft}from"../chunks/DocNotebookDropdown.d9dfeb24.js";import{H as ot,E as wt}from"../chunks/EditOnGithub.8916dc7c.js";function ht(Ve){let b,ee,K,te,j,le,g,se,J,Re="让我们从安装 Gradio 开始吧！ 由于它是一个 Python 包，只需运行：",ae,x,Pe="<code>$ pip install gradio</code>",ne,T,Xe=`您可以在任何地方运行 Gradio，无论是从您最喜欢的 Python IDE、Jupyter notebook 还是 Google Colab 🤯！
所以无论你在哪里运行 Python，都可以安装 Gradio！`,pe,$,qe="让我们从一个简单的“Hello World”示例开始，熟悉 Gradio 语法：",oe,G,ie,k,Se="让我们看一下上面的代码：",re,v,Qe="-首先，我们定义一个名为 <code>greet()</code> 的函数。 在这种情况下，它是一个在您的名字前添加“Hello”的简单函数，但它通常可以是 <em>any</em> Python 函数。 例如，在机器学习应用程序中，此函数将<em>调用模型以对输入进行预测</em>并返回输出。",me,U,Ee="<li>然后，我们创建一个带有三个参数的 Gradio <code>Interface</code>，<code>fn</code>、<code>inputs</code> 和 <code>outputs</code>。 这些参数定义了预测函数，以及我们想要的输入和输出组件的<em>type</em>。 在我们的例子中，两个组件都是简单的文本框。</li> <li>然后我们在我们创建的 <code>Interface</code> 上调用 <code>launch()</code> 方法。</li>",ce,B,Ye='如果你运行这段代码，下面的界面会自动出现在 Jupyter/Colab notebook 中，或者在浏览器中弹出 <strong><a href="http://localhost:7860/" rel="nofollow">http://localhost:7860</a></strong> 如果运行 从一个脚本。',ue,m,Ne,de,C,Le="立即尝试使用您自己的姓名或其他输入来使用此 GUI！",ye,I,ze="您会注意到，在这个 GUI 中，Gradio 自动推断输入参数的名称 (<code>name</code>)并将其应用为文本框顶部的标签。 如果你想改变它怎么办？或者，如果您想以其他方式自定义文本框？ 在这种情况下，您可以实例化一个表示输入组件的类对象。",fe,Z,Fe="看看下面的例子：",we,H,he,c,Ae,Me,_,Ke="在这里，我们创建了一个带有标签、占位符和一组行数的输入文本框。您可以对输出文本框执行相同的操作，但我们现在将其保留。",be,W,De="我们已经看到，只需几行代码，Gradio 就可以让您围绕任何具有任何类型输入或输出的函数创建一个简单的界面。 在本节中，我们从一个简单的文本框开始，但在接下来的部分中，我们将介绍其他类型的输入和输出。 现在让我们看看在 Gradio 应用程序中包含一些 NLP。",je,V,ge,R,Oe="现在让我们构建一个简单的界面，让您可以演示像 GPT-2 这样的<strong>文本生成</strong>模型。",Je,P,et=`我们将使用 🤗 Transformers 中的 <code>pipeline()</code> 函数加载我们的模型。
如果您需要快速复习，您可以返回到 <a href="/course/chapter1/3#text-generation">第 1 章中的那个部分</a>。`,xe,X,tt="首先，我们定义一个接受文本提示并返回文本完成的预测函数：",Te,q,$e,S,lt="此函数完成您提供的提示，您可以使用自己的输入提示运行它以查看它是如何工作的。 这是一个示例（您可能会得到不同的完成）：",Ge,Q,ke,E,ve,Y,st="现在我们有了一个生成预测的函数，我们可以像之前一样创建和启动一个“接口”：",Ue,N,Be,L,at="就是这样！ 您现在可以使用此接口使用 GPT-2 模型生成文本，如下所示 🤯.",Ce,u,nt,Ie,z,pt="继续阅读以了解如何使用 Gradio 构建其他类型的演示！",Ze,F,He,D,_e;return j=new ot({props:{title:"构建你的第一个演示",local:"构建你的第一个演示",headingTag:"h1"}}),g=new ft({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/chapter9/section2.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter9/section2.ipynb"}]}}),G=new A({props:{code:"aW1wb3J0JTIwZ3JhZGlvJTIwYXMlMjBnciUwQSUwQSUwQWRlZiUyMGdyZWV0KG5hbWUpJTNBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTIySGVsbG8lMjAlMjIlMjAlMkIlMjBuYW1lJTBBJTBBJTBBZGVtbyUyMCUzRCUyMGdyLkludGVyZmFjZShmbiUzRGdyZWV0JTJDJTIwaW5wdXRzJTNEJTIydGV4dCUyMiUyQyUyMG91dHB1dHMlM0QlMjJ0ZXh0JTIyKSUwQSUwQWRlbW8ubGF1bmNoKCk=",highlighted:`<span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr


<span class="hljs-keyword">def</span> <span class="hljs-title function_">greet</span>(<span class="hljs-params">name</span>):
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello &quot;</span> + name


demo = gr.Interface(fn=greet, inputs=<span class="hljs-string">&quot;text&quot;</span>, outputs=<span class="hljs-string">&quot;text&quot;</span>)

demo.launch()`,wrap:!1}}),H=new A({props:{code:"aW1wb3J0JTIwZ3JhZGlvJTIwYXMlMjBnciUwQSUwQSUwQWRlZiUyMGdyZWV0KG5hbWUpJTNBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTIySGVsbG8lMjAlMjIlMjAlMkIlMjBuYW1lJTBBJTBBJTBBJTIzJTIwV2UlMjBpbnN0YW50aWF0ZSUyMHRoZSUyMFRleHRib3glMjBjbGFzcyUwQXRleHRib3glMjAlM0QlMjBnci5UZXh0Ym94KGxhYmVsJTNEJTIyVHlwZSUyMHlvdXIlMjBuYW1lJTIwaGVyZSUzQSUyMiUyQyUyMHBsYWNlaG9sZGVyJTNEJTIySm9obiUyMERvZSUyMiUyQyUyMGxpbmVzJTNEMiklMEElMEFnci5JbnRlcmZhY2UoZm4lM0RncmVldCUyQyUyMGlucHV0cyUzRHRleHRib3glMkMlMjBvdXRwdXRzJTNEJTIydGV4dCUyMikubGF1bmNoKCk=",highlighted:`<span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr


<span class="hljs-keyword">def</span> <span class="hljs-title function_">greet</span>(<span class="hljs-params">name</span>):
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello &quot;</span> + name


<span class="hljs-comment"># We instantiate the Textbox class</span>
textbox = gr.Textbox(label=<span class="hljs-string">&quot;Type your name here:&quot;</span>, placeholder=<span class="hljs-string">&quot;John Doe&quot;</span>, lines=<span class="hljs-number">2</span>)

gr.Interface(fn=greet, inputs=textbox, outputs=<span class="hljs-string">&quot;text&quot;</span>).launch()`,wrap:!1}}),V=new ot({props:{title:"🤖 包括模型预测",local:"🤖 包括模型预测",headingTag:"h2"}}),q=new A({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBbW9kZWwlMjAlM0QlMjBwaXBlbGluZSglMjJ0ZXh0LWdlbmVyYXRpb24lMjIpJTBBJTBBJTBBZGVmJTIwcHJlZGljdChwcm9tcHQpJTNBJTBBJTIwJTIwJTIwJTIwY29tcGxldGlvbiUyMCUzRCUyMG1vZGVsKHByb21wdCklNUIwJTVEJTVCJTIyZ2VuZXJhdGVkX3RleHQlMjIlNUQlMEElMjAlMjAlMjAlMjByZXR1cm4lMjBjb21wbGV0aW9u",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

model = pipeline(<span class="hljs-string">&quot;text-generation&quot;</span>)


<span class="hljs-keyword">def</span> <span class="hljs-title function_">predict</span>(<span class="hljs-params">prompt</span>):
    completion = model(prompt)[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;generated_text&quot;</span>]
    <span class="hljs-keyword">return</span> completion`,wrap:!1}}),Q=new A({props:{code:"cHJlZGljdCglMjJNeSUyMGZhdm9yaXRlJTIwcHJvZ3JhbW1pbmclMjBsYW5ndWFnZSUyMGlzJTIyKQ==",highlighted:'<span class="hljs-function"><span class="hljs-title">predict</span><span class="hljs-params">(<span class="hljs-string">&quot;My favorite programming language is&quot;</span>)</span></span>',wrap:!1}}),E=new A({props:{code:"JTNFJTNFJTIwTXklMjBmYXZvcml0ZSUyMHByb2dyYW1taW5nJTIwbGFuZ3VhZ2UlMjBpcyUyMEhhc2tlbGwuJTIwSSUyMHJlYWxseSUyMGVuam95ZWQlMjB0aGUlMjBIYXNrZWxsJTIwbGFuZ3VhZ2UlMkMlMjBidXQlMjBpdCUyMGRvZXNuJ3QlMjBoYXZlJTIwYWxsJTIwdGhlJTIwZmVhdHVyZXMlMjB0aGF0JTIwY2FuJTIwYmUlMjBhcHBsaWVkJTIwdG8lMjBhbnklMjBvdGhlciUyMGxhbmd1YWdlLiUyMEZvciUyMGV4YW1wbGUlMkMlMjBhbGwlMjBpdCUyMGRvZXMlMjBpcyUyMGNvbXBpbGUlMjB0byUyMGElMjBieXRlJTIwYXJyYXku",highlighted:'&gt;&gt; My favorite programming language <span class="hljs-keyword">is</span> Haskell. I really enjoyed <span class="hljs-keyword">the</span> Haskell language, <span class="hljs-keyword">but</span> <span class="hljs-keyword">it</span> doesn&#x27;t have all <span class="hljs-keyword">the</span> features <span class="hljs-keyword">that</span> can be applied <span class="hljs-keyword">to</span> any other language. For example, all <span class="hljs-keyword">it</span> <span class="hljs-keyword">does</span> <span class="hljs-keyword">is</span> compile <span class="hljs-keyword">to</span> a byte array.',wrap:!1}}),N=new A({props:{code:"aW1wb3J0JTIwZ3JhZGlvJTIwYXMlMjBnciUwQSUwQWdyLkludGVyZmFjZShmbiUzRHByZWRpY3QlMkMlMjBpbnB1dHMlM0QlMjJ0ZXh0JTIyJTJDJTIwb3V0cHV0cyUzRCUyMnRleHQlMjIpLmxhdW5jaCgp",highlighted:`<span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr

gr.Interface(fn=predict, inputs=<span class="hljs-string">&quot;text&quot;</span>, outputs=<span class="hljs-string">&quot;text&quot;</span>).launch()`,wrap:!1}}),F=new wt({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter9/2.mdx"}}),{c(){b=p("meta"),ee=a(),K=p("p"),te=a(),d(j.$$.fragment),le=a(),d(g.$$.fragment),se=a(),J=p("p"),J.textContent=Re,ae=a(),x=p("p"),x.innerHTML=Pe,ne=a(),T=p("p"),T.textContent=Xe,pe=a(),$=p("p"),$.textContent=qe,oe=a(),d(G.$$.fragment),ie=a(),k=p("p"),k.textContent=Se,re=a(),v=p("p"),v.innerHTML=Qe,me=a(),U=p("ul"),U.innerHTML=Ee,ce=a(),B=p("p"),B.innerHTML=Ye,ue=a(),m=p("iframe"),de=a(),C=p("p"),C.textContent=Le,ye=a(),I=p("p"),I.innerHTML=ze,fe=a(),Z=p("p"),Z.textContent=Fe,we=a(),d(H.$$.fragment),he=a(),c=p("iframe"),Me=a(),_=p("p"),_.textContent=Ke,be=a(),W=p("p"),W.textContent=De,je=a(),d(V.$$.fragment),ge=a(),R=p("p"),R.innerHTML=Oe,Je=a(),P=p("p"),P.innerHTML=et,xe=a(),X=p("p"),X.textContent=tt,Te=a(),d(q.$$.fragment),$e=a(),S=p("p"),S.textContent=lt,Ge=a(),d(Q.$$.fragment),ke=a(),d(E.$$.fragment),ve=a(),Y=p("p"),Y.textContent=st,Ue=a(),d(N.$$.fragment),Be=a(),L=p("p"),L.textContent=at,Ce=a(),u=p("iframe"),Ie=a(),z=p("p"),z.textContent=pt,Ze=a(),d(F.$$.fragment),He=a(),D=p("p"),this.h()},l(e){const t=dt("svelte-u9bgzb",document.head);b=o(t,"META",{name:!0,content:!0}),t.forEach(l),ee=n(e),K=o(e,"P",{}),O(K).forEach(l),te=n(e),y(j.$$.fragment,e),le=n(e),y(g.$$.fragment,e),se=n(e),J=o(e,"P",{"data-svelte-h":!0}),r(J)!=="svelte-13kpg45"&&(J.textContent=Re),ae=n(e),x=o(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-sv8g3f"&&(x.innerHTML=Pe),ne=n(e),T=o(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-12hdhgp"&&(T.textContent=Xe),pe=n(e),$=o(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-1rxpupk"&&($.textContent=qe),oe=n(e),y(G.$$.fragment,e),ie=n(e),k=o(e,"P",{"data-svelte-h":!0}),r(k)!=="svelte-1h9q0nw"&&(k.textContent=Se),re=n(e),v=o(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-1hloex4"&&(v.innerHTML=Qe),me=n(e),U=o(e,"UL",{"data-svelte-h":!0}),r(U)!=="svelte-1w1xsak"&&(U.innerHTML=Ee),ce=n(e),B=o(e,"P",{"data-svelte-h":!0}),r(B)!=="svelte-14i60of"&&(B.innerHTML=Ye),ue=n(e),m=o(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),O(m).forEach(l),de=n(e),C=o(e,"P",{"data-svelte-h":!0}),r(C)!=="svelte-1a96120"&&(C.textContent=Le),ye=n(e),I=o(e,"P",{"data-svelte-h":!0}),r(I)!=="svelte-1jsb1w6"&&(I.innerHTML=ze),fe=n(e),Z=o(e,"P",{"data-svelte-h":!0}),r(Z)!=="svelte-tqqosc"&&(Z.textContent=Fe),we=n(e),y(H.$$.fragment,e),he=n(e),c=o(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),O(c).forEach(l),Me=n(e),_=o(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-33e9jk"&&(_.textContent=Ke),be=n(e),W=o(e,"P",{"data-svelte-h":!0}),r(W)!=="svelte-kxv5bf"&&(W.textContent=De),je=n(e),y(V.$$.fragment,e),ge=n(e),R=o(e,"P",{"data-svelte-h":!0}),r(R)!=="svelte-1gvdm82"&&(R.innerHTML=Oe),Je=n(e),P=o(e,"P",{"data-svelte-h":!0}),r(P)!=="svelte-pcdk6z"&&(P.innerHTML=et),xe=n(e),X=o(e,"P",{"data-svelte-h":!0}),r(X)!=="svelte-1hw79n6"&&(X.textContent=tt),Te=n(e),y(q.$$.fragment,e),$e=n(e),S=o(e,"P",{"data-svelte-h":!0}),r(S)!=="svelte-1huilbm"&&(S.textContent=lt),Ge=n(e),y(Q.$$.fragment,e),ke=n(e),y(E.$$.fragment,e),ve=n(e),Y=o(e,"P",{"data-svelte-h":!0}),r(Y)!=="svelte-crn2qx"&&(Y.textContent=st),Ue=n(e),y(N.$$.fragment,e),Be=n(e),L=o(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-1cx3751"&&(L.textContent=at),Ce=n(e),u=o(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),O(u).forEach(l),Ie=n(e),z=o(e,"P",{"data-svelte-h":!0}),r(z)!=="svelte-1xbhefn"&&(z.textContent=pt),Ze=n(e),y(F.$$.fragment,e),He=n(e),D=o(e,"P",{}),O(D).forEach(l),this.h()},h(){i(b,"name","hf:doc:metadata"),i(b,"content",Mt),We(m.src,Ne="https://course-demos-hello-world.hf.space")||i(m,"src",Ne),i(m,"frameborder","0"),i(m,"height","250"),i(m,"title","Gradio app"),i(m,"class","container p-0 flex-grow space-iframe"),i(m,"allow","accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking"),i(m,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),We(c.src,Ae="https://course-demos-hello-world-custom.hf.space")||i(c,"src",Ae),i(c,"frameborder","0"),i(c,"height","300"),i(c,"title","Gradio app"),i(c,"class","container p-0 flex-grow space-iframe"),i(c,"allow","accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking"),i(c,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),We(u.src,nt="https://course-demos-gpt-2.hf.space")||i(u,"src",nt),i(u,"frameborder","0"),i(u,"height","300"),i(u,"title","Gradio app"),i(u,"class","container p-0 flex-grow space-iframe"),i(u,"allow","accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking"),i(u,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads")},m(e,t){yt(document.head,b),s(e,ee,t),s(e,K,t),s(e,te,t),f(j,e,t),s(e,le,t),f(g,e,t),s(e,se,t),s(e,J,t),s(e,ae,t),s(e,x,t),s(e,ne,t),s(e,T,t),s(e,pe,t),s(e,$,t),s(e,oe,t),f(G,e,t),s(e,ie,t),s(e,k,t),s(e,re,t),s(e,v,t),s(e,me,t),s(e,U,t),s(e,ce,t),s(e,B,t),s(e,ue,t),s(e,m,t),s(e,de,t),s(e,C,t),s(e,ye,t),s(e,I,t),s(e,fe,t),s(e,Z,t),s(e,we,t),f(H,e,t),s(e,he,t),s(e,c,t),s(e,Me,t),s(e,_,t),s(e,be,t),s(e,W,t),s(e,je,t),f(V,e,t),s(e,ge,t),s(e,R,t),s(e,Je,t),s(e,P,t),s(e,xe,t),s(e,X,t),s(e,Te,t),f(q,e,t),s(e,$e,t),s(e,S,t),s(e,Ge,t),f(Q,e,t),s(e,ke,t),f(E,e,t),s(e,ve,t),s(e,Y,t),s(e,Ue,t),f(N,e,t),s(e,Be,t),s(e,L,t),s(e,Ce,t),s(e,u,t),s(e,Ie,t),s(e,z,t),s(e,Ze,t),f(F,e,t),s(e,He,t),s(e,D,t),_e=!0},p:rt,i(e){_e||(w(j.$$.fragment,e),w(g.$$.fragment,e),w(G.$$.fragment,e),w(H.$$.fragment,e),w(V.$$.fragment,e),w(q.$$.fragment,e),w(Q.$$.fragment,e),w(E.$$.fragment,e),w(N.$$.fragment,e),w(F.$$.fragment,e),_e=!0)},o(e){h(j.$$.fragment,e),h(g.$$.fragment,e),h(G.$$.fragment,e),h(H.$$.fragment,e),h(V.$$.fragment,e),h(q.$$.fragment,e),h(Q.$$.fragment,e),h(E.$$.fragment,e),h(N.$$.fragment,e),h(F.$$.fragment,e),_e=!1},d(e){e&&(l(ee),l(K),l(te),l(le),l(se),l(J),l(ae),l(x),l(ne),l(T),l(pe),l($),l(oe),l(ie),l(k),l(re),l(v),l(me),l(U),l(ce),l(B),l(ue),l(m),l(de),l(C),l(ye),l(I),l(fe),l(Z),l(we),l(he),l(c),l(Me),l(_),l(be),l(W),l(je),l(ge),l(R),l(Je),l(P),l(xe),l(X),l(Te),l($e),l(S),l(Ge),l(ke),l(ve),l(Y),l(Ue),l(Be),l(L),l(Ce),l(u),l(Ie),l(z),l(Ze),l(He),l(D)),l(b),M(j,e),M(g,e),M(G,e),M(H,e),M(V,e),M(q,e),M(Q,e),M(E,e),M(N,e),M(F,e)}}}const Mt='{"title":"构建你的第一个演示","local":"构建你的第一个演示","sections":[{"title":"🤖 包括模型预测","local":"🤖 包括模型预测","sections":[],"depth":2}],"depth":1}';function bt(Ve){return mt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class $t extends ct{constructor(b){super(),ut(this,b,bt,ht,it,{})}}export{$t as component};
