import{S as Ts,i as js,s as gs,e as i,k as p,w as J,t as o,M as Us,c as r,d as s,m as u,a as c,x as T,h as n,b as j,G as t,g as l,y as g,L as Is,q as U,o as I,B as k,v as ks}from"../../chunks/vendor-hf-doc-builder.js";import{I as vs}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as z}from"../../chunks/CodeBlock-hf-doc-builder.js";function $s(ss){let m,ae,y,M,P,v,ke,O,ve,le,W,$e,oe,G,_e,ne,f,qe,$,Ee,Ae,V,Ce,Ne,ie,w,xe,L,ze,We,re,_,ce,h,Ge,X,De,Ze,Y,Be,Qe,pe,q,ue,D,Se,fe,E,he,d,Fe,H,Re,Pe,K,Oe,Ve,de,A,me,Z,ee,Le,ye,C,Me,B,Xe,we,N,be,Q,se,Ye,Je,x,Te,b,He,te,Ke,es,je;return v=new vs({}),_=new z({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwQXVkaW8lMEElMEFtaW5kcyUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJQb2x5QUklMkZtaW5kczE0JTIyJTJDJTIwbmFtZSUzRCUyMmVuLUFVJTIyJTJDJTIwc3BsaXQlM0QlMjJ0cmFpbiUyMiklMEFtaW5kcyUyMCUzRCUyMG1pbmRzLmNhc3RfY29sdW1uKCUyMmF1ZGlvJTIyJTJDJTIwQXVkaW8oc2FtcGxpbmdfcmF0ZSUzRDE2XzAwMCkp",highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset
<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Audio

minds = load_dataset(<span class="hljs-string">&quot;PolyAI/minds14&quot;</span>, name=<span class="hljs-string">&quot;en-AU&quot;</span>, split=<span class="hljs-string">&quot;train&quot;</span>)
minds = minds.cast_column(<span class="hljs-string">&quot;audio&quot;</span>, Audio(sampling_rate=<span class="hljs-number">16_000</span>))`}}),q=new z({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBY2xhc3NpZmllciUyMCUzRCUyMHBpcGVsaW5lKCUwQSUyMCUyMCUyMCUyMCUyMmF1ZGlvLWNsYXNzaWZpY2F0aW9uJTIyJTJDJTBBJTIwJTIwJTIwJTIwbW9kZWwlM0QlMjJhbnRvbi1sJTJGeHRyZW1lX3NfeGxzcl8zMDBtX21pbmRzMTQlMjIlMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

classifier = pipeline(
    <span class="hljs-string">&quot;audio-classification&quot;</span>,
    model=<span class="hljs-string">&quot;anton-l/xtreme_s_xlsr_300m_minds14&quot;</span>,
)`}}),E=new z({props:{code:"ZXhhbXBsZSUyMCUzRCUyMG1pbmRzJTVCMCU1RA==",highlighted:'example = minds[<span class="hljs-number">0</span>]'}}),A=new z({props:{code:"Y2xhc3NpZmllcihleGFtcGxlJTVCJTIyYXVkaW8lMjIlNUQlNUIlMjJhcnJheSUyMiU1RCk=",highlighted:'classifier(example[<span class="hljs-string">&quot;audio&quot;</span>][<span class="hljs-string">&quot;array&quot;</span>])'}}),C=new z({props:{code:"JTVCJTBBJTIwJTIwJTIwJTIwJTdCJTIyc2NvcmUlMjIlM0ElMjAwLjk2MzE1MjUyNzgwOTE0MzElMkMlMjAlMjJsYWJlbCUyMiUzQSUyMCUyMnBheV9iaWxsJTIyJTdEJTJDJTBBJTIwJTIwJTIwJTIwJTdCJTIyc2NvcmUlMjIlM0ElMjAwLjAyODE5Njk4MzAzOTM3OTEyJTJDJTIwJTIybGFiZWwlMjIlM0ElMjAlMjJmcmVlemUlMjIlN0QlMkMlMEElMjAlMjAlMjAlMjAlN0IlMjJzY29yZSUyMiUzQSUyMDAuMDAzMjc4NzQ5Mjk2NDQxNjc0MiUyQyUyMCUyMmxhYmVsJTIyJTNBJTIwJTIyY2FyZF9pc3N1ZXMlMjIlN0QlMkMlMEElMjAlMjAlMjAlMjAlN0IlMjJzY29yZSUyMiUzQSUyMDAuMDAxOTQxNDQ0NTg1MDk5ODE2MyUyQyUyMCUyMmxhYmVsJTIyJTNBJTIwJTIyYWJyb2FkJTIyJTdEJTJDJTBBJTIwJTIwJTIwJTIwJTdCJTIyc2NvcmUlMjIlM0ElMjAwLjAwMDgzNzg2OTM2MDExMTY1MzglMkMlMjAlMjJsYWJlbCUyMiUzQSUyMCUyMmhpZ2hfdmFsdWVfcGF5bWVudCUyMiU3RCUyQyUwQSU1RA==",highlighted:`[
    {<span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.9631525278091431</span>, <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;pay_bill&quot;</span>},
    {<span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.02819698303937912</span>, <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;freeze&quot;</span>},
    {<span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.0032787492964416742</span>, <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;card_issues&quot;</span>},
    {<span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.0019414445850998163</span>, <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;abroad&quot;</span>},
    {<span class="hljs-string">&quot;score&quot;</span>: <span class="hljs-number">0.0008378693601116538</span>, <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;high_value_payment&quot;</span>},
]`}}),N=new z({props:{code:"aWQybGFiZWwlMjAlM0QlMjBtaW5kcy5mZWF0dXJlcyU1QiUyMmludGVudF9jbGFzcyUyMiU1RC5pbnQyc3RyJTBBaWQybGFiZWwoZXhhbXBsZSU1QiUyMmludGVudF9jbGFzcyUyMiU1RCk=",highlighted:`id2label = minds.features[<span class="hljs-string">&quot;intent_class&quot;</span>].int2str
id2label(example[<span class="hljs-string">&quot;intent_class&quot;</span>])`}}),x=new z({props:{code:"JTIycGF5X2JpbGwlMjI=",highlighted:'<span class="hljs-string">&quot;pay_bill&quot;</span>'}}),{c(){m=i("meta"),ae=p(),y=i("h1"),M=i("a"),P=i("span"),J(v.$$.fragment),ke=p(),O=i("span"),ve=o("Audio classification with a pipeline"),le=p(),W=i("p"),$e=o(`Audio classification involves assigning one or more labels to an audio recording based on its content. The labels
could correspond to different sound categories, such as music, speech, or noise, or more specific categories like
bird song or car engine sounds.`),oe=p(),G=i("p"),_e=o(`Before diving into details on how the most popular audio transformers work, and before fine-tuning a custom model, let\u2019s
see how you can use an off-the-shelf pre-trained model for audio classification with only a few lines of code with \u{1F917} Transformers.`),ne=p(),f=i("p"),qe=o("Let\u2019s go ahead and use the same "),$=i("a"),Ee=o("MINDS-14"),Ae=o(` dataset that you have explored
in the previous unit. If you recall, MINDS-14 contains recordings of people asking an e-banking system questions in several
languages and dialects, and has the `),V=i("code"),Ce=o("intent_class"),Ne=o(" for each recording. We can classify the recordings by intent of the call."),ie=p(),w=i("p"),xe=o("Just as before, let\u2019s start by loading the "),L=i("code"),ze=o("en-AU"),We=o(` subset of the data to try out the pipeline, and upsample it to 16kHz
sampling rate which is what most speech models require.`),re=p(),J(_.$$.fragment),ce=p(),h=i("p"),Ge=o("To classify an audio recording into a set of classes, we can use the "),X=i("code"),De=o("audio-classification"),Ze=o(` pipeline from \u{1F917} Transformers.
In our case, we need a model that\u2019s been fine-tuned for intent classification, and specifically on
the MINDS-14 dataset. Luckily for us, the Hub has a model that does just that! Let\u2019s load it by using the `),Y=i("code"),Be=o("pipeline()"),Qe=o(" function:"),pe=p(),J(q.$$.fragment),ue=p(),D=i("p"),Se=o(`This pipeline expects the audio data as a NumPy array. All the preprocessing of the raw audio data will be conveniently
handled for us by the pipeline. Let\u2019s pick an example to try it out:`),fe=p(),J(E.$$.fragment),he=p(),d=i("p"),Fe=o("If you recall the structure of the dataset, the raw audio data is stored in a NumPy array under "),H=i("code"),Re=o('["audio"]["array"]'),Pe=o(`, let\u2019s
pass it straight to the `),K=i("code"),Oe=o("classifier"),Ve=o(":"),de=p(),J(A.$$.fragment),me=p(),Z=i("p"),ee=i("strong"),Le=o("Output:"),ye=p(),J(C.$$.fragment),Me=p(),B=i("p"),Xe=o(`The model is very confident that the caller intended to learn about paying their bill. Let\u2019s see what the actual label for
this example is:`),we=p(),J(N.$$.fragment),be=p(),Q=i("p"),se=i("strong"),Ye=o("Output:"),Je=p(),J(x.$$.fragment),Te=p(),b=i("p"),He=o(`Hooray! The predicted label was correct! Here we were lucky to find a model that can classify the exact labels that we need.
A lot of the times, when dealing with a classification task, a pre-trained model\u2019s set of classes is not exactly the same
as the classes you need the model to distinguish. In this case, you can fine-tune a pre-trained model to \u201Ccalibrate\u201D it to
your exact set of class labels. We\u2019ll learn how to do this in the upcoming units. Now, let\u2019s take a look at another very
common task in speech processing, `),te=i("em"),Ke=o("automatic speech recognition"),es=o("."),this.h()},l(e){const a=Us('[data-svelte="svelte-1phssyn"]',document.head);m=r(a,"META",{name:!0,content:!0}),a.forEach(s),ae=u(e),y=r(e,"H1",{class:!0});var ge=c(y);M=r(ge,"A",{id:!0,class:!0,href:!0});var ts=c(M);P=r(ts,"SPAN",{});var as=c(P);T(v.$$.fragment,as),as.forEach(s),ts.forEach(s),ke=u(ge),O=r(ge,"SPAN",{});var ls=c(O);ve=n(ls,"Audio classification with a pipeline"),ls.forEach(s),ge.forEach(s),le=u(e),W=r(e,"P",{});var os=c(W);$e=n(os,`Audio classification involves assigning one or more labels to an audio recording based on its content. The labels
could correspond to different sound categories, such as music, speech, or noise, or more specific categories like
bird song or car engine sounds.`),os.forEach(s),oe=u(e),G=r(e,"P",{});var ns=c(G);_e=n(ns,`Before diving into details on how the most popular audio transformers work, and before fine-tuning a custom model, let\u2019s
see how you can use an off-the-shelf pre-trained model for audio classification with only a few lines of code with \u{1F917} Transformers.`),ns.forEach(s),ne=u(e),f=r(e,"P",{});var S=c(f);qe=n(S,"Let\u2019s go ahead and use the same "),$=r(S,"A",{href:!0,rel:!0});var is=c($);Ee=n(is,"MINDS-14"),is.forEach(s),Ae=n(S,` dataset that you have explored
in the previous unit. If you recall, MINDS-14 contains recordings of people asking an e-banking system questions in several
languages and dialects, and has the `),V=r(S,"CODE",{});var rs=c(V);Ce=n(rs,"intent_class"),rs.forEach(s),Ne=n(S," for each recording. We can classify the recordings by intent of the call."),S.forEach(s),ie=u(e),w=r(e,"P",{});var Ue=c(w);xe=n(Ue,"Just as before, let\u2019s start by loading the "),L=r(Ue,"CODE",{});var cs=c(L);ze=n(cs,"en-AU"),cs.forEach(s),We=n(Ue,` subset of the data to try out the pipeline, and upsample it to 16kHz
sampling rate which is what most speech models require.`),Ue.forEach(s),re=u(e),T(_.$$.fragment,e),ce=u(e),h=r(e,"P",{});var F=c(h);Ge=n(F,"To classify an audio recording into a set of classes, we can use the "),X=r(F,"CODE",{});var ps=c(X);De=n(ps,"audio-classification"),ps.forEach(s),Ze=n(F,` pipeline from \u{1F917} Transformers.
In our case, we need a model that\u2019s been fine-tuned for intent classification, and specifically on
the MINDS-14 dataset. Luckily for us, the Hub has a model that does just that! Let\u2019s load it by using the `),Y=r(F,"CODE",{});var us=c(Y);Be=n(us,"pipeline()"),us.forEach(s),Qe=n(F," function:"),F.forEach(s),pe=u(e),T(q.$$.fragment,e),ue=u(e),D=r(e,"P",{});var fs=c(D);Se=n(fs,`This pipeline expects the audio data as a NumPy array. All the preprocessing of the raw audio data will be conveniently
handled for us by the pipeline. Let\u2019s pick an example to try it out:`),fs.forEach(s),fe=u(e),T(E.$$.fragment,e),he=u(e),d=r(e,"P",{});var R=c(d);Fe=n(R,"If you recall the structure of the dataset, the raw audio data is stored in a NumPy array under "),H=r(R,"CODE",{});var hs=c(H);Re=n(hs,'["audio"]["array"]'),hs.forEach(s),Pe=n(R,`, let\u2019s
pass it straight to the `),K=r(R,"CODE",{});var ds=c(K);Oe=n(ds,"classifier"),ds.forEach(s),Ve=n(R,":"),R.forEach(s),de=u(e),T(A.$$.fragment,e),me=u(e),Z=r(e,"P",{});var ms=c(Z);ee=r(ms,"STRONG",{});var ys=c(ee);Le=n(ys,"Output:"),ys.forEach(s),ms.forEach(s),ye=u(e),T(C.$$.fragment,e),Me=u(e),B=r(e,"P",{});var Ms=c(B);Xe=n(Ms,`The model is very confident that the caller intended to learn about paying their bill. Let\u2019s see what the actual label for
this example is:`),Ms.forEach(s),we=u(e),T(N.$$.fragment,e),be=u(e),Q=r(e,"P",{});var ws=c(Q);se=r(ws,"STRONG",{});var bs=c(se);Ye=n(bs,"Output:"),bs.forEach(s),ws.forEach(s),Je=u(e),T(x.$$.fragment,e),Te=u(e),b=r(e,"P",{});var Ie=c(b);He=n(Ie,`Hooray! The predicted label was correct! Here we were lucky to find a model that can classify the exact labels that we need.
A lot of the times, when dealing with a classification task, a pre-trained model\u2019s set of classes is not exactly the same
as the classes you need the model to distinguish. In this case, you can fine-tune a pre-trained model to \u201Ccalibrate\u201D it to
your exact set of class labels. We\u2019ll learn how to do this in the upcoming units. Now, let\u2019s take a look at another very
common task in speech processing, `),te=r(Ie,"EM",{});var Js=c(te);Ke=n(Js,"automatic speech recognition"),Js.forEach(s),es=n(Ie,"."),Ie.forEach(s),this.h()},h(){j(m,"name","hf:doc:metadata"),j(m,"content",JSON.stringify(_s)),j(M,"id","audio-classification-with-a-pipeline"),j(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"),j(M,"href","#audio-classification-with-a-pipeline"),j(y,"class","relative group"),j($,"href","https://huggingface.co/datasets/PolyAI/minds14"),j($,"rel","nofollow")},m(e,a){t(document.head,m),l(e,ae,a),l(e,y,a),t(y,M),t(M,P),g(v,P,null),t(y,ke),t(y,O),t(O,ve),l(e,le,a),l(e,W,a),t(W,$e),l(e,oe,a),l(e,G,a),t(G,_e),l(e,ne,a),l(e,f,a),t(f,qe),t(f,$),t($,Ee),t(f,Ae),t(f,V),t(V,Ce),t(f,Ne),l(e,ie,a),l(e,w,a),t(w,xe),t(w,L),t(L,ze),t(w,We),l(e,re,a),g(_,e,a),l(e,ce,a),l(e,h,a),t(h,Ge),t(h,X),t(X,De),t(h,Ze),t(h,Y),t(Y,Be),t(h,Qe),l(e,pe,a),g(q,e,a),l(e,ue,a),l(e,D,a),t(D,Se),l(e,fe,a),g(E,e,a),l(e,he,a),l(e,d,a),t(d,Fe),t(d,H),t(H,Re),t(d,Pe),t(d,K),t(K,Oe),t(d,Ve),l(e,de,a),g(A,e,a),l(e,me,a),l(e,Z,a),t(Z,ee),t(ee,Le),l(e,ye,a),g(C,e,a),l(e,Me,a),l(e,B,a),t(B,Xe),l(e,we,a),g(N,e,a),l(e,be,a),l(e,Q,a),t(Q,se),t(se,Ye),l(e,Je,a),g(x,e,a),l(e,Te,a),l(e,b,a),t(b,He),t(b,te),t(te,Ke),t(b,es),je=!0},p:Is,i(e){je||(U(v.$$.fragment,e),U(_.$$.fragment,e),U(q.$$.fragment,e),U(E.$$.fragment,e),U(A.$$.fragment,e),U(C.$$.fragment,e),U(N.$$.fragment,e),U(x.$$.fragment,e),je=!0)},o(e){I(v.$$.fragment,e),I(_.$$.fragment,e),I(q.$$.fragment,e),I(E.$$.fragment,e),I(A.$$.fragment,e),I(C.$$.fragment,e),I(N.$$.fragment,e),I(x.$$.fragment,e),je=!1},d(e){s(m),e&&s(ae),e&&s(y),k(v),e&&s(le),e&&s(W),e&&s(oe),e&&s(G),e&&s(ne),e&&s(f),e&&s(ie),e&&s(w),e&&s(re),k(_,e),e&&s(ce),e&&s(h),e&&s(pe),k(q,e),e&&s(ue),e&&s(D),e&&s(fe),k(E,e),e&&s(he),e&&s(d),e&&s(de),k(A,e),e&&s(me),e&&s(Z),e&&s(ye),k(C,e),e&&s(Me),e&&s(B),e&&s(we),k(N,e),e&&s(be),e&&s(Q),e&&s(Je),k(x,e),e&&s(Te),e&&s(b)}}}const _s={local:"audio-classification-with-a-pipeline",title:"Audio classification with a pipeline"};function qs(ss){return ks(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ns extends Ts{constructor(m){super();js(this,m,qs,$s,gs,{})}}export{Ns as default,_s as metadata};
