{"id":204411,"date":"2025-05-29T09:26:04","date_gmt":"2025-05-29T01:26:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204411\/"},"modified":"2025-05-29T09:26:04","modified_gmt":"2025-05-29T01:26:04","slug":"%e4%bd%bf%e7%94%a8-gradio-%e5%92%8c-hugging-face-%e5%9c%a8-lines-%e4%b8%8b%e4%bd%bf%e7%94%a8-python-%e4%bb%a3%e7%a0%81%e6%9e%84%e5%bb%ba%e6%96%87%e6%9c%ac%e6%8f%90%e5%8f%96%e5%99%a8%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204411\/","title":{"rendered":"\u4f7f\u7528 Gradio \u548c Hugging Face \u5728 Lines \u4e0b\u4f7f\u7528 Python \u4ee3\u7801\u6784\u5efa\u6587\u672c\u63d0\u53d6\u5668\u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 Gradio \u548c Hugging Face \u5728 Lines \u4e0b\u4f7f\u7528 Python \u4ee3\u7801\u6784\u5efa\u6587\u672c\u63d0\u53d6\u5668\u5e94\u7528\u7a0b\u5e8f<\/h1>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 Gradio \u548c Hugging Face \u5728 Lines \u4e0b\u4f7f\u7528 Python \u4ee3\u7801\u6784\u5efa\u6587\u672c\u63d0\u53d6\u5668\u5e94\u7528\u7a0b\u5e8f\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241107\/1730944483672c1de3915d8.jpg\" class=\"aligncenter\"><\/p>\n<p>\u539f\u5e16\uff1ahttps:\/\/baxin.netlify.app\/build-text-extractor-python-under-30-lines\/<\/p>\n<p>\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6\u6587\u672c\uff0c\u79f0\u4e3a\u5149\u5b66\u5b57\u7b26\u8bc6\u522b (ocr)\uff0c\u5bf9\u4e8e\u6587\u6863\u5904\u7406\u3001\u6570\u636e\u63d0\u53d6\u548c\u53ef\u8bbf\u95ee\u6027\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\u662f\u4e00\u9879\u5f88\u6709\u4ef7\u503c\u7684\u529f\u80fd\u3002\u5728\u672c\u6307\u5357\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 python \u5e93\u521b\u5efa\u4e00\u4e2a ocr \u5e94\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982\u7528\u4e8e ocr \u7684 pytesseract\u3001\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684 pillow \u4ee5\u53ca\u7528\u4e8e\u6784\u5efa\u4ea4\u4e92\u5f0f ui \u7684 gradio\u3002\u6211\u4eec\u5c06\u5728 hugging face spaces \u4e0a\u90e8\u7f72\u6b64\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5f00\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u4e00\u4e2a hugging face \u5e10\u6237\u5e76\u5bf9 docker \u6709\u57fa\u672c\u7684\u4e86\u89e3\u3002<\/p>\n<ol>\n<li> <strong>\u5bfc\u822a\u5230 hugging face spaces<\/strong>\uff1a\u767b\u5f55 hugging face \u5e76\u8f6c\u5230\u201cspaces\u201d\u90e8\u5206\u3002<\/li>\n<li> <strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7a7a\u95f4<\/strong>\uff1a\n<ul>\n<li>\u70b9\u51fb\u201c\u65b0\u7a7a\u95f4\u201d\u3002<\/li>\n<li>\u4e3a\u60a8\u7684\u7a7a\u95f4\u547d\u540d\uff08\u4f8b\u5982\uff0c\u56fe\u50cf\u6587\u672c\u63d0\u53d6\u5668\uff09\u3002<\/li>\n<li>\u9009\u62e9<strong>gradio<\/strong>\u4f5c\u4e3asdk\u5e76\u8bbe\u7f6e\u53ef\u89c1\u6027\uff08\u516c\u5171\u6216\u79c1\u6709\uff09\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u521b\u5efa\u7a7a\u95f4\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u8981\u5728\u5177\u6709\u6240\u9700\u7cfb\u7edf\u4f9d\u8d56\u9879\u7684 hugging face spaces \u4e0a\u90e8\u7f72\uff08\u4f8b\u5982\u7528\u4e8e ocr \u7684 tesseract\uff09\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u7528\u4e8e\u914d\u7f6e\u73af\u5883\u7684 dockerfile\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\u7684 dockerfile\uff1a<\/p>\n<pre># use an official python runtime as a parent image\nfrom python:3.12\nenv pip_root_user_action=ignore\n\n# set the working directory in the container\nworkdir $home\/app\n\n# install system dependencies\nrun apt-get update &amp;&amp; apt-get install -y\nrun apt-get install -y tesseract-ocr\nrun apt-get install -y libtesseract-dev\nrun apt-get install -y libgl1-mesa-glx\nrun apt-get install -y libglib2.0-0\nrun pip install --upgrade pip\n\n# copy requirements and install dependencies\ncopy requirements.txt requirements.txt\nrun pip install --no-cache-dir -r requirements.txt\n\n# copy the app code\ncopy app.py .\/\n\n# expose the port for gradio\nexpose 7860\n\n# run the application\ncmd [\"python\", \"app.py\"]\n<\/pre>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a app.py \u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a <\/li>\n<\/ol>\n<pre>import gradio as gr\nimport pytesseract\nfrom pil import image\nimport os\n\ndef extract_text(image_path):\n    if not image_path:\n        return \"no image uploaded. please upload an image.\"\n\n    if not os.path.exists(image_path):\n        return f\"error: file not found at {image_path}\"\n\n    try:\n        img = image.open(image_path)\n        text = pytesseract.image_to_string(img)\n        return text if text.strip() else \"no text detected in the image.\"\n    except exception as e:\n        return f\"an error occurred: {str(e)}\"\n\niface = gr.interface(\n    fn=extract_text,\n    inputs=gr.image(type=\"filepath\", label=\"upload an image\"),\n    outputs=gr.textbox(label=\"extracted text\"),\n    title=\"image text extractor\",\n    description=\"upload an image and extract text from it using ocr.\"\n)\n\niface.launch(server_name=\"0.0.0.0\", server_port=7860)\n<\/pre>\n<ol>\n<li>\u521b\u5efarequirements.txt\u6587\u4ef6\u6765\u6307\u5b9a\u4f9d\u8d56\u9879\uff1a <\/li>\n<\/ol>\n<pre>gradio\npytesseract\nPillow\n<\/pre>\n<p>\u6b64\u8bbe\u7f6e\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u56fe\u50cf\u4e0a\u4f20\uff1agr.image(type=&#8221;filepath&#8221;) \u5141\u8bb8\u7528\u6237\u5c06\u56fe\u50cf\u4f5c\u4e3a\u6587\u4ef6\u8def\u5f84\u4e0a\u4f20\uff0c\u7531 pytesseract \u5904\u7406\u3002<\/li>\n<li>\u6587\u672c\u63d0\u53d6\uff1apytesseract.image_to_string \u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6\u6587\u672c\u3002<\/li>\n<li>\u7528\u6237\u754c\u9762\uff1agradio \u751f\u6210\u4e00\u4e2a\u7b80\u5355\u7684 ui\uff0c\u4f9b\u7528\u6237\u4e0a\u4f20\u56fe\u50cf\u548c\u67e5\u770b\u63d0\u53d6\u7684\u6587\u672c\u3002<\/li>\n<\/ul>\n<p>\u521b\u5efa\u6240\u6709\u6587\u4ef6\u540e\uff0c\u5c06\u5b83\u4eec\u63a8\u9001\u5230\u60a8\u7684\u62e5\u62b1\u7a7a\u95f4<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4f7f\u7528 Gradio \u548c Hugging Face \u5728 Lines \u4e0b\u4f7f\u7528 Python \u4ee3\u7801\u6784\u5efa\u6587\u672c\u63d0\u53d6\u5668\u5e94\u7528\u7a0b\u5e8f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Gradio \u548c Hugg&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204411","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204411"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204411\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}