更多示例

本指南涵盖了你可以使用示例执行的更多操作:从目录加载示例、提供部分示例和缓存。 如果 Examples 对你来说是新手,请查看主要功能指南中的介绍。

This guide covers what more you can do with Examples: Loading examples from a directory, providing partial examples, and caching. If Examples is new to you, check out the intro in the Key Features guide.

提供例子

正如Key Features指南中所述,向界面添加示例就像向 examples 关键字参数提供列表列表一样简单。 每个子列表都是一个数据样本,其中每个元素对应于预测函数的输入。 输入的顺序必须与预测函数期望的顺序相同。

As covered in the Key Features guide, adding examples to an Interface is as easy as providing a list of lists to the examples keyword argument. Each sublist is a data sample, where each element corresponds to an input of the prediction function. The inputs must be ordered in the same order as the prediction function expects them.

如果你的界面只有一个输入组件,那么你可以将示例作为常规列表而不是列表列表提供。

If your interface only has one input component, then you can provide your examples as a regular list instead of a list of lists.

从目录加载示例

Loading Examples from a Directory

你还可以指定包含示例的目录的路径。 如果你的界面只接受单一文件类型的输入,例如图像分类器,你可以简单地将目录文件路径传递给 examples= 参数, Interface 将加载目录中的图像作为示例。 在多个输入的情况下,此目录必须包含一个带有示例值的 log.csv 文件。 在计算器演示的上下文中,我们可以设置 examples='/demo/calculator/examples' 并在该目录中包含以下 log.csv 文件:

You can also specify a path to a directory containing your examples. If your Interface takes only a single file-type input, e.g. an image classifier, you can simply pass a directory filepath to the examples= argument, and the Interface will load the images in the directory as examples. In the case of multiple inputs, this directory must contain a log.csv file with the example values. In the context of the calculator demo, we can set examples='/demo/calculator/examples' and in that directory we include the following log.csv file:

num,operation,num2
5,"add",3
4,"divide",2
5,"multiply",3

这在浏览标记的数据时很有用。 只需指向标记的目录, Interface 就会从标记的数据中加载示例。

This can be helpful when browsing flagged data. Simply point to the flagged directory and the Interface will load the examples from the flagged data.

提供部分示例

Providing Partial Examples

有时你的应用程序有很多输入组件,但你只想提供其中一部分的示例。 为了从示例中排除某些输入,请为与这些特定组件对应的所有数据样本传递 None

Sometimes your app has many input components, but you would only like to provide examples for a subset of them. In order to exclude some inputs from the examples, pass None for all data samples corresponding to those particular components.

缓存示例

你可能希望提供一些模型的缓存示例以供用户快速试用,以防你的模型需要一段时间才能正常运行。 如果 cache_examples=TrueInterface 将通过你的应用程序运行所有示例,并在你调用 launch() 方法时保存输出。 此数据将保存在名为 gradio_cached_examples 的目录中。

You may wish to provide some cached examples of your model for users to quickly try out, in case your model takes a while to run normally. If cache_examples=True, the Interface will run all of your examples through your app and save the outputs when you call the launch() method. This data will be saved in a directory called gradio_cached_examples.

每当用户点击一个示例时,输出将自动填充到应用程序中,使用来自这个缓存目录的数据而不是实际运行该函数。 这很有用,因此用户可以在不增加任何负载的情况下快速试用你的模型!

Whenever a user clicks on an example, the output will automatically be populated in the app now, using data from this cached directory instead of actually running the function. This is useful so users can quickly try out your model without adding any load!

请记住,一旦生成缓存,它就不会在以后的发布中更新。 如果示例或功能逻辑发生变化,请删除缓存文件夹以清除缓存并使用另一个 launch() 重建它。

Keep in mind once the cache is generated, it will not be updated in future launches. If the examples or function logic change, delete the cache folder to clear the cache and rebuild it with another launch().