{"id":21131,"date":"2024-07-12T11:14:15","date_gmt":"2024-07-12T03:14:15","guid":{"rendered":"https:\/\/aif.amtbbs.org\/?p=21131"},"modified":"2024-07-12T11:14:15","modified_gmt":"2024-07-12T03:14:15","slug":"%e5%80%9f%e5%8a%a9huggingface%e8%bd%bb%e6%9d%be%e5%ae%9e%e6%96%bd%e4%b8%80%e4%b8%aa%e7%ab%af%e5%88%b0%e7%ab%af%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/aif.amtbbs.org\/index.php\/2024\/07\/12\/21131\/","title":{"rendered":"\u501f\u52a9HuggingFace\u8f7b\u677e\u5b9e\u65bd\u4e00\u4e2a\u7aef\u5230\u7aef\u9879\u76ee"},"content":{"rendered":"<p style=\"font-weight: 400;\">\u4eca\u5929\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Hugging Face\u3001FastAPI\u548cDocker\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b\uff0c\u6f14\u793a\u5982\u4f55\u6709\u6548\u5730\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u6807\u3002<\/p>\n<p><img data-dominant-color=\"a8b1b9\" data-has-transparency=\"false\" style=\"--dominant-color: #a8b1b9;\" loading=\"lazy\" decoding=\"async\" class=\"not-transparent alignnone size-full wp-image-21135\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/63b6294451c0b3443eb897574a95873dbebdf0-300x199-1.png\" width=\"300\" height=\"199\" alt=\"\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/63b6294451c0b3443eb897574a95873dbebdf0-300x199-1.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/63b6294451c0b3443eb897574a95873dbebdf0-300x199-1-150x100.png 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528FastAPI\u548cDocker\u751f\u6210\u4e00\u4e2a\u968f\u65f6\u53ef\u7528\u7684Hugging Face\u6a21\u578b\u3002<\/p>\n<p><img data-dominant-color=\"e4e6e3\" data-has-transparency=\"false\" style=\"--dominant-color: #e4e6e3;\" loading=\"lazy\" decoding=\"async\" class=\"not-transparent alignnone size-full wp-image-21136\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/e4f5955523d89ac4b97708e97ffb4789be89ce-300x118-1.png\" width=\"300\" height=\"118\" alt=\"\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/e4f5955523d89ac4b97708e97ffb4789be89ce-300x118-1.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/e4f5955523d89ac4b97708e97ffb4789be89ce-300x118-1-150x59.png 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>\u60f3\u8c61\u4e00\u4e0b\uff0c\u5229\u7528Hugging Face\u6a21\u578b\u6765\u786e\u5b9a\u8bc4\u8bba\u7684\u60c5\u7eea\u3002\u5728\u8fc7\u53bb\uff0c\u7b2c\u4e00\u6b65\u662f\u5236\u4f5c\u8fd9\u6837\u4e00\u4e2a\u6a21\u578b\uff0c\u5e76\u786e\u4fdd\u5b83\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>\u7136\u800c\uff0c\u4eca\u5929\u7684\u9884\u8bad\u7ec3\u6a21\u578b\u8ba9\u6211\u4eec\u53ea\u9700\u82b1\u5f88\u5c11\u7684\u7cbe\u529b\uff0c\u5c31\u80fd\u51c6\u5907\u597d\u8fd9\u6837\u7684\u5927\u8bed\u8a00\u6a21\u578b(LLM)\u3002<\/p>\n<p>\u4e00\u65e6\u6211\u4eec\u51c6\u5907\u597d\u4f7f\u7528\u8fd9\u4e2a\u6a21\u578b\uff0c\u4e3b\u8981\u76ee\u6807\u662f\u8ba9\u516c\u53f8\u7684\u540c\u4e8b\u80fd\u591f\u4f7f\u7528\u8fd9\u4e2a\u6a21\u578b\uff0c\u800c\u4e0d\u9700\u8981\u4e0b\u8f7d\u6216\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u5b83\u3002<\/p>\n<p>\u4e3a\u6b64\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7aef\u70b9API\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u72ec\u7acb\u5730\u8c03\u7528\u548c\u4f7f\u7528\u6a21\u578b\u3002\u8fd9\u5c31\u662f\u6211\u4eec\u6240\u8bf4\u7684\u4ece\u5934\u5230\u5c3e\u6784\u5efa\u7684\u7aef\u5230\u7aef\u9879\u76ee\u3002<\/p>\n<p>\u4eca\u5929\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Hugging Face\u3001FastAPI\u548cDocker\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b\uff0c\u6f14\u793a\u5982\u4f55\u6709\u6548\u5730\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u6807\u3002<\/p>\n<h4>\u7b2c1\u6b65\uff1a\u9009\u62e9\u6211\u4eec\u7684Hugging Face\u6a21\u578b<\/h4>\n<p>\u9996\u5148\u8981\u505a\u7684\u662f\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u6211\u4eec\u9700\u8981\u7684Hugging Face\u6a21\u578b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728\u6211\u4eec\u7684\u73af\u5883\u4e2d\u8f7b\u677e\u5b89\u88c5Hugging Face\uff1a<\/p>\n<p>\u590d\u5236<\/p>\n<p>pip install transformers # remember to work with transformers we need either tensorflow or pytorch installed as well pip install torch pip install tensorflow<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<\/ul>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Transformer\u5e93\u7684\u7ba1\u9053\u547d\u4ee4\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>from transformers import pipeline<\/p>\n<ul>\n<li>1.<\/li>\n<\/ul>\n<p>\u7136\u540e\uff0c\u4f7f\u7528pipeline\u547d\u4ee4\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u4e00\u4e2a\u6a21\u578b\u6765\u5b9a\u4e49\u7279\u5b9a\u6587\u672c\u7684\u60c5\u7eea\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1a\u901a\u8fc7\u5b9a\u4e49\u4efb\u52a1\u201c\u60c5\u7eea\u5206\u6790\u201d\u6216\u901a\u8fc7\u5b9a\u4e49\u6a21\u578b\uff0c\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p># Defining directly the task we want to implement. pipe = pipeline(task=&#8221;sentiment-analysis&#8221;) # Defining the model we choose. pipe = pipeline(model=&#8221;model-to-be-used&#8221;)<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<\/ul>\n<p>\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u57fa\u4e8e\u4efb\u52a1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u9650\u5236\u4e86\u6211\u4eec\u5bf9\u6240\u4f7f\u7528\u7684\u7279\u5b9a\u6a21\u578b\u7684\u63a7\u5236\u3002<\/p>\n<p>\u5728\u672c\u6587\u4f8b\u5b50\u4e2d\uff0c\u6211\u9009\u62e9\u4e86\u201cdistilbert-base-uncase-fine tuned-sst-2-english\u201d\uff0c\u4f46\u4f60\u53ef\u4ee5\u968f\u610f\u6d4f\u89c8Hugging Face Hub\uff0c\u9009\u62e9\u9002\u5408\u9700\u8981\u7684\u4efb\u4f55\u578b\u53f7\u3002\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u6587\u7ae0(https:\/\/www.datacamp.com\/tutorial\/what-is-hugging-face)\u4e2d\u627e\u5230Hugging Face\u7684\u7b80\u5355\u6307\u5357\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>pipe = pipeline(model=&#8221;distilbert\/distilbert-base-uncased-finetuned-sst-2-english&#8221;)<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<\/ul>\n<p>\u6211\u4eec\u5df2\u5b9a\u4e49\u4e86\u7ba1\u9053\u6a21\u578b\uff0c\u53ea\u9700\u53d1\u9001\u4e00\u4e2a\u7b80\u5355\u7684\u63d0\u793a\uff0c\u5c31\u53ef\u4ee5\u8fd4\u56de\u7ed3\u679c\u3002\u6bd4\u5982\u8bf4\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>\u590d\u5236<\/p>\n<p>print(pipe(&#8220;This tutorial is great!&#8221;))<\/p>\n<ul>\n<li>1.<\/li>\n<\/ul>\n<p>\u6211\u4eec\u5c06\u5f97\u5230[{&#8216;label&#8217;: &#8216;POSITIVE&#8217;\uff0c &#8216;score&#8217;: 0.9998689889907837}]<\/p>\n<p>\u60f3\u8c61\u4e00\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u6211\u4eec\u7684\u7528\u6237\u5f97\u5230\u4e00\u4e2a\u5173\u4e8e\u8fd9\u4e2a\u5206\u7c7b\u7684\u81ea\u7136\u8bed\u8a00\u53e5\u5b50\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9e\u65bd\u7b80\u5355\u7684Python\u4ee3\u7801\u540c\u6837\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u7684\uff1a<\/p>\n<p>\u590d\u5236<\/p>\n<p>def generate_response(prompt:str): response = pipe(&#8220;This is a great tutorial!&#8221;) label = response[0][&#8220;label&#8221;] score = response[0][&#8220;score&#8221;] return f&#8221;The &#8216;{prompt}&#8217; input is {label} with a score of {score}&#8221; print(generate_response(&#8220;This tutorial is great!&#8221;))<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<\/ul>\n<p>\u91cd\u590d\u540c\u6837\u7684\u8bd5\u9a8c\uff0c\u6211\u4eec\u4f1a\u5f97\u5230\uff1a<\/p>\n<p>\u590d\u5236<\/p>\n<p>The &#8216;This tutorial is great!&#8217; input is POSITIVE with a score of 0.9997909665107727<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<\/ul>\n<p>\u73b0\u5728\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u5207\u5b9e\u53ef\u884c\u7684\u6a21\u578b\uff0c\u53ef\u4ee5\u7ee7\u7eed\u5b9a\u4e49\u6211\u4eec\u7684API\u3002<\/p>\n<h4>\u7b2c2\u6b65\uff1a\u4f7f\u7528FastAPI\u4e3a\u6a21\u578b\u7f16\u5199API\u7aef\u70b9<\/h4>\n<p>\u4e3a\u4e86\u5b9a\u4e49API\uff0c\u6211\u4eec\u5c06\u4f7f\u7528FastAPI\u3002\u5b83\u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u9ad8\u6027\u80fdWeb API\u7684Python\u6846\u67b6\u3002\u9996\u5148\uff0c\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5FastAPI\u5e93\uff0c\u5e76\u5c06\u5176\u5bfc\u5165\u5230\u6211\u4eec\u7684\u73af\u5883\u4e2d\u3002\u6b64\u5916\uff0c\u6211\u4eec\u5c06\u5229\u7528pydantic\u5e93\u6765\u786e\u4fdd\u8f93\u5165\u662f\u6240\u9700\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u751f\u6210\u5207\u5b9e\u53ef\u884c\u7684API\uff0c\u6211\u4eec\u7684\u540c\u4e8b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>from fastapi import FastAPI from pydantic import BaseModel from transformers import pipeline # You can check any other model in the Hugging Face Hub pipe = pipeline(model=&#8221;distilbert\/distilbert-base-uncased-finetuned-sst-2-english&#8221;) # We define the app app = FastAPI() # We define that we expect our input to be a string class RequestModel(BaseModel): input: str # Now we define that we accept post requests @app.post(&#8220;\/sentiment&#8221;) def get_response(request: RequestModel): prompt = request.input response = pipe(prompt) label = response[0][&#8220;label&#8221;] score = response[0][&#8220;score&#8221;] return f&#8221;The &#8216;{prompt}&#8217; input is {label} with a score of {score}&#8221;<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u662f\u4ee3\u7801\u4e2d\u9010\u6b65\u53d1\u751f\u7684\u4e8b\u60c5\uff1a<\/p>\n<ul data-id=\"u738a58b-VjYg22lN\">\n<li data-id=\"ld70c578-odvD0W3X\">\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a\u4ee3\u7801\u5148\u5bfc\u5165FastAPI\u548cPydantic\uff0c\u786e\u4fdd\u6211\u4eec\u6536\u53d1\u7684\u6570\u636e\u7ed3\u6784\u6b63\u786e\u3002<\/li>\n<li data-id=\"ld70c578-MrPwz1Zd\">\u52a0\u8f7d\u6a21\u578b\uff1a\u52a0\u8f7d\u4e00\u4e2a\u9884\u8bad\u7ec3\u7684\u60c5\u7eea\u5206\u6790\u6a21\u578b\uff0c\u6b63\u5982\u6211\u4eec\u5728\u7b2c\u4e00\u6b65\u4e2d\u6240\u505a\u7684\u90a3\u6837\u3002<\/li>\n<li data-id=\"ld70c578-UgHmTC3A\">\u8bbe\u7f6eFastAPI\u5e94\u7528\u7a0b\u5e8f\uff1aapp = FastAPI()\u521d\u59cb\u5316FastAPI\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u5176\u51c6\u5907\u597d\u5904\u7406\u8bf7\u6c42\u3002<\/li>\n<li data-id=\"ld70c578-SzFJEUyb\">\u5b9a\u4e49\u8bf7\u6c42\u6a21\u578b\uff1a\u4f7f\u7528Pydantic\uff0c\u5b9a\u4e49RequestModel\u7c7b\u3002\u8be5\u7c7b\u6307\u5b9a\u4e86\u6211\u4eec\u671f\u671b\u8f93\u5165\u5b57\u7b26\u4e32\uff0c\u786e\u4fddAPI\u53ea\u63a5\u53d7\u6b63\u786e\u683c\u5f0f\u7684\u6570\u636e\u3002<\/li>\n<li data-id=\"ld70c578-Z87EqtlF\">\u521b\u5efa\u7aef\u70b9\uff1a@app.post(&#8220;\/sentiment&#8221;)\u88c5\u9970\u5668\u544a\u8bc9FastAPI\uff0c\u5f53\u5411\/sentiment\u7aef\u70b9\u53d1\u51faPOST\u8bf7\u6c42\u65f6\uff0c\u5e94\u8be5\u89e6\u53d1\u8be5\u51fd\u6570\u3002get_response\u51fd\u6570\u63a5\u53d7RequestModel\u5bf9\u8c61\u4f5c\u4e3a\u8f93\u5165\uff0c\u8f93\u5165\u542b\u6709\u6211\u4eec\u60f3\u8981\u5206\u6790\u7684\u6587\u672c\u3002<\/li>\n<li data-id=\"ld70c578-4yWXk259\">\u5904\u7406\u8bf7\u6c42\uff1a\u5728get_response\u51fd\u6570\u4e2d\uff0c\u4ece\u8bf7\u6c42\u4e2d\u63d0\u53d6\u6587\u672c\u5e76\u4f20\u9012\u7ed9\u6a21\u578b(pipe(prompt))\u3002\u6a21\u578b\u8fd4\u56de\u5e26\u6709\u60c5\u7eea\u6807\u7b7e(\u6bd4\u5982\u201dPOSITIVE\u201d\u6216\u201cNEGATIVE\u201d)\u7684\u54cd\u5e94\u548c\u8868\u793a\u9884\u6d4b\u7f6e\u4fe1\u5ea6\u7684\u5206\u6570\u3002<\/li>\n<li data-id=\"ld70c578-QxaIYyb6\">\u8fd4\u56de\u54cd\u5e94\uff1a\u6700\u540e\uff0c\u51fd\u6570\u8fd4\u56de\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u8f93\u5165\u6587\u672c\u3001\u60c5\u7eea\u6807\u7b7e\u548c\u7f6e\u4fe1\u5ea6\u5206\u6570\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a\u6e05\u6670\u800c\u7b80\u6d01\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u6211\u4eec\u6267\u884c\u4ee3\u7801\uff0cAPI\u5c06\u5728\u672c\u5730\u4e3b\u673a\u4e2d\u53ef\u7528\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img data-dominant-color=\"f7fbf8\" data-has-transparency=\"false\" style=\"--dominant-color: #f7fbf8;\" loading=\"lazy\" decoding=\"async\" class=\"not-transparent alignnone size-full wp-image-21137\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/28fb42c302a8cc0dcbe419de6f84a4255539c2-300x99-1.png\" width=\"300\" height=\"99\" alt=\"\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/28fb42c302a8cc0dcbe419de6f84a4255539c2-300x99-1.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/07\/28fb42c302a8cc0dcbe419de6f84a4255539c2-300x99-1-150x50.png 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>\u7b80\u800c\u8a00\u4e4b\uff0c\u8fd9\u6bb5\u4ee3\u7801\u8bbe\u7f6e\u7b80\u5355\u7684Web\u670d\u52a1\uff0c\u4f60\u53ef\u4ee5\u5f80\u8be5\u670d\u52a1\u53d1\u9001\u4e00\u6bb5\u6587\u672c\uff0c\u5176\u7ed9\u51fa\u7684\u56de\u590d\u662f\u5206\u6790\u8be5\u6587\u672c\u7684\u60c5\u7eea\uff0c\u901a\u8fc7FastAPI\u5145\u5206\u5229\u7528Hugging Face\u6a21\u578b\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5e94\u8be5\u5c06\u5e94\u7528\u7a0b\u5e8f\u5bb9\u5668\u5316\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u6267\u884c\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u6267\u884c\u3002\u8fd9\u5c06\u786e\u4fdd\u66f4\u597d\u7684\u53ef\u79fb\u690d\u6027\u548c\u6613\u4e8e\u90e8\u7f72\u3002<\/p>\n<h4>\u7b2c3\u6b65\uff1a\u4f7f\u7528Docker\u8fd0\u884c\u6211\u4eec\u7684\u6a21\u578b<\/h4>\n<p>\u5bb9\u5668\u5316\u9700\u8981\u5c06\u5e94\u7528\u7a0b\u5e8f\u653e\u5165\u5bb9\u5668\u4e2d\u3002Docker\u5bb9\u5668\u8fd0\u884cDocker\u955c\u50cf\u7684\u5b9e\u4f8b\uff0c\u8fd9\u5305\u62ec\u5b83\u81ea\u5df1\u7684\u64cd\u4f5c\u7cfb\u7edf\u548c\u5e94\u7528\u7a0b\u5e8f\u6240\u9700\u7684\u6240\u6709\u4f9d\u8d56\u9879\u3002<\/p>\n<p>\u6bd4\u5982\u8bf4\uff0c\u4f60\u53ef\u4ee5\u5728\u5bb9\u5668\u4e2d\u5b89\u88c5Python\u548c\u6240\u6709\u5fc5\u9700\u7684\u5305\uff0c\u8fd9\u6837\u5b83\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u8fd0\u884c\uff0c\u4e0d\u9700\u8981\u5b89\u88c5\u8fd9\u4e9b\u5e93\u3002<\/p>\n<p>\u4e3a\u4e86\u5728Docker\u5bb9\u5668\u4e2d\u8fd0\u884c\u6211\u4eec\u7684\u60c5\u7eea\u5206\u6790\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u5148\u9700\u8981\u521b\u5efaDocker\u955c\u50cf\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5305\u62ec\u5199\u4e00\u4e2aDockerfile\uff0c\u6307\u5b9aDocker\u955c\u50cf\u5e94\u8be5\u542b\u6709\u4ec0\u4e48\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u6ca1\u6709\u5b89\u88c5Docker\uff0c\u53ef\u4ee5\u4eceDocker\u7684\u7f51\u7ad9\u4e0a\u4e0b\u8f7d\u3002\u8fd9\u662f\u6211\u4eec\u5c06\u5728\u8fd9\u4e2a\u9879\u76ee\u4e2d\u4f7f\u7528\u7684Dockerfile\uff0c\u5728\u5b58\u50a8\u5e93\u4e2d\u540d\u4e3aDockerfile\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p># Use an official Python runtime as a parent image FROM python:3.10-slim # Set the working directory in the container WORKDIR \/sentiment # Copy the requirements.txt file into the root COPY requirements.txt . # Copy the current directory contents into the container at \/app as well COPY .\/app .\/app # Install any needed packages specified in requirements.txt RUN pip install -r requirements.txt # Make port 8000 available to the world outside this container EXPOSE 8000 # Run main.py when the container launches, as it is contained under the app folder, we define app.main CMD [&#8220;uvicorn&#8221;, &#8220;app.main:app&#8221;, &#8220;&#8211;host&#8221;, &#8220;0.0.0.0&#8221;, &#8220;&#8211;port&#8221;, &#8220;8000&#8221;]<\/p>\n<ul>\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<\/ul>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u6784\u5efaDocker\u955c\u50cf\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>docker build -t sentit-app<\/p>\n<ul>\n<li>1.<\/li>\n<\/ul>\n<p>\u7136\u540e\u4e3a\u4e86\u6267\u884c\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u9009\u9879\uff1a<\/p>\n<p>\u4f7f\u7528\u5e26\u6709\u547d\u4ee4\u7684\u7ec8\u7aef\u3002<\/p>\n<p>\u590d\u5236<\/p>\n<p>docker run -p 8000:8000 &#8211;name name_of_cointainer sentiment-hf<\/p>\n<ul>\n<li>1.<\/li>\n<\/ul>\n<p>\u4f7f\u7528docker hub\u3002\u6211\u4eec\u5f88\u5bb9\u6613\u8fdb\u5165docker hub\uff0c\u70b9\u51fb\u955c\u50cf\u7684\u8fd0\u884c\u6309\u94ae\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u5168\u90e8\u7ec6\u8282!\u73b0\u5728\uff0c\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u5207\u5b9e\u53ef\u884c\u7684\u60c5\u7eea\u5206\u7c7b\u6a21\u578b\uff0c\u5b83\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u5de5\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528API\u6765\u6267\u884c\u3002<\/p>\n<h4>\u7ed3\u8bed<\/h4>\n<p>\u5177\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ul data-id=\"u738a58b-cgH05XRG\">\n<li data-id=\"ld70c578-qE7uyN02\">\u6a21\u578b\u9009\u62e9\u548c\u8bbe\u7f6e\uff1a\u9009\u62e9\u548c\u914d\u7f6e\u4e00\u4e2aHugging Face\u9884\u8bad\u7ec3\u6a21\u578b\u8fdb\u884c\u60c5\u7eea\u5206\u6790\uff0c\u786e\u4fdd\u5b83\u6ee1\u8db3\u4f60\u7684\u8981\u6c42\u3002<\/li>\n<li data-id=\"ld70c578-ovwW7p99\">\u4f7f\u7528FastAPI\u8fdb\u884cAPI\u5f00\u53d1\uff1a\u4f7f\u7528FastAPI\u521b\u5efaAPI\u7aef\u70b9\uff0c\u5b9e\u73b0\u4e0e\u60c5\u7eea\u5206\u6790\u6a21\u578b\u7684\u8f7b\u677e\u4ea4\u4e92\u3002<\/li>\n<li data-id=\"ld70c578-HRP9P86Z\">Docker\u5bb9\u5668\u5316\uff1a\u4f7f\u7528Docker\u5bb9\u5668\u5316\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ee5\u786e\u4fdd\u53ef\u79fb\u690d\u6027\u548c\u8de8\u4e0d\u540c\u73af\u5883\u7684\u65e0\u7f1d\u90e8\u7f72\u3002<\/li>\n<\/ul>\n<p>\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684GitHub\u4ee3\u7801\u5e93\u4e2d\u67e5\u770b\u6211\u7684\u5168\u90e8\u4ee3\u7801\uff1ahttps:\/\/github.com\/rfeers\/data-science-portfolio\/tree\/main\/end-to-end-projects\/simple-docker-hf-model\u3002<\/p>\n<p>\u539f\u6587\u6807\u9898\uff1aA Simple to Implement End-to-End Project with HuggingFace\uff0c\u4f5c\u8005\uff1aJosep Ferrer<\/p>\n<p>\u94fe\u63a5\uff1a<\/p>\n<p>https:\/\/www.kdnuggets.com\/a-simple-to-implement-end-to-end-project-with-huggingface\u3002<\/p>\n<p>\u60f3\u4e86\u89e3\u66f4\u591aAIGC\u7684\u5185\u5bb9\uff0c\u8bf7\u8bbf\u95ee\uff1a<\/p>\n<p>51CTO AI.x\u793e\u533a<\/p>\n<p>https:\/\/www.51cto.com\/aigc\/<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_21131\" class=\"pvc_stats total_only  \" data-element-id=\"21131\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Hugging Face\u3001FastAPI\u548cDocker\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b\uff0c\u6f14\u793a\u5982\u4f55\u6709\u6548\u5730\u5b9e\u73b0\u8fd9 [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_21131\" class=\"pvc_stats total_only  \" data-element-id=\"21131\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":56,"featured_media":21135,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,20,80],"tags":[1200],"class_list":["post-21131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-23","category-20","category-80","tag-fastapi-docker-"],"_links":{"self":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/21131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/comments?post=21131"}],"version-history":[{"count":1,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/21131\/revisions"}],"predecessor-version":[{"id":21138,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/21131\/revisions\/21138"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media\/21135"}],"wp:attachment":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media?parent=21131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/categories?post=21131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/tags?post=21131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}