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 的名稱。

還記得第一節中移除影像背景的演示嗎?讓我們從 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 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.