是否曾经想将 Hugging Face 模型部署到 AWS Lambda,但却被容器构建、冷启动和模型缓存所困扰?以下是如何使用 Scaffoldly 在 5 分钟内完成此操作。
长话短说
-
在 AWS 中创建名为 .cache 的 EFS 文件系统:
- 转到 AWS EFS 控制台
- 点击“创建文件系统”
- 将其命名为.cache
- 选择任意 VPC(Scaffoldly 会处理剩下的事情!)
-
从 python-huggingface 分支创建您的应用程序:
npx scaffoldly create app --template python-huggingface
-
部署它:
cd my-app && npx scaffoldly deploy
就是这样!您将获得在 Lambda 上运行的 Hugging Face 模型(以 openai-community/gpt2 为例),并配有适当的缓存和容器部署。
专业提示:对于 EFS 设置,您可以将其自定义为突发模式下的单个 AZ,以进一步节省成本。 Scaffoldly 会将 Lambda 函数与 EFS 的 VPC、子网和安全组进行匹配。
✨ 查看现场演示和示例代码!
问题
将机器学习模型部署到 AWS Lambda 传统上涉及:
- 构建和管理 Docker 容器
- 弄清楚模型缓存和存储
- 处理 Lambda 的大小限制
- 管理冷启动
- 设置 API 端点
当您只想为模型提供服务时,需要进行大量基础设施工作!
解决方案
Scaffoldly 通过一个简单的配置文件来处理所有这些复杂性。这是一个提供 Hugging Face 模型的完整应用程序(以 openai-community/gpt2 为例):
# app.py from flask import Flask from transformers import pipeline app = Flask(__name__) generator = pipeline('text-generation', model='openai-community/gpt2') @app.route("/") def hello_world(): output = generator("Hello, world,") return output[0]['generated_text']
// requirements.txt Flask ~= 3.0 gunicorn ~= 23.0 torch ~= 2.5 numpy ~= 2.1 transformers ~= 4.46 huggingface_hub[cli] ~= 0.26
// scaffoldly.json { "name": "python-huggingface", "runtime": "python:3.12", "handler": "localhost:8000", "files": ["app.py"], "packages": ["pip:requirements.txt"], "resources": ["arn::elasticfilesystem:::file-system/.cache"], "schedules": { "@immediately": "huggingface-cli download openai-community/gpt2" }, "scripts": { "start": "gunicorn app:app" }, "memorySize": 1024 }
它是如何运作的
Scaffoldly 在幕后做了一些聪明的事情:
-
智能集装箱建筑:
- 自动创建针对 Lambda 优化的 Docker 容器
- 处理所有 Python 依赖项,包括 PyTorch
- 无需编写任何 Docker 命令即可推送到 ECR
-
高效的模型处理:
- 使用 Amazon EFS 缓存模型文件
- 部署后预下载模型以加快冷启动
- 在 Lambda 中自动挂载缓存
-
Lambda 就绪设置:
- Rus 建立一个合适的 WSGI 服务器(gunicorn)
- 创建公共 Lambda 函数 URL
- 代理函数 URL 请求gunicorn
- 管理 IAM 角色和权限
部署是什么样的
这是我在此示例中运行的 npx 脚手架部署命令的输出:
现实世界的性能和成本
✅ 成本:AWS Lambda、ECR 和 EFS 约 0.20 美元/天
✅ 冷启动:第一次请求约 20 秒(模型加载)
✅ 热烈请求:5-20秒(基于CPU的推理)
虽然此设置使用 CPU 推理(比 GPU 慢),但这是一种试验 ML 模型或服务低流量端点的极其经济高效的方法。
其他型号定制
想要使用不同的模型吗?只需更新两个文件:
- 更改app.py中的模型:
npx scaffoldly create app --template python-huggingface
- 更新scaffoldly.json中的下载:
cd my-app && npx scaffoldly deploy
使用私有或门控模型
Scaffoldly 通过 HF_TOKEN 环境变量支持私有和门控模型。您可以通过多种方式添加 Hugging Face 令牌:
- 本地开发:添加到您的 shell 配置文件(.bashrc、.zprofile 等):
# app.py from flask import Flask from transformers import pipeline app = Flask(__name__) generator = pipeline('text-generation', model='openai-community/gpt2') @app.route("/") def hello_world(): output = generator("Hello, world,") return output[0]['generated_text']
- CI/CD:添加为 GitHub Actions Secret:
// requirements.txt Flask ~= 3.0 gunicorn ~= 23.0 torch ~= 2.5 numpy ~= 2.1 transformers ~= 4.46 huggingface_hub[cli] ~= 0.26
令牌将自动用于下载和访问您的私人或门控模型。
CI/CD 奖金
Scaffoldly 甚至生成用于自动部署的 GitHub Action:
// scaffoldly.json { "name": "python-huggingface", "runtime": "python:3.12", "handler": "localhost:8000", "files": ["app.py"], "packages": ["pip:requirements.txt"], "resources": ["arn::elasticfilesystem:::file-system/.cache"], "schedules": { "@immediately": "huggingface-cli download openai-community/gpt2" }, "scripts": { "start": "gunicorn app:app" }, "memorySize": 1024 }
自己尝试一下
完整的示例可以在 GitHub 上找到:
脚手架/脚手架示例#python-huggingface
您可以通过运行以下命令创建您自己的示例副本:
generator = pipeline('text-generation', model='your-model-here')
您可以看到它正在实时运行(尽管由于 CPU 推断,响应可能会很慢):
现场演示
接下来是什么?
- 尝试部署不同的拥抱脸模型
- 加入 Discord 上的 Scaffoldly 社区
- 查看其他示例
- 如果您觉得这有用,请给我们的代码库加星标!
- 脚手架工具链
- Scaffoldly 示例存储库
许可证
Scaffoldly 是开源的,欢迎社区贡献。
- 这些示例已获得 Apache-2.0 许可证的许可。
- 脚手架工具链已获得 FSL-1.1-Apache-2.0 许可证。
您还想在 AWS Lambda 中运行哪些其他模型?请在评论中告诉我!
以上是只需几步即可将拥抱脸部模型部署到 AWS Lambda的详细内容。更多信息请关注PHP中文网其他相关文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpyThonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,withoverHeadeBheadaroundAroundaroundaround64bytaround64bitson64-bitsysysysyssyssyssyssyssyssysssys2)

toensurepythonscriptsbehavecorrectlyacrycrossdevelvermations,登台和生产,USETHESTERTATE:1)Environment varriablesforsimplesettings,2)configurationFilesForefilesForcomPlexSetups,3)dynamiCofforAdaptapity.eachmethodofferSuniquebeneiquebeneiquebeneniqueBenefitsaniqueBenefitsandrefitsandRequiresandRequireSandRequireSca

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),