Skip to main content
Gradio provides several flagging callback classes to handle user feedback and data logging.

FlaggingCallback

class FlaggingCallback(ABC)
An abstract class for defining the methods that any FlaggingCallback should have.

Methods

setup

setup(components, flagging_dir)
This method should be overridden and ensure that everything is set up correctly for flag(). This method gets called once at the beginning of the Interface.launch() method.
components
Sequence[Component]
required
Set of components that will provide flagged data.
flagging_dir
str
required
A string containing the path to the directory where the flagging file should be stored.

flag

flag(flag_data, flag_option=None, username=None)
This method should be overridden by the FlaggingCallback subclass. This gets called every time the flag button is pressed.
flag_data
list[Any]
required
The data to be flagged.
flag_option
str | None
default:"None"
In the case that flagging_options are provided, the flag option that is being used.
username
str | None
default:"None"
The username of the user that is flagging the data, if logged in.
returns
int
The total number of samples that have been flagged.

SimpleCSVLogger

gr.SimpleCSVLogger()
A simplified implementation of the FlaggingCallback abstract class. Each flagged sample (both the input and output data) is logged to a CSV file on the machine running the gradio app.

Example

import gradio as gr

def image_classifier(inp):
    return {'cat': 0.3, 'dog': 0.7}

demo = gr.Interface(
    fn=image_classifier,
    inputs="image",
    outputs="label",
    flagging_callback=gr.SimpleCSVLogger()
)

ClassicCSVLogger

gr.ClassicCSVLogger(simplify_file_data=True)
The classic implementation of the FlaggingCallback abstract class in Gradio versions before 5.0. Each flagged sample (both the input and output data) is logged to a CSV file with headers on the machine running the gradio app.

Parameters

simplify_file_data
bool
default:"True"
If True, the file data will be simplified before being written to the CSV file.

Example

import gradio as gr

def image_classifier(inp):
    return {'cat': 0.3, 'dog': 0.7}

demo = gr.Interface(
    fn=image_classifier,
    inputs="image",
    outputs="label",
    flagging_callback=gr.ClassicCSVLogger()
)

CSVLogger

gr.CSVLogger(
    simplify_file_data=True,
    verbose=True,
    dataset_file_name=None
)
The default implementation of the FlaggingCallback abstract class in gradio>=5.0. Each flagged sample (both the input and output data) is logged to a CSV file with headers on the machine running the gradio app. This implementation is concurrent-safe and creates a new dataset file every time the headers of the CSV change.

Parameters

simplify_file_data
bool
default:"True"
If True, the file data will be simplified before being written to the CSV file. If CSVLogger is being used to cache examples, this is set to False to preserve the original FileData class.
verbose
bool
default:"True"
If True, prints messages to the console about the dataset file creation.
dataset_file_name
str | None
default:"None"
The name of the dataset file to be created (should end in “.csv”). If None, the dataset file will be named “dataset1.csv” or the next available number.

Example

import gradio as gr

def image_classifier(inp):
    return {'cat': 0.3, 'dog': 0.7}

demo = gr.Interface(
    fn=image_classifier,
    inputs="image",
    outputs="label",
    flagging_callback=gr.CSVLogger()
)

ChatCSVLogger

class ChatCSVLogger
Flagging callback for chat conversations. Flagged conversations and like/dislike reactions are logged to a CSV file on the machine running the gradio app.

Methods

setup

setup(flagging_dir)
flagging_dir
str
required
Path to the directory where flagged data is stored.

flag

flag(like_data, messages)
like_data
LikeData
required
The like/dislike data from the event.
messages
list
required
The chat messages.

FlagMethod

class FlagMethod
Helper class that contains the flagging options and calls the flagging method. Also provides visual feedback to the user when flag is clicked.

Constructor

FlagMethod(
    flagging_callback,
    label,
    value,
    visual_feedback=True
)
flagging_callback
FlaggingCallback
required
The flagging callback to use.
label
str
required
The label for the flag button.
value
str | None
required
The value associated with this flag option.
visual_feedback
bool
default:"True"
Whether to provide visual feedback when the flag button is clicked.

Build docs developers (and LLMs) love