Runnable examples for the frozen Gradio Lite 5.45.0 browser runtime.
Compare two strings and display additions and removals with Gradio HighlightedText.
This coding playground returns categorized text spans through gr.HighlightedText. Click Run, edit either text box, and submit to inspect the changed tokens.
Code
from difflib import Differimport gradio as grdef diff_texts(text1, text2): d = Differ()return [ (token[2:], token[0] if token[0] !=" "elseNone)for token in d.compare(text1, text2) ]demo = gr.Interface( diff_texts, [ gr.Textbox( label="Text 1", info="Initial text", lines=3, value="The quick brown fox jumped over the lazy dogs.", ), gr.Textbox( label="Text 2", info="Text to compare", lines=3, value="The fast brown fox jumps over lazy dogs.", ), ], gr.HighlightedText( label="Diff", combine_adjacent=True, show_legend=True, color_map={"+": "red", "-": "green"}, ), flagging_mode="never",)demo.launch()
from difflib import Differ
import gradio as gr
def diff_texts(text1, text2):
d = Differ()
return [
(token[2:], token[0] if token[0] != " " else None)
for token in d.compare(text1, text2)
]
demo = gr.Interface(
diff_texts,
[
gr.Textbox(
label="Text 1",
info="Initial text",
lines=3,
value="The quick brown fox jumped over the lazy dogs.",
),
gr.Textbox(
label="Text 2",
info="Text to compare",
lines=3,
value="The fast brown fox jumps over lazy dogs.",
),
],
gr.HighlightedText(
label="Diff",
combine_adjacent=True,
show_legend=True,
color_map={"+": "red", "-": "green"},
),
flagging_mode="never",
)
demo.launch()