import{s as nt,f as He,n as ot,o as it}from"../chunks/scheduler.37c15a92.js";import{S as rt,i as pt,g as o,s,r as d,A as ut,h as i,f as l,c as n,j as D,u as h,x as p,k as r,y as mt,a,v as f,d as w,t as y,w as b}from"../chunks/index.7cb9c9b8.js";import{C as z}from"../chunks/CodeBlock.abae2786.js";import{C as ct}from"../chunks/CourseFloatingBanner.df82c153.js";import{H as st,E as dt}from"../chunks/getInferenceSnippets.b819cf97.js";function ht(We){let g,O,A,ee,M,te,x,le,j,_e="Let’s start by installing Gradio! Since it is a Python package, simply run:",ae,T,Ve="<code>$ pip install gradio</code>",se,J,Re=`You can run Gradio anywhere, be it from your favourite Python IDE, to Jupyter notebooks or even in Google Colab 🤯!
So install Gradio wherever you run Python!`,ne,k,Se="Let’s get started with a simple “Hello World” example to get familiar with the Gradio syntax:",oe,v,ie,$,Xe="Let’s walk through the code above:",re,G,Ye="<li>First, we define a function called <code>greet()</code>. In this case, it is a simple function that adds “Hello” before your name, but it can be <em>any</em> Python function in general. For example, in machine learning applications, this function would <em>call a model to make a prediction</em> on an input and return the output.</li> <li>Then, we create a Gradio <code>Interface</code> with three arguments, <code>fn</code>, <code>inputs</code>, and <code>outputs</code>. These arguments define the prediction function, as well as the <em>type</em> of input and output components we would like. In our case, both components are simple text boxes.</li> <li>We then call the <code>launch()</code> method on the <code>Interface</code> that we created.</li>",pe,I,qe='If you run this code, the interface below will appear automatically within a Jupyter/Colab notebook, or pop in a browser on <strong><a href="http://localhost:7860/" rel="nofollow">http://localhost:7860</a></strong> if running from a script.',ue,u,Pe,me,B,Qe="Try using this GUI right now with your own name or some other input!",ce,U,Ee=`You’ll notice that in this GUI, Gradio automatically inferred the name of the input parameter (<code>name</code>)
and applied it as a label on top of the textbox. What if you’d like to change that?
Or if you’d like to customize the textbox in some other way? In that case, you can
instantiate a class object representing the input component.`,de,C,Le="Take a look at the example below:",he,Z,fe,m,Fe,we,H,Ne=`Here, we’ve created an input textbox with a label, a placeholder, and a set number of lines.
You could do the same for the output textbox, but we’ll leave that for now.`,ye,W,ze=`We’ve seen that with just a few lines of code, Gradio lets you create a simple interface around any function
with any kind of inputs or outputs. In this section, we’ve started with a
simple textbox, but in the next sections, we’ll cover other kinds of inputs and outputs. Let’s now take a look at including some NLP in a Gradio application.`,be,_,ge,V,Ae="Let’s now build a simple interface that allows you to demo a <strong>text-generation</strong> model like GPT-2.",Me,R,Ke=`We’ll load our model using the <code>pipeline()</code> function from 🤗 Transformers.
If you need a quick refresher, you can go back to <a href="/course/chapter1/3#text-generation">that section in Chapter 1</a>.`,xe,S,De="First, we define a prediction function that takes in a text prompt and returns the text completion:",je,X,Te,Y,Oe="This function completes prompts that you provide, and you can run it with your own input prompts to see how it works. Here is an example (you might get a different completion):",Je,q,ke,P,ve,Q,et="Now that we have a function for generating predictions, we can create and launch an <code>Interface</code> in the same way we did earlier:",$e,E,Ge,L,tt="That’s it! You can now use this interface to generate text using the GPT-2 model as shown below 🤯.",Ie,c,lt,Be,F,at="Keep reading to see how to build other kinds of demos with Gradio!",Ue,N,Ce,K,Ze;return M=new st({props:{title:"Building your first demo",local:"building-your-first-demo",headingTag:"h1"}}),x=new ct({props:{chapter:9,classNames:"absolute z-10 right-0 top-0",notebooks:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section2.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section2.ipynb"}]}}),v=new z({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}}),Z=new z({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}}),_=new st({props:{title:"🤖 Including model predictions",local:"including-model-predictions",headingTag:"h2"}}),X=new z({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 z({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}}),P=new z({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}}),E=new z({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}}),N=new dt({props:{source:"https://github.com/huggingface/course/blob/main/chapters/en/chapter9/2.mdx"}}),{c(){g=o("meta"),O=s(),A=o("p"),ee=s(),d(M.$$.fragment),te=s(),d(x.$$.fragment),le=s(),j=o("p"),j.textContent=_e,ae=s(),T=o("p"),T.innerHTML=Ve,se=s(),J=o("p"),J.textContent=Re,ne=s(),k=o("p"),k.textContent=Se,oe=s(),d(v.$$.fragment),ie=s(),$=o("p"),$.textContent=Xe,re=s(),G=o("ul"),G.innerHTML=Ye,pe=s(),I=o("p"),I.innerHTML=qe,ue=s(),u=o("iframe"),me=s(),B=o("p"),B.textContent=Qe,ce=s(),U=o("p"),U.innerHTML=Ee,de=s(),C=o("p"),C.textContent=Le,he=s(),d(Z.$$.fragment),fe=s(),m=o("iframe"),we=s(),H=o("p"),H.textContent=Ne,ye=s(),W=o("p"),W.textContent=ze,be=s(),d(_.$$.fragment),ge=s(),V=o("p"),V.innerHTML=Ae,Me=s(),R=o("p"),R.innerHTML=Ke,xe=s(),S=o("p"),S.textContent=De,je=s(),d(X.$$.fragment),Te=s(),Y=o("p"),Y.textContent=Oe,Je=s(),d(q.$$.fragment),ke=s(),d(P.$$.fragment),ve=s(),Q=o("p"),Q.innerHTML=et,$e=s(),d(E.$$.fragment),Ge=s(),L=o("p"),L.textContent=tt,Ie=s(),c=o("iframe"),Be=s(),F=o("p"),F.textContent=at,Ue=s(),d(N.$$.fragment),Ce=s(),K=o("p"),this.h()},l(e){const t=ut("svelte-u9bgzb",document.head);g=i(t,"META",{name:!0,content:!0}),t.forEach(l),O=n(e),A=i(e,"P",{}),D(A).forEach(l),ee=n(e),h(M.$$.fragment,e),te=n(e),h(x.$$.fragment,e),le=n(e),j=i(e,"P",{"data-svelte-h":!0}),p(j)!=="svelte-1majxmb"&&(j.textContent=_e),ae=n(e),T=i(e,"P",{"data-svelte-h":!0}),p(T)!=="svelte-sv8g3f"&&(T.innerHTML=Ve),se=n(e),J=i(e,"P",{"data-svelte-h":!0}),p(J)!=="svelte-m0sejh"&&(J.textContent=Re),ne=n(e),k=i(e,"P",{"data-svelte-h":!0}),p(k)!=="svelte-t8uf68"&&(k.textContent=Se),oe=n(e),h(v.$$.fragment,e),ie=n(e),$=i(e,"P",{"data-svelte-h":!0}),p($)!=="svelte-ncdl3e"&&($.textContent=Xe),re=n(e),G=i(e,"UL",{"data-svelte-h":!0}),p(G)!=="svelte-hfhpkt"&&(G.innerHTML=Ye),pe=n(e),I=i(e,"P",{"data-svelte-h":!0}),p(I)!=="svelte-wp0shj"&&(I.innerHTML=qe),ue=n(e),u=i(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),D(u).forEach(l),me=n(e),B=i(e,"P",{"data-svelte-h":!0}),p(B)!=="svelte-4s0wmk"&&(B.textContent=Qe),ce=n(e),U=i(e,"P",{"data-svelte-h":!0}),p(U)!=="svelte-fjgg8g"&&(U.innerHTML=Ee),de=n(e),C=i(e,"P",{"data-svelte-h":!0}),p(C)!=="svelte-wrn35m"&&(C.textContent=Le),he=n(e),h(Z.$$.fragment,e),fe=n(e),m=i(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),D(m).forEach(l),we=n(e),H=i(e,"P",{"data-svelte-h":!0}),p(H)!=="svelte-1e1b3uv"&&(H.textContent=Ne),ye=n(e),W=i(e,"P",{"data-svelte-h":!0}),p(W)!=="svelte-tl90qv"&&(W.textContent=ze),be=n(e),h(_.$$.fragment,e),ge=n(e),V=i(e,"P",{"data-svelte-h":!0}),p(V)!=="svelte-b3uqtx"&&(V.innerHTML=Ae),Me=n(e),R=i(e,"P",{"data-svelte-h":!0}),p(R)!=="svelte-1kqxpib"&&(R.innerHTML=Ke),xe=n(e),S=i(e,"P",{"data-svelte-h":!0}),p(S)!=="svelte-zuidxn"&&(S.textContent=De),je=n(e),h(X.$$.fragment,e),Te=n(e),Y=i(e,"P",{"data-svelte-h":!0}),p(Y)!=="svelte-1cnd6y7"&&(Y.textContent=Oe),Je=n(e),h(q.$$.fragment,e),ke=n(e),h(P.$$.fragment,e),ve=n(e),Q=i(e,"P",{"data-svelte-h":!0}),p(Q)!=="svelte-mg1sf2"&&(Q.innerHTML=et),$e=n(e),h(E.$$.fragment,e),Ge=n(e),L=i(e,"P",{"data-svelte-h":!0}),p(L)!=="svelte-ptoolc"&&(L.textContent=tt),Ie=n(e),c=i(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),D(c).forEach(l),Be=n(e),F=i(e,"P",{"data-svelte-h":!0}),p(F)!=="svelte-md4bqz"&&(F.textContent=at),Ue=n(e),h(N.$$.fragment,e),Ce=n(e),K=i(e,"P",{}),D(K).forEach(l),this.h()},h(){r(g,"name","hf:doc:metadata"),r(g,"content",ft),He(u.src,Pe="https://course-demos-hello-world.hf.space")||r(u,"src",Pe),r(u,"frameborder","0"),r(u,"height","250"),r(u,"title","Gradio app"),r(u,"class","container p-0 flex-grow space-iframe"),r(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"),r(u,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),He(m.src,Fe="https://course-demos-hello-world-custom.hf.space")||r(m,"src",Fe),r(m,"frameborder","0"),r(m,"height","300"),r(m,"title","Gradio app"),r(m,"class","container p-0 flex-grow space-iframe"),r(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"),r(m,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),He(c.src,lt="https://course-demos-gpt-2.hf.space")||r(c,"src",lt),r(c,"frameborder","0"),r(c,"height","300"),r(c,"title","Gradio app"),r(c,"class","container p-0 flex-grow space-iframe"),r(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"),r(c,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads")},m(e,t){mt(document.head,g),a(e,O,t),a(e,A,t),a(e,ee,t),f(M,e,t),a(e,te,t),f(x,e,t),a(e,le,t),a(e,j,t),a(e,ae,t),a(e,T,t),a(e,se,t),a(e,J,t),a(e,ne,t),a(e,k,t),a(e,oe,t),f(v,e,t),a(e,ie,t),a(e,$,t),a(e,re,t),a(e,G,t),a(e,pe,t),a(e,I,t),a(e,ue,t),a(e,u,t),a(e,me,t),a(e,B,t),a(e,ce,t),a(e,U,t),a(e,de,t),a(e,C,t),a(e,he,t),f(Z,e,t),a(e,fe,t),a(e,m,t),a(e,we,t),a(e,H,t),a(e,ye,t),a(e,W,t),a(e,be,t),f(_,e,t),a(e,ge,t),a(e,V,t),a(e,Me,t),a(e,R,t),a(e,xe,t),a(e,S,t),a(e,je,t),f(X,e,t),a(e,Te,t),a(e,Y,t),a(e,Je,t),f(q,e,t),a(e,ke,t),f(P,e,t),a(e,ve,t),a(e,Q,t),a(e,$e,t),f(E,e,t),a(e,Ge,t),a(e,L,t),a(e,Ie,t),a(e,c,t),a(e,Be,t),a(e,F,t),a(e,Ue,t),f(N,e,t),a(e,Ce,t),a(e,K,t),Ze=!0},p:ot,i(e){Ze||(w(M.$$.fragment,e),w(x.$$.fragment,e),w(v.$$.fragment,e),w(Z.$$.fragment,e),w(_.$$.fragment,e),w(X.$$.fragment,e),w(q.$$.fragment,e),w(P.$$.fragment,e),w(E.$$.fragment,e),w(N.$$.fragment,e),Ze=!0)},o(e){y(M.$$.fragment,e),y(x.$$.fragment,e),y(v.$$.fragment,e),y(Z.$$.fragment,e),y(_.$$.fragment,e),y(X.$$.fragment,e),y(q.$$.fragment,e),y(P.$$.fragment,e),y(E.$$.fragment,e),y(N.$$.fragment,e),Ze=!1},d(e){e&&(l(O),l(A),l(ee),l(te),l(le),l(j),l(ae),l(T),l(se),l(J),l(ne),l(k),l(oe),l(ie),l($),l(re),l(G),l(pe),l(I),l(ue),l(u),l(me),l(B),l(ce),l(U),l(de),l(C),l(he),l(fe),l(m),l(we),l(H),l(ye),l(W),l(be),l(ge),l(V),l(Me),l(R),l(xe),l(S),l(je),l(Te),l(Y),l(Je),l(ke),l(ve),l(Q),l($e),l(Ge),l(L),l(Ie),l(c),l(Be),l(F),l(Ue),l(Ce),l(K)),l(g),b(M,e),b(x,e),b(v,e),b(Z,e),b(_,e),b(X,e),b(q,e),b(P,e),b(E,e),b(N,e)}}}const ft='{"title":"Building your first demo","local":"building-your-first-demo","sections":[{"title":"🤖 Including model predictions","local":"including-model-predictions","sections":[],"depth":2}],"depth":1}';function wt(We){return it(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class jt extends rt{constructor(g){super(),pt(this,g,wt,ht,nt,{})}}export{jt as component};
