{"id":394,"date":"2026-03-06T10:15:10","date_gmt":"2026-03-06T02:15:10","guid":{"rendered":"https:\/\/www.liaoxinghui.com\/?p=394"},"modified":"2026-03-06T10:15:10","modified_gmt":"2026-03-06T02:15:10","slug":"docker-compose-huggingface-text-classification-api","status":"publish","type":"post","link":"https:\/\/www.liaoxinghui.com\/?p=394","title":{"rendered":"\u4f7f\u7528Docker Compose\u90e8\u7f72Hugging Face\u6587\u672c\u5206\u7c7b\u6a21\u578b\u5e76\u63d0\u4f9bREST API"},"content":{"rendered":"<h1>\u4f7f\u7528Docker Compose\u90e8\u7f72Hugging Face\u6587\u672c\u5206\u7c7b\u6a21\u578b\u5e76\u63d0\u4f9bREST API<\/h1>\n<h2>1. \u4e1a\u52a1\u573a\u666f\u4e0e\u76ee\u6807<\/h2>\n<p>\u4f01\u4e1a\u9700\u8981\u5c06\u9884\u8bad\u7ec3\u7684\u6587\u672c\u5206\u7c7b\u6a21\u578b\uff08\u5982BERT\uff09\u5feb\u901f\u90e8\u7f72\u4e3aAPI\u670d\u52a1\uff0c\u4f9b\u5185\u90e8\u5e94\u7528\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u5ba2\u670d\u7cfb\u7edf\u9700\u8981\u5b9e\u65f6\u5206\u6790\u7528\u6237\u53cd\u9988\u7684\u60c5\u611f\u503e\u5411\uff08\u6b63\u9762\/\u8d1f\u9762\uff09\u3002\u76ee\u6807\uff1a\u7528Docker Compose\u5b9e\u73b0\u4e00\u952e\u90e8\u7f72\uff0c\u63d0\u4f9b\u9ad8\u53ef\u7528REST API\uff0c\u652f\u6301\u5e76\u53d1\u8bf7\u6c42\uff0c\u5e76\u6613\u4e8e\u7248\u672c\u7ba1\u7406\u548c\u6269\u5c55\u3002<\/p>\n<h2>2. \u73af\u5883\u51c6\u5907\uff08uv + \u4f9d\u8d56\uff09<\/h2>\n<p>\u4f7f\u7528uv\u7ba1\u7406Python\u73af\u5883\uff0c\u786e\u4fdd\u4f9d\u8d56\u4e00\u81f4\u3002<\/p>\n<pre><code class=\"lang-bash language-bash bash\"># \u5b89\u88c5uv\uff08\u5982\u679c\u672a\u5b89\u88c5\uff09\ncurl -LsSf https:\/\/astral.sh\/uv\/install.sh | sh\n\n# \u521b\u5efa\u9879\u76ee\u76ee\u5f55\u5e76\u521d\u59cb\u5316\nmkdir text-classification-api &amp;&amp; cd text-classification-api\nuv init\n\n# \u6dfb\u52a0\u4f9d\u8d56\u5230pyproject.toml\uff08\u6216\u76f4\u63a5\u8fd0\u884c\uff09\nuv add fastapi uvicorn transformers torch docker-compose\n\n# \u540c\u6b65\u4f9d\u8d56\nuv sync<\/code><\/pre>\n<h2>3. \u6570\u636e\u8bf4\u660e\uff08\u771f\u5b9e\u6570\u636e\u53e3\u5f84\u6216\u6a21\u62df\u6570\u636e\u751f\u6210\u903b\u8f91\uff09<\/h2>\n<p>\u4efb\u52a1\u7c7b\u578b\uff1a\u4e8c\u5206\u7c7b\uff08\u60c5\u611f\u5206\u6790\uff1a\u6b63\u9762\/\u8d1f\u9762\uff09\u3002\u4f7f\u7528\u516c\u5f00\u6570\u636e\u96c6\u6a21\u62df\uff0c\u5982IMDB\u5f71\u8bc4\uff0c\u4f46\u8fd9\u91cc\u76f4\u63a5\u4f7f\u7528Hugging Face\u9884\u8bad\u7ec3\u6a21\u578b<code>distilbert-base-uncased-finetuned-sst-2-english<\/code>\uff0c\u5b83\u5df2\u5728SST-2\u6570\u636e\u96c6\u4e0a\u5fae\u8c03\uff0c\u9002\u7528\u4e8e\u901a\u7528\u82f1\u6587\u60c5\u611f\u5206\u6790\u3002\u8f93\u5165\u4e3a\u6587\u672c\u5b57\u7b26\u4e32\uff0c\u8f93\u51fa\u4e3a\u6982\u7387\u5206\u5e03\u3002<\/p>\n<h2>4. \u8bad\u7ec3\/\u5b9e\u73b0\u6b65\u9aa4\uff08\u5b8c\u6574\u4ee3\u7801\uff09<\/h2>\n<p>\u8df3\u8fc7\u8bad\u7ec3\uff0c\u76f4\u63a5\u90e8\u7f72\u9884\u8bad\u7ec3\u6a21\u578b\u3002\u521b\u5efaFastAPI\u5e94\u7528\u548cDocker\u914d\u7f6e\u3002<\/p>\n<p><strong>app\/main.py<\/strong><\/p>\n<pre><code class=\"lang-python language-python python\">from fastapi import FastAPI, HTTPException\nfrom pydantic import BaseModel\nfrom transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification\nimport torch\nimport logging\n\n# \u521d\u59cb\u5316FastAPI\u5e94\u7528\napp = FastAPI(title=&quot;Text Classification API&quot;, version=&quot;1.0&quot;)\n\n# \u914d\u7f6e\u65e5\u5fd7\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(__name__)\n\n# \u52a0\u8f7d\u6a21\u578b\u548ctokenizer\uff08\u4f7f\u7528\u7f13\u5b58\uff0c\u907f\u514d\u6bcf\u6b21\u8bf7\u6c42\u52a0\u8f7d\uff09\nmodel_name = &quot;distilbert-base-uncased-finetuned-sst-2-english&quot;\ntry:\n    tokenizer = AutoTokenizer.from_pretrained(model_name)\n    model = AutoModelForSequenceClassification.from_pretrained(model_name)\n    classifier = pipeline(&quot;text-classification&quot;, model=model, tokenizer=tokenizer)\n    logger.info(f&quot;Model {model_name} loaded successfully.&quot;)\nexcept Exception as e:\n    logger.error(f&quot;Failed to load model: {e}&quot;)\n    raise\n\n# \u5b9a\u4e49\u8bf7\u6c42\/\u54cd\u5e94\u6a21\u578b\nclass TextRequest(BaseModel):\n    text: str\n\nclass PredictionResponse(BaseModel):\n    label: str\n    score: float\n\n@app.get(&quot;\/health&quot;)\nasync def health_check():\n    return {&quot;status&quot;: &quot;healthy&quot;}\n\n@app.post(&quot;\/predict&quot;, response_model=PredictionResponse)\nasync def predict(request: TextRequest):\n    &quot;&quot;&quot;\n    \u9884\u6d4b\u6587\u672c\u60c5\u611f\uff08\u6b63\u9762\/\u8d1f\u9762\uff09\u3002\n    &quot;&quot;&quot;\n    if not request.text.strip():\n        raise HTTPException(status_code=400, detail=&quot;Text cannot be empty.&quot;)\n\n    try:\n        # \u4f7f\u7528pipeline\u8fdb\u884c\u9884\u6d4b\n        result = classifier(request.text, truncation=True, max_length=512)\n        # \u7ed3\u679c\u793a\u4f8b: [{&#039;label&#039;: &#039;POSITIVE&#039;, &#039;score&#039;: 0.9998}]\n        prediction = result[0]\n        return PredictionResponse(label=prediction[&#039;label&#039;], score=prediction[&#039;score&#039;])\n    except Exception as e:\n        logger.error(f&quot;Prediction error: {e}&quot;)\n        raise HTTPException(status_code=500, detail=&quot;Internal server error during prediction.&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    import uvicorn\n    uvicorn.run(app, host=&quot;0.0.0.0&quot;, port=8000)<\/code><\/pre>\n<p><strong>Dockerfile<\/strong><\/p>\n<pre><code class=\"lang-dockerfile language-dockerfile dockerfile\"># \u4f7f\u7528Python 3.9 slim\u955c\u50cf\u51cf\u5c0f\u4f53\u79ef\nFROM python:3.9-slim\n\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\nWORKDIR \/app\n\n# \u5b89\u88c5\u7cfb\u7edf\u4f9d\u8d56\uff08\u53ef\u9009\uff0c\u7528\u4e8e\u4f18\u5316\uff09\nRUN apt-get update &amp;&amp; apt-get install -y --no-install-recommends \\\n    gcc \\\n    &amp;&amp; rm -rf \/var\/lib\/apt\/lists\/*\n\n# \u590d\u5236\u4f9d\u8d56\u6587\u4ef6\nCOPY pyproject.toml .\n\n# \u4f7f\u7528uv\u5b89\u88c5\u4f9d\u8d56\uff08\u590d\u5236uv\u53ef\u6267\u884c\u6587\u4ef6\u6216\u76f4\u63a5\u5b89\u88c5\uff09\n# \u8fd9\u91cc\u7b80\u5316\uff0c\u4f7f\u7528pip\u5b89\u88c5uv\u7136\u540e\u540c\u6b65\nRUN pip install --no-cache-dir uv \\\n    &amp;&amp; uv sync --frozen \\\n    &amp;&amp; pip uninstall -y uv \\\n    &amp;&amp; apt-get purge -y gcc \\\n    &amp;&amp; apt-get autoremove -y\n\n# \u590d\u5236\u5e94\u7528\u4ee3\u7801\nCOPY app\/ .\/app\/\n\n# \u66b4\u9732\u7aef\u53e3\nEXPOSE 8000\n\n# \u542f\u52a8\u547d\u4ee4\nCMD [&quot;uvicorn&quot;, &quot;app.main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]<\/code><\/pre>\n<p><strong>docker-compose.yml<\/strong><\/p>\n<pre><code class=\"lang-yaml language-yaml yaml\">version: &#039;3.8&#039;\n\nservices:\n  text-classification-api:\n    build: .\n    container_name: huggingface-text-classifier\n    ports:\n      - &quot;8000:8000&quot;\n    environment:\n      - TRANSFORMERS_CACHE=\/app\/model-cache  # \u8bbe\u7f6e\u6a21\u578b\u7f13\u5b58\u8def\u5f84\n    volumes:\n      - model-cache:\/app\/model-cache  # \u6301\u4e45\u5316\u6a21\u578b\u7f13\u5b58\uff0c\u907f\u514d\u91cd\u590d\u4e0b\u8f7d\n    restart: unless-stopped\n    healthcheck:\n      test: [&quot;CMD&quot;, &quot;curl&quot;, &quot;-f&quot;, &quot;http:\/\/localhost:8000\/health&quot;]\n      interval: 30s\n      timeout: 10s\n      retries: 3\n\nvolumes:\n  model-cache:<\/code><\/pre>\n<h2>5. \u8c03\u7528\u65b9\u5f0f\uff08\u79bb\u7ebf\u6279\u91cf + \u5355\u6761\u793a\u4f8b\uff09<\/h2>\n<p><strong>\u5355\u6761\u793a\u4f8b\uff08\u4f7f\u7528curl\uff09<\/strong><\/p>\n<pre><code class=\"lang-bash language-bash bash\"># \u542f\u52a8\u670d\u52a1\ncd text-classification-api\ndocker-compose up --build\n\n# \u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u8c03\u7528API\ncurl -X POST &quot;http:\/\/localhost:8000\/predict&quot; \\\n  -H &quot;Content-Type: application\/json&quot; \\\n  -d &#039;{&quot;text&quot;: &quot;I love this product, it works perfectly!&quot;}&#039;\n\n# \u9884\u671f\u54cd\u5e94\n# {&quot;label&quot;:&quot;POSITIVE&quot;,&quot;score&quot;:0.9998}<\/code><\/pre>\n<p><strong>\u79bb\u7ebf\u6279\u91cf\u8c03\u7528\uff08Python\u811a\u672c\u793a\u4f8b\uff09<\/strong><\/p>\n<pre><code class=\"lang-python language-python python\">import requests\nimport json\n\n# API\u7aef\u70b9\nurl = &quot;http:\/\/localhost:8000\/predict&quot;\n\n# \u6279\u91cf\u6587\u672c\ntexts = [\n    &quot;This movie is terrible.&quot;,\n    &quot;Amazing experience, highly recommend!&quot;,\n    &quot;It&#039;s okay, not great but not bad.&quot;\n]\n\n# \u53d1\u9001\u8bf7\u6c42\nfor text in texts:\n    response = requests.post(url, json={&quot;text&quot;: text})\n    if response.status_code == 200:\n        result = response.json()\n        print(f&quot;Text: {text} -&gt; Label: {result[&#039;label&#039;]}, Score: {result[&#039;score&#039;]}&quot;)\n    else:\n        print(f&quot;Error for text &#039;{text}&#039;: {response.status_code}&quot;)<\/code><\/pre>\n<h2>6. \u6307\u6807\u8bf4\u660e<\/h2>\n<p>\u4efb\u52a1\u7c7b\u578b\uff1a\u4e8c\u5206\u7c7b\u3002<\/p>\n<ul>\n<li><strong>AUC\uff08Area Under ROC Curve\uff09<\/strong>\uff1a\u8303\u56f40-1\uff0c\u503c\u8d8a\u63a5\u8fd11\u6a21\u578b\u8d8a\u597d\u3002\u7528\u4e8e\u8861\u91cf\u6a21\u578b\u5728\u4e0d\u540c\u9608\u503c\u4e0b\u533a\u5206\u6b63\u8d1f\u7c7b\u7684\u80fd\u529b\uff0c\u9002\u5408\u7c7b\u522b\u4e0d\u5e73\u8861\u573a\u666f\uff08\u5982\u8d1f\u9762\u8bc4\u8bba\u8f83\u5c11\uff09\u3002<\/li>\n<li><strong>F1 Score<\/strong>\uff1a\u7cbe\u786e\u7387\u548c\u53ec\u56de\u7387\u7684\u8c03\u548c\u5e73\u5747\uff0c\u8303\u56f40-1\u3002\u5f53\u9700\u8981\u5e73\u8861\u8bef\u62a5\u548c\u6f0f\u62a5\u65f6\u4f7f\u7528\uff08\u4f8b\u5982\uff0c\u5ba2\u670d\u7cfb\u7edf\u65e2\u8981\u51cf\u5c11\u8bef\u5224\u8d1f\u9762\uff0c\u4e5f\u8981\u907f\u514d\u6f0f\u6389\u8d1f\u9762\u53cd\u9988\uff09\u3002<\/li>\n<li><strong>Accuracy<\/strong>\uff1a\u6b63\u786e\u9884\u6d4b\u7684\u6bd4\u4f8b\uff0c\u7b80\u5355\u4f46\u53ef\u80fd\u5728\u4e0d\u5e73\u8861\u6570\u636e\u4e0a\u8bef\u5bfc\u3002\n\u672c\u4f8b\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u6307\u6807\u5df2\u5728\u539f\u59cb\u8bad\u7ec3\u4e2d\u8bc4\u4f30\uff08\u5982SST-2\u4e0aAccuracy\u7ea691%\uff09\uff0c\u90e8\u7f72\u65f6\u66f4\u5173\u6ce8API\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2>7. \u4e0a\u7ebf\u540e\u8bc4\u4f30\uff08\u79bb\u7ebf\u76d1\u63a7\u3001\u7ebf\u4e0a\u6307\u6807\u3001\u91cd\u8bad\u89e6\u53d1\u6761\u4ef6\uff09<\/h2>\n<ul>\n<li><strong>\u79bb\u7ebf\u76d1\u63a7<\/strong>\uff1a\u5b9a\u671f\uff08\u5982\u6bcf\u5468\uff09\u7528\u6807\u6ce8\u6570\u636e\u6d4b\u8bd5API\uff0c\u8ba1\u7b97AUC\/F1\uff0c\u5bf9\u6bd4\u57fa\u7ebf\u4e0b\u964d&gt;5%\u5219\u544a\u8b66\u3002<\/li>\n<li><strong>\u7ebf\u4e0a\u6307\u6807<\/strong>\uff1a\u76d1\u63a7API\u5ef6\u8fdf\uff08P95 &lt; 500ms\uff09\u3001\u9519\u8bef\u7387\uff08&lt; 1%\uff09\u3001\u541e\u5410\u91cf\uff08QPS\uff09\uff0c\u4f7f\u7528Prometheus+Grafana\u3002<\/li>\n<li><strong>\u91cd\u8bad\u89e6\u53d1\u6761\u4ef6<\/strong>\uff1a1) \u79bb\u7ebf\u6307\u6807\u6301\u7eed\u4e0b\u964d\uff1b2) \u4e1a\u52a1\u6570\u636e\u5206\u5e03\u6f02\u79fb\uff08\u5982\u65b0\u9886\u57df\u6587\u672c\uff09\uff1b3) \u6a21\u578b\u7248\u672c\u66f4\u65b0\uff08Hugging Face\u53d1\u5e03\u65b0\u7248\u672c\uff09\u3002<\/li>\n<\/ul>\n<h2>8. \u5e38\u89c1\u5751\u4e0e\u6392\u67e5<\/h2>\n<ol>\n<li><strong>Docker\u955c\u50cf\u8fc7\u5927<\/strong>\uff1a\u4f7f\u7528<code>slim<\/code>\u57fa\u7840\u955c\u50cf\uff0c\u6e05\u7406\u7f13\u5b58\uff08\u5982<code>apt-get autoremove<\/code>\uff09\uff0c\u591a\u9636\u6bb5\u6784\u5efa\u53ef\u8fdb\u4e00\u6b65\u4f18\u5316\u3002<\/li>\n<li><strong>\u6a21\u578b\u52a0\u8f7d\u6162<\/strong>\uff1a\u9996\u6b21\u542f\u52a8\u4f1a\u4e0b\u8f7d\u6a21\u578b\uff08\u7ea6250MB\uff09\uff0c\u4f7f\u7528volumes\u6301\u4e45\u5316\u7f13\u5b58\u907f\u514d\u91cd\u590d\u4e0b\u8f7d\u3002<\/li>\n<li><strong>API\u5e76\u53d1\u6027\u80fd\u4e0d\u8db3<\/strong>\uff1aFastAPI\u9ed8\u8ba4\u5f02\u6b65\uff0c\u4f46\u6a21\u578b\u63a8\u7406\u662fCPU\/GPU\u74f6\u9888\u3002\u4f18\u5316\uff1a\u4f7f\u7528GPU\u3001\u6279\u5904\u7406\u8bf7\u6c42\u3001\u589e\u52a0worker\u6570\uff08\u5982<code>uvicorn --workers 4<\/code>\uff09\u3002<\/li>\n<li><strong>\u5185\u5b58\u6ea2\u51fa<\/strong>\uff1a\u5927\u6587\u672c\u6216\u9ad8\u5e76\u53d1\u53ef\u80fd\u5bfc\u81f4OOM\u3002\u9650\u5236\u8f93\u5165\u957f\u5ea6\uff08\u4ee3\u7801\u4e2d\u5df2\u8bbe\u7f6e<code>max_length=512<\/code>\uff09\uff0c\u76d1\u63a7\u5bb9\u5668\u5185\u5b58\u4f7f\u7528\u3002<\/li>\n<li><strong>\u7248\u672c\u7ba1\u7406\u6df7\u4e71<\/strong>\uff1a\u5728Dockerfile\u4e2d\u56fa\u5b9a\u6a21\u578b\u7248\u672c\uff08\u5982<code>distilbert-base-uncased-finetuned-sst-2-english<\/code>\uff09\uff0c\u4f7f\u7528\u6807\u7b7e\u533a\u5206\u955c\u50cf\u7248\u672c\u3002<\/li>\n<\/ol>","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u624b\u628a\u624b\u6559\u4f60\u7528Docker Compose\u4e00\u952e\u90e8\u7f72Hugging Face\u9884\u8bad\u7ec3\u6587\u672c\u5206\u7c7b\u6a21\u578b\uff0c\u63d0\u4f9b\u751f\u4ea7\u7ea7REST API\u670d\u52a1\uff0c\u5305\u542b\u5b8c\u6574\u4ee3\u7801\u3001\u6027\u80fd\u4f18\u5316\u548c\u5e38\u89c1\u5751\u6392\u67e5\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94],"tags":[89,95,96],"class_list":["post-394","post","type-post","status-publish","format-standard","hentry","category-ai","tag-devops","tag-docker","tag-huggingface"],"views":168,"_links":{"self":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=394"}],"version-history":[{"count":1,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions"}],"predecessor-version":[{"id":417,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions\/417"}],"wp:attachment":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}