NLP課程文件

與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載入模型

首先,從Hub中選擇Hugging Face提供的數千個模型之一,如第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的推理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類的一些高階功能。這是下一節的主題!

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