LLM 课程文档

与 Hugging Face Hub 的集成

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

与 Hugging Face Hub 的集成

Ask a Question Open In Colab Open In Studio Lab

为了让你的生活更轻松,Gradio 直接与 Hugging Face Hub 和 Hugging Face Spaces 集成。你只需一行代码即可从 Hub 和 Spaces 加载演示。

从 Hugging Face Hub 加载模型

首先,选择 Hugging Face 通过 Hub 提供的数千个模型之一,如第 4 章所述。

使用特殊的 Interface.load() 方法,你传递 "model/" (或等效的 "huggingface/"),后跟模型名称。例如,以下是为 GPT-J (大型语言模型) 构建演示的代码,并添加几个示例输入

import gradio as gr

title = "GPT-J-6B"
description = "Gradio Demo for GPT-J 6B, a transformer model trained using Ben Wang's Mesh Transformer JAX. 'GPT-J' refers to the class of model, while '6B' represents the number of trainable parameters. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."
article = "<p style='text-align: center'><a href='https://github.com/kingoflolz/mesh-transformer-jax' target='_blank'>GPT-J-6B: A 6 Billion Parameter Autoregressive Language Model</a></p>"

gr.Interface.load(
    "huggingface/EleutherAI/gpt-j-6B",
    inputs=gr.Textbox(lines=5, label="Input Text"),
    title=title,
    description=description,
    article=article,
).launch()

上面的代码将生成以下界面

以这种方式加载模型使用 Hugging Face 的 Inference API,而不是将模型加载到内存中。这对于像 GPT-J 或 T0pp 这样需要大量 RAM 的大型模型来说是理想的选择。

从 Hugging Face Spaces 加载

要从 Hugging Face Hub 加载任何 Space 并在本地重新创建它,你可以将 spaces/ 传递给 Interface,后跟 Space 的名称。

还记得第 1 节中移除图像背景的演示吗?让我们从 Hugging Face Spaces 加载它

gr.Interface.load("spaces/abidlabs/remove-bg").launch()

从 Hub 或 Spaces 加载演示的一个很酷的事情是,你可以通过覆盖任何参数来自定义它们。在这里,我们添加了一个标题并使其与网络摄像头一起工作

gr.Interface.load(
    "spaces/abidlabs/remove-bg", inputs="webcam", title="Remove your webcam background!"
).launch()

现在我们已经探索了几种将 Gradio 与 Hugging Face Hub 集成的方法,让我们来看看 Interface 类的一些高级功能。那是下一节的主题!

< > 在 GitHub 上更新