使用 Gradio 在 Spaces 中展示你的專案

釋出於 2021 年 10 月 5 日
在 GitHub 上更新

多虧了 Gradio,展示機器學習專案變得如此簡單。

在這篇博文中,我們將引導你瞭解

  • 最近的 Gradio 整合,它透過利用 推理 API,幫助你用幾行程式碼無縫地演示 Hub 中的模型。
  • 如何使用 Hugging Face Spaces 來託管你自己的模型演示。

Gradio 中的 Hugging Face Hub 整合

你可以輕鬆地在 Hub 中演示你的模型。你只需要定義一個 Interface,它包括

  • 你想要推斷的模型的倉庫 ID
  • 描述和標題
  • 示例輸入來引導你的受眾

定義好你的 Interface 後,只需呼叫 .launch(),你的演示就會開始執行。你可以在 Colab 中執行此操作,但如果你想與社群分享,一個很好的選擇是使用 Spaces!

Spaces 是一種簡單、免費的方式來託管你的 Python ML 演示應用程式。為此,你可以在 https://huggingface.co/new-space 建立一個倉庫,並選擇 Gradio 作為 SDK。完成後,你可以建立一個名為 app.py 的檔案,複製以下程式碼,你的應用程式將在幾秒鐘內啟動並執行!

import gradio as gr

description = "Story generation with GPT-2"
title = "Generate your own story"
examples = [["Adventurer is approached by a mysterious stranger in the tavern for a new quest."]]

interface = gr.Interface.load("huggingface/pranavpsv/gpt2-genre-story-generator",
            description=description,
            examples=examples
)

interface.launch()

你可以在這裡玩故事生成模型

story-gen

在底層,Gradio 呼叫了推理 API,該 API 支援 Transformers 以及其他流行的 ML 框架,如 spaCy、SpeechBrain 和 Asteroid。此整合支援不同型別的模型,包括 image-to-textspeech-to-texttext-to-speech 等。你可以在這裡檢視這個 BigGAN ImageNet text-to-image 模型的示例。實現如下。

import gradio as gr
description = "BigGAN text-to-image demo."
title = "BigGAN ImageNet"
interface = gr.Interface.load("huggingface/osanseviero/BigGAN-deep-128", 
            description=description,
            title = title,
            examples=[["american robin"]]
)
interface.launch()

big-gan

在 Hugging Face Spaces 中使用 Gradio 提供自定義模型檢查點服務

即使推理 API 不支援你的模型,你也可以在 Spaces 中提供你的模型服務。只需將你的模型推理封裝在 Gradio Interface 中,如下所述,並將其放入 Spaces。 imagenet-demo

混合搭配模型!

使用 Gradio Series,你可以混合搭配不同的模型!在這裡,我們在故事生成器頂部放置了一個法語到英語的翻譯模型,並在生成器模型末尾放置了一個英語到法語的翻譯模型,以簡單地建立一個法語故事生成器。

import gradio as gr
from gradio.mix import Series

description = "Generate your own D&D story!"
title = "French Story Generator using Opus MT and GPT-2"
translator_fr = gr.Interface.load("huggingface/Helsinki-NLP/opus-mt-fr-en")
story_gen = gr.Interface.load("huggingface/pranavpsv/gpt2-genre-story-generator")
translator_en = gr.Interface.load("huggingface/Helsinki-NLP/opus-mt-en-fr")
examples = [["L'aventurier est approché par un mystérieux étranger, pour une nouvelle quête."]]

Series(translator_fr, story_gen, translator_en, description = description,
        title = title,
        examples=examples, inputs = gr.inputs.Textbox(lines = 10)).launch()

你可以在這裡檢視法語故事生成器 story-gen-fr

將你的模型上傳到 Spaces

多虧了 Spaces,你可以在 Hugging Face 中提供你的演示服務!為此,只需建立一個新的 Space,然後拖放你的演示或使用 Git。

spaces-demo

輕鬆地在這裡構建你的第一個演示!

社群

註冊登入 評論

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