Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Pipelines pour l’inférence","local":"pipelines-pour-linférence","sections":[{"title":"Utilisation du pipeline","local":"utilisation-du-pipeline","sections":[],"depth":2},{"title":"Paramètres","local":"paramètres","sections":[{"title":"Device","local":"device","sections":[],"depth":3},{"title":"Batch size","local":"batch-size","sections":[],"depth":3},{"title":"Paramètres spécifiques à la tâche","local":"paramètres-spécifiques-à-la-tâche","sections":[],"depth":3}],"depth":2},{"title":"Utilisation des pipelines sur un ensemble de données","local":"utilisation-des-pipelines-sur-un-ensemble-de-données","sections":[],"depth":2},{"title":"Utilisation des pipelines pour un serveur web","local":"utilisation-des-pipelines-pour-un-serveur-web","sections":[],"depth":2},{"title":"Pipeline de vision","local":"pipeline-de-vision","sections":[],"depth":2},{"title":"Pipeline de texte","local":"pipeline-de-texte","sections":[],"depth":2},{"title":"Pipeline multimodal","local":"pipeline-multimodal","sections":[],"depth":2},{"title":"Utilisation de pipeline sur de grands modèles avec 🤗 accelerate :","local":"utilisation-de-pipeline-sur-de-grands-modèles-avec--accelerate-","sections":[],"depth":2},{"title":"Création de démonstrations web à partir de pipelines avec gradio","local":"création-de-démonstrations-web-à-partir-de-pipelines-avec-gradio","sections":[],"depth":2}],"depth":1}"> | |
| <link href="/docs/transformers/main/fr/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/entry/start.0c02efce.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/scheduler.987d3921.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/singletons.c72b8bca.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/index.42b1abb7.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/paths.75c06916.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/entry/app.29d06fb0.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/index.c8b1fed4.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/nodes/0.3b3961fe.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/nodes/8.0768cae9.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/Tip.6bc1e794.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/CodeBlock.18094d58.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/fr/_app/immutable/chunks/EditOnGithub.a3fde557.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Pipelines pour l’inférence","local":"pipelines-pour-linférence","sections":[{"title":"Utilisation du pipeline","local":"utilisation-du-pipeline","sections":[],"depth":2},{"title":"Paramètres","local":"paramètres","sections":[{"title":"Device","local":"device","sections":[],"depth":3},{"title":"Batch size","local":"batch-size","sections":[],"depth":3},{"title":"Paramètres spécifiques à la tâche","local":"paramètres-spécifiques-à-la-tâche","sections":[],"depth":3}],"depth":2},{"title":"Utilisation des pipelines sur un ensemble de données","local":"utilisation-des-pipelines-sur-un-ensemble-de-données","sections":[],"depth":2},{"title":"Utilisation des pipelines pour un serveur web","local":"utilisation-des-pipelines-pour-un-serveur-web","sections":[],"depth":2},{"title":"Pipeline de vision","local":"pipeline-de-vision","sections":[],"depth":2},{"title":"Pipeline de texte","local":"pipeline-de-texte","sections":[],"depth":2},{"title":"Pipeline multimodal","local":"pipeline-multimodal","sections":[],"depth":2},{"title":"Utilisation de pipeline sur de grands modèles avec 🤗 accelerate :","local":"utilisation-de-pipeline-sur-de-grands-modèles-avec--accelerate-","sections":[],"depth":2},{"title":"Création de démonstrations web à partir de pipelines avec gradio","local":"création-de-démonstrations-web-à-partir-de-pipelines-avec-gradio","sections":[],"depth":2}],"depth":1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <h1 class="relative group"><a id="pipelines-pour-linférence" 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" href="#pipelines-pour-linférence"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pipelines pour l’inférence</span></h1> <p data-svelte-h="svelte-h0w3ee">L’objet <code>pipeline()</code> rend simple l’utilisation de n’importe quel modèle du <a href="https://huggingface.co/models" rel="nofollow">Hub</a> pour l’inférence sur n’importe quelle langue, tâches de vision par ordinateur, d’audio et multimodales. Même si vous n’avez pas d’expérience avec une modalité spécifique ou si vous n’êtes pas familier avec le code ci-dessous des modèles, vous pouvez toujours les utiliser pour l’inférence avec la <code>pipeline()</code> ! Ce tutoriel vous apprendra à :</p> <ul data-svelte-h="svelte-10scum8"><li>Utiliser un <code>pipeline()</code> pour l’inférence.</li> <li>Utiliser un tokenizer ou modèle spécifique.</li> <li>Utiliser un <code>pipeline()</code> pour des tâches audio, de vision et multimodales.</li></ul> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p data-svelte-h="svelte-1bbt0n6">Consultez la documentation du <code>pipeline()</code> pour une liste complète des tâches prises en charge et des paramètres disponibles.</p></div> <h2 class="relative group"><a id="utilisation-du-pipeline" 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" href="#utilisation-du-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Utilisation du pipeline</span></h2> <p data-svelte-h="svelte-1u9jsz2">Bien que chaque tâche ait son propre <code>pipeline()</code>, il est plus simple d’utiliser le <code>pipeline()</code> générale qui inclut tous les pipelines spécifiques aux différentes tâches. Cette approche charge automatiquement un modèle par défaut et une classe de prétraitement adaptée à votre tâche, simplifiant ainsi votre utilisation. Prenons l’exemple de l’utilisation du <code>pipeline()</code> pour la reconnaissance automatique de la parole (ASR) ou de la transcription de la parole en texte.</p> <ol data-svelte-h="svelte-1arzf4j"><li>Commencez par créer un <code>pipeline()</code> et spécifiez la tâche d’inférence :</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline | |
| <span class="hljs-meta">>>> </span>transcriber = pipeline(task=<span class="hljs-string">"automatic-speech-recognition"</span>)<!-- HTML_TAG_END --></pre></div> <ol start="2" data-svelte-h="svelte-gnpjsb"><li>Passez votre entrée au <code>pipeline()</code>. Dans le cas de la reconnaissance vocale, il s’agit d’un fichier audio :</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>transcriber(<span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"</span>) | |
| {<span class="hljs-string">'text'</span>: <span class="hljs-string">'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP LIVE UP THE TRUE MEANING OF ITS TREES'</span>}<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1td4esp">Pas le résultat que vous aviez en tête ? Consultez certains des <a href="https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&sort=trending" rel="nofollow">modèles de reconnaissance vocale automatique les plus téléchargés</a> | |
| sur le Hub pour voir si vous pouvez obtenir une meilleure transcription.</p> <p data-svelte-h="svelte-xt9r0p">Essayons le modèle <a href="https://huggingface.co/openai/whisper-large" rel="nofollow">Whisper large-v2</a> de OpenAI. Whisper a été publié 2 ans après Wav2Vec2 et a été entraîné sur près de 10 fois plus de données. En tant que tel, il surpasse Wav2Vec2 sur la plupart des benchmarks en aval. Il a également l’avantage supplémentaire de prédire la ponctuation et la casse, ce qui n’est pas possible avec Wav2Vec2.</p> <p data-svelte-h="svelte-m5ompv">Essayons-le ici pour voir comment il fonctionne :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>transcriber = pipeline(model=<span class="hljs-string">"openai/whisper-large-v2"</span>) | |
| <span class="hljs-meta">>>> </span>transcriber(<span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"</span>) | |
| {<span class="hljs-string">'text'</span>: <span class="hljs-string">' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'</span>}<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-10s5tz3">Maintenant, ce résultat semble plus précis ! Pour une comparaison approfondie entre Wav2Vec2 et Whisper, consultez le <a href="https://huggingface.co/learn/audio-course/chapter5/asr_models" rel="nofollow">cours Audio Transformers</a>. | |
| Nous vous encourageons vraiment à consulter le Hub pour des modèles dans différentes langues, des modèles spécialisés dans votre domaine, et plus encore. | |
| Vous pouvez consulter et comparer les résultats des modèles directement depuis votre navigateur sur le Hub pour voir s’ils conviennent ou gèrent mieux les cas particuliers que d’autres. | |
| Et si vous ne trouvez pas de modèle pour votre cas d’utilisation, vous pouvez toujours commencer à <a href="training">entraîner</a> le vôtre !</p> <p data-svelte-h="svelte-19hcx9z">Si vous avez plusieurs entrées, vous pouvez passer votre entrée sous forme de liste :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->transcriber( | |
| [ | |
| <span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"</span>, | |
| <span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac"</span>, | |
| ] | |
| )<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1fq1d9s">Les pipelines sont excellents pour l’expérimentation car passer d’un modèle à un autre est trivial ; cependant, il existe des moyens de les optimiser pour des charges de travail plus importantes que l’expérimentation. Consultez les guides suivants qui expliquent comment itérer sur des ensembles de données complets ou utiliser des pipelines dans un serveur web : | |
| de la documentation :</p> <ul data-svelte-h="svelte-ajxopt"><li><a href="#using-pipelines-on-a-dataset">Utilisation des pipelines sur un ensemble de données</a></li> <li><a href="./pipeline_webserver">Utilisation des pipelines pour un serveur web</a></li></ul> <h2 class="relative group"><a id="paramètres" 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" href="#paramètres"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Paramètres</span></h2> <p data-svelte-h="svelte-14znczl"><code>pipeline()</code> prend en charge de nombreux paramètres ; certains sont spécifiques à la tâche et d’autres sont généraux pour tous les pipelines. | |
| En général, vous pouvez spécifier les paramètres où vous le souhaitez :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->transcriber = pipeline(model=<span class="hljs-string">"openai/whisper-large-v2"</span>, my_parameter=<span class="hljs-number">1</span>) | |
| out = transcriber(...) <span class="hljs-comment"># This will use `my_parameter=1`.</span> | |
| out = transcriber(..., my_parameter=<span class="hljs-number">2</span>) <span class="hljs-comment"># This will override and use `my_parameter=2`.</span> | |
| out = transcriber(...) <span class="hljs-comment"># This will go back to using `my_parameter=1`.</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-mi1z3p">Voyons 3 paramètres importants :</p> <h3 class="relative group"><a id="device" 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" href="#device"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Device</span></h3> <p data-svelte-h="svelte-1i17yb2">Si vous utilisez <code>device=n</code>, le pipeline met automatiquement le modèle sur l’appareil spécifié. | |
| Cela fonctionnera que vous utilisiez PyTorch ou Tensorflow.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->transcriber = pipeline(model=<span class="hljs-string">"openai/whisper-large-v2"</span>, device=<span class="hljs-number">0</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-tcmrd6">Si le modèle est trop grand pour un seul GPU et que vous utilisez PyTorch, vous pouvez définir <code>device_map="auto"</code> pour déterminer automatiquement comment charger et stocker les poids du modèle. L’utilisation de l’argument <code>device_map</code> nécessite le package 🤗 <a href="https://huggingface.co/docs/accelerate" rel="nofollow">Accelerate</a> :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->pip install --upgrade accelerate<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1xs45t9">Le code suivant charge et stocke automatiquement les poids du modèle sur plusieurs appareils :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->transcriber = pipeline(model=<span class="hljs-string">"openai/whisper-large-v2"</span>, device_map=<span class="hljs-string">"auto"</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-10m6e1b">Notez que si <code>device_map="auto"</code> est passé, il n’est pas nécessaire d’ajouter l’argument <code>device=device</code> lors de l’instanciation de votre <code>pipeline</code> car vous pourriez rencontrer des comportements inattendus !</p> <h3 class="relative group"><a id="batch-size" 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" href="#batch-size"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Batch size</span></h3> <p data-svelte-h="svelte-avukc5">Par défaut, les pipelines ne feront pas d’inférence en batch pour des raisons expliquées en détail <a href="https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching" rel="nofollow">ici</a>. La raison est que le batching n’est pas nécessairement plus rapide, et peut en fait être beaucoup plus lent dans certains cas.</p> <p data-svelte-h="svelte-fff3nw">Mais si cela fonctionne dans votre cas d’utilisation, vous pouvez utiliser :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->transcriber = pipeline(model=<span class="hljs-string">"openai/whisper-large-v2"</span>, device=<span class="hljs-number">0</span>, batch_size=<span class="hljs-number">2</span>) | |
| audio_filenames = [<span class="hljs-string">f"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/<span class="hljs-subst">{i}</span>.flac"</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-number">5</span>)] | |
| texts = transcriber(audio_filenames)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1cyvpe4">Cela exécute le pipeline sur les 4 fichiers audio fournis, mais les passera par batch de 2 au modèle (qui est sur un GPU, où le batching est plus susceptible d’aider) sans nécessiter de code supplémentaire de votre part. | |
| La sortie doit toujours correspondre à ce que vous auriez reçu sans batching. Il s’agit uniquement d’un moyen de vous aider à obtenir plus de vitesse avec un pipeline.</p> <p data-svelte-h="svelte-1lwuvpp">Les pipelines peuvent également atténuer certaines des complexités du batching car, pour certains pipelines, un seul élément (comme un long fichier audio) doit être divisé en plusieurs parties pour être traité par un modèle. Le pipeline effectue ce <a href="./main_classes/pipelines#pipeline-chunk-batching"><em>batching par morceaux</em></a> pour vous.</p> <h3 class="relative group"><a id="paramètres-spécifiques-à-la-tâche" 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" href="#paramètres-spécifiques-à-la-tâche"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Paramètres spécifiques à la tâche</span></h3> <p data-svelte-h="svelte-1uupeqb">Toutes les tâches fournissent des paramètres spécifiques à la tâche qui permettent une flexibilité et des options supplémentaires pour vous aider à accomplir votre travail. | |
| Par exemple, la méthode <code>transformers.AutomaticSpeechRecognitionPipeline.__call__()</code> dispose d’un paramètre <code>return_timestamps</code> qui semble prometteur pour le sous-titrage des vidéos :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>transcriber = pipeline(model=<span class="hljs-string">"openai/whisper-large-v2"</span>, return_timestamps=<span class="hljs-literal">True</span>) | |
| <span class="hljs-meta">>>> </span>transcriber(<span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"</span>) | |
| {<span class="hljs-string">'text'</span>: <span class="hljs-string">' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'</span>, <span class="hljs-string">'chunks'</span>: [{<span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">0.0</span>, <span class="hljs-number">11.88</span>), <span class="hljs-string">'text'</span>: <span class="hljs-string">' I have a dream that one day this nation will rise up and live out the true meaning of its'</span>}, {<span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">11.88</span>, <span class="hljs-number">12.38</span>), <span class="hljs-string">'text'</span>: <span class="hljs-string">' creed.'</span>}]}<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-nj0nps">Comme vous pouvez le voir, le modèle a inféré le texte et a également indiqué <strong>quand</strong> les différentes phrases ont été prononcées.</p> <p data-svelte-h="svelte-bgzba0">Il existe de nombreux paramètres disponibles pour chaque tâche, alors consultez la référence API de chaque tâche pour voir ce que vous pouvez ajuster ! | |
| Par exemple, le <code>AutomaticSpeechRecognitionPipeline</code> dispose d’un paramètre <code>chunk_length_s</code> qui est utile pour travailler sur des fichiers audio très longs (par exemple, le sous-titrage de films entiers ou de vidéos d’une heure) qu’un modèle ne peut généralement pas gérer seul :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>transcriber = pipeline(model=<span class="hljs-string">"openai/whisper-large-v2"</span>, chunk_length_s=<span class="hljs-number">30</span>) | |
| <span class="hljs-meta">>>> </span>transcriber(<span class="hljs-string">"https://huggingface.co/datasets/reach-vb/random-audios/resolve/main/ted_60.wav"</span>) | |
| {<span class="hljs-string">'text'</span>: <span class="hljs-string">" So in college, I was a government major, which means I had to write a lot of papers. Now, when a normal student writes a paper, they might spread the work out a little like this. So, you know. You get started maybe a little slowly, but you get enough done in the first week that with some heavier days later on, everything gets done and things stay civil. And I would want to do that like that. That would be the plan. I would have it all ready to go, but then actually the paper would come along, and then I would kind of do this. And that would happen every single paper. But then came my 90-page senior thesis, a paper you're supposed to spend a year on. I knew for a paper like that, my normal workflow was not an option, it was way too big a project. So I planned things out and I decided I kind of had to go something like this. This is how the year would go. So I'd start off light and I'd bump it up"</span>}<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-14jr8cl">Si vous ne trouvez pas un paramètre qui vous aiderait vraiment, n’hésitez pas à <a href="https://github.com/huggingface/transformers/issues/new?assignees=&labels=feature&template=feature-request.yml" rel="nofollow">le demander</a> !</p> <h2 class="relative group"><a id="utilisation-des-pipelines-sur-un-ensemble-de-données" 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" href="#utilisation-des-pipelines-sur-un-ensemble-de-données"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Utilisation des pipelines sur un ensemble de données</span></h2> <p data-svelte-h="svelte-13zedtm">Le pipeline peut également exécuter des inférences sur un grand ensemble de données. Le moyen le plus simple que nous recommandons pour cela est d’utiliser un itérateur :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">data</span>(): | |
| <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>): | |
| <span class="hljs-keyword">yield</span> <span class="hljs-string">f"My example <span class="hljs-subst">{i}</span>"</span> | |
| pipe = pipeline(model=<span class="hljs-string">"openai-community/gpt2"</span>, device=<span class="hljs-number">0</span>) | |
| generated_characters = <span class="hljs-number">0</span> | |
| <span class="hljs-keyword">for</span> out <span class="hljs-keyword">in</span> pipe(data()): | |
| generated_characters += <span class="hljs-built_in">len</span>(out[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1ruicpo">L’itérateur <code>data()</code> génère chaque résultat, et le pipeline reconnaît automatiquement que l’entrée est itérable et commencera à récupérer les données tout en continuant à les traiter sur le GPU (cela utilise <a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader" rel="nofollow">DataLoader</a> sous le capot). | |
| C’est important car vous n’avez pas besoin d’allouer de mémoire pour l’ensemble de données complet et vous pouvez alimenter le GPU aussi rapidement que possible.</p> <p data-svelte-h="svelte-i6whev">Étant donné que le lotissement pourrait accélérer les choses, il peut être utile d’essayer de régler le paramètre <code>batch_size</code> ici.</p> <p data-svelte-h="svelte-1xorsvm">La façon la plus simple d’itérer sur un ensemble de données est d’en charger un depuis 🤗 <a href="https://github.com/huggingface/datasets" rel="nofollow">Datasets</a> :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># KeyDataset is a util that will just output the item we're interested in.</span> | |
| <span class="hljs-keyword">from</span> transformers.pipelines.pt_utils <span class="hljs-keyword">import</span> KeyDataset | |
| <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset | |
| pipe = pipeline(model=<span class="hljs-string">"hf-internal-testing/tiny-random-wav2vec2"</span>, device=<span class="hljs-number">0</span>) | |
| dataset = load_dataset(<span class="hljs-string">"hf-internal-testing/librispeech_asr_dummy"</span>, <span class="hljs-string">"clean"</span>, split=<span class="hljs-string">"validation[:10]"</span>) | |
| <span class="hljs-keyword">for</span> out <span class="hljs-keyword">in</span> pipe(KeyDataset(dataset, <span class="hljs-string">"audio"</span>)): | |
| <span class="hljs-built_in">print</span>(out)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="utilisation-des-pipelines-pour-un-serveur-web" 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" href="#utilisation-des-pipelines-pour-un-serveur-web"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Utilisation des pipelines pour un serveur web</span></h2> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">Créer un moteur d'inférence est un sujet complexe qui mérite sa propre page.</div> <p data-svelte-h="svelte-r28pea"><a href="./pipeline_webserver">Lien</a></p> <h2 class="relative group"><a id="pipeline-de-vision" 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" href="#pipeline-de-vision"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pipeline de vision</span></h2> <p data-svelte-h="svelte-nrbs13">Utiliser un <code>pipeline()</code> pour les tâches de vision est pratiquement identique.</p> <p data-svelte-h="svelte-9t37uo">Spécifiez votre tâche et passez votre image au classificateur. L’image peut être un lien, un chemin local ou une image encodée en base64. Par exemple, quelle espèce de chat est montrée ci-dessous ?</p> <p data-svelte-h="svelte-1bks2jw"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" alt="pipeline-cat-chonk"></p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline | |
| <span class="hljs-meta">>>> </span>vision_classifier = pipeline(model=<span class="hljs-string">"google/vit-base-patch16-224"</span>) | |
| <span class="hljs-meta">>>> </span>preds = vision_classifier( | |
| <span class="hljs-meta">... </span> images=<span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"</span> | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>preds = [{<span class="hljs-string">"score"</span>: <span class="hljs-built_in">round</span>(pred[<span class="hljs-string">"score"</span>], <span class="hljs-number">4</span>), <span class="hljs-string">"label"</span>: pred[<span class="hljs-string">"label"</span>]} <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> preds] | |
| <span class="hljs-meta">>>> </span>preds | |
| [{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.4335</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'lynx, catamount'</span>}, {<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0348</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'</span>}, {<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0324</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'snow leopard, ounce, Panthera uncia'</span>}, {<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0239</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'Egyptian cat'</span>}, {<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0229</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'tiger cat'</span>}]<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="pipeline-de-texte" 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" href="#pipeline-de-texte"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pipeline de texte</span></h2> <p data-svelte-h="svelte-mknqi7">Utiliser un <code>pipeline()</code> pour les tâches de NLP est pratiquement identique.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline | |
| <span class="hljs-meta">>>> </span><span class="hljs-comment"># This model is a `zero-shot-classification` model.</span> | |
| <span class="hljs-meta">>>> </span><span class="hljs-comment"># It will classify text, except you are free to choose any label you might imagine</span> | |
| <span class="hljs-meta">>>> </span>classifier = pipeline(model=<span class="hljs-string">"facebook/bart-large-mnli"</span>) | |
| <span class="hljs-meta">>>> </span>classifier( | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"I have a problem with my iphone that needs to be resolved asap!!"</span>, | |
| <span class="hljs-meta">... </span> candidate_labels=[<span class="hljs-string">"urgent"</span>, <span class="hljs-string">"not urgent"</span>, <span class="hljs-string">"phone"</span>, <span class="hljs-string">"tablet"</span>, <span class="hljs-string">"computer"</span>], | |
| <span class="hljs-meta">... </span>) | |
| {<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'I have a problem with my iphone that needs to be resolved asap!!'</span>, <span class="hljs-string">'labels'</span>: [<span class="hljs-string">'urgent'</span>, <span class="hljs-string">'phone'</span>, <span class="hljs-string">'computer'</span>, <span class="hljs-string">'not urgent'</span>, <span class="hljs-string">'tablet'</span>], <span class="hljs-string">'scores'</span>: [<span class="hljs-number">0.504</span>, <span class="hljs-number">0.479</span>, <span class="hljs-number">0.013</span>, <span class="hljs-number">0.003</span>, <span class="hljs-number">0.002</span>]}<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="pipeline-multimodal" 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" href="#pipeline-multimodal"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pipeline multimodal</span></h2> <p data-svelte-h="svelte-19n1ves">Le <code>pipeline()</code> prend en charge plus d’une modalité. Par exemple, une tâche de réponse à des questions visuelles (VQA) combine texte et image. N’hésitez pas à utiliser n’importe quel lien d’image que vous aimez et une question que vous souhaitez poser à propos de l’image. L’image peut être une URL ou un chemin local vers l’image.</p> <p data-svelte-h="svelte-2yzyld">Par exemple, si vous utilisez cette <a href="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png" rel="nofollow">image de facture</a> :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline | |
| <span class="hljs-meta">>>> </span>vqa = pipeline(model=<span class="hljs-string">"impira/layoutlm-document-qa"</span>) | |
| <span class="hljs-meta">>>> </span>output = vqa( | |
| <span class="hljs-meta">... </span> image=<span class="hljs-string">"https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"</span>, | |
| <span class="hljs-meta">... </span> question=<span class="hljs-string">"What is the invoice number?"</span>, | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>output[<span class="hljs-number">0</span>][<span class="hljs-string">"score"</span>] = <span class="hljs-built_in">round</span>(output[<span class="hljs-number">0</span>][<span class="hljs-string">"score"</span>], <span class="hljs-number">3</span>) | |
| <span class="hljs-meta">>>> </span>output | |
| [{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.425</span>, <span class="hljs-string">'answer'</span>: <span class="hljs-string">'us-001'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">16</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">16</span>}]<!-- HTML_TAG_END --></pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p data-svelte-h="svelte-1c5b3uo">Pour exécuter l’exemple ci-dessus, vous devez avoir <a href="https://pypi.org/project/pytesseract/" rel="nofollow"><code>pytesseract</code></a> installé en plus de 🤗 Transformers :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->sudo apt install -y tesseract-ocr | |
| pip install pytesseract<!-- HTML_TAG_END --></pre></div></div> <h2 class="relative group"><a id="utilisation-de-pipeline-sur-de-grands-modèles-avec--accelerate-" 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" href="#utilisation-de-pipeline-sur-de-grands-modèles-avec--accelerate-"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Utilisation de pipeline sur de grands modèles avec 🤗 accelerate :</span></h2> <p data-svelte-h="svelte-dh90b">Vous pouvez facilement exécuter <code>pipeline</code> sur de grands modèles en utilisant 🤗 <code>accelerate</code> ! Assurez-vous d’abord d’avoir installé <code>accelerate</code> avec <code>pip install accelerate</code>.</p> <p data-svelte-h="svelte-1x92yvt">Chargez d’abord votre modèle en utilisant <code>device_map="auto"</code> ! Nous utiliserons <code>facebook/opt-1.3b</code> pour notre exemple.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># pip install accelerate</span> | |
| <span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline | |
| pipe = pipeline(model=<span class="hljs-string">"facebook/opt-1.3b"</span>, torch_dtype=torch.bfloat16, device_map=<span class="hljs-string">"auto"</span>) | |
| output = pipe(<span class="hljs-string">"This is a cool example!"</span>, do_sample=<span class="hljs-literal">True</span>, top_p=<span class="hljs-number">0.95</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-75hct4">Vous pouvez également passer des modèles chargés en 8 bits si vous installez <code>bitsandbytes</code> et ajoutez l’argument <code>load_in_8bit=True</code> | |
| Notez que vous pouvez remplacer le point de contrôle par n’importe quel modèle.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># pip install accelerate bitsandbytes</span> | |
| <span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline | |
| pipe = pipeline(model=<span class="hljs-string">"facebook/opt-1.3b"</span>, device_map=<span class="hljs-string">"auto"</span>, model_kwargs={<span class="hljs-string">"load_in_8bit"</span>: <span class="hljs-literal">True</span>}) | |
| output = pipe(<span class="hljs-string">"This is a cool example!"</span>, do_sample=<span class="hljs-literal">True</span>, top_p=<span class="hljs-number">0.95</span>)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="création-de-démonstrations-web-à-partir-de-pipelines-avec-gradio" 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" href="#création-de-démonstrations-web-à-partir-de-pipelines-avec-gradio"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Création de démonstrations web à partir de pipelines avec gradio</span></h2> <p data-svelte-h="svelte-a5zg71">Hugging Face prenant en charge le chargement de grands modèles, comme BLOOM. | |
| Les pipelines sont automatiquement pris en charge dans <a href="https://github.com/gradio-app/gradio/" rel="nofollow">Gradio</a>, une bibliothèque qui facilite la création d’applications d’apprentissage automatique belles et conviviales sur le web. Tout d’abord, assurez-vous que Gradio est installé :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->pip <span class="hljs-keyword">install</span> gradio<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-sz552t">Ensuite, vous pouvez créer une démonstration web autour d’un pipeline de classification d’images (ou tout autre pipeline) en une seule ligne de code en appelant la fonction <a href="https://www.gradio.app/docs/interface#interface-from-pipeline" rel="nofollow"><code>Interface.from_pipeline</code></a> de Gradio pour lancer le pipeline. Cela crée une interface intuitive de glisser-déposer dans votre navigateur :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline | |
| <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr | |
| pipe = pipeline(<span class="hljs-string">"image-classification"</span>, model=<span class="hljs-string">"google/vit-base-patch16-224"</span>) | |
| gr.Interface.from_pipeline(pipe).launch()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-rjj5jb"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/panda-classification.png"></p> <p data-svelte-h="svelte-1r40b7i">Par défaut, la démonstration web s’exécute sur un serveur local. Si vous souhaitez la partager avec d’autres, vous pouvez générer un lien public temporaire en définissant <code>share=True</code> dans <code>launch()</code>. Vous pouvez également héberger votre démonstration sur <a href="https://huggingface.co/spaces" rel="nofollow">Hugging Face Spaces</a> pour obtenir un lien permanent.</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/fr/tutoriel_pipeline.md" target="_blank"><span data-svelte-h="svelte-1kd6by1"><</span> <span data-svelte-h="svelte-x0xyl0">></span> <span data-svelte-h="svelte-1dajgef"><span class="underline ml-1.5">Update</span> on GitHub</span></a> <p></p> | |
| <script> | |
| { | |
| __sveltekit_z3d0l4 = { | |
| assets: "/docs/transformers/main/fr", | |
| base: "/docs/transformers/main/fr", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/transformers/main/fr/_app/immutable/entry/start.0c02efce.js"), | |
| import("/docs/transformers/main/fr/_app/immutable/entry/app.29d06fb0.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 8], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 76.3 kB
- Xet hash:
- fef9a32c3be3b7c1481c4a7b8702dd84ec9f34e189cd884acc5eb9c2d4c1ca5e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.