Spaces:
Running
Running
File size: 4,487 Bytes
90afd57 |
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import streamlit.components.v1
from htbuilder import HtmlElement, div, span, styles
from htbuilder.units import px, rem, em
def annotation(body, label="", background="#ddd", color="#333", **style):
"""Build an HtmlElement span object with the given body and annotation label.
The end result will look something like this:
[body | label]
Parameters
----------
body : string
The string to put in the "body" part of the annotation.
label : string
The string to put in the "label" part of the annotation.
background : string
The color to use for the background "chip" containing this annotation.
color : string
The color to use for the body and label text.
**style : dict
Any CSS you want to use to customize the containing "chip".
Examples
--------
Produce a simple annotation with default colors:
>>> annotation("apple", "fruit")
Produce an annotation with custom colors:
>>> annotation("apple", "fruit", background="#FF0", color="black")
Produce an annotation with crazy CSS:
>>> annotation("apple", "fruit", background="#FF0", border="1px dashed red")
"""
if "font_family" not in style:
style["font_family"] = "sans-serif"
return span(
style=styles(
background=background,
border_radius=rem(0.33),
color=color,
padding=(rem(0.17), rem(0.67)),
display="inline-flex",
justify_content="center",
align_items="center",
**style,
)
)(
body,
span(
style=styles(
color=color,
font_size=em(0.67),
opacity=0.5,
padding_left=rem(0.5),
text_transform="uppercase",
margin_bottom=px(-2),
)
)(label),
)
def annotated_text(*args, **kwargs):
"""Writes test with annotations into your Streamlit app.
Parameters
----------
*args : str, tuple or htbuilder.HtmlElement
Arguments can be:
- strings, to draw the string as-is on the screen.
- tuples of the form (main_text, annotation_text, background, color) where
background and foreground colors are optional and should be an CSS-valid string such as
"#aabbcc" or "rgb(10, 20, 30)"
- HtmlElement objects in case you want to customize the annotations further. In particular,
you can import the `annotation()` function from this module to easily produce annotations
whose CSS you can customize via keyword arguments.
Examples
--------
>>> annotated_text(
... "This ",
... ("is", "verb", "#8ef"),
... " some ",
... ("annotated", "adj", "#faa"),
... ("text", "noun", "#afa"),
... " for those of ",
... ("you", "pronoun", "#fea"),
... " who ",
... ("like", "verb", "#8ef"),
... " this sort of ",
... ("thing", "noun", "#afa"),
... )
>>> annotated_text(
... "Hello ",
... annotation("world!", "noun", color="#8ef", border="1px dashed red"),
... )
"""
out = div(
style=styles(
font_family="sans-serif",
line_height="1.45",
font_size=px(16),
text_align="right",
)
)
for arg in args:
if isinstance(arg, str):
out(arg)
elif isinstance(arg, HtmlElement):
out(arg)
elif isinstance(arg, tuple):
out(annotation(*arg))
else:
raise Exception("Oh noes!")
streamlit.components.v1.html(str(out), **kwargs)
def shorten_text(text, n, reverse=False):
if text.isspace() or text == "":
return text
if reverse:
text = text[::-1]
words = iter(text.split())
lines, current = [], next(words)
for word in words:
if len(current) + 1 + len(word) > n:
break
else:
current += " " + word
lines.append(current)
if reverse:
return lines[0][::-1]
return lines[0]
def annotate_answer(result):
annotated_text(
shorten_text(
result["original"][: result["new_start"]],
500,
reverse=True,
),
(result["new_answer"], "جواب", "#8ef"),
shorten_text(result["original"][result["new_end"] :], 500) + " ...... إلخ",
)
|