gradio/guides/02_building-interfaces/03_more-on-examples.md
Ali Abdalla cd693708cf
Converting the website into sveltekit (#3437)
* index page

* demos page

* guides gallery page

* guides

* some docs work

* changes

* changes

* docs work

* refactor some to ssr

* more refactoring

* add metatags

* add special docs pages and improve nav

* fix prev next in combining

* add changelog

* Site slugs for new website (#3431)

* safe slugs for docs

* add slugs to guides

* changes

* add flagging

---------

Co-authored-by: aliabd <ali.si3luwa@gmail.com>

* make anchor tags visible on hover

* add anchor tags to docs

* fix @html in codeblocks

* fix demos in guides

* syntax highlighting code in example usage

* fix @html in changelog

* fix contributing lin

* fix assets in guides

* fix broken assets on build

* error page

* fix meta tags updating

* move guides to be /guides/[guide] instead of /[guide]

* add headers to sections and make them linkable - freddy feedback

* add guides section to docs

* tighten width and add second nav bar

* styling second nav bar

* smooth scrolling in docs and guides

* make components clickable in event listener graph

* load latest gradio.js

* menu bar on docs mobile

* scrolling highlight menu and remove base docs page

* vercel

* refactor guides

* fix slugs in docs

* fix &lt; and code formatting in guides

* added search

* redirect all old links

* fix bad merge

* fix paths

* Fix css issue with spaces logo

* add status page link to footer

* add themes to docs

* fix new documentation.py path

* add python client docs

* make docs faster

* add clients ot docs

* colors

* convert to adapter static

* prerender

* fix broken paths in guides

* fix broken slugs

* Aliabd/website sveltekit test (#4572)

* fixes to paths

* fixes

* typechecking

* fix

* fix

* fix

* types lib

* more type fixes

* extends fix

* typing fix

* typing fix

* json typing fix

* add jsons

* rollup

* tweak

* fix lockfile

* fix maybe

* fix maybe

* changes

* ui functional fix

* oops

* pnpm version

* fix app

---------

Co-authored-by: pngwn <hello@pngwn.io>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2023-07-04 16:33:22 +03:00

2.7 KiB

More on 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.

Providing 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

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

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

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.

Caching 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!

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().