gradio/demo/diff_texts.py

29 lines
662 B
Python
Raw Normal View History

2020-08-22 05:20:05 +08:00
# Demo: (Textbox, Textbox) -> (HighlightedText)
2020-08-20 05:27:22 +08:00
import gradio as gr
from difflib import Differ
2020-08-28 23:56:03 +08:00
2020-08-20 05:27:22 +08:00
def diff_texts(text1, text2):
d = Differ()
return [
2021-05-24 14:53:30 +08:00
(token[2:], token[0] if token[0] != " " else None) for token in d.compare(text1, text2)
2020-08-20 05:27:22 +08:00
]
2020-08-28 23:56:03 +08:00
2020-11-11 22:15:53 +08:00
iface = gr.Interface(
2020-08-20 05:27:22 +08:00
diff_texts,
[
gr.inputs.Textbox(lines=3, default="The quick brown fox jumped over the lazy dogs."),
gr.inputs.Textbox(lines=3, default="The fast brown fox jumps over lazy dogs."),
],
gr.outputs.HighlightedText(color_map={
"+": "lightgreen",
"-": "pink",
}))
2020-08-28 23:56:03 +08:00
2020-11-11 22:15:53 +08:00
iface.test_launch()
if __name__ == "__main__":
2020-11-11 22:21:05 +08:00
iface.launch()