像函数一样使用渐变块

标签:翻译,HUB,空间

先决条件:本指南建立在块介绍的基础上。 请务必先阅读该指南

Prerequisite: This Guide builds on the Blocks Introduction. Make sure to read that guide first.

介绍

你知道吗,除了作为一个全栈机器学习演示之外,Gradio Blocks 应用程序还是一个普通的旧 python 函数!?

Did you know that apart from being a full-stack machine learning demo, a Gradio Blocks app is also a regular-old python function!?

这意味着如果你有一个名为 demo gradio Blocks(或界面)应用程序,你可以像使用任何 python 函数一样使用 demo

This means that if you have a gradio Blocks (or Interface) app called demo, you can use demo like you would any python function.

因此,执行类似 output = demo("Hello", "friend") 操作将在输入“Hello”和“friend”上运行 demo 中定义的第一个事件,并将其存储在变量 output 中。

So doing something like output = demo("Hello", "friend") will run the first event defined in demo on the inputs "Hello" and "friend" and store it in the variable output.

如果我让你睡觉🥱,请多多包涵! 通过像函数一样使用应用程序,你可以无缝地组合 Gradio 应用程序。 下一节将展示如何。

If I put you to sleep 🥱, please bear with me! By using apps like functions, you can seamlessly compose Gradio apps. The following section will show how.

像对待函数一样对待空间

假设我们有以下将英语文本翻译成德语文本的演示。

Let's say we have the following demo that translates english text to german text.

import gradio as gr

from transformers import pipeline

pipe = pipeline("translation", model="t5-base")


def translate(text):
    return pipe(text)[0]["translation_text"]


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            english = gr.Textbox(label="English text")
            translate_btn = gr.Button(value="Translate")
        with gr.Column():
            german = gr.Textbox(label="German Text")

    translate_btn.click(translate, inputs=english, outputs=german, api_name="translate-to-german")
    examples = gr.Examples(examples=["I went to the supermarket yesterday.", "Helen is a good swimmer."],
                           inputs=[english])

demo.launch()

我已经将它托管在gradio/english_translator的 Hugging Face 空间中。

I already went ahead and hosted it in Hugging Face spaces at gradio/english_translator.

你也可以看到下面的演示:

You can see the demo below as well:

现在,假设你有一个生成英文文本的应用程序,但你还想生成德文文本。

Now, let's say you have an app that generates english text, but you wanted to additionally generate german text.

你可以:

You could either:

  1. 复制我的英德翻译源代码并将其粘贴到你的应用程序中。

    Copy the source code of my english-to-german translation and paste it in your app.

  2. 在你的应用程序中加载我的英德翻译,并将其视为普通的 Python 函数。

    Load my english-to-german translation in your app and treat it like a normal python function.

选项 1 在技术上总是可行的,但它通常会引入不必要的复杂性。

Option 1 technically always works, but it often introduces unwanted complexity.

选项 2 允许你借用你想要的功能,而无需紧密耦合我们的应用程序。

Option 2 lets you borrow the functionality you want without tightly coupling our apps.

你所要做的就是在源文件中调用 Blocks.load 类方法。 之后,你可以像使用常规 python 函数一样使用我的翻译应用程序!

All you have to do is call the Blocks.load class method in your source file. After that, you can use my translation app like a regular python function!

以下代码片段和演示展示了如何使用 Blocks.load

The following code snippet and demo shows how to use Blocks.load.

请注意,变量 english_translator 是我的英语到德语应用程序,但它像常规函数一样在 generate_text 中使用。

Note that the variable english_translator is my english to german app, but its used in generate_text like a regular function.

import gradio as gr

from transformers import pipeline

english_translator = gr.Blocks.load(name="spaces/gradio/english_translator")
english_generator = pipeline("text-generation", model="distilgpt2")


def generate_text(text):
    english_text = english_generator(text)[0]["generated_text"]
    german_text = english_translator(english_text)
    return english_text, german_text


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            seed = gr.Text(label="Input Phrase")
        with gr.Column():
            english = gr.Text(label="Generated English Text")
            german = gr.Text(label="Generated German Text")
    btn = gr.Button("Generate")
    btn.click(generate_text, inputs=[seed], outputs=[english, german])
    gr.Examples(["My name is Clara and I am"], inputs=[seed])

demo.launch()

如何控制使用应用程序中的哪个功能

如果你正在加载的应用程序定义了多个函数,你可以通过 fn_indexapi_name 参数指定要使用的函数。

If the app you are loading defines more than one function, you can specify which function to use with the fn_index and api_name parameters.

在我们的英语到德语演示代码中,你将看到以下行:

In the code for our english to german demo, you'll see the following line:

translate_btn.click(translate, inputs=english, outputs=german, api_name="translate-to-german")

api_name 在我们的应用程序中为这个函数提供了一个唯一的名称。 你可以使用此名称来告诉 gradio 你要使用上游空间中的哪个函数:

The api_name gives this function a unique name in our app. You can use this name to tell gradio which function in the upstream space you want to use:

english_generator(text, api_name="translate-to-german")[0]["generated_text"]

你还可以使用 fn_index 参数。 想象一下,我的应用程序还定义了一个英语到西班牙语的翻译功能。 为了在我们的文本生成应用程序中使用它,我们将使用以下代码:

You can also use the fn_index parameter. Imagine my app also defined an english to spanish translation function. In order to use it in our text generation app, we would use the following code:

english_generator(text, fn_index=1)[0]["generated_text"]

gradio 空间中的函数是零索引的,因此由于西班牙语翻译器将是我空间中的第二个函数,因此你将使用索引 1。

Functions in gradio spaces are zero-indexed, so since the spanish translator would be the second function in my space, you would use index 1.

离别辞

我们展示了如何将 Blocks 应用程序视为常规 python 来帮助你跨不同应用程序组合功能。 任何 Blocks 应用程序都可以像功能一样对待,但一个强大的模式是先 load 托管在Hugging Face Spaces上的应用程序,然后再将其视为你自己应用程序中的功能。 你还可以加载托管在Hugging Face Model Hub上的模型 - 有关示例,请参阅使用 Hugging Face Integrations指南。

We showed how treating a Blocks app like a regular python helps you compose functionality across different apps. Any Blocks app can be treated like a function, but a powerful pattern is to load an app hosted on Hugging Face Spaces prior to treating it like a function in your own app. You can also load models hosted on the Hugging Face Model Hub - see the Using Hugging Face Integrations guide for an example.

快乐的建筑! ⚒️

Happy building! ⚒️