import{S as Xo,i as Qo,s as Po,e as n,k as h,w as d,t,M as Ho,c as o,d as l,m as f,a as r,x as m,h as a,b as c,G as s,g as p,y,q as w,o as M,B as j,v as Vo,L as Fo}from"../../chunks/vendor-hf-doc-builder.js";import{T as zo}from"../../chunks/Tip-hf-doc-builder.js";import{I as D}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as U}from"../../chunks/CodeBlock-hf-doc-builder.js";function qo(ls){let u,I,v,E,C;return{c(){u=n("p"),I=t("Although you can always use the "),v=n("a"),E=t("Python library"),C=t(" for server-side inference, using Transformers.js means that you can write all of your code in JavaScript (instead of having to set up and communicate with a separate Python process)."),this.h()},l(T){u=o(T,"P",{});var b=r(u);I=a(b,"Although you can always use the "),v=o(b,"A",{href:!0,rel:!0});var $=r(v);E=a($,"Python library"),$.forEach(l),C=a(b," for server-side inference, using Transformers.js means that you can write all of your code in JavaScript (instead of having to set up and communicate with a separate Python process)."),b.forEach(l),this.h()},h(){c(v,"href","https://github.com/huggingface/transformers"),c(v,"rel","nofollow")},m(T,b){p(T,u,b),s(u,I),s(u,v),s(v,E),s(u,C)},d(T){T&&l(u)}}}function Do(ls){let u,I,v,E,C,T,b,$;return b=new U({props:{code:"TXlDbGFzc2lmaWNhdGlvblBpcGVsaW5lLmdldEluc3RhbmNlKCklM0I=",highlighted:'<span class="hljs-title class_">MyClassificationPipeline</span>.<span class="hljs-title function_">getInstance</span>();'}}),{c(){u=n("p"),I=t("Since we use lazy loading, the first request made to the server will also be responsible for loading the pipeline. If you would like to begin loading the pipeline as soon as the server starts running, you can add the following line of code after defining "),v=n("code"),E=t("MyClassificationPipeline"),C=t(":"),T=h(),d(b.$$.fragment)},l(J){u=o(J,"P",{});var g=r(u);I=a(g,"Since we use lazy loading, the first request made to the server will also be responsible for loading the pipeline. If you would like to begin loading the pipeline as soon as the server starts running, you can add the following line of code after defining "),v=o(g,"CODE",{});var Y=r(v);E=a(Y,"MyClassificationPipeline"),Y.forEach(l),C=a(g,":"),g.forE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hl,me,un,ll,dn,mn,Vl,Oe,Fl,R,yn,tl,wn,Mn,al,jn,vn,ql,Le,Dl;return T=new D({}),L=new zo({props:{$$slots:{default:[qo]},$$scope:{ctx:ls}}}),Ee=new D({}),$e=new D({}),Ie=new U({props:{code:"bnBtJTIwaW5pdCUyMC15JTBBbnBtJTIwaSUyMCU0MHhlbm92YSUyRnRyYW5zZm9ybWVycw==",highlighted:`npm init -y
npm i @xenova/transformers`}}),_e=new D({}),ke=new U({props:{code:"JTdCJTBBJTIwJTIwLi4uJTBBJTIwJTIwJTIydHlwZSUyMiUzQSUyMCUyMm1vZHVsZSUyMiUyQyUwQSUyMCUyMC4uLiUwQSU3RA==",highlighted:`<span class="hljs-punctuation">{</span>
  ...
  <span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;module&quot;</span><span class="hljs-punctuation">,</span>
  ...
<span class="hljs-punctuation">}</span>`}}),Se=new U({props:{code:"aW1wb3J0JTIwaHR0cCUyMGZyb20lMjAnaHR0cCclM0IlMEFpbXBvcnQlMjBxdWVyeXN0cmluZyUyMGZyb20lMjAncXVlcnlzdHJpbmcnJTNCJTBBaW1wb3J0JTIwdXJsJTIwZnJvbSUyMCd1cmwnJTNC",highlighted:`<span class="hljs-keyword">import</span> http <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;http&#x27;</span>;
<span class="hljs-keyword">import</span> querystring <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;querystring&#x27;</span>;
<span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;url&#x27;</span>;`}}),Ae=new U({props:{code:"aW1wb3J0JTIwJTdCJTIwcGlwZWxpbmUlMkMlMjBlbnYlMjAlN0QlMjBmcm9tJTIwJyU0MHhlbm92YSUyRnRyYW5zZm9ybWVycyclM0IlMEElMEFjbGFzcyUyME15Q2xhc3NpZmljYXRpb25QaXBlbGluZSUyMCU3QiUwQSUyMCUyMHN0YXRpYyUyMHRhc2slMjAlM0QlMjAndGV4dC1jbGFzc2lmaWNhdGlvbiclM0IlMEElMjAlMjBzdGF0aWMlMjBtb2RlbCUyMCUzRCUyMCdYZW5vdmElMkZkaXN0aWxiZXJ0LWJhc2UtdW5jYXNlZC1maW5ldHVuZWQtc3N0LTItZW5nbGlzaCclM0IlMEElMjAlMjBzdGF0aWMlMjBpbnN0YW5jZSUyMCUzRCUyMG51bGwlM0IlMEElMEElMjAlMjBzdGF0aWMlMjBhc3luYyUyMGdldEluc3RhbmNlKHByb2dyZXNzX2NhbGxiYWNrJTIwJTNEJTIwbnVsbCklMjAlN0IlMEElMjAlMjAlMjAlMjBpZiUyMCh0aGlzLmluc3RhbmNlJTIwJTNEJTNEJTNEJTIwbnVsbCklMjAlN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMkYlMkYlMjBOT1RFJTNBJTIwVW5jb21tZW50JTIwdGhpcyUyMHRvJTIwY2hhbmdlJTIwdGhlJTIwY2FjaGUlMjBkaXJlY3RvcnklMEElMjAlMjAlMjAlMjAlMjAlMjAlMkYlMkYlMjBlbnYuY2FjaGVEaXIlMjAlM0QlMjAnLiUyRi5jYWNoZSclM0IlMEElMEElMjAlMjAlMjAlMjAlMjAlMjB0aGlzLmluc3RhbmNlJTIwJTNEJTIwcGlwZWxpbmUodGhpcy50YXNrJTJDJTIwdGhpcy5tb2RlbCUyQyUyMCU3QiUyMHByb2dyZXNzX2NhbGxiYWNrJTIwJTdEKSUzQiUwQSUyMCUyMCUyMCUyMCU3RCUwQSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMHRoaXMuaW5zdGFuY2UlM0IlMEElMjAlMjAlN0QlMEElN0Q=",highlighted:`<span class="hljs-keyword">import</span> { pipeline, env } <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@xenova/transformers&#x27;</span>;

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyClassificationPipeline</span> {
  <span class="hljs-keyword">static</span> task = <span class="hljs-string">&#x27;text-classification&#x27;</span>;
  <span class="hljs-keyword">static</span> model = <span class="hljs-string">&#x27;Xenova/distilbert-base-uncased-finetuned-sst-2-english&#x27;</span>;
  <span class="hljs-keyword">static</span> instance = <span class="hljs-literal">null</span>;

  <span class="hljs-keyword">static</span> <span class="hljs-keyword">async</span> <span class="hljs-title function_">getInstance</span>(<span class="hljs-params">progress_callback = <span class="hljs-literal">null</span></span>) {
    <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">instance</span> === <span class="hljs-literal">null</span>) {
      <span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> Uncomment this to change the cache directory</span>
      <span class="hljs-comment">// env.cacheDir = &#x27;./.cache&#x27;;</span>

      <span class="hljs-variable language_">this</span>.<span class="hljs-property">instance</span> = <span class="hljs-title function_">pipeline</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">task</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>, { progress_callback });
    }

    <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">instance</span>;
  }
}`}}),Ge=new D({}),xe=new U({props:{code:"Y29uc3QlMjBodHRwJTIwJTNEJTIwcmVxdWlyZSgnaHR0cCcpJTNCJTBBY29uc3QlMjBxdWVyeXN0cmluZyUyMCUzRCUyMHJlcXVpcmUoJ3F1ZXJ5c3RyaW5nJyklM0IlMEFjb25zdCUyMHVybCUyMCUzRCUyMHJlcXVpcmUoJ3VybCcpJTNC",highlighted:`<span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;http&#x27;</span>);
<span class="hljs-keyword">const</span> querystring = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;querystring&#x27;</span>);
<span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;url&#x27;</span>);`}}),Ze=new U({props:{code:"Y2xhc3MlMjBNeUNsYXNzaWZpY2F0aW9uUGlwZWxpbmUlMjAlN0IlMEElMjA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TIwJTdEKSUzQiUwQSUyMCUyMCUyMCUyMCU3RCUwQSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMHRoaXMuaW5zdGFuY2UlM0IlMEElMjAlMjAlN0QlMEElN0Q=",highlighted:`<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyClassificationPipeline</span> {
  <span class="hljs-keyword">static</span> task = <span class="hljs-string">&#x27;text-classification&#x27;</span>;
  <span class="hljs-keyword">static</span> model = <span class="hljs-string">&#x27;Xenova/distilbert-base-uncased-finetuned-sst-2-english&#x27;</span>;
  <span class="hljs-keyword">static</span> instance = <span class="hljs-literal">null</span>;

  <span class="hljs-keyword">static</span> <span class="hljs-keyword">async</span> <span class="hljs-title function_">getInstance</span>(<span class="hljs-params">progress_callback = <span class="hljs-literal">null</span></span>) {
    <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">instance</span> === <span class="hljs-literal">null</span>) {
      <span class="hljs-comment">// Dynamically import the Transformers.js library</span>
      <span class="hljs-keyword">let</span> { pipeline, env } = <span class="hljs-keyword">await</span> <span class="hljs-title function_">import</span>(<span class="hljs-string">&#x27;@xenova/transformers&#x27;</span>);

      <span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> Uncomment this to change the cache directory</span>
      <span class="hljs-comment">// env.cacheDir = &#x27;./.cache&#x27;;</span>

      <span class="hljs-variable language_">this</span>.<span class="hljs-property">instance</span> = <span class="hljs-title function_">pipeline</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">task</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>, { progress_callback });
    }

    <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">instance</span>;
  }
}`}}),Re=new D({}),We=new U({props:{code:"JTJGJTJGJTIwRGVmaW5lJTIwdGhlJTIwSFRUUCUyMHNlcnZlciUwQWNvbnN0JTIwc2VydmVyJTIwJTNEJTIwaHR0cC5jcmVhdGVTZXJ2ZXIoKSUzQiUwQWN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hc3NpZmllcih0ZXh0KSUzQiUwQSUyMCUyMCUyMCUyMHJlcy5zdGF0dXNDb2RlJTIwJTNEJTIwMjAwJTNCJTBBJTIwJTIwJTdEJTIwZWxzZSUyMCU3QiUwQSUyMCUyMCUyMCUyMHJlc3BvbnNlJTIwJTNEJTIwJTdCJTIwJ2Vycm9yJyUzQSUyMCdCYWQlMjByZXF1ZXN0JyUyMCU3RCUwQSUyMCUyMCUyMCUyMHJlcy5zdGF0dXNDb2RlJTIwJTNEJTIwNDAwJTNCJTBBJTIwJTIwJTdEJTBBJTBBJTIwJTIwJTJGJTJGJTIwU2VuZCUyMHRoZSUyMEpTT04lMjByZXNwb25zZSUwQSUyMCUyMHJlcy5lbmQoSlNPTi5zdHJpbmdpZnkocmVzcG9uc2UpKSUzQiUwQSU3RCklM0IlMEElMEFzZXJ2ZXIubGlzdGVuKHBvcnQlMkMlMjBob3N0bmFtZSUyQyUyMCgpJTIwJTNEJTNFJTIwJTdCJTBBJTIwJTIwY29uc29sZS5sb2coJTYwU2VydmVyJTIwcnVubmluZyUyMGF0JTIwaHR0cCUzQSUyRiUyRiUyNCU3Qmhvc3RuYW1lJTdEJTNBJTI0JTdCcG9ydCU3RCUyRiU2MCklM0IlMEElN0QpJTNCJTBB",highlighted:`<span class="hljs-comment">// Define the HTTP server</span>
<span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>();
<span class="hljs-keyword">const</span> hostname = <span class="hljs-string">&#x27;127.0.0.1&#x27;</span>;
<span class="hljs-keyword">const</span> port = <span class="hljs-number">3000</span>;

<span class="hljs-comment">// Listen for requests made to the server</span>
server.<span class="hljs-title function_">on</span>(<span class="hljs-string">&#x27;request&#x27;</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-comment">// Parse the request URL</span>
  <span class="hljs-keyword">const</span> parsedUrl = url.<span class="hljs-title function_">parse</span>(req.<span class="hljs-property">url</span>);

  <span class="hljs-comment">// Extract the query parameters</span>
  <span class="hljs-keyword">const</span> { text } = querystring.<span class="hljs-title function_">parse</span>(parsedUrl.<span class="hljs-property">query</span>);

  <span class="hljs-comment">// Set the response headers</span>
  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">&#x27;Content-Type&#x27;</span>, <span class="hljs-string">&#x27;application/json&#x27;</span>);

  <span class="hljs-keyword">let</span> response;
  <span class="hljs-keyword">if</span> (parsedUrl.<span class="hljs-property">pathname</span> === <span class="hljs-string">&#x27;/classify&#x27;</span> &amp;&amp; text) {
    <span class="hljs-keyword">const</span> classifier = <span class="hljs-keyword">await</span> <span class="hljs-title class_">MyClassificationPipeline</span>.<span class="hljs-title function_">getInstance</span>();
    response = <span class="hljs-keyword">await</span> <span class="hljs-title function_">classifier</span>(text);
    res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">200</span>;
  } <span class="hljs-keyword">else</span> {
    response = { <span class="hljs-string">&#x27;error&#x27;</span>: <span class="hljs-string">&#x27;Bad request&#x27;</span> }
    res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">400</span>;
  }

  <span class="hljs-comment">// Send the JSON response</span>
  res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(response));
});

server.<span class="hljs-title function_">listen</span>(port, hostname, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">\`Server running at http://<span class="hljs-subst">\${hostname}</span>:<span class="hljs-subst">\${port}</span>/\`</span>);
});
`}}),ce=new zo({props:{$$slots:{default:[Do]},$$scope:{ctx:ls}}}),ze=new U({props:{code:"bm9kZSUyMGFwcC5qcw==",highlighted:"node app.js"}}),Qe=new U({props:{code:"JTdCJTIyZXJyb3IlMjIlM0ElMjJCYWQlMjByZXF1ZXN0JTIyJTdE",highlighted:'<span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;error&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;Bad request&quot;</span><span class="hljs-punctuation">}</span>'}}),He=new U({props:{code:"JTVCJTdCJTIybGFiZWwlMjIlM0ElMjJQT1NJVElWRSUyMiUyQyUyMnNjb3JlJTIyJTNBMC45OTk2NzIxMTQ4NDkwOTA2JTdEJTVE",highlighted:'<span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;label&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;POSITIVE&quot;</span><span class="hljs-punctuation">,</span><span class="hljs-attr">&quot;score&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">0.9996721148490906</span><span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span>'}}),Ve=new D({}),Fe=new D({}),qe=new U({props:{code:"ZW52LmNhY2hlRGlyJTIwJTNEJTIwJy4lMkYuY2FjaGUnJTNC",highlighted:'env.<span class="hljs-property">cacheDir</span> = <span class="hljs-string">&#x27;./.cache&#x27;</span>;'}}),De=new D({}),Oe=new U({props:{code:"JTJGJTJGJTIwU3BlY2lmeSUyMGElMjBjdXN0b20lMjBsb2NhdGlvbiUyMGZvciUyMG1vZGVscyUyMChkZWZhdWx0cyUyMHRvJTIwJyUyRm1vZGVscyUyRicpLiUwQWVudi5sb2NhbE1vZGVsUGF0aCUyMCUzRCUyMCclMkZwYXRoJTJGdG8lMkZtb2RlbHMlMkYnJTNC",highlighted:`<span class="hljs-comment">// Specify a custom location for models (defaults to &#x27;/models/&#x27;).</span>
env.<span class="hljs-property">localModelPath</span> = <span class="hljs-string">&#x27;/path/to/models/&#x27;</span>;`}}),Le=new U({props:{code:"JTJGJTJGJTIwRGlzYWJsZSUyMHRoZSUyMGxvYWRpbmclMjBvZiUyMHJlbW90ZSUyMG1vZGVscyUyMGZyb20lMjB0aGUlMjBIdWdnaW5nJTIwRmFjZSUyMEh1YiUzQSUwQWVudi5hbGxvd1JlbW90ZU1vZGVscyUyMCUzRCUyMGZhbHNlJTNC",highlighted:`<span class="hljs-comment">// Disable the loading of remote models from the Hugging Face Hub:</span>
env.<span class="hljs-property">allowRemoteModels</span> = <span class="hljs-literal">false</span>;`}}),{c(){u=n("meta"),I=h(),v=n("h1"),E=n("a"),C=n("span"),d(T.$$.fragment),b=h(),$=n("span"),J=t("Server-side Inference in Node.js"),g=h(),Y=n("p"),dt=t("Although Transformers.js was originally designed to be used in the browser, it\u2019s also able to run inference on the server. In this tutorial, we will design a simple Node.js API that uses Transformers.js for sentiment analysis."),ol=h(),ts=n("p"),mt=t("We\u2019ll also show you how to use the library in both CommonJS and ECMAScript modules, so you can choose the module system that works best for your project:"),rl=h(),O=n("ul"),G=n("li"),as=n("a"),yt=t("ECMAScript modules (ESM)"),wt=t(` - The official standard format
to package JavaScript code for reuse. It\u2019s the default module system in modern
browsers, with modules imported using `),ws=n("code"),Mt=t("import"),jt=t(" and exported using "),Ms=n("code"),vt=t("export"),Jt=t(`.
Fortunately, starting with version 13.2.0, Node.js has stable support of ES modules.`),Tt=h(),x=n("li"),ns=n("a"),bt=t("CommonJS"),Et=t(` - The default module system in Node.js. In this system,
modules are imported using `),js=n("code"),Ut=t("require()"),Ct=t(" and exported using "),vs=n("code"),$t=t("module.exports"),gt=t("."),il=h(),d(L.$$.fragment),pl=h(),os=n("p"),Js=n("strong"),It=t("Useful links:"),cl=h(),K=n("ul"),W=n("li"),Nt=t("Source code ("),Je=n("a"),_t=t("ESM"),kt=t(" or "),Te=n("a"),St=t("CommonJS"),At=t(")"),Gt=h(),Ts=n("li"),be=n("a"),xt=t("Documentation"),hl=h(),z=n("h2"),ee=n("a"),bs=n("span"),d(Ee.$$.fragment),Bt=h(),Es=n("span"),Zt=t("Prerequisites"),fl=h(),se=n("ul"),rs=n("li"),Ue=n("a"),Rt=t("Node.js"),Yt=t(" version 16+"),Wt=h(),is=n("li"),Ce=n("a"),zt=t("npm"),Xt=t(" version 7+"),ul=h(),X=n("h2"),le=n("a"),Us=n("span"),d($e.$$.fragment),Qt=h(),Cs=n("span"),Pt=t("Getting started"),dl=h(),te=n("p"),Ht=t("Let\u2019s start by creating a new Node.js project and installing Transformers.js via "),ge=n("a"),Vt=t("NPM"),Ft=t(":"),ml=h(),d(Ie.$$.fragment),yl=h(),N=n("p"),qt=t("Next, create a new file called "),$s=n("code"),Dt=t("app.js"),Ot=t(", which will be the entry point for our application. Depending on whether you\u2019re using "),ps=n("a"),Lt=t("ECMAScript modules"),Kt=t(" or "),cs=n("a"),ea=t("CommonJS"),sa=t(", you will need to do some things differently (see below)."),wl=h(),_=n("p"),la=t("We\u2019ll also create a helper class called "),gs=n("code"),ta=t("MyClassificationPipeline"),aa=t(" control the loading of the pipeline. It uses the "),Ne=n("a"),na=t("singleton pattern"),oa=t(" to lazily create a single instance of the pipeline when "),Is=n("code"),ra=t("getInstance"),ia=t(" is first called, and uses this pipeline for all subsequent calls:"),Ml=h(),Q=n("h3"),ae=n("a"),Ns=n("span"),d(_e.$$.fragment),pa=h(),_s=n("span"),ca=t("ECMAScript modules (ESM)"),jl=h(),B=n("p"),ha=t("To indicate that your project uses ECMAScript modules, you need to add "),ks=n("code"),fa=t('"type": "module"'),ua=t(" to your "),Ss=n("code"),da=t("package.json"),ma=t(":"),vl=h(),d(ke.$$.fragment),Jl=h(),ne=n("p"),ya=t("Next, you will need to add the following imports to the top of "),As=n("code"),wa=t("app.js"),Ma=t(":"),Tl=h(),d(Se.$$.fragment),bl=h(),oe=n("p"),ja=t("Following that, let\u2019s import Transformers.js and define the "),Gs=n("code"),va=t("MyClassificationPipeline"),Ja=t(" class."),El=h(),d(Ae.$$.fragment),Ul=h(),P=n("h3"),re=n("a"),xs=n("span"),d(Ge.$$.fragment),Ta=h(),Bs=n("span"),ba=t("CommonJS"),Cl=h(),ie=n("p"),Ea=t("Start by adding the following imports to the top of "),Zs=n("code"),Ua=t("app.js"),Ca=t(":"),$l=h(),d(xe.$$.fragment),gl=h(),Z=n("p"),$a=t("Following that, let\u2019s import Transformers.js and define the "),Rs=n("code"),ga=t("MyClassificationPipeline"),Ia=t(" class. Since Transformers.js is an ESM module, we will need to dynamically import the library using the "),Be=n("a"),Ys=n("code"),Na=t("import()"),_a=t(" function:"),Il=h(),d(Ze.$$.fragment),Nl=h(),H=n("h2"),pe=n("a"),Ws=n("span"),d(Re.$$.fragment),ka=h(),zs=n("span"),Sa=t("Creating a basic HTTP server"),_l=h(),k=n("p"),Aa=t("Next, let\u2019s create a basic server with the built-in "),Ye=n("a"),Ga=t("HTTP"),xa=t(" module. We will listen for requests made to the server (using the "),Xs=n("code"),Ba=t("/classify"),Za=t(" endpoint), extract the "),Qs=n("code"),Ra=t("text"),Ya=t(" query parameter, and run this through the pipeline."),kl=h(),d(We.$$.fragment),Sl=h(),d(ce.$$.fragment),Al=h(),hs=n("p"),Wa=t("To start the server, run the following command:"),Gl=h(),d(ze.$$.fragment),xl=h(),he=n("p"),za=t("The server should be live at "),Xe=n("a"),Xa=t("http://127.0.0.1:3000/"),Qa=t(", which you can visit in your web browser. You should see the following message:"),Bl=h(),d(Qe.$$.fragment),Zl=h(),S=n("p"),Pa=t("This is because we aren\u2019t targeting the "),Ps=n("code"),Ha=t("/classify"),Va=t(" endpoint with a valid "),Hs=n("code"),Fa=t("text"),qa=t(" query parameter. Let\u2019s try again, this time with a valid request. For example, you can visit "),Pe=n("a"),Da=t("http://127.0.0.1:3000/classify?text=I%20love%20Transformers.js"),Oa=t(" and you should see:"),Rl=h(),d(He.$$.fragment),Yl=h(),fs=n("p"),La=t("Great! We\u2019ve successfully created a basic HTTP server that uses Transformers.js to classify text."),Wl=h(),V=n("h2"),fe=n("a"),Vs=n("span"),d(Ve.$$.fragment),Ka=h(),Fs=n("span"),en=t("(Optional) Customization"),zl=h(),F=n("h3"),ue=n("a"),qs=n("span"),d(Fe.$$.fragment),sn=h(),Ds=n("span"),ln=t("Model caching"),Xl=h(),A=n("p"),tn=t("By default, the first time you run the application, it will download the model files and cache them on your file system (in "),Os=n("code"),an=t("./node_modules/@xenova/transformers/.cache/"),nn=t("). All subsequent requests will then use this model. You can change the location of the cache by setting "),Ls=n("code"),on=t("env.cacheDir"),rn=t(". For example, to cache the model in the "),Ks=n("code"),pn=t(".cache"),cn=t(" directory in the current working directory, you can add:"),Ql=h(),d(qe.$$.fragment),Pl=h(),q=n("h3"),de=n("a"),el=n("span"),d(De.$$.fragment),hn=h(),sl=n("span"),fn=t("Use local models"),Hl=h(),me=n("p"),un=t("If you want to use local model files, you can set "),ll=n("code"),dn=t("env.localModelPath"),mn=t(" as follows:"),Vl=h(),d(Oe.$$.fragment),Fl=h(),R=n("p"),yn=t("You can also disable loading of remote models by setting "),tl=n("code"),wn=t("env.allowRemoteModels"),Mn=t(" to "),al=n("code"),jn=t("false"),vn=t(":"),ql=h(),d(Le.$$.fragment),this.h()},l(e){const i=Ho('[data-svelte="svelte-1phssyn"]',document.head);u=o(i,"META",{name:!0,content:!0}),i.forEach(l),I=f(e),v=o(e,"H1",{class:!0});var Ke=r(v);E=o(Ke,"A",{id:!0,class:!0,href:!0});var nl=r(E);C=o(nl,"SPAN",{});var bn=r(C);m(T.$$.fragment,bn),bn.forEach(l),nl.forEach(l),b=f(Ke),$=o(Ke,"SPAN",{});var En=r($);J=a(En,"Server-side Inference in Node.js"),En.forEach(l),Ke.forEach(l),g=f(e),Y=o(e,"P",{});var Un=r(Y);dt=a(Un,"Although Transformers.js was originally designed to be used in the browser, it\u2019s also able to run inference on the server. In this tutorial, we will design a simple Node.js API that uses Transformers.js for sentiment analysis."),Un.forEach(l),ol=f(e),ts=o(e,"P",{});var Cn=r(ts);mt=a(Cn,"We\u2019ll also show you how to use the library in both CommonJS and ECMAScript modules, so you can choose the module system that works best for your project:"),Cn.forEach(l),rl=f(e),O=o(e,"UL",{});var Ol=r(O);G=o(Ol,"LI",{});var es=r(G);as=o(es,"A",{href:!0});var $n=r(as);yt=a($n,"ECMAScript modules (ESM)"),$n.forEach(l),wt=a(es,` - The official standard format
to package JavaScript code for reuse. It\u2019s the default module system in modern
browsers, with modules imported using `),ws=o(es,"CODE",{});var gn=r(ws);Mt=a(gn,"import"),gn.forEach(l),jt=a(es," and exported using "),Ms=o(es,"CODE",{});var In=r(Ms);vt=a(In,"export"),In.forEach(l),Jt=a(es,`.
Fortunately, starting with version 13.2.0, Node.js has stable support of ES modules.`),es.forEach(l),Tt=f(Ol),x=o(Ol,"LI",{});var ss=r(x);ns=o(ss,"A",{href:!0});var Nn=r(ns);bt=a(Nn,"CommonJS"),Nn.forEach(l),Et=a(ss,` - The default module system in Node.js. In this system,
modules are imported using `),js=o(ss,"CODE",{});var _n=r(js);Ut=a(_n,"require()"),_n.forEach(l),Ct=a(ss," and exported using "),vs=o(ss,"CODE",{});var kn=r(vs);$t=a(kn,"module.exports"),kn.forEach(l),gt=a(ss,"."),ss.forEach(l),Ol.forEach(l),il=f(e),m(L.$$.fragment,e),pl=f(e),os=o(e,"P",{});var Sn=r(os);Js=o(Sn,"STRONG",{});var An=r(Js);It=a(An,"Useful links:"),An.forEach(l),Sn.forEach(l),cl=f(e),K=o(e,"UL",{});var Ll=r(K);W=o(Ll,"LI",{});var us=r(W);Nt=a(us,"Source code ("),Je=o(us,"A",{href:!0,rel:!0});var Gn=r(Je);_t=a(Gn,"ESM"),Gn.forEach(l),kt=a(us," or "),Te=o(us,"A",{href:!0,rel:!0});var xn=r(Te);St=a(xn,"CommonJS"),xn.forEach(l),At=a(us,")"),us.forEach(l),Gt=f(Ll),Ts=o(Ll,"LI",{});var Bn=r(Ts);be=o(Bn,"A",{href:!0,rel:!0});var Zn=r(be);xt=a(Zn,"Documentation"),Zn.forEach(l),Bn.forEach(l),Ll.forEach(l),hl=f(e),z=o(e,"H2",{class:!0});var Kl=r(z);ee=o(Kl,"A",{id:!0,class:!0,href:!0});var Rn=r(ee);bs=o(Rn,"SPAN",{});var Yn=r(bs);m(Ee.$$.fragment,Yn),Yn.forEach(l),Rn.forEach(l),Bt=f(Kl),Es=o(Kl,"SPAN",{});var Wn=r(Es);Zt=a(Wn,"Prerequisites"),Wn.forEach(l),Kl.forEach(l),fl=f(e),se=o(e,"UL",{});var et=r(se);rs=o(et,"LI",{});var Jn=r(rs);Ue=o(Jn,"A",{href:!0,rel:!0});var zn=r(Ue);Rt=a(zn,"Node.js"),zn.forEach(l),Yt=a(Jn," version 16+"),Jn.forEach(l),Wt=f(et),is=o(et,"LI",{});var Tn=r(is);Ce=o(Tn,"A",{href:!0,rel:!0});var Xn=r(Ce);zt=a(Xn,"npm"),Xn.forEach(l),Xt=a(Tn," version 7+"),Tn.forEach(l),et.forEach(l),ul=f(e),X=o(e,"H2",{class:!0});var st=r(X);le=o(st,"A",{id:!0,class:!0,href:!0});var Qn=r(le);Us=o(Qn,"SPAN",{});var Pn=r(Us);m($e.$$.fragment,Pn),Pn.forEach(l),Qn.forEach(l),Qt=f(st),Cs=o(st,"SPAN",{});var Hn=r(Cs);Pt=a(Hn,"Getting started"),Hn.forEach(l),st.forEach(l),dl=f(e),te=o(e,"P",{});var lt=r(te);Ht=a(lt,"Let\u2019s start by creating a new Node.js project and installing Transformers.js via "),ge=o(lt,"A",{href:!0,rel:!0});var Vn=r(ge);Vt=a(Vn,"NPM"),Vn.forEach(l),Ft=a(lt,":"),lt.forEach(l),ml=f(e),m(Ie.$$.fragment,e),yl=f(e),N=o(e,"P",{});var ye=r(N);qt=a(ye,"Next, create a new file called "),$s=o(ye,"CODE",{});var Fn=r($s);Dt=a(Fn,"app.js"),Fn.forEach(l),Ot=a(ye,", which will be the entry point for our application. Depending on whether you\u2019re using "),ps=o(ye,"A",{href:!0});var qn=r(ps);Lt=a(qn,"ECMAScript modules"),qn.forEach(l),Kt=a(ye," or "),cs=o(ye,"A",{href:!0});var Dn=r(cs);ea=a(Dn,"CommonJS"),Dn.forEach(l),sa=a(ye,", you will need to do some things differently (see below)."),ye.forEach(l),wl=f(e),_=o(e,"P",{});var we=r(_);la=a(we,"We\u2019ll also create a helper class called "),gs=o(we,"CODE",{});var On=r(gs);ta=a(On,"MyClassificationPipeline"),On.forEach(l),aa=a(we," control the loading of the pipeline. It uses the "),Ne=o(we,"A",{href:!0,rel:!0});var Ln=r(Ne);na=a(Ln,"singleton pattern"),Ln.forEach(l),oa=a(we," to lazily create a single instance of the pipeline when "),Is=o(we,"CODE",{});var Kn=r(Is);ra=a(Kn,"getInstance"),Kn.forEach(l),ia=a(we," is first called, and uses this pipeline for all subsequent calls:"),we.forEach(l),Ml=f(e),Q=o(e,"H3",{class:!0});var tt=r(Q);ae=o(tt,"A",{id:!0,class:!0,href:!0});var eo=r(ae);Ns=o(eo,"SPAN",{});var so=r(Ns);m(_e.$$.fragment,so),so.forEach(l),eo.forEach(l),pa=f(tt),_s=o(tt,"SPAN",{});var lo=r(_s);ca=a(lo,"ECMAScript modules (ESM)"),lo.forEach(l),tt.forEach(l),jl=f(e),B=o(e,"P",{});var ds=r(B);ha=a(ds,"To indicate that your project uses ECMAScript modules, you need to add "),ks=o(ds,"CODE",{});var to=r(ks);fa=a(to,'"type": "module"'),to.forEach(l),ua=a(ds," to your "),Ss=o(ds,"CODE",{});var ao=r(Ss);da=a(ao,"package.json"),ao.forEach(l),ma=a(ds,":"),ds.forEach(l),vl=f(e),m(ke.$$.fragment,e),Jl=f(e),ne=o(e,"P",{});var at=r(ne);ya=a(at,"Next, you will need to add the following imports to the top of "),As=o(at,"CODE",{});var no=r(As);wa=a(no,"app.js"),no.forEach(l),Ma=a(at,":"),at.forEach(l),Tl=f(e),m(Se.$$.fragment,e),bl=f(e),oe=o(e,"P",{});var nt=r(oe);ja=a(nt,"Following that, let\u2019s import Transformers.js and define the "),Gs=o(nt,"CODE",{});var oo=r(Gs);va=a(oo,"MyClassificationPipeline"),oo.forEach(l),Ja=a(nt," class."),nt.forEach(l),El=f(e),m(Ae.$$.fragment,e),Ul=f(e),P=o(e,"H3",{class:!0});var ot=r(P);re=o(ot,"A",{id:!0,class:!0,href:!0});var ro=r(re);xs=o(ro,"SPAN",{});var io=r(xs);m(Ge.$$.fragment,io),io.forEach(l),ro.forEach(l),Ta=f(ot),Bs=o(ot,"SPAN",{});var po=r(Bs);ba=a(po,"CommonJS"),po.forEach(l),ot.forEach(l),Cl=f(e),ie=o(e,"P",{});var rt=r(ie);Ea=a(rt,"Start by adding the following imports to the top of "),Zs=o(rt,"CODE",{});var co=r(Zs);Ua=a(co,"app.js"),co.forEach(l),Ca=a(rt,":"),rt.forEach(l),$l=f(e),m(xe.$$.fragment,e),gl=f(e),Z=o(e,"P",{});var ms=r(Z);$a=a(ms,"Following that, let\u2019s import Transformers.js and define the "),Rs=o(ms,"CODE",{});var ho=r(Rs);ga=a(ho,"MyClassificationPipeline"),ho.forEach(l),Ia=a(ms," class. Since Transformers.js is an ESM module, we will need to dynamically import the library using the "),Be=o(ms,"A",{href:!0,rel:!0});var fo=r(Be);Ys=o(fo,"CODE",{});var uo=r(Ys);Na=a(uo,"import()"),uo.forEach(l),fo.forEach(l),_a=a(ms," function:"),ms.forEach(l),Il=f(e),m(Ze.$$.fragment,e),Nl=f(e),H=o(e,"H2",{class:!0});var it=r(H);pe=o(it,"A",{id:!0,class:!0,href:!0});var mo=r(pe);Ws=o(mo,"SPAN",{});var yo=r(Ws);m(Re.$$.fragment,yo),yo.forEach(l),mo.forEach(l),ka=f(it),zs=o(it,"SPAN",{});var wo=r(zs);Sa=a(wo,"Creating a basic HTTP server"),wo.forEach(l),it.forEach(l),_l=f(e),k=o(e,"P",{});var Me=r(k);Aa=a(Me,"Next, let\u2019s create a basic server with the built-in "),Ye=o(Me,"A",{href:!0,rel:!0});var Mo=r(Ye);Ga=a(Mo,"HTTP"),Mo.forEach(l),xa=a(Me," module. We will listen for requests made to the server (using the "),Xs=o(Me,"CODE",{});var jo=r(Xs);Ba=a(jo,"/classify"),jo.forEach(l),Za=a(Me," endpoint), extract the "),Qs=o(Me,"CODE",{});var vo=r(Qs);Ra=a(vo,"text"),vo.forEach(l),Ya=a(Me," query parameter, and run this through the pipeline."),Me.forEach(l),kl=f(e),m(We.$$.fragment,e),Sl=f(e),m(ce.$$.fragment,e),Al=f(e),hs=o(e,"P",{});var Jo=r(hs);Wa=a(Jo,"To start the server, run the following command:"),Jo.forEach(l),Gl=f(e),m(ze.$$.fragment,e),xl=f(e),he=o(e,"P",{});var pt=r(he);za=a(pt,"The server should be live at "),Xe=o(pt,"A",{href:!0,rel:!0});var To=r(Xe);Xa=a(To,"http://127.0.0.1:3000/"),To.forEach(l),Qa=a(pt,", which you can visit in your web browser. You should see the following message:"),pt.forEach(l),Bl=f(e),m(Qe.$$.fragment,e),Zl=f(e),S=o(e,"P",{});var je=r(S);Pa=a(je,"This is because we aren\u2019t targeting the "),Ps=o(je,"CODE",{});var bo=r(Ps);Ha=a(bo,"/classify"),bo.forEach(l),Va=a(je," endpoint with a valid "),Hs=o(je,"CODE",{});var Eo=r(Hs);Fa=a(Eo,"text"),Eo.forEach(l),qa=a(je," query parameter. Let\u2019s try again, this time with a valid request. For example, you can visit "),Pe=o(je,"A",{href:!0,rel:!0});var Uo=r(Pe);Da=a(Uo,"http://127.0.0.1:3000/classify?text=I%20love%20Transformers.js"),Uo.forEach(l),Oa=a(je," and you should see:"),je.forEach(l),Rl=f(e),m(He.$$.fragment,e),Yl=f(e),fs=o(e,"P",{});var Co=r(fs);La=a(Co,"Great! We\u2019ve successfully created a basic HTTP server that uses Transformers.js to classify text."),Co.forEach(l),Wl=f(e),V=o(e,"H2",{class:!0});var ct=r(V);fe=o(ct,"A",{id:!0,class:!0,href:!0});var $o=r(fe);Vs=o($o,"SPAN",{});var go=r(Vs);m(Ve.$$.fragment,go),go.forEach(l),$o.forEach(l),Ka=f(ct),Fs=o(ct,"SPAN",{});var Io=r(Fs);en=a(Io,"(Optional) Customization"),Io.forEach(l),ct.forEach(l),zl=f(e),F=o(e,"H3",{class:!0});var ht=r(F);ue=o(ht,"A",{id:!0,class:!0,href:!0});var No=r(ue);qs=o(No,"SPAN",{});var _o=r(qs);m(Fe.$$.fragment,_o),_o.forEach(l),No.forEach(l),sn=f(ht),Ds=o(ht,"SPAN",{});var ko=r(Ds);ln=a(ko,"Model caching"),ko.forEach(l),ht.forEach(l),Xl=f(e),A=o(e,"P",{});var ve=r(A);tn=a(ve,"By default, the first time you run the application, it will download the model files and cache them on your file system (in "),Os=o(ve,"CODE",{});var So=r(Os);an=a(So,"./node_modules/@xenova/transformers/.cache/"),So.forEach(l),nn=a(ve,"). All subsequent requests will then use this model. You can change the location of the cache by setting "),Ls=o(ve,"CODE",{});var Ao=r(Ls);on=a(Ao,"env.cacheDir"),Ao.forEach(l),rn=a(ve,". For example, to cache the model in the "),Ks=o(ve,"CODE",{});var Go=r(Ks);pn=a(Go,".cache"),Go.forEach(l),cn=a(ve," directory in the current working directory, you can add:"),ve.forEach(l),Ql=f(e),m(qe.$$.fragment,e),Pl=f(e),q=o(e,"H3",{class:!0});var ft=r(q);de=o(ft,"A",{id:!0,class:!0,href:!0});var xo=r(de);el=o(xo,"SPAN",{});var Bo=r(el);m(De.$$.fragment,Bo),Bo.forEach(l),xo.forEach(l),hn=f(ft),sl=o(ft,"SPAN",{});var Zo=r(sl);fn=a(Zo,"Use local models"),Zo.forEach(l),ft.forEach(l),Hl=f(e),me=o(e,"P",{});var ut=r(me);un=a(ut,"If you want to use local model files, you can set "),ll=o(ut,"CODE",{});var Ro=r(ll);dn=a(Ro,"env.localModelPath"),Ro.forEach(l),mn=a(ut," as follows:"),ut.forEach(l),Vl=f(e),m(Oe.$$.fragment,e),Fl=f(e),R=o(e,"P",{});var ys=r(R);yn=a(ys,"You can also disable loading of remote models by setting "),tl=o(ys,"CODE",{});var Yo=r(tl);wn=a(Yo,"env.allowRemoteModels"),Yo.forEach(l),Mn=a(ys," to "),al=o(ys,"CODE",{});var Wo=r(al);jn=a(Wo,"false"),Wo.forEach(l),vn=a(ys,":"),ys.forEach(l),ql=f(e),m(Le.$$.fragment,e),this.h()},h(){c(u,"name","hf:doc:metadata"),c(u,"content",JSON.stringify(Lo)),c(E,"id","serverside-inference-in-nodejs"),c(E,"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"),c(E,"href","#serverside-inference-in-nodejs"),c(v,"class","relative group"),c(as,"href","#ecmascript-modules-esm"),c(ns,"href","#commonjs"),c(Je,"href","https://github.com/xenova/transformers.js/tree/main/examples/node/esm/app.js"),c(Je,"rel","nofollow"),c(Te,"href","https://github.com/xenova/transformers.js/tree/main/examples/node/commonjs/app.js"),c(Te,"rel","nofollow"),c(be,"href","https://huggingface.co/docs/transformers.js"),c(be,"rel","nofollow"),c(ee,"id","prerequisites"),c(ee,"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"),c(ee,"href","#prerequisites"),c(z,"class","relative group"),c(Ue,"href","https://nodejs.org/en/"),c(Ue,"rel","nofollow"),c(Ce,"href","https://www.npmjs.com/"),c(Ce,"rel","nofollow"),c(le,"id","getting-started"),c(le,"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"),c(le,"href","#getting-started"),c(X,"class","relative group"),c(ge,"href","https://www.npmjs.com/package/@xenova/transformers"),c(ge,"rel","nofollow"),c(ps,"href","#ecmascript-modules-esm"),c(cs,"href","#commonjs"),c(Ne,"href","https://en.wikipedia.org/wiki/Singleton_pattern"),c(Ne,"rel","nofollow"),c(ae,"id","ecmascript-modules-esm"),c(ae,"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"),c(ae,"href","#ecmascript-modules-esm"),c(Q,"class","relative group"),c(re,"id","commonjs"),c(re,"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"),c(re,"href","#commonjs"),c(P,"class","relative group"),c(Be,"href","https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import"),c(Be,"rel","nofollow"),c(pe,"id","creating-a-basic-http-server"),c(pe,"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"),c(pe,"href","#creating-a-basic-http-server"),c(H,"class","relative group"),c(Ye,"href","https://nodejs.org/api/http.html#http"),c(Ye,"rel","nofollow"),c(Xe,"href","http://127.0.0.1:3000/"),c(Xe,"rel","nofollow"),c(Pe,"href","http://127.0.0.1:3000/classify?text=I%20love%20Transformers.js"),c(Pe,"rel","nofollow"),c(fe,"id","optional-customization"),c(fe,"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"),c(fe,"href","#optional-customization"),c(V,"class","relative group"),c(ue,"id","model-caching"),c(ue,"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"),c(ue,"href","#model-caching"),c(F,"class","relative group"),c(de,"id","use-local-models"),c(de,"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"),c(de,"href","#use-local-models"),c(q,"class","relative group")},m(e,i){s(document.head,u),p(e,I,i),p(e,v,i),s(v,E),s(E,C),y(T,C,null),s(v,b),s(v,$),s($,J),p(e,g,i),p(e,Y,i),s(Y,dt),p(e,ol,i),p(e,ts,i),s(ts,mt),p(e,rl,i),p(e,O,i),s(O,G),s(G,as),s(as,yt),s(G,wt),s(G,ws),s(ws,Mt),s(G,jt),s(G,Ms),s(Ms,vt),s(G,Jt),s(O,Tt),s(O,x),s(x,ns),s(ns,bt),s(x,Et),s(x,js),s(js,Ut),s(x,Ct),s(x,vs),s(vs,$t),s(x,gt),p(e,il,i),y(L,e,i),p(e,pl,i),p(e,os,i),s(os,Js),s(Js,It),p(e,cl,i),p(e,K,i),s(K,W),s(W,Nt),s(W,Je),s(Je,_t),s(W,kt),s(W,Te),s(Te,St),s(W,At),s(K,Gt),s(K,Ts),s(Ts,be),s(be,xt),p(e,hl,i),p(e,z,i),s(z,ee),s(ee,bs),y(Ee,bs,null),s(z,Bt),s(z,Es),s(Es,Zt),p(e,fl,i),p(e,se,i),s(se,rs),s(rs,Ue),s(Ue,Rt),s(rs,Yt),s(se,Wt),s(se,is),s(is,Ce),s(Ce,zt),s(is,Xt),p(e,ul,i),p(e,X,i),s(X,le),s(le,Us),y($e,Us,null),s(X,Qt),s(X,Cs),s(Cs,Pt),p(e,dl,i),p(e,te,i),s(te,Ht),s(te,ge),s(ge,Vt),s(te,Ft),p(e,ml,i),y(Ie,e,i),p(e,yl,i),p(e,N,i),s(N,qt),s(N,$s),s($s,Dt),s(N,Ot),s(N,ps),s(ps,Lt),s(N,Kt),s(N,cs),s(cs,ea),s(N,sa),p(e,wl,i),p(e,_,i),s(_,la),s(_,gs),s(gs,ta),s(_,aa),s(_,Ne),s(Ne,na),s(_,oa),s(_,Is),s(Is,ra),s(_,ia),p(e,Ml,i),p(e,Q,i),s(Q,ae),s(ae,Ns),y(_e,Ns,null),s(Q,pa),s(Q,_s),s(_s,ca),p(e,jl,i),p(e,B,i),s(B,ha),s(B,ks),s(ks,fa),s(B,ua),s(B,Ss),s(Ss,da),s(B,ma),p(e,vl,i),y(ke,e,i),p(e,Jl,i),p(e,ne,i),s(ne,ya),s(ne,As),s(As,wa),s(ne,Ma),p(e,Tl,i),y(Se,e,i),p(e,bl,i),p(e,oe,i),s(oe,ja),s(oe,Gs),s(Gs,va),s(oe,Ja),p(e,El,i),y(Ae,e,i),p(e,Ul,i),p(e,P,i),s(P,re),s(re,xs),y(Ge,xs,null),s(P,Ta),s(P,Bs),s(Bs,ba),p(e,Cl,i),p(e,ie,i),s(ie,Ea),s(ie,Zs),s(Zs,Ua),s(ie,Ca),p(e,$l,i),y(xe,e,i),p(e,gl,i),p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l=!0},p(e,[i]){const Ke={};i&2&&(Ke.$$scope={dirty:i,ctx:e}),L.$set(Ke);const nl={};i&2&&(nl.$$scope={dirty:i,ctx:e}),ce.$set(nl)},i(e){Dl||(w(T.$$.fragment,e),w(L.$$.fragment,e),w(Ee.$$.fragment,e),w($e.$$.fragment,e),w(Ie.$$.fragment,e),w(_e.$$.fragment,e),w(ke.$$.fragment,e),w(Se.$$.fragment,e),w(Ae.$$.fragment,e),w(Ge.$$.fragment,e),w(xe.$$.fragment,e),w(Ze.$$.fragment,e),w(Re.$$.fragment,e),w(We.$$.fragment,e),w(ce.$$.fragment,e),w(ze.$$.fragment,e),w(Qe.$$.fragment,e),w(He.$$.fragment,e),w(Ve.$$.fragment,e),w(Fe.$$.fragment,e),w(qe.$$.fragment,e),w(De.$$.fragment,e),w(Oe.$$.fragment,e),w(Le.$$.fragment,e),Dl=!0)},o(e){M(T.$$.fragment,e),M(L.$$.fragment,e),M(Ee.$$.fragment,e),M($e.$$.fragment,e),M(Ie.$$.fragment,e),M(_e.$$.fragment,e),M(ke.$$.fragment,e),M(Se.$$.fragment,e),M(Ae.$$.fragment,e),M(Ge.$$.fragment,e),M(xe.$$.fragment,e),M(Ze.$$.fragment,e),M(Re.$$.fragment,e),M(We.$$.fragment,e),M(ce.$$.fragment,e),M(ze.$$.fragment,e),M(Qe.$$.fragment,e),M(He.$$.fragment,e),M(Ve.$$.fragment,e),M(Fe.$$.fragment,e),M(qe.$$.fragment,e),M(De.$$.fragment,e),M(Oe.$$.fragment,e),M(Le.$$.fragment,e),Dl=!1},d(e){l(u),e&&l(I),e&&l(v),j(T),e&&l(g),e&&l(Y),e&&l(ol),e&&l(ts),e&&l(rl),e&&l(O),e&&l(il),j(L,e),e&&l(pl),e&&l(os),e&&l(cl),e&&l(K),e&&l(hl),e&&l(z),j(Ee),e&&l(fl),e&&l(se),e&&l(ul),e&&l(X),j($e),e&&l(dl),e&&l(te),e&&l(ml),j(Ie,e),e&&l(yl),e&&l(N),e&&l(wl),e&&l(_),e&&l(Ml),e&&l(Q),j(_e),e&&l(jl),e&&l(B),e&&l(vl),j(ke,e),e&&l(Jl),e&&l(ne),e&&l(Tl),j(Se,e),e&&l(bl),e&&l(oe),e&&l(El),j(Ae,e),e&&l(Ul),e&&l(P),j(Ge),e&&l(Cl),e&&l(ie),e&&l($l),j(xe,e),e&&l(gl),e&&l(Z),e&&l(Il),j(Ze,e),e&&l(Nl),e&&l(H),j(Re),e&&l(_l),e&&l(k),e&&l(kl),j(We,e),e&&l(Sl),j(ce,e),e&&l(Al),e&&l(hs),e&&l(Gl),j(ze,e),e&&l(xl),e&&l(he),e&&l(Bl),j(Qe,e),e&&l(Zl),e&&l(S),e&&l(Rl),j(He,e),e&&l(Yl),e&&l(fs),e&&l(Wl),e&&l(V),j(Ve),e&&l(zl),e&&l(F),j(Fe),e&&l(Xl),e&&l(A),e&&l(Ql),j(qe,e),e&&l(Pl),e&&l(q),j(De),e&&l(Hl),e&&l(me),e&&l(Vl),j(Oe,e),e&&l(Fl),e&&l(R),e&&l(ql),j(Le,e)}}}const Lo={local:"serverside-inference-in-nodejs",sections:[{local:"prerequisites",title:"Prerequisites"},{local:"getting-started",sections:[{local:"ecmascript-modules-esm",title:"ECMAScript modules (ESM)"},{local:"commonjs",title:"CommonJS"}],title:"Getting started"},{local:"creating-a-basic-http-server",title:"Creating a basic HTTP server"},{local:"optional-customization",sections:[{local:"model-caching",title:"Model caching"},{local:"use-local-models",title:"Use local models"}],title:"(Optional) Customization"}],title:"Server-side Inference in Node.js"};function Ko(ls){return Vo(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ar extends Xo{constructor(u){super();Qo(this,u,Ko,Oo,Po,{})}}export{ar as default,Lo as metadata};
