import{s as mt,a as Re,n as ct,o as ut}from"../chunks/scheduler.cc52f4b9.js";import{S as dt,i as yt,e as p,s as a,c as d,h as ft,a as o,d as l,b as n,f as ee,g as y,j as r,k as i,l as wt,m as s,n as f,t as w,o as h,p as M}from"../chunks/index.9480bb2b.js";import{C as ht,H as rt,E as Mt}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.52103c90.js";import{C as K}from"../chunks/CodeBlock.c5404643.js";import{D as bt}from"../chunks/DocNotebookDropdown.ea74f0e0.js";function jt(qe){let b,te,D,le,j,se,g,ae,J,ne,T,Pe="讓我們從安裝 Gradio 開始吧！ 由於它是一個 Python 包，只需運行：",pe,x,Xe="<code>$ pip install gradio</code>",oe,$,Se=`您可以在任何地方運行 Gradio，無論是從您最喜歡的 Python IDE、Jupyter notebook 還是 Google Colab 🤯！
所以無論你在哪裡運行 Python，都可以安裝 Gradio！`,ie,v,Qe="讓我們從一個簡單的“Hello World”示例開始，熟悉 Gradio 語法：",re,G,me,k,Ee="讓我們看一下上面的代碼：",ce,U,Ye="-首先，我們定義一個名為 <code>greet()</code> 的函數。 在這種情況下，它是一個在您的名字前添加“Hello”的簡單函數，但它通常可以是 <em>any</em> Python 函數。 例如，在機器學習應用程序中，此函數將<em>調用模型以對輸入進行預測</em>並返回輸出。",ue,B,ze="<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>",de,C,Le='如果你運行這段代碼，下面的界面會自動出現在 Jupyter/Colab notebook 中，或者在瀏覽器中彈出 <strong><a href="http://localhost:7860/" rel="nofollow">http://localhost:7860</a></strong> 如果運行 從一個腳本。',ye,m,Ne,fe,I,Fe="立即嘗試使用您自己的姓名或其他輸入來使用此 GUI！",we,Z,Ae="您會注意到，在這個 GUI 中，Gradio 自動推斷輸入參數的名稱 (<code>name</code>)並將其應用為文本框頂部的標籤。 如果你想改變它怎麼辦？或者，如果您想以其他方式自定義文本框？ 在這種情況下，您可以實例化一個表示輸入組件的類對象。",he,H,Ke="看看下面的例子：",Me,_,be,c,De,je,W,Oe="在這裡，我們創建了一個帶有標籤、佔位符和一組行數的輸入文本框。您可以對輸出文本框執行相同的操作，但我們現在將其保留。",ge,V,et="我們已經看到，只需幾行代碼，Gradio 就可以讓您圍繞任何具有任何類型輸入或輸出的函數創建一個簡單的界面。 在本節中，我們從一個簡單的文本框開始，但在接下來的部分中，我們將介紹其他類型的輸入和輸出。 現在讓我們看看在 Gradio 應用程序中包含一些 NLP。",Je,R,Te,q,tt="現在讓我們構建一個簡單的界面，讓您可以演示像 GPT-2 這樣的<strong>文本生成</strong>模型。",xe,P,lt=`我們將使用 🤗 Transformers 中的 <code>pipeline()</code> 函數加載我們的模型。
如果您需要快速複習，您可以返回到 <a href="/course/chapter1/3#text-generation">第 1 章中的那個部分</a>。`,$e,X,st="首先，我們定義一個接受文本提示並返回文本完成的預測函數：",ve,S,Ge,Q,at="此函數完成您提供的提示，您可以使用自己的輸入提示運行它以查看它是如何工作的。 這是一個示例（您可能會得到不同的完成）：",ke,E,Ue,Y,Be,z,nt="現在我們有了一個生成預測的函數，我們可以像之前一樣創建和啟動一個“接口”：",Ce,L,Ie,N,pt="就是這樣！ 您現在可以使用此接口使用 GPT-2 模型生成文本，如下所示 🤯.",Ze,u,ot,He,F,it="繼續閱讀以瞭解如何使用 Gradio 構建其他類型的演示！",_e,A,We,O,Ve;return j=new ht({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),g=new rt({props:{title:"構建你的第一個演示",local:"構建你的第一個演示",headingTag:"h1"}}),J=new bt({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 K({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}}),_=new K({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}}),R=new rt({props:{title:"🤖 包括模型預測",local:"-包括模型預測",headingTag:"h2"}}),S=new K({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}}),E=new K({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}}),Y=new K({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}}),L=new K({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}}),A=new Mt({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-TW/chapter9/2.mdx"}}),{c(){b=p("meta"),te=a(),D=p("p"),le=a(),d(j.$$.fragment),se=a(),d(g.$$.fragment),ae=a(),d(J.$$.fragment),ne=a(),T=p("p"),T.textContent=Pe,pe=a(),x=p("p"),x.innerHTML=Xe,oe=a(),$=p("p"),$.textContent=Se,ie=a(),v=p("p"),v.textContent=Qe,re=a(),d(G.$$.fragment),me=a(),k=p("p"),k.textContent=Ee,ce=a(),U=p("p"),U.innerHTML=Ye,ue=a(),B=p("ul"),B.innerHTML=ze,de=a(),C=p("p"),C.innerHTML=Le,ye=a(),m=p("iframe"),fe=a(),I=p("p"),I.textContent=Fe,we=a(),Z=p("p"),Z.innerHTML=Ae,he=a(),H=p("p"),H.textContent=Ke,Me=a(),d(_.$$.fragment),be=a(),c=p("iframe"),je=a(),W=p("p"),W.textContent=Oe,ge=a(),V=p("p"),V.textContent=et,Je=a(),d(R.$$.fragment),Te=a(),q=p("p"),q.innerHTML=tt,xe=a(),P=p("p"),P.innerHTML=lt,$e=a(),X=p("p"),X.textContent=st,ve=a(),d(S.$$.fragment),Ge=a(),Q=p("p"),Q.textContent=at,ke=a(),d(E.$$.fragment),Ue=a(),d(Y.$$.fragment),Be=a(),z=p("p"),z.textContent=nt,Ce=a(),d(L.$$.fragment),Ie=a(),N=p("p"),N.textContent=pt,Ze=a(),u=p("iframe"),He=a(),F=p("p"),F.textContent=it,_e=a(),d(A.$$.fragment),We=a(),O=p("p"),this.h()},l(e){const t=ft("svelte-u9bgzb",document.head);b=o(t,"META",{name:!0,content:!0}),t.forEach(l),te=n(e),D=o(e,"P",{}),ee(D).forEach(l),le=n(e),y(j.$$.fragment,e),se=n(e),y(g.$$.fragment,e),ae=n(e),y(J.$$.fragment,e),ne=n(e),T=o(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-135lmmj"&&(T.textContent=Pe),pe=n(e),x=o(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-sv8g3f"&&(x.innerHTML=Xe),oe=n(e),$=o(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-dxphzn"&&($.textContent=Se),ie=n(e),v=o(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-iuv3ii"&&(v.textContent=Qe),re=n(e),y(G.$$.fragment,e),me=n(e),k=o(e,"P",{"data-svelte-h":!0}),r(k)!=="svelte-st3qa6"&&(k.textContent=Ee),ce=n(e),U=o(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-wjb59t"&&(U.innerHTML=Ye),ue=n(e),B=o(e,"UL",{"data-svelte-h":!0}),r(B)!=="svelte-hh14jc"&&(B.innerHTML=ze),de=n(e),C=o(e,"P",{"data-svelte-h":!0}),r(C)!=="svelte-1r2h8n4"&&(C.innerHTML=Le),ye=n(e),m=o(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),ee(m).forEach(l),fe=n(e),I=o(e,"P",{"data-svelte-h":!0}),r(I)!=="svelte-ec1f8p"&&(I.textContent=Fe),we=n(e),Z=o(e,"P",{"data-svelte-h":!0}),r(Z)!=="svelte-t1aus2"&&(Z.innerHTML=Ae),he=n(e),H=o(e,"P",{"data-svelte-h":!0}),r(H)!=="svelte-tqqosc"&&(H.textContent=Ke),Me=n(e),y(_.$$.fragment,e),be=n(e),c=o(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),ee(c).forEach(l),je=n(e),W=o(e,"P",{"data-svelte-h":!0}),r(W)!=="svelte-so50zz"&&(W.textContent=Oe),ge=n(e),V=o(e,"P",{"data-svelte-h":!0}),r(V)!=="svelte-1sjy9vc"&&(V.textContent=et),Je=n(e),y(R.$$.fragment,e),Te=n(e),q=o(e,"P",{"data-svelte-h":!0}),r(q)!=="svelte-mmwmhr"&&(q.innerHTML=tt),xe=n(e),P=o(e,"P",{"data-svelte-h":!0}),r(P)!=="svelte-1298r7"&&(P.innerHTML=lt),$e=n(e),X=o(e,"P",{"data-svelte-h":!0}),r(X)!=="svelte-kkyquv"&&(X.textContent=st),ve=n(e),y(S.$$.fragment,e),Ge=n(e),Q=o(e,"P",{"data-svelte-h":!0}),r(Q)!=="svelte-mmi9bg"&&(Q.textContent=at),ke=n(e),y(E.$$.fragment,e),Ue=n(e),y(Y.$$.fragment,e),Be=n(e),z=o(e,"P",{"data-svelte-h":!0}),r(z)!=="svelte-1ch1qjj"&&(z.textContent=nt),Ce=n(e),y(L.$$.fragment,e),Ie=n(e),N=o(e,"P",{"data-svelte-h":!0}),r(N)!=="svelte-ks0qyb"&&(N.textContent=pt),Ze=n(e),u=o(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),ee(u).forEach(l),He=n(e),F=o(e,"P",{"data-svelte-h":!0}),r(F)!=="svelte-bvvtyd"&&(F.textContent=it),_e=n(e),y(A.$$.fragment,e),We=n(e),O=o(e,"P",{}),ee(O).forEach(l),this.h()},h(){i(b,"name","hf:doc:metadata"),i(b,"content",gt),Re(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"),Re(c.src,De="https://course-demos-hello-world-custom.hf.space")||i(c,"src",De),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"),Re(u.src,ot="https://course-demos-gpt-2.hf.space")||i(u,"src",ot),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){wt(document.head,b),s(e,te,t),s(e,D,t),s(e,le,t),f(j,e,t),s(e,se,t),f(g,e,t),s(e,ae,t),f(J,e,t),s(e,ne,t),s(e,T,t),s(e,pe,t),s(e,x,t),s(e,oe,t),s(e,$,t),s(e,ie,t),s(e,v,t),s(e,re,t),f(G,e,t),s(e,me,t),s(e,k,t),s(e,ce,t),s(e,U,t),s(e,ue,t),s(e,B,t),s(e,de,t),s(e,C,t),s(e,ye,t),s(e,m,t),s(e,fe,t),s(e,I,t),s(e,we,t),s(e,Z,t),s(e,he,t),s(e,H,t),s(e,Me,t),f(_,e,t),s(e,be,t),s(e,c,t),s(e,je,t),s(e,W,t),s(e,ge,t),s(e,V,t),s(e,Je,t),f(R,e,t),s(e,Te,t),s(e,q,t),s(e,xe,t),s(e,P,t),s(e,$e,t),s(e,X,t),s(e,ve,t),f(S,e,t),s(e,Ge,t),s(e,Q,t),s(e,ke,t),f(E,e,t),s(e,Ue,t),f(Y,e,t),s(e,Be,t),s(e,z,t),s(e,Ce,t),f(L,e,t),s(e,Ie,t),s(e,N,t),s(e,Ze,t),s(e,u,t),s(e,He,t),s(e,F,t),s(e,_e,t),f(A,e,t),s(e,We,t),s(e,O,t),Ve=!0},p:ct,i(e){Ve||(w(j.$$.fragment,e),w(g.$$.fragment,e),w(J.$$.fragment,e),w(G.$$.fragment,e),w(_.$$.fragment,e),w(R.$$.fragment,e),w(S.$$.fragment,e),w(E.$$.fragment,e),w(Y.$$.fragment,e),w(L.$$.fragment,e),w(A.$$.fragment,e),Ve=!0)},o(e){h(j.$$.fragment,e),h(g.$$.fragment,e),h(J.$$.fragment,e),h(G.$$.fragment,e),h(_.$$.fragment,e),h(R.$$.fragment,e),h(S.$$.fragment,e),h(E.$$.fragment,e),h(Y.$$.fragment,e),h(L.$$.fragment,e),h(A.$$.fragment,e),Ve=!1},d(e){e&&(l(te),l(D),l(le),l(se),l(ae),l(ne),l(T),l(pe),l(x),l(oe),l($),l(ie),l(v),l(re),l(me),l(k),l(ce),l(U),l(ue),l(B),l(de),l(C),l(ye),l(m),l(fe),l(I),l(we),l(Z),l(he),l(H),l(Me),l(be),l(c),l(je),l(W),l(ge),l(V),l(Je),l(Te),l(q),l(xe),l(P),l($e),l(X),l(ve),l(Ge),l(Q),l(ke),l(Ue),l(Be),l(z),l(Ce),l(Ie),l(N),l(Ze),l(u),l(He),l(F),l(_e),l(We),l(O)),l(b),M(j,e),M(g,e),M(J,e),M(G,e),M(_,e),M(R,e),M(S,e),M(E,e),M(Y,e),M(L,e),M(A,e)}}}const gt='{"title":"構建你的第一個演示","local":"構建你的第一個演示","sections":[{"title":"🤖 包括模型預測","local":"-包括模型預測","sections":[],"depth":2}],"depth":1}';function Jt(qe){return ut(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class kt extends dt{constructor(b){super(),yt(this,b,Jt,jt,mt,{})}}export{kt as component};
