Spaces:
Runtime error
Runtime error
File size: 4,485 Bytes
4954c84 b3a02b3 4954c84 55479e9 09bd8f1 55479e9 4954c84 55479e9 a3fa4c8 09bd8f1 a3fa4c8 1d4ebdb 88503f9 a3fa4c8 4954c84 09bd8f1 88503f9 09bd8f1 88503f9 09bd8f1 88503f9 09bd8f1 1d4ebdb 4954c84 1d4ebdb a3fa4c8 55479e9 1d4ebdb 55479e9 09bd8f1 55479e9 1d4ebdb a3fa4c8 1d4ebdb a3fa4c8 1d4ebdb a3fa4c8 88503f9 a473567 a3fa4c8 1d4ebdb 55479e9 1d4ebdb 55479e9 1d4ebdb a3fa4c8 1d4ebdb 88503f9 1d4ebdb 88503f9 1d4ebdb 88503f9 1d4ebdb 88503f9 1d4ebdb 55479e9 09bd8f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import gradio as gr
from ..backend.gradio_highlightedtextbox import HighlightedTextbox
def convert_tagged_text_to_highlighted_text(
tagged_text: str,
tag_id: str | list[str],
tag_open: str | list[str],
tag_close: str | list[str],
) -> list[tuple[str, str | None]]:
return HighlightedTextbox.tagged_text_to_tuples(
tagged_text, tag_id, tag_open, tag_close
)
def convert_highlighted_text_to_tagged_text(
highlighted_text: dict[str, str | list[tuple[str, str | None]]],
tag_id: str | list[str],
tag_open: str | list[str],
tag_close: str | list[str],
) -> str:
return HighlightedTextbox.tuples_to_tagged_text(
highlighted_text["data"], tag_id, tag_open, tag_close
)
def show_info(
highlighted_text: dict[str, str | list[tuple[str, str | None]]],
tag_id: str | list[str],
tag_open: str | list[str],
tag_close: str | list[str],
msg: str,
) -> None:
gr.Info(
f"{msg}: {HighlightedTextbox.tuples_to_tagged_text(highlighted_text['data'], tag_id, tag_open, tag_close)}"
)
initial_text = "It is not something to be ashamed of: it is no different from the <a>personal fears</a> and <b>dislikes</b> of other things that <c>manny peopl</c> have."
with gr.Blocks() as demo:
gr.Markdown("### Parameters to control the highlighted textbox:")
with gr.Row():
tag_id = gr.Dropdown(
choices=["Error A", "Error B", "Error C"],
value=["Error A", "Error B", "Error C"],
multiselect=True,
allow_custom_value=True,
label="Tag ID",
show_label=True,
info="Insert one or more tag IDs to use in the highlighted textbox.",
)
tag_open = gr.Dropdown(
choices=["<a>", "<b>", "<c>"],
value=["<a>", "<b>", "<c>"],
multiselect=True,
allow_custom_value=True,
label="Tag open",
show_label=True,
info="Insert one or more tags to mark the beginning of a highlighted section.",
)
tag_close = gr.Dropdown(
choices=["</a>", "</b>", "</c>"],
value=["</a>", "</b>", "</c>"],
multiselect=True,
allow_custom_value=True,
label="Tag close",
show_label=True,
info="Insert one or more tags to mark the end of a highlighted section.",
)
gr.Markdown(
"""
### Example:
The following text is tagged using the parameters above to mark spans that will be highlighted.
Both the tagged text and the highlighted text are editable, so you can see how the changes in one affect the other.
Highlights will disappear if the highlighted text is edited. Modals will appear upon focus, change, and blur events on the highlighted text.
"""
)
with gr.Row():
tagged = gr.Textbox(
initial_text,
interactive=True,
label="Tagged text",
show_label=True,
info="Tagged text using the format above to mark spans that will be highlighted.",
)
high = HighlightedTextbox(
convert_tagged_text_to_highlighted_text(
tagged.value, tag_id.value, tag_open.value, tag_close.value
),
interactive=True,
label="Highlighted text",
info="Textbox containing editable text with custom highlights.",
show_legend=True,
show_label=True,
legend_label="Legend:",
show_legend_label=True,
show_remove_tags_button=True,
show_copy_button=False,
color_map={"Error A": "blue", "Error B": "red", "Error C": "green"},
)
# Functions
tagged.input(
fn=convert_tagged_text_to_highlighted_text,
inputs=[tagged, tag_id, tag_open, tag_close],
outputs=high,
)
high.input(
fn=convert_highlighted_text_to_tagged_text,
inputs=[high, tag_id, tag_open, tag_close],
outputs=tagged,
)
high.focus(
fn=show_info,
inputs=[high, tag_id, tag_open, tag_close, gr.State("Focus")],
outputs=None,
)
high.blur(
fn=show_info,
inputs=[high, tag_id, tag_open, tag_close, gr.State("Blur")],
outputs=None,
)
high.clear(
fn=show_info,
inputs=[high, tag_id, tag_open, tag_close, gr.State("Remove tags")],
outputs=None,
)
if __name__ == "__main__":
demo.launch()
|