The Label component displays classification results with confidence scores.
Basic usage
import gradio as gr
def classify(text):
return {"positive": 0.8, "negative": 0.2}
gr.Interface(
fn=classify,
inputs=gr.Textbox(),
outputs=gr.Label()
).launch()
Constructor
value
dict[str, float] | str | float | Callable | None
default:"None"
Default value:
- Dict of
{label: confidence} pairs
- Single string/number label
- Function that returns label data
Number of top classes to display
Label displayed above component
Background color as CSS color name or hex code
Whether to show the top class as heading
Events
- change - Triggered when value changes
- select - Triggered when label is selected
Examples
Multi-class classification
import gradio as gr
def predict(image):
return {
"cat": 0.6,
"dog": 0.3,
"bird": 0.1
}
gr.Interface(
fn=predict,
inputs=gr.Image(),
outputs=gr.Label(num_top_classes=3)
).launch()
Single label
import gradio as gr
def categorize(text):
return "Category A" if len(text) > 10 else "Category B"
gr.Interface(
fn=categorize,
inputs=gr.Textbox(),
outputs=gr.Label()
).launch()
With custom color
import gradio as gr
gr.Label(
value={"success": 1.0},
color="green"
)