A Pythonic framework to simplify AI service building
Homepage • API Playground • Examples • Documentation • CLI References • Twitter • Blog
简化AI服务构建的Pythonic框架
主页• API Playground • 示例• 文档• CLI 参考• Twitter • 博客
LeptonAI python 库允许您轻松地从 python 代码构建 AI 服务。主要特点包括:
- 一个 pythonic 抽象
Photon
,允许您使用几行代码将研究和建模代码转换为服务。 - 简单的抽象,只需几行代码即可启动HuggingFace上的模型。
- 常见模型的预构建示例,例如 Llama、SDXL、Whisper 等。
- AI 定制电池包括自动批处理、后台作业等。
- 一个像原生 Python 函数一样自动调用您的服务的客户端。
- Pythonic 配置规范可以轻松地在云环境中发布。
安装库:
pip install -U leptonai
这将安装leptonai
python 库以及命令行界面lep
。gpt2
然后,您可以用一行代码启动 HuggingFace 模型:
lep photon run --name gpt2 --model hf:gpt2 --local
如果您有权访问 Llama2 模型(在此处申请访问)并且拥有大小合理的 GPU,则可以使用以下命令启动它:
# hint: you can also write `-n` and `-m` for short lep photon run -n llama2 -m hf:meta-llama/Llama-2-7b-chat-hf --local
(请务必使用-hf
Llama2 版本,它与 Huggingface 管道兼容。)
然后您可以通过以下方式访问该服务:
from leptonai.client import Client, local c = Client(local(port=8080)) # Use the following to print the doc print(c.run.__doc__) print(c.run(inputs="I enjoy walking with my cute dog"))
完全托管的 Llama2 模型和 CodeLlama 模型可以在Playground中找到。
支持许多标准 HuggingFace 管道 - 在文档中查找更多详细信息。但并非所有 HuggingFace 模型都受支持,因为其中许多模型包含自定义代码并且不是标准管道。如果您发现想要支持的流行模型,请提出问题或 PR。
您可以从示例存储库中找到更多示例。例如,使用以下命令启动 Stable Diffusion XL 模型:
git clone git@github.com:leptonai/examples.git
cd examples
lep photon run -n sdxl -m advanced/sdxl/sdxl.py --local
服务运行后,您可以通过以下方式访问它:
from leptonai.client import Client, localc = 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)
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)" tabindex="0" role="button">
或者通过http://localhost:8080/ui访问已安装的 Gradio UI 。检查README 文件以获取更多详细信息。
完全托管的 SDXL 托管在https://dashboard.lepton.ai/playground/sdxl上,具有 API 访问权限。
编写自己的光子很简单:编写一个 python Photon 类并用@Photon.handler
. 只要您的输入和输出是 JSON 可序列化的,您就可以开始了。例如,以下代码启动一个简单的 echo 服务:
# my_photon.py from leptonai.photon import Photonclass Echo(Photon): @Photon.handler def echo(self, inputs: str) -> str: """ A simple example to return the original input. """ return inputs
class Echo(Photon): @Photon.handler def echo(self, inputs: str) -> str: """ A simple example to return the original input. """ return inputs" tabindex="0" role="button">
然后您可以使用以下命令启动该服务:
lep photon run -n echo -m my_photon.py --local
然后,您可以按如下方式使用您的服务:
from leptonai.client import Client, localc = Client(local(port=8080))
# will print available paths print(c.paths()) # will print the doc for c.echo. You can also use
c.echo?
in Jupyter. print(c.echo.doc) # will actually call echo. c.echo(inputs="hello world")
c = Client(local(port=8080))
print(c.paths())
print(c.echo.doc)
c.echo(inputs="hello world")" tabindex="0" role="button">
欢迎并高度赞赏贡献和合作。请查看贡献者指南以了解如何参与。
Lepton AI python 库是在 Apache 2.0 许可证下发布的。
开发者说明:LeptonAI 的早期开发是在一个单独的单一存储库中进行的,这就是为什么您可能会看到来自该leptonai/lepton
存储库的提交。我们打算使用这个开源存储库作为未来的真相来源。