Transformers documentation

机器学习应用

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

机器学习应用

Gradio 是一个用于快速构建和共享机器学习应用的便捷库,它与 Pipeline 集成,可快速为推理创建简单的界面。

在开始之前,请确保已安装 Gradio。

!pip install gradio

为你的任务创建一个 pipeline,然后将其传递给 Gradio 的 Interface.from_pipeline 函数以创建界面。Gradio 会自动为 Pipeline 确定合适的输入和输出组件。

添加 launch 以创建 Web 服务器并启动应用。

from transformers import pipeline
import gradio as gr

pipeline = pipeline("image-classification", model="google/vit-base-patch16-224")
gr.Interface.from_pipeline(pipeline).launch()

Web 应用默认在本地服务器上运行。若要与其他用户共享该应用,请在 launch 中设置 share=True 以生成一个临时的公开链接。如需更持久的方案,可将应用托管在 Hugging Face Spaces 上。

gr.Interface.from_pipeline(pipeline).launch(share=True)

下面的 Space 使用上述代码创建,并托管在 Spaces 上。

Update on GitHub