优化Python框架,轻松构建AI服务
LeptonAI框架提供了一种Pythonic抽象方式,将研究和建模代码快速转化为AI服务。支持启动包括Llama、SDXL、Whisper等常见模型,具备自动批处理和后台作业等AI优化功能。用户可以通过Python客户端轻松调度服务,也能快速在云环境中部署。开发者可参考丰富的文档和示例库,灵活定制Photon类和Handler函数。
一个简化AI服务构建的Python框架
<a href="https://lepton.ai/">主页</a> • <a href="https://dashboard.lepton.ai/playground">API Playground</a> • <a href="https://github.com/leptonai/examples">示例</a> • <a href="https://lepton.ai/docs/">文档</a> • <a href="https://lepton.ai/references">CLI 参考</a> • <a href="https://twitter.com/leptonai">推特</a> • <a href="https://leptonai.medium.com/">博客</a>
LeptonAI Python库让您可以轻松地从Python代码构建AI服务。主要特性包括:
Photon
, 让您可以通过几行代码将研究和建模代码转化成服务。安装库:
pip install -U leptonai
这会安装 leptonai
Python库, 以及命令行接口 lep
。然后,您可以用一行代码启动一个HuggingFace模型,例如gpt2
:
lep photon runlocal --name gpt2 --model hf:gpt2
如果您有权限访问Llama2模型(在这里申请访问)且您有合适大小的GPU,可以用:
# 提示:您也可以用 `-n` 和 `-m` 来简写 lep photon runlocal -n llama2 -m hf:meta-llama/Llama-2-7b-chat-hf
(确保使用Llama2的-hf
版本,它兼容huggingface的pipelines。)
然后,您可以用以下代码访问服务:
from leptonai.client import Client, local c = Client(local(port=8080)) # 打印文档 print(c.run.__doc__) print(c.run(inputs="I enjoy walking with my cute dog"))
完全托管的Llama2模型和CodeLlama模型可以在playground找到。
许多标准的HuggingFace pipelines都得到了支持 - 了解更多细节请查看文档。并非所有HuggingFace模型都得到了支持,因为其中许多包含自定义代码并非标准pipelines。如果您发现有流行模型希望得到支持,请提出问题或提交PR。
您可以在示例库中找到更多示例。例如,启动Stable Diffusion XL模型:
git clone git@github.com:leptonai/examples.git cd examples
lep photon runlocal -n sdxl -m advanced/sdxl/sdxl.py
一旦服务运行了,您可以用以下代码访问它:
from leptonai.client import Client, local c = Client(local(port=8080)) img_content = c.run(prompt="a cat launching rocket", seed=1234) with open("cat.png", "wb") as fid: fid.write(img_content)
或者访问挂载的Gradio UI http://localhost:8080/ui