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.4e987730.js";import{D as ft}from"../chunks/DocNotebookDropdown.efc1fb7c.js";import{H as ot,E as wt}from"../chunks/getInferenceSnippets.1837c472.js";function ht(Ve){let b,ee,K,te,j,le,J,se,g,Re="讓我們從安裝 Gradio 開始吧！ 由於它是一個 Python 包，只需運行：",ae,T,qe="<code>$ pip install gradio</code>",ne,x,Pe=`您可以在任何地方運行 Gradio，無論是從您最喜歡的 Python IDE、Jupyter notebook 還是 Google Colab 🤯！
所以無論你在哪裡運行 Python，都可以安裝 Gradio！`,pe,$,Xe="讓我們從一個簡單的“Hello World”示例開始，熟悉 Gradio 語法：",oe,v,ie,G,Se="讓我們看一下上面的代碼：",re,k,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,ze,de,C,Ne="立即嘗試使用您自己的姓名或其他輸入來使用此 GUI！",ye,I,Le="您會注意到，在這個 GUI 中，Gradio 自動推斷輸入參數的名稱 (<code>name</code>)並將其應用為文本框頂部的標籤。 如果你想改變它怎麼辦？或者，如果您想以其他方式自定義文本框？ 在這種情況下，您可以實例化一個表示輸入組件的類對象。",fe,Z,Fe="看看下面的例子：",we,H,he,c,Ae,Me,_,Ke="在這裡，我們創建了一個帶有標籤、佔位符和一組行數的輸入文本框。您可以對輸出文本框執行相同的操作，但我們現在將其保留。",be,W,De="我們已經看到，只需幾行代碼，Gradio 就可以讓您圍繞任何具有任何類型輸入或輸出的函數創建一個簡單的界面。 在本節中，我們從一個簡單的文本框開始，但在接下來的部分中，我們將介紹其他類型的輸入和輸出。 現在讓我們看看在 Gradio 應用程序中包含一些 NLP。",je,V,Je,R,Oe="現在讓我們構建一個簡單的界面，讓您可以演示像 GPT-2 這樣的<strong>文本生成</strong>模型。",ge,q,et=`我們將使用 🤗 Transformers 中的 <code>pipeline()</code> 函數加載我們的模型。
如果您需要快速複習，您可以返回到 <a href="/course/chapter1/3#text-generation">第 1 章中的那個部分</a>。`,Te,P,tt="首先，我們定義一個接受文本提示並返回文本完成的預測函數：",xe,X,$e,S,lt="此函數完成您提供的提示，您可以使用自己的輸入提示運行它以查看它是如何工作的。 這是一個示例（您可能會得到不同的完成）：",ve,Q,Ge,E,ke,Y,st="現在我們有了一個生成預測的函數，我們可以像之前一樣創建和啟動一個“接口”：",Ue,z,Be,N,at="就是這樣！ 您現在可以使用此接口使用 GPT-2 模型生成文本，如下所示 🤯.",Ce,u,nt,Ie,L,pt="繼續閱讀以瞭解如何使用 Gradio 構建其他類型的演示！",Ze,F,He,D,_e;return j=new ot({props:{title:"構建你的第一個演示",local:"構建你的第一個演示",headingTag:"h1"}}),J=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"}]}}),v=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"}}),X=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}}),z=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-TW/chapter9/2.mdx"}}),{c(){b=p("meta"),ee=a(),K=p("p"),te=a(),d(j.$$.fragment),le=a(),d(J.$$.fragment),se=a(),g=p("p"),g.textContent=Re,ae=a(),T=p("p"),T.innerHTML=qe,ne=a(),x=p("p"),x.textContent=Pe,pe=a(),$=p("p"),$.textContent=Xe,oe=a(),d(v.$$.fragment),ie=a(),G=p("p"),G.textContent=Se,re=a(),k=p("p"),k.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=Ne,ye=a(),I=p("p"),I.innerHTML=Le,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),Je=a(),R=p("p"),R.innerHTML=Oe,ge=a(),q=p("p"),q.innerHTML=et,Te=a(),P=p("p"),P.textContent=tt,xe=a(),d(X.$$.fragment),$e=a(),S=p("p"),S.textContent=lt,ve=a(),d(Q.$$.fragment),Ge=a(),d(E.$$.fragment),ke=a(),Y=p("p"),Y.textContent=st,Ue=a(),d(z.$$.fragment),Be=a(),N=p("p"),N.textContent=at,Ce=a(),u=p("iframe"),Ie=a(),L=p("p"),L.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(J.$$.fragment,e),se=n(e),g=o(e,"P",{"data-svelte-h":!0}),r(g)!=="svelte-135lmmj"&&(g.textContent=Re),ae=n(e),T=o(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-sv8g3f"&&(T.innerHTML=qe),ne=n(e),x=o(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-dxphzn"&&(x.textContent=Pe),pe=n(e),$=o(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-iuv3ii"&&($.textContent=Xe),oe=n(e),y(v.$$.fragment,e),ie=n(e),G=o(e,"P",{"data-svelte-h":!0}),r(G)!=="svelte-st3qa6"&&(G.textContent=Se),re=n(e),k=o(e,"P",{"data-svelte-h":!0}),r(k)!=="svelte-wjb59t"&&(k.innerHTML=Qe),me=n(e),U=o(e,"UL",{"data-svelte-h":!0}),r(U)!=="svelte-hh14jc"&&(U.innerHTML=Ee),ce=n(e),B=o(e,"P",{"data-svelte-h":!0}),r(B)!=="svelte-1r2h8n4"&&(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-ec1f8p"&&(C.textContent=Ne),ye=n(e),I=o(e,"P",{"data-svelte-h":!0}),r(I)!=="svelte-t1aus2"&&(I.innerHTML=Le),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-so50zz"&&(_.textContent=Ke),be=n(e),W=o(e,"P",{"data-svelte-h":!0}),r(W)!=="svelte-1sjy9vc"&&(W.textContent=De),je=n(e),y(V.$$.fragment,e),Je=n(e),R=o(e,"P",{"data-svelte-h":!0}),r(R)!=="svelte-mmwmhr"&&(R.innerHTML=Oe),ge=n(e),q=o(e,"P",{"data-svelte-h":!0}),r(q)!=="svelte-1298r7"&&(q.innerHTML=et),Te=n(e),P=o(e,"P",{"data-svelte-h":!0}),r(P)!=="svelte-kkyquv"&&(P.textContent=tt),xe=n(e),y(X.$$.fragment,e),$e=n(e),S=o(e,"P",{"data-svelte-h":!0}),r(S)!=="svelte-mmi9bg"&&(S.textContent=lt),ve=n(e),y(Q.$$.fragment,e),Ge=n(e),y(E.$$.fragment,e),ke=n(e),Y=o(e,"P",{"data-svelte-h":!0}),r(Y)!=="svelte-1ch1qjj"&&(Y.textContent=st),Ue=n(e),y(z.$$.fragment,e),Be=n(e),N=o(e,"P",{"data-svelte-h":!0}),r(N)!=="svelte-ks0qyb"&&(N.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),L=o(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-bvvtyd"&&(L.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,ze="https://course-demos-hello-world.hf.space")||i(m,"src",ze),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(J,e,t),s(e,se,t),s(e,g,t),s(e,ae,t),s(e,T,t),s(e,ne,t),s(e,x,t),s(e,pe,t),s(e,$,t),s(e,oe,t),f(v,e,t),s(e,ie,t),s(e,G,t),s(e,re,t),s(e,k,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,Je,t),s(e,R,t),s(e,ge,t),s(e,q,t),s(e,Te,t),s(e,P,t),s(e,xe,t),f(X,e,t),s(e,$e,t),s(e,S,t),s(e,ve,t),f(Q,e,t),s(e,Ge,t),f(E,e,t),s(e,ke,t),s(e,Y,t),s(e,Ue,t),f(z,e,t),s(e,Be,t),s(e,N,t),s(e,Ce,t),s(e,u,t),s(e,Ie,t),s(e,L,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(J.$$.fragment,e),w(v.$$.fragment,e),w(H.$$.fragment,e),w(V.$$.fragment,e),w(X.$$.fragment,e),w(Q.$$.fragment,e),w(E.$$.fragment,e),w(z.$$.fragment,e),w(F.$$.fragment,e),_e=!0)},o(e){h(j.$$.fragment,e),h(J.$$.fragment,e),h(v.$$.fragment,e),h(H.$$.fragment,e),h(V.$$.fragment,e),h(X.$$.fragment,e),h(Q.$$.fragment,e),h(E.$$.fragment,e),h(z.$$.fragment,e),h(F.$$.fragment,e),_e=!1},d(e){e&&(l(ee),l(K),l(te),l(le),l(se),l(g),l(ae),l(T),l(ne),l(x),l(pe),l($),l(oe),l(ie),l(G),l(re),l(k),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(Je),l(R),l(ge),l(q),l(Te),l(P),l(xe),l($e),l(S),l(ve),l(Ge),l(ke),l(Y),l(Ue),l(Be),l(N),l(Ce),l(u),l(Ie),l(L),l(Ze),l(He),l(D)),l(b),M(j,e),M(J,e),M(v,e),M(H,e),M(V,e),M(X,e),M(Q,e),M(E,e),M(z,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};
