Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Models and Demos

Text and NLP Demos

Build Gradio demos for language tasks: classification, summarization, named entities with HighlightedText, translation and keyword counts. Includes runnable tokenizer stand-ins.

Text demos are the easiest to build and the most common to share: a box goes in, something readable comes out. The skill is choosing an output component that shows the result well. A sentiment score fits gr.Label, entities fit gr.HighlightedText, a summary fits a gr.Textbox, and a table of tokens fits gr.Dataframe.

As elsewhere in this track, the Gradio code is real but read-only (the library cannot run on this site). The runnable blocks reproduce the text-processing logic in plain Python and are labelled stand-ins.

Matching tasks to components

Task Return type Output component
Classification {label: probability} gr.Label
Summarization, translation str gr.Textbox
Named entities, spans list of (text, label) tuples gr.HighlightedText
Token statistics list of rows gr.Dataframe
Long answer with formatting Markdown string gr.Markdown

A multi-output interface returns one value per output, in order:

Python
import gradio as gr

def analyze(text):
    words = text.split()
    summary = " ".join(words[:12]) + ("..." if len(words) > 12 else "")
    stats = {"words": len(words), "characters": len(text)}
    return summary, stats

demo = gr.Interface(
    fn=analyze,
    inputs=gr.Textbox(lines=6, label="Article"),
    outputs=[gr.Textbox(label="Short version"), gr.JSON(label="Stats")],
)
demo.launch()

The page shows a six-line box. After submitting, a textbox shows the first twelve words and a collapsible JSON viewer shows the counts.

Highlighting entities

gr.HighlightedText takes a list of (text, label) pairs. The label can be a string or None for unmarked text, and the component colors each label. This is the standard way to show named-entity recognition, keyword spotting or "which words influenced the score":

Python
def find_entities(text):
    out = nlp(text)               # a real NER pipeline
    spans, last = [], 0
    for ent in out:
        if ent["start"] > last:
            spans.append((text[last:ent["start"]], None))
        spans.append((text[ent["start"]:ent["end"]], ent["entity_group"]))
        last = ent["end"]
    spans.append((text[last:], None))
    return spans

gr.Interface(find_entities, "text", gr.HighlightedText())

For a sentence mentioning a person and a city, the page shows the sentence with the two names in colored boxes captioned PER and LOC. Slicing by character offsets (and filling the gaps with None) is the key trick, and it works with any model that returns offsets. The next block does exactly this with a hand-made rule instead of a neural model.

Python Highlight spans, the HighlightedText way

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output lists plain text pieces with a - label and the three city names labelled CITY. If you join all pieces you get the original sentence back: always check that property, because a highlight function that loses characters is a classic bug.

Summaries, translations and long inputs

Language models have input limits, so demos for long text should truncate or chunk. A simple, honest approach is to split on paragraphs, process each, and join:

Python
def summarize(text, max_length=60):
    chunks = [p for p in text.split("\n\n") if p.strip()]
    parts = [summarizer(c, max_length=max_length)[0]["summary_text"] for c in chunks]
    return "\n".join(parts)

Add sliders so users can tune parameters: gr.Slider(20, 200, value=60, label="Max summary length") as a second input maps to the max_length argument. Use gr.Dropdown(["en", "fr", "de"]) to pick a translation language. Inputs are passed to the function in the order of the inputs list.

Token and word statistics

Teaching demos often show what the model sees. This stand-in tokenizes text, counts words and returns table rows, the same data a gr.Dataframe output expects.

Python Word counts as table rows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

cat appears four times and dog twice, so cat tops the table. A gr.Dataframe(headers=["word", "count", "bar"]) would render these rows as a table; pass a pandas DataFrame or a list of lists.

Translation, zero-shot and other hub tasks

The same recipe covers most Hugging Face tasks: load a pipeline("task-name") once, call it in predict, reshape the result for the right output component. Zero-shot classification takes candidate labels from a second textbox ("sports, politics, tech"), splits on commas and returns the scores dictionary for gr.Label. Question answering takes a context and a question and returns an answer plus a score. Loading a ready-made demo for any hub model is covered in loading hub models.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which output component displays entities as colored spans?

    HighlightedText takes (text, label) tuples and colors each label.
  2. 2. In a highlight function, what must be true of the returned pieces?

    Unlabelled gaps use None so that no characters are lost.
  3. 3. A function with two outputs returns:

    Gradio maps the returned tuple to the outputs list in order.
  4. 4. How should a demo handle inputs longer than the model limit?

    Models have context limits; chunking or truncating keeps the demo reliable.

Summary

  • Choose the output component that fits the result: Label, Textbox, HighlightedText, Dataframe, Markdown.
  • Return multiple values for multiple outputs, in order.
  • HighlightedText uses (text, label) pairs; fill gaps with None so no text is lost.
  • Chunk or truncate long inputs and expose tuning knobs with sliders and dropdowns.

Next: Loading Hub Models and Spaces