gradio/guides/using_flagging.md
Abubakar Abid 4bee781da4
Guides Section and Redesign Parts of the Website (#490)
* added emojis to navbar; added guides main page

* Added guides list

* chatbot tutorial

* chatbot tutorial almost complete

* embedding chatbot

* fixed html rendering inside pre tags issue

* finished guide

* basic search

* search guides content

* design changes

* reading author and date

* sketchpad tutorial

* font change

* added version badge and spaces links

* sketch app

* auto meta images, removed date and author

* removed empty guides

* navbar separate; updated readme

* added navbar; fixed guide

* added flagging guide

* added related spaces to flagging guide

* added tags, small fixes

* footer design

Co-authored-by: Abubakar Abid <aaabid93@gmail.com>
Co-authored-by: Abubakar Abid <a12d@stanford.edu>
Co-authored-by: aliabd <ali.si3luwa@gmail.com>
2022-02-05 01:42:49 +04:00

7.8 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

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 takes a list of strings.
    • If provided, 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. -----> INSERT VIDEO FOR FLAGGING_OPTIONS
  • flagging_dir:
    • This parameter takes a string.
    • What to name the directory where flagged data is stored.
  • flagging_callback:
    • Using this parameter allows you to write custom code that gets run when the flag button is clicked
    • One example is setting it to gr.HuggingFaceDatasetSaver which can allow you to pipe any flagged data into a HuggingFace Dataset.

The 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 :)