{"id":9368,"date":"2024-07-10T02:23:45","date_gmt":"2024-07-09T18:23:45","guid":{"rendered":"\/?p=9368"},"modified":"2024-07-10T02:23:45","modified_gmt":"2024-07-09T18:23:45","slug":"huggingface%e7%9a%84%e7%ae%a1%e9%81%93%e5%b7%a5%e5%85%b7pipeline","status":"publish","type":"post","link":"\/?p=9368","title":{"rendered":"HuggingFace\u7684\u7ba1\u9053\u5de5\u5177pipeline"},"content":{"rendered":"<h3>\u4ec0\u4e48\u662f Hugging Face \u7684 <code>pipeline<\/code>\uff1f<\/h3>\n<p>Hugging Face \u63d0\u4f9b\u7684 <code>transformers<\/code> \u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u96c6\uff0c\u7528\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4efb\u52a1\u3002<code>pipeline<\/code> \u662f\u8fd9\u4e2a\u5e93\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u5de5\u5177\uff0c\u65e8\u5728\u7b80\u5316\u5404\u79cdNLP\u4efb\u52a1\u7684\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f <code>pipeline<\/code> \u7684\u4ecb\u7ecd\u53ca\u5176\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<p><code>pipeline<\/code> \u662f\u4e00\u4e2a\u9ad8\u7ea7\u63a5\u53e3\uff0c\u53ef\u4ee5\u8ba9\u4f60\u65b9\u4fbf\u5730\u4f7f\u7528\u9884\u8bad\u7ec3\u7684\u6a21\u578b\u6765\u5904\u7406NLP\u4efb\u52a1\u3002\u5b83\u5c01\u88c5\u4e86\u6a21\u578b\u52a0\u8f7d\u3001\u9884\u5904\u7406\u3001\u63a8\u7406\u548c\u540e\u5904\u7406\u7b49\u6b65\u9aa4\uff0c\u4f7f\u5f97\u4f60\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u8fdb\u884c\u6587\u672c\u5206\u7c7b\u3001\u60c5\u611f\u5206\u6790\u3001\u7ffb\u8bd1\u3001\u6587\u672c\u751f\u6210\u7b49\u4efb\u52a1\u3002<\/p>\n<h3>\u652f\u6301\u7684\u4efb\u52a1<\/h3>\n<p><code>pipeline<\/code> \u652f\u6301\u7684\u4efb\u52a1\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n<ul>\n<li><strong>\u6587\u672c\u5206\u7c7b<\/strong> (Text Classification)<\/li>\n<li><strong>\u547d\u540d\u5b9e\u4f53\u8bc6\u522b<\/strong> (Named Entity Recognition, NER)<\/li>\n<li><strong>\u95ee\u7b54\u7cfb\u7edf<\/strong> (Question Answering)<\/li>\n<li><strong>\u6587\u672c\u751f\u6210<\/strong> (Text Generation)<\/li>\n<li><strong>\u7ffb\u8bd1<\/strong> (Translation)<\/li>\n<li><strong>\u603b\u7ed3<\/strong> (Summarization)<\/li>\n<li><strong>\u586b\u7a7a<\/strong> (Fill-Mask)<\/li>\n<li><strong>\u7279\u5f81\u62bd\u53d6<\/strong> (Feature Extraction)<\/li>\n<\/ul>\n<h3>\u5982\u4f55\u4f7f\u7528 <code>pipeline<\/code><\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u4efb\u52a1\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<h4>1. \u6587\u672c\u5206\u7c7b\uff08\u60c5\u611f\u5206\u6790\uff09<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u521b\u5efa\u60c5\u611f\u5206\u6790\u7ba1\u9053\nclassifier = pipeline(&#039;sentiment-analysis&#039;)\n\n# \u4f7f\u7528\u7ba1\u9053\u8fdb\u884c\u60c5\u611f\u5206\u6790\nresult = classifier(&quot;I love using Hugging Face&#039;s transformers library!&quot;)\nprint(result)<\/code><\/pre>\n<h4>2. \u547d\u540d\u5b9e\u4f53\u8bc6\u522b\uff08NER\uff09<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u521b\u5efaNER\u7ba1\u9053\nner = pipeline(&#039;ner&#039;, grouped_entities=True)\n\n# \u4f7f\u7528\u7ba1\u9053\u8fdb\u884c\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\nresult = ner(&quot;Hugging Face Inc. is a company based in New York City.&quot;)\nprint(result)<\/code><\/pre>\n<h4>3. \u95ee\u7b54\u7cfb\u7edf<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u521b\u5efa\u95ee\u7b54\u7ba1\u9053\nqa = pipeline(&#039;question-answering&#039;)\n\n# \u4f7f\u7528\u7ba1\u9053\u8fdb\u884c\u95ee\u7b54\nresult = qa(question=&quot;Where is Hugging Face based?&quot;, context=&quot;Hugging Face Inc. is a company based in New York City.&quot;)\nprint(result)<\/code><\/pre>\n<h4>4. \u6587\u672c\u751f\u6210<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u521b\u5efa\u6587\u672c\u751f\u6210\u7ba1\u9053\ngenerator = pipeline(&#039;text-generation&#039;, model=&#039;gpt-2&#039;)\n\n# \u4f7f\u7528\u7ba1\u9053\u751f\u6210\u6587\u672c\nresult = generator(&quot;Once upon a time, in a land far, far away&quot;, max_length=50, num_return_sequences=1)\nprint(result)<\/code><\/pre>\n<h4>5. \u7ffb\u8bd1<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u521b\u5efa\u7ffb\u8bd1\u7ba1\u9053\uff08\u82f1\u8bed\u5230\u6cd5\u8bed\uff09\ntranslator = pipeline(&#039;translation_en_to_fr&#039;)\n\n# \u4f7f\u7528\u7ba1\u9053\u8fdb\u884c\u7ffb\u8bd1\nresult = translator(&quot;Hugging Face is creating a tool that democratizes AI.&quot;, max_length=40)\nprint(result)<\/code><\/pre>\n<h3>\u5207\u6362\u6a21\u578b\u7684\u6b65\u9aa4<\/h3>\n<p>Hugging Face \u7684 <code>pipeline<\/code> \u5de5\u5177\u652f\u6301\u901a\u8fc7\u6307\u5b9a\u6a21\u578b\u540d\u79f0\u6765\u5207\u6362\u4e0d\u540c\u7684\u9884\u8bad\u7ec3\u6a21\u578b\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u6a21\u578b\u6765\u6267\u884c\u76f8\u540c\u7684\u4efb\u52a1\uff0c\u4ece\u800c\u6bd4\u8f83\u5b83\u4eec\u7684\u6027\u80fd\u6216\u9002\u5e94\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 <code>pipeline<\/code> \u4e2d\u5207\u6362\u4e0d\u540c\u6a21\u578b\u7684\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li><strong>\u9009\u62e9\u6a21\u578b\u540d\u79f0<\/strong>: \u4ece Hugging Face \u6a21\u578b\u5e93\u4e2d\u9009\u62e9\u4f60\u60f3\u8981\u4f7f\u7528\u7684\u6a21\u578b\u3002\u6a21\u578b\u5e93\u4e2d\u6709\u8bb8\u591a\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u6bcf\u4e2a\u6a21\u578b\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u540d\u79f0\u3002<\/li>\n<li><strong>\u5728 <code>pipeline<\/code> \u4e2d\u6307\u5b9a\u6a21\u578b\u540d\u79f0<\/strong>: \u5728\u521b\u5efa <code>pipeline<\/code> \u5bf9\u8c61\u65f6\uff0c\u901a\u8fc7 <code>model<\/code> \u53c2\u6570\u6307\u5b9a\u4f60\u9009\u62e9\u7684\u6a21\u578b\u540d\u79f0\u3002<\/li>\n<\/ol>\n<h3>\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u4efb\u52a1\u5982\u4f55\u5207\u6362\u4e0d\u540c\u7684\u6a21\u578b\uff1a<\/p>\n<h4>1. \u6587\u672c\u5206\u7c7b\uff08\u60c5\u611f\u5206\u6790\uff09<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u4f7f\u7528\u9ed8\u8ba4\u6a21\u578b\ndefault_classifier = pipeline(&#039;sentiment-analysis&#039;)\ndefault_result = default_classifier(&quot;I love using Hugging Face&#039;s transformers library!&quot;)\nprint(&quot;Default model:&quot;, default_result)\n\n# \u4f7f\u7528\u7279\u5b9a\u6a21\u578b\uff08\u4f8b\u5982distilbert-base-uncased-finetuned-sst-2-english\uff09\ncustom_classifier = pipeline(&#039;sentiment-analysis&#039;, model=&#039;distilbert-base-uncased-finetuned-sst-2-english&#039;)\ncustom_result = custom_classifier(&quot;I love using Hugging Face&#039;s transformers library!&quot;)\nprint(&quot;Custom model:&quot;, custom_result)<\/code><\/pre>\n<h4>2. \u547d\u540d\u5b9e\u4f53\u8bc6\u522b\uff08NER\uff09<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u4f7f\u7528\u9ed8\u8ba4\u6a21\u578b\ndefault_ner = pipeline(&#039;ner&#039;, grouped_entities=True)\ndefault_result = default_ner(&quot;Hugging Face Inc. is a company based in New York City.&quot;)\nprint(&quot;Default model:&quot;, default_result)\n\n# \u4f7f\u7528\u7279\u5b9a\u6a21\u578b\uff08\u4f8b\u5982dbmdz\/bert-large-cased-finetuned-conll03-english\uff09\ncustom_ner = pipeline(&#039;ner&#039;, model=&#039;dbmdz\/bert-large-cased-finetuned-conll03-english&#039;, grouped_entities=True)\ncustom_result = custom_ner(&quot;Hugging Face Inc. is a company based in New York City.&quot;)\nprint(&quot;Custom model:&quot;, custom_result)<\/code><\/pre>\n<h4>3. \u95ee\u7b54\u7cfb\u7edf<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u4f7f\u7528\u9ed8\u8ba4\u6a21\u578b\ndefault_qa = pipeline(&#039;question-answering&#039;)\ndefault_result = default_qa(question=&quot;Where is Hugging Face based?&quot;, context=&quot;Hugging Face Inc. is a company based in New York City.&quot;)\nprint(&quot;Default model:&quot;, default_result)\n\n# \u4f7f\u7528\u7279\u5b9a\u6a21\u578b\uff08\u4f8b\u5982distilbert-base-cased-distilled-squad\uff09\ncustom_qa = pipeline(&#039;question-answering&#039;, model=&#039;distilbert-base-cased-distilled-squad&#039;)\ncustom_result = custom_qa(question=&quot;Where is Hugging Face based?&quot;, context=&quot;Hugging Face Inc. is a company based in New York City.&quot;)\nprint(&quot;Custom model:&quot;, custom_result)<\/code><\/pre>\n<h4>4. \u6587\u672c\u751f\u6210<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u4f7f\u7528\u9ed8\u8ba4\u6a21\u578b\ndefault_generator = pipeline(&#039;text-generation&#039;, model=&#039;gpt-2&#039;)\ndefault_result = default_generator(&quot;Once upon a time, in a land far, far away&quot;, max_length=50, num_return_sequences=1)\nprint(&quot;Default model:&quot;, default_result)\n\n# \u4f7f\u7528\u7279\u5b9a\u6a21\u578b\uff08\u4f8b\u5982gpt2-medium\uff09\ncustom_generator = pipeline(&#039;text-generation&#039;, model=&#039;gpt2-medium&#039;)\ncustom_result = custom_generator(&quot;Once upon a time, in a land far, far away&quot;, max_length=50, num_return_sequences=1)\nprint(&quot;Custom model:&quot;, custom_result)<\/code><\/pre>\n<h4>5. \u7ffb\u8bd1<\/h4>\n<pre><code>from transformers import pipeline\n\n# \u4f7f\u7528\u9ed8\u8ba4\u6a21\u578b\ndefault_translator = pipeline(&#039;translation_en_to_fr&#039;)\ndefault_result = default_translator(&quot;Hugging Face is creating a tool that democratizes AI.&quot;, max_length=40)\nprint(&quot;Default model:&quot;, default_result)\n\n# \u4f7f\u7528\u7279\u5b9a\u6a21\u578b\uff08\u4f8b\u5982Helsinki-NLP\/opus-mt-en-fr\uff09\ncustom_translator = pipeline(&#039;translation_en_to_fr&#039;, model=&#039;Helsinki-NLP\/opus-mt-en-fr&#039;)\ncustom_result = custom_translator(&quot;Hugging Face is creating a tool that democratizes AI.&quot;, max_length=40)\nprint(&quot;Custom model:&quot;, custom_result)<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>Hugging Face \u7684 <code>pipeline<\/code> \u5de5\u5177\u6781\u5927\u5730\u7b80\u5316\u4e86NLP\u4efb\u52a1\u7684\u5b9e\u73b0\uff0c\u4f7f\u5f97\u5373\u4fbf\u662f\u975e\u4e13\u4e1a\u4eba\u5458\u4e5f\u80fd\u8f7b\u677e\u5730\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\u5904\u7406\u590d\u6742\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u3002\u901a\u8fc7\u7b80\u5355\u7684\u51e0\u884c\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u548c\u90e8\u7f72\u5404\u79cdNLP\u5e94\u7528\u3002<\/p>\n<p>\u901a\u8fc7\u5728 <code>pipeline<\/code> \u4e2d\u6307\u5b9a\u4e0d\u540c\u7684\u6a21\u578b\u540d\u79f0\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5207\u6362\u4e0d\u540c\u7684\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u6bd4\u8f83\u5b83\u4eec\u7684\u6027\u80fd\uff0c\u5e76\u9009\u62e9\u6700\u9002\u5408\u4f60\u9700\u6c42\u7684\u6a21\u578b\u3002\u8fd9\u4f7f\u5f97 <code>pipeline<\/code> \u5de5\u5177\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f Hugging Face \u7684 pipeline\uff1f Hugging Face \u63d0\u4f9b\u7684 transformers \u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[225],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9368"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9368"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9368\/revisions"}],"predecessor-version":[{"id":9369,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9368\/revisions\/9369"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9368"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}