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.
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.
In the case that flagging_options are provided, the flag option that is being used.
The username of the user that is flagging the data, if logged in.
The total number of samples that have been flagged.
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
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
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.
If True, prints messages to the console about the dataset file creation.
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
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
Path to the directory where flagged data is stored.
flag
flag(like_data, messages)
The like/dislike data from the event.
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
)
The flagging callback to use.
The label for the flag button.
The value associated with this flag option.
Whether to provide visual feedback when the flag button is clicked.