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