import{S as Be,i as Ie,s as Ee,e as r,k as m,w as X,t as o,M as We,c as p,d as t,m as f,a as c,x as F,h as i,b as s,N as Re,G as a,g as d,y as z,L as Ze,q as P,o as K,B as L,v as Ae}from"../../chunks/vendor-hf-doc-builder.js";import{I as Ce}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as we}from"../../chunks/CodeBlock-hf-doc-builder.js";function He(ye){let w,R,y,M,$,j,D,B,O,Z,n,ee,b,te,ae,_,le,se,I,oe,ie,G,ne,re,E,pe,ce,A,v,C,g,de,W,he,ue,H,k,N,T,me,Q,J,S,U,fe,x,h,Me,V;return j=new Ce({}),v=new we({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBbW9kZWxfaWQlMjAlM0QlMjAlMjJzYW5jaGl0LWdhbmRoaSUyRmRpc3RpbGh1YmVydC1maW5ldHVuZWQtZ3R6YW4lMjIlMEFwaXBlJTIwJTNEJTIwcGlwZWxpbmUoJTIyYXVkaW8tY2xhc3NpZmljYXRpb24lMjIlMkMlMjBtb2RlbCUzRG1vZGVsX2lkKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

model_id = <span class="hljs-string">&quot;sanchit-gandhi/distilhubert-finetuned-gtzan&quot;</span>
pipe = pipeline(<span class="hljs-string">&quot;audio-classification&quot;</span>, model=model_id)`}}),k=new we({props:{code:"ZGVmJTIwY2xhc3NpZnlfYXVkaW8oZmlsZXBhdGgpJTNBJTBBJTIwJTIwJTIwJTIwcHJlZHMlMjAlM0QlMjBwaXBlKGZpbGVwYXRoKSUwQSUyMCUyMCUyMCUyMG91dHB1dHMlMjAlM0QlMjAlN0IlN0QlMEElMjAlMjAlMjAlMjBmb3IlMjBwJTIwaW4lMjBwcmVkcyUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dHMlNUJwJTVCJTIybGFiZWwlMjIlNUQlNUQlMjAlM0QlMjBwJTVCJTIyc2NvcmUlMjIlNUQlMEElMjAlMjAlMjAlMjByZXR1cm4lMjBvdXRwdXRz",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">classify_audio</span>(<span class="hljs-params">filepath</span>):
    preds = pipe(filepath)
    outputs = {}
    <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> preds:
        outputs[p[<span class="hljs-string">&quot;label&quot;</span>]] = p[<span class="hljs-string">&quot;score&quot;</span>]
    <span class="hljs-keyword">return</span> outputs`}}),J=new we({props:{code:"aW1wb3J0JTIwZ3JhZGlvJTIwYXMlMjBnciUwQSUwQWRlbW8lMjAlM0QlMjBnci5JbnRlcmZhY2UoJTBBJTIwJTIwJTIwJTIwZm4lM0RjbGFzc2lmeV9hdWRpbyUyQyUyMGlucHV0cyUzRGdyLkF1ZGlvKHR5cGUlM0QlMjJmaWxlcGF0aCUyMiklMkMlMjBvdXRwdXRzJTNEZ3Iub3V0cHV0cy5MYWJlbCgpJTBBKSUwQWRlbW8ubGF1bmNoKGRlYnVnJTNEVHJ1ZSk=",highlighted:`<span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr

demo = gr.Interface(
    fn=classify_audio, inputs=gr.Audio(<span class="hljs-built_in">type</span>=<span class="hljs-string">&quot;filepath&quot;</span>), outputs=gr.outputs.Label()
)
demo.launch(debug=<span class="hljs-literal">True</span>)`}}),{c(){w=r("meta"),R=m(),y=r("h1"),M=r("a"),$=r("span"),X(j.$$.fragment),D=m(),B=r("span"),O=o("Build a demo with Gradio"),Z=m(),n=r("p"),ee=o("In this final section on audio classification, we\u2019ll build a "),b=r("a"),te=o("Gradio"),ae=o(` demo to showcase the music
classification model that we just trained on the `),_=r("a"),le=o("GTZAN"),se=o(` dataset. The first
thing to do is load up the fine-tuned checkpoint using the `),I=r("code"),oe=o("pipeline()"),ie=o(` class - this is very familiar now from the section
on `),G=r("a"),ne=o("pre-trained models"),re=o(". You can change the "),E=r("code"),pe=o("model_id"),ce=o(` to the namespace of your fine-tuned model
on the Hugging Face Hub:`),A=m(),X(v.$$.fragment),C=m(),g=r("p"),de=o(`Secondly, we\u2019ll define a function that takes the filepath for an audio input and passes it through the pipeline. Here,
the pipeline automatically takes care of loading the audio file, resampling it to the correct sampling rate, and running
inference with the model. We take the models predictions of `),W=r("code"),he=o("preds"),ue=o(` and format them as a dictionary object to be displayed on the
output:`),H=m(),X(k.$$.fragment),N=m(),T=r("p"),me=o("Finally, we launch the Gradio demo using the function we\u2019ve just defined:"),Q=m(),X(J.$$.fragment),S=m(),U=r("p"),fe=o("This will launch a Gradio demo similar to the one running on the Hugging Face Space:"),x=m(),h=r("iframe"),this.h()},l(e){const l=We('[data-svelte="svelte-1phssyn"]',document.head);w=p(l,"META",{name:!0,content:!0}),l.forEach(t),R=f(e),y=p(e,"H1",{class:!0});var Y=c(y);M=p(Y,"A",{id:!0,class:!0,href:!0});var ge=c(M);$=p(ge,"SPAN",{});var je=c($);F(j.$$.fragment,je),je.forEach(t),ge.forEach(t),D=f(Y),B=p(Y,"SPAN",{});var be=c(B);O=i(be,"Build a demo with Gradio"),be.forEach(t),Y.forEach(t),Z=f(e),n=p(e,"P",{});var u=c(n);ee=i(u,"In this final section on audio classification, we\u2019ll build a "),b=p(u,"A",{href:!0,rel:!0});var _e=c(b);te=i(_e,"Gradio"),_e.forEach(t),ae=i(u,` demo to showcase the music
classification model that we just trained on the `),_=p(u,"A",{href:!0,rel:!0});var ve=c(_);le=i(ve,"GTZAN"),ve.forEach(t),se=i(u,` dataset. The first
thing to do is load up the fine-tuned checkpoint using the `),I=p(u,"CODE",{});var ke=c(I);oe=i(ke,"pipeline()"),ke.forEach(t),ie=i(u,` class - this is very familiar now from the section
on `),G=p(u,"A",{href:!0});var Je=c(G);ne=i(Je,"pre-trained models"),Je.forEach(t),re=i(u,". You can change the "),E=p(u,"CODE",{});var Ge=c(E);pe=i(Ge,"model_id"),Ge.forEach(t),ce=i(u,` to the namespace of your fine-tuned model
on the Hugging Face Hub:`),u.forEach(t),A=f(e),F(v.$$.fragment,e),C=f(e),g=p(e,"P",{});var q=c(g);de=i(q,`Secondly, we\u2019ll define a function that takes the filepath for an audio input and passes it through the pipeline. Here,
the pipeline automatically takes care of loading the audio file, resampling it to the correct sampling rate, and running
inference with the model. We take the models predictions of `),W=p(q,"CODE",{});var Te=c(W);he=i(Te,"preds"),Te.forEach(t),ue=i(q,` and format them as a dictionary object to be displayed on the
output:`),q.forEach(t),H=f(e),F(k.$$.fragment,e),N=f(e),T=p(e,"P",{});var Ue=c(T);me=i(Ue,"Finally, we launch the Gradio demo using the function we\u2019ve just defined:"),Ue.forEach(t),Q=f(e),F(J.$$.fragment,e),S=f(e),U=p(e,"P",{});var $e=c(U);fe=i($e,"This will launch a Gradio demo similar to the one running on the Hugging Face Space:"),$e.forEach(t),x=f(e),h=p(e,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),c(h).forEach(t),this.h()},h(){s(w,"name","hf:doc:metadata"),s(w,"content",JSON.stringify(Ne)),s(M,"id","build-a-demo-with-gradio"),s(M,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),s(M,"href","#build-a-demo-with-gradio"),s(y,"class","relative group"),s(b,"href","https://gradio.app"),s(b,"rel","nofollow"),s(_,"href","https://huggingface.co/datasets/marsyas/gtzan"),s(_,"rel","nofollow"),s(G,"href","classification_models"),Re(h.src,Me="https://course-demos-song-classifier.hf.space")||s(h,"src",Me),s(h,"frameborder","0"),s(h,"height","450"),s(h,"title","Gradio app"),s(h,"class","container p-0 flex-grow space-iframe"),s(h,"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"),s(h,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads")},m(e,l){a(document.head,w),d(e,R,l),d(e,y,l),a(y,M),a(M,$),z(j,$,null),a(y,D),a(y,B),a(B,O),d(e,Z,l),d(e,n,l),a(n,ee),a(n,b),a(b,te),a(n,ae),a(n,_),a(_,le),a(n,se),a(n,I),a(I,oe),a(n,ie),a(n,G),a(G,ne),a(n,re),a(n,E),a(E,pe),a(n,ce),d(e,A,l),z(v,e,l),d(e,C,l),d(e,g,l),a(g,de),a(g,W),a(W,he),a(g,ue),d(e,H,l),z(k,e,l),d(e,N,l),d(e,T,l),a(T,me),d(e,Q,l),z(J,e,l),d(e,S,l),d(e,U,l),a(U,fe),d(e,x,l),d(e,h,l),V=!0},p:Ze,i(e){V||(P(j.$$.fragment,e),P(v.$$.fragment,e),P(k.$$.fragment,e),P(J.$$.fragment,e),V=!0)},o(e){K(j.$$.fragment,e),K(v.$$.fragment,e),K(k.$$.fragment,e),K(J.$$.fragment,e),V=!1},d(e){t(w),e&&t(R),e&&t(y),L(j),e&&t(Z),e&&t(n),e&&t(A),L(v,e),e&&t(C),e&&t(g),e&&t(H),L(k,e),e&&t(N),e&&t(T),e&&t(Q),L(J,e),e&&t(S),e&&t(U),e&&t(x),e&&t(h)}}}const Ne={local:"build-a-demo-with-gradio",title:"Build a demo with Gradio"};function Qe(ye){return Ae(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ye extends Be{constructor(w){super();Ie(this,w,Qe,He,Ee,{})}}export{Ye as default,Ne as metadata};
