gradio/guides/using_flagging.md
aliabid94 70ebf698fa
Live website changes (#1578)
* fix audio output cache (#804)

* fix audio output cache

* changes

* version update

Co-authored-by: Ali Abid <aliabid94@gmail.com>

* Website Tracker Slackbot (#797)

* added commands to reload script

* catch errors with git pull

* read new webhook from os variable

* correcting bash

* bash fixes

* formatting

* more robust error checking

* only sends success if git changes

* catching error from script

* escaping error text to send with curl

* correct text escaping for error message

* fix search bug in guides (#809)

* Update getting_started.md (#808)

* Fix type of server returned by `Launchable` (#810)

* `Launchable` returns a FastAPI now

* Update .gitignore

* Add a missing line to getting started (#816)



Former-commit-id: 81e271ca22 [formerly 96f203108b]
Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f

* Add a missing line to getting started (#816)



Former-commit-id: 81e271ca22 [formerly 81e271ca22 [formerly 96f203108b]]
Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f
Former-commit-id: b5112c3f42

* Add a missing line to getting started (#816)



Former-commit-id: 81e271ca22 [formerly 81e271ca22 [formerly 81e271ca22 [formerly 96f203108b]]]
Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f
Former-commit-id: b5112c3f42
Former-commit-id: bce6f9c4c5

* Add a missing line to getting started (#816)



Former-commit-id: 81e271ca22 [formerly 81e271ca22 [formerly 81e271ca22 [formerly 81e271ca22 [formerly 96f203108b]]]]
Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f
Former-commit-id: b5112c3f42
Former-commit-id: bce6f9c4c5
Former-commit-id: feba0888e3

* Add a missing line to getting started (#816)

* Clean-History
- Remove 51MB file with this commit


Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c

* Clean-History
- Remove 51MB file with this commit


Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c
Former-commit-id: dd700c33cc

* Clean-History
- Remove 51MB file with this commit


Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c
Former-commit-id: dd700c33cc
Former-commit-id: 0d80e6a056

* Clean-History
- Remove 51MB file with this commit


Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c
Former-commit-id: dd700c33cc
Former-commit-id: 0d80e6a056
Former-commit-id: 20523b0519

* changes

* changes

* Homepage: header image size (#1347)

* image size

* image in local assets

* add dall-e mini banner

* undo ui changes

* changes

* changes

* updates

* updates

* changes

* changes

* changes

* h11 dependency

* add npm build-mac

* expand demo button to all classes

* add demos to docstrings

* add anchor tags to headers

* add required tag to param table

* add consistent styling for headers

* skip param beginning with underscore from docs

* skip kwargs param from docs

* remove types in param docstring

* override signature to reflect usage

* add supported events

* add step-by-step guides

* fix guide contribution link

* add related spaces

* fix img styling on guides

* pin quickstart, advanced, and block guides to top

* margin fix

* autogenerated copy buttons for all codeblocks

* changes

* documentaiton

* format

* launch

* formatting

* style changes

* remove backticks

* changes

* changes

Co-authored-by: Ali Abid <aliabid94@gmail.com>
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
Co-authored-by: Julien Chaumond <julien@huggingface.co>
Co-authored-by: Ömer Faruk Özdemir <farukozderim@gmail.com>
Co-authored-by: Ali <ali.abid@huggingface.co>
Co-authored-by: Victor Muštar <victor.mustar@gmail.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2022-07-06 16:22:10 -07:00

8.6 KiB

Using Flagging

Related spaces: https://huggingface.co/spaces/aliabd/calculator-flagging-crowdsourced, https://huggingface.co/spaces/aliabd/calculator-flagging-options, https://huggingface.co/spaces/aliabd/calculator-flag-basic Tags: FLAGGING, DATA

Introduction

When you deploy or demo a machine learning model, you may find that it behaves differently than how you expected (e.g. the model makes an incorrect prediction) when a user tries it with their own data. Capturing these "hard" data points is important because it allows you to make you machine learning model more reliable and robust.

Gradio simplifies the collection of this data by including a FLAG button with every Interface. This allows your user or tester to easily send data back to you, whether the model is running locally or has been shared by setting share=True.

The Flag button

Underneath the output interfaces, there is a button marked Flag. When a user testing your model sees input with interesting output, such as erroneous or unexpected model behaviour, they can flag the input for the interface creator to review.

flag button

There are four parameters gr.Interface that control how flagging works. We will go over them in greater detail.

  • allow_flagging: this parameter can be set to either "manual", "auto", or "never".
    • manual: users will see a button to flag, and events are only flagged when it's clicked.
    • auto: users will not see a button to flag, but every event will be flagged automatically.
    • never: users will not see a button to flag, and no event will be flagged.
  • flagging_options: this parameter can be either None (default) or a list of strings.
    • If None, then the user simply clicks on the Flag button and no additional options are shown.
    • If a list of strings are provided, this allows user to select from a list of options when flagging. Only applies if allow_flagging is "manual".
    • The chosen option is then piped along with the input and output.
  • flagging_dir: this parameter takes a string.
    • It represents what to name the directory where flagged data is stored.
  • flagging_callback: this parameter takes an instance of a subclass of the FlaggingCallback class
    • Using this parameter allows you to write custom code that gets run when the flag button is clicked
    • By default, this is set to an instance of gr.CSVLogger
    • One example is setting it to an instance of gr.HuggingFaceDatasetSaver which can allow you to pipe any flagged data into a HuggingFace Dataset.

What happens to flagged data?

Within the directory provided by the flagging_dir argument, a CSV file will log the flagged data.

Here's an example: The code below creates the calculator interface embedded below it:

import gradio as gr


def calculator(num1, operation, num2):
    if operation == "add":
        return num1 + num2
    elif operation == "subtract":
        return num1 - num2
    elif operation == "multiply":
        return num1 * num2
    elif operation == "divide":
        return num1 / num2


iface = gr.Interface(
    calculator,
    ["number", gr.inputs.Radio(["add", "subtract", "multiply", "divide"]), "number"],
    "number",
    allow_flagging="manual"
)

iface.launch()

When you click the flag button above, the directory where the interface was launched will include a new flagged subfolder, with a csv file inside it. This csv file includes all the data that was flagged.

+-- flagged/
|   +-- logs.csv

flagged/logs.csv

num1,operation,num2,Output,timestamp
5,add,7,12,2022-01-31 11:40:51.093412
6,subtract,1.5,4.5,2022-01-31 03:25:32.023542

If the interface involves file data, such as for Image and Audio components, folders will be created to store those flagged data as well. For example an image input to image output interface will create the following structure.

+-- flagged/
|   +-- logs.csv
|   +-- image/
|   |   +-- 0.png
|   |   +-- 1.png
|   +-- Output/
|   |   +-- 0.png
|   |   +-- 1.png

flagged/logs.csv

im,Output timestamp
im/0.png,Output/0.png,2022-02-04 19:49:58.026963
im/1.png,Output/1.png,2022-02-02 10:40:51.093412

If you wish for the user to provide a reason for flagging, you can pass a list of strings to the flagging_options argument of Interface. Users will have to select one of the strings when flagging, which will be saved as an additional column to the CSV.

If we go back to the calculator example, the following code will create the interface embedded below it.

iface = gr.Interface(
    calculator,
    ["number", gr.inputs.Radio(["add", "subtract", "multiply", "divide"]), "number"],
    "number",
    allow_flagging="manual",
    flagging_options=["wrong sign", "off by one", "other"]
)

iface.launch()

When users click the flag button, the csv file will now include a column indicating the selected option.

flagged/logs.csv

num1,operation,num2,Output,flag,timestamp
5,add,7,-12,wrong sign,2022-02-04 11:40:51.093412
6,subtract,1.5,3.5,off by one,2022-02-04 11:42:32.062512

Doing more with the data

Suppose you want to take some action on the flagged data, instead of just saving it. Perhaps you want to trigger your model to retrain, or even just share it with others in a cloud dataset. We've made this super easy with the flagging_callback parameter.

For example, below we're going to pipe flagged data from our calculator example into a crowd-sourced Hugging Face Dataset.

import os

HF_TOKEN = os.getenv('HF_TOKEN')
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-calculator-demo")

iface = gr.Interface(
    calculator,
    ["number", gr.inputs.Radio(["add", "subtract", "multiply", "divide"]), "number"],
    "number",
    allow_flagging="manual",
    flagging_options=["wrong sign", "off by one", "other"],
    flagging_callback=hf_writer
)

iface.launch()

You can now see all the examples flagged above in this public HF dataset.

flagging callback hf

We created the gr.HuggingFaceDatasetSaver class, but you can pass your own custom class as long as it inherits from FLaggingCallback defined in this file. If you create a cool callback, please contribute it to the repo!

Privacy

Please make sure your users understand when the data they submit is being saved, and what you plan on doing with it. This is especially important when you use allow_flagging=auto. We suggest including this info in the description so that it's read before the interface.

That's all! Happy building :)