{"id":6085,"date":"2025-12-18T16:57:41","date_gmt":"2025-12-18T08:57:41","guid":{"rendered":"https:\/\/benson80.eu.org\/?p=6085"},"modified":"2025-12-18T16:58:46","modified_gmt":"2025-12-18T08:58:46","slug":"hugging-face-%e6%a8%a1%e5%9e%8b%e4%b8%8b%e8%bd%bd%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/benson80.eu.org\/?p=6085","title":{"rendered":"Hugging Face \u6a21\u578b\u4e0b\u8f7d\u6559\u7a0b"},"content":{"rendered":"\n<p>\u8981\u5728 Hugging Face \u4e0a\u4e0b\u8f7d\u6a21\u578b\uff0c\u9996\u5148\u9700\u8981\u8fdb\u884c\u767b\u5f55\u3002\u4e0b\u9762\u662f\u4f7f\u7528 <code>huggingface-cli<\/code> \u767b\u5f55\u5e76\u4e0b\u8f7d\u6a21\u578b\u7684\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5 <code>huggingface_hub<\/code>\uff08\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff09<\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 <code>huggingface_hub<\/code> \u5305\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install huggingface_hub<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u767b\u5f55 Hugging Face \u8d26\u53f7<\/h3>\n\n\n\n<p>\u4f60\u9700\u8981\u5728 Hugging Face \u4e0a\u521b\u5efa\u4e00\u4e2a\u8d26\u53f7\u5e76\u83b7\u53d6 API Token\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u767b\u5f55 <a href=\"https:\/\/huggingface.co\/\" target=\"_blank\" rel=\"noreferrer noopener\">Hugging Face<\/a> \u8d26\u53f7\u3002<\/li>\n\n\n\n<li>\u5728\u53f3\u4e0a\u89d2\u70b9\u51fb\u4f60\u7684\u5934\u50cf\uff0c\u9009\u62e9 <strong>Settings<\/strong>\u3002<\/li>\n\n\n\n<li>\u5728 <strong>Access Tokens<\/strong> \u90e8\u5206\u751f\u6210\u4e00\u4e2a\u65b0\u7684 token\uff0c\u590d\u5236\u5b83\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u7ec8\u7aef\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">huggingface-cli login<\/pre>\n\n\n\n<p>\u7cfb\u7edf\u4f1a\u63d0\u793a\u4f60\u8f93\u5165 API Token\uff0c\u5c06\u4f60\u4ece Hugging Face \u83b7\u53d6\u7684 token \u7c98\u8d34\u8fdb\u53bb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4e0b\u8f7d\u6a21\u578b<\/h3>\n\n\n\n<p>\u767b\u5f55\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>huggingface-cli<\/code> \u6216\u8005 Python \u811a\u672c\u4e0b\u8f7d\u6a21\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>huggingface-cli<\/code> \u4e0b\u8f7d\u6a21\u578b<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>huggingface-cli<\/code> \u6765\u76f4\u63a5\u4e0b\u8f7d\u6a21\u578b\u3002\u4f8b\u5982\uff0c\u4e0b\u8f7d\u4e00\u4e2a\u540d\u4e3a <code>stabilityai\/stable-diffusion-2<\/code> \u7684\u6a21\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">huggingface-cli download stabilityai\/stable-diffusion-2<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 Python \u4e0b\u8f7d\u6a21\u578b<\/h4>\n\n\n\n<p>\u5728 Python \u811a\u672c\u4e2d\uff0c\u4f7f\u7528 <code>from_pretrained<\/code> \u65b9\u6cd5\u4e0b\u8f7d\u5e76\u52a0\u8f7d\u6a21\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from transformers import AutoModelForCausalLM, AutoTokenizer<br>\u200b<br>model_name = \"gpt2\"  # \u4f60\u8981\u4e0b\u8f7d\u7684\u6a21\u578b\u540d<br>model = AutoModelForCausalLM.from_pretrained(model_name)<br>tokenizer = AutoTokenizer.from_pretrained(model_name)<\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4f1a\u4e0b\u8f7d\u6307\u5b9a\u6a21\u578b\u5e76\u8fdb\u884c\u52a0\u8f7d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u8bbf\u95ee\u79c1\u4eba\u6a21\u578b<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u8981\u8bbf\u95ee\u79c1\u6709\u6a21\u578b\uff0c\u5728\u767b\u5f55\u4e4b\u540e\u4f7f\u7528\u76f8\u540c\u7684 token\uff0c\u5b83\u4f1a\u81ea\u52a8\u6388\u6743\u4f60\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u67e5\u770b\u4e0b\u8f7d\u8def\u5f84<\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6a21\u578b\u4f1a\u4e0b\u8f7d\u5230 Hugging Face \u7684\u7f13\u5b58\u76ee\u5f55\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u67e5\u770b\u6a21\u578b\u7684\u7f13\u5b58\u76ee\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from huggingface_hub import hf_hub_download<br>\u200b<br>file_path = hf_hub_download(repo_id=\"gpt2\", filename=\"pytorch_model.bin\")<br>print(file_path)<\/pre>\n\n\n\n<p>\u8fd9\u5c06\u6253\u5370\u51fa\u6587\u4ef6\u7684\u5b9e\u9645\u5b58\u50a8\u8def\u5f84\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u9000\u51fa\u767b\u5f55<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u4e0d\u518d\u9700\u8981\u767b\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9000\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">huggingface-cli logout<\/pre>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u767b\u5f55 Hugging Face \u5e76\u4e0b\u8f7d\u4f60\u6240\u9700\u8981\u7684\u6a21\u578b\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u5728 Hugging Face \u4e0a\u4e0b\u8f7d\u6a21\u578b\uff0c\u9996\u5148\u9700 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-6085","post","type-post","status-publish","format-standard","hentry","category-stable-diffusion"],"_links":{"self":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/6085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6085"}],"version-history":[{"count":2,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/6085\/revisions"}],"predecessor-version":[{"id":6087,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/6085\/revisions\/6087"}],"wp:attachment":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}