2020-08-20 05:27:22 +08:00
|
|
|
from difflib import Differ
|
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
2020-08-20 05:27:22 +08:00
|
|
|
def diff_texts(text1, text2):
|
|
|
|
d = Differ()
|
|
|
|
return [
|
2022-01-21 21:44:12 +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
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2022-03-28 23:11:14 +08:00
|
|
|
demo = gr.Interface(
|
2020-08-20 05:27:22 +08:00
|
|
|
diff_texts,
|
|
|
|
[
|
2022-05-11 00:01:36 +08:00
|
|
|
gr.Textbox(
|
2023-02-23 07:16:15 +08:00
|
|
|
label="Text 1",
|
|
|
|
info="Initial text",
|
2022-05-11 00:01:36 +08:00
|
|
|
lines=3,
|
2022-05-11 08:11:43 +08:00
|
|
|
value="The quick brown fox jumped over the lazy dogs.",
|
2022-05-11 00:01:36 +08:00
|
|
|
),
|
|
|
|
gr.Textbox(
|
2023-02-23 07:16:15 +08:00
|
|
|
label="Text 2",
|
|
|
|
info="Text to compare",
|
2022-05-11 00:01:36 +08:00
|
|
|
lines=3,
|
2022-05-11 08:11:43 +08:00
|
|
|
value="The fast brown fox jumps over lazy dogs.",
|
2022-05-11 00:01:36 +08:00
|
|
|
),
|
2020-08-20 05:27:22 +08:00
|
|
|
],
|
2022-06-02 01:02:18 +08:00
|
|
|
gr.HighlightedText(
|
|
|
|
label="Diff",
|
|
|
|
combine_adjacent=True,
|
|
|
|
).style(color_map={"+": "red", "-": "green"}),
|
2022-01-21 21:44:12 +08:00
|
|
|
)
|
2020-10-23 22:28:30 +08:00
|
|
|
if __name__ == "__main__":
|
2022-03-28 23:11:14 +08:00
|
|
|
demo.launch()
|