Spaces:
Running
Running
Initial commit of demo
Browse files- app.py +97 -0
- requirements.txt +3 -0
app.py
CHANGED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spacy.displacy
|
2 |
+
import streamlit as st
|
3 |
+
from flair.models import SequenceTagger
|
4 |
+
from flair.splitter import SegtokSentenceSplitter
|
5 |
+
from colorhash import ColorHash
|
6 |
+
|
7 |
+
# st.title("Flair NER Demo")
|
8 |
+
st.set_page_config(layout="centered")
|
9 |
+
|
10 |
+
# models to choose from
|
11 |
+
model_map = {
|
12 |
+
"find Entities (default)": "ner",
|
13 |
+
"find Entities (18-class)": "ner-ontonotes",
|
14 |
+
"find Parts-of-Speech": "upos",
|
15 |
+
}
|
16 |
+
|
17 |
+
# Block 1: Users can select a model
|
18 |
+
st.subheader("Select Model")
|
19 |
+
selected_model_id = st.selectbox("This is a check box",
|
20 |
+
model_map.keys(),
|
21 |
+
label_visibility="collapsed",
|
22 |
+
)
|
23 |
+
|
24 |
+
# Block 2: Users can input text
|
25 |
+
st.subheader("Input your text here")
|
26 |
+
input_text = st.text_area('Write or Paste Text Below',
|
27 |
+
value="George was born in Washington.",
|
28 |
+
height=128,
|
29 |
+
max_chars=None,
|
30 |
+
label_visibility="collapsed")
|
31 |
+
|
32 |
+
|
33 |
+
@st.cache_resource()
|
34 |
+
def get_model(model_name):
|
35 |
+
return SequenceTagger.load(model_map[model_name])
|
36 |
+
|
37 |
+
|
38 |
+
def get_html(html: str):
|
39 |
+
WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
|
40 |
+
html = html.replace("\n", " ")
|
41 |
+
return WRAPPER.format(html)
|
42 |
+
|
43 |
+
|
44 |
+
def color_variant(hex_color, brightness_offset=1):
|
45 |
+
""" takes a color like #87c95f and produces a lighter or darker variant
|
46 |
+
taken from: https://chase-seibert.github.io/blog/2011/07/29/python-calculate-lighterdarker-rgb-colors.html
|
47 |
+
"""
|
48 |
+
if len(hex_color) != 7:
|
49 |
+
raise Exception("Passed %s into color_variant(), needs to be in #87c95f format." % hex_color)
|
50 |
+
rgb_hex = [hex_color[x:x + 2] for x in [1, 3, 5]]
|
51 |
+
new_rgb_int = [int(hex_value, 16) + brightness_offset for hex_value in rgb_hex]
|
52 |
+
new_rgb_int = [min([255, max([0, i])]) for i in new_rgb_int] # make sure new values are between 0 and 255
|
53 |
+
# hex() produces "0x88", we want just "88"
|
54 |
+
return "#" + "".join([hex(i)[2:] for i in new_rgb_int])
|
55 |
+
|
56 |
+
|
57 |
+
# Block 3: Output is displayed
|
58 |
+
button_clicked = st.button("**Click to tag the input text**", key=None)
|
59 |
+
|
60 |
+
if button_clicked:
|
61 |
+
|
62 |
+
# get a sentence splitter and split text into sentences
|
63 |
+
splitter = SegtokSentenceSplitter()
|
64 |
+
sentences = splitter.split(input_text)
|
65 |
+
|
66 |
+
# get the model and predict
|
67 |
+
model = get_model(selected_model_id)
|
68 |
+
model.predict(sentences)
|
69 |
+
|
70 |
+
spacy_display = {"ents": [], "text": input_text, "title": None}
|
71 |
+
|
72 |
+
predicted_labels = set()
|
73 |
+
for sentence in sentences:
|
74 |
+
for prediction in sentence.get_labels():
|
75 |
+
spacy_display["ents"].append(
|
76 |
+
{"start": prediction.data_point.start_position + sentence.start_position,
|
77 |
+
"end": prediction.data_point.end_position + sentence.start_position,
|
78 |
+
"label": prediction.value})
|
79 |
+
predicted_labels.add(prediction.value)
|
80 |
+
|
81 |
+
# create colors for each label
|
82 |
+
colors = {}
|
83 |
+
for label in predicted_labels:
|
84 |
+
colors[label] = color_variant(ColorHash(label).hex, brightness_offset=85)
|
85 |
+
|
86 |
+
# use displacy to render
|
87 |
+
html = spacy.displacy.render(spacy_display,
|
88 |
+
style="ent",
|
89 |
+
minify=True,
|
90 |
+
manual=True,
|
91 |
+
options={
|
92 |
+
"colors": colors,
|
93 |
+
},
|
94 |
+
)
|
95 |
+
style = "<style>mark.entity { display: inline-block }</style>"
|
96 |
+
st.subheader("Found entities")
|
97 |
+
st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
flair
|
2 |
+
spacy
|
3 |
+
colorhash
|