Interface 的高级功能

Open In Colab Open In Studio Lab

现在我们已经能够构建和共享基本界面了,让我们来探索一些 Interface 更高级的功能,比如会话状态和解释。

使用会话状态保存数据

Gradio 支持存储会话状态,以及保留多次提交的数据。会话状态对于构建聊天机器人等需要在用户与模型交互时保持数据的演示非常有用。请注意,会话状态不会在不同用户之间共享数据。

要将数据存储在会话状态中,你需要做三件事:

  1. 向预测函数中传递一个额外的参数,该参数表示 Interface 此时的状态。
  2. 在预测函数结束时,将状态的更新值添加到函数的返回值中。
  3. 在创建 Interface 时添加 state 输入和 state 输出组件。

请参考下面的聊天机器人示例:

import random

import gradio as gr


def chat(message, history):
    history = history or []
    if message.startswith("How many"):
        response = random.randint(1, 10)
    elif message.startswith("How"):
        response = random.choice(["Great", "Good", "Okay", "Bad"])
    elif message.startswith("Where"):
        response = random.choice(["Here", "There", "Somewhere"])
    else:
        response = "I don't know"
    history.append((message, response))
    return history, history


iface = gr.Interface(
    chat,
    ["text", "state"],
    ["chatbot", "state"],
    allow_screenshot=False,
    allow_flagging="never",
)
iface.launch()

请注意,输出组件的每次提交都被记录了下来。 注:我们还可以向状态参数传递一个默认值,该值会作为会话状态的初始值。

通过解释来理解预测

大多数机器学习模型都是黑盒子,函数的内部逻辑对使用的用户是隐藏的。为了提高透明度,我们设计了一个功能,使得为模型添加解释非常简单,只需将 Interface 类中的 interpretation 参数设置为 default 即可。这样,你的用户就可以理解输入的哪些部分对输出有影响。请看下面的简单界面示例,显示了一个带有解释功能的图像分类器:

import requests
import tensorflow as tf

import gradio as gr

inception_net = tf.keras.applications.MobileNetV2()  # 加载模型

# 下载 ImageNet 的可读标签
response = requests.get("https://git.io/JJkYN")
labels = response.text.split("\n")


def classify_image(inp):
    inp = inp.reshape((-1, 224, 224, 3))
    inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp)
    prediction = inception_net.predict(inp).flatten()
    return {labels[i]: float(prediction[i]) for i in range(1000)}


image = gr.Image(shape=(224, 224))
label = gr.Label(num_top_classes=3)

title = "Gradio Image Classifiction + Interpretation Example"
gr.Interface(
    fn=classify_image, inputs=image, outputs=label, interpretation="default", title=title
).launch()

提交一个输入,单击输出组件中的“Interpret”按钮,将显示图像中的哪一部分区域对 分类的结果产生了影响。

除了 Gradio 提供的默认解释方法之外,你还可以在 interpretation 参数中指定 shap ,并设置 num_shap 参数。这将使用基于 Shapley 的解释方法,你可以在 这里 阅读关于 shap 更多的信息。最后,还可以将自己的解释函数传入 interpretation 参数。在 Gradio 的 开始页面 中可以看到一个例子。

这就是我们对 Gradio 的 Interface 类的深入研究。正如我们所看到的,这个类使得用几行 Python 代码创建机器学习演示变得简单。然而,有时你会想通过改变布局或将多个预测函数链接在一起来定制你的演示。如果我们能以某种方式将 Interface 拆分可定制的 “块”,那不是很好吗?幸运的是,我们可以做到!这是最后一节的主题。

< > Update on GitHub