Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer, DebertaV2Tokenizer, DebertaV2Model
|
2 |
+
import sentencepiece
|
3 |
+
import streamlit as at
|
4 |
+
import pandas as pd
|
5 |
+
import spacy
|
6 |
+
|
7 |
+
example_list = [
|
8 |
+
|
9 |
+
]
|
10 |
+
|
11 |
+
st.set_page_config(layout="wide")
|
12 |
+
|
13 |
+
st.title("Vocabulary Categorizer")
|
14 |
+
|
15 |
+
model_list = ['spacy/en_core_web_sm',
|
16 |
+
'xlm-roberta-large-finetuned-conll03-english']
|
17 |
+
|
18 |
+
st.sidebar.header("Select a vocabulary categorizer")
|
19 |
+
model_checkpoint = st.sidebar.radio("", model_list)
|
20 |
+
|
21 |
+
st.sidebar.write("Which model highlights the most vocabulary words? Which model highlights the most accurately?")
|
22 |
+
st.sidebar.write("")
|
23 |
+
|
24 |
+
xlm_agg_strategy_info = "'aggregation_strategy' can be selected as 'simple' or 'none' for 'xlm-roberta'."
|
25 |
+
|
26 |
+
st.sidebar.header("Select Aggregation Strategy Type")
|
27 |
+
if model_checkpoint == "xlm-roberta-large-finetuned-conll03-english":
|
28 |
+
aggregation = st.sidebar.radio("", ('simple', 'none'))
|
29 |
+
st.sidebar.write(xlm_agg_strategy_info)
|
30 |
+
st.sidebar.write("")
|
31 |
+
|
32 |
+
st.subheader("Select Text Input Method")
|
33 |
+
input_method = st.radio("", ('Select from Examples', 'Write or Paste New Text'))
|
34 |
+
if input_method == 'Select from Examples':
|
35 |
+
selected_text = st.selectbox('Select Text from List', example_list, index=0, key=1)
|
36 |
+
st.subheader("Text to Run")
|
37 |
+
input_text = st.text_area("Selected Text", selected_text, height=128, max_chars=None, key=2)
|
38 |
+
elif input_method == "Write or Paste New Text":
|
39 |
+
st.subheader("Text to Run")
|
40 |
+
input_text = st.text_area('Write or Paste Text Below', value="", height=128, max_chars=None, key=2)
|
41 |
+
|
42 |
+
@st.cache(allow_output_mutation=True)
|
43 |
+
def setModel(model_checkpoint, aggregation):
|
44 |
+
model = AutoModelForTokenClassification.from_pretrained(model_checkpoint)
|
45 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
46 |
+
return pipeline('ner', model=model, tokenizer=tokenizer, aggregation_strategy=aggregation)
|
47 |
+
|
48 |
+
@st.cache(allow_output_mutation=True)
|
49 |
+
def get_html(html: str):
|
50 |
+
WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
|
51 |
+
html = html.replace("\n", " ")
|
52 |
+
return WRAPPER.format(html)
|
53 |
+
|
54 |
+
Run_Button = st.button("Run", key=None)
|
55 |
+
if Run_Button == True:
|
56 |
+
|
57 |
+
ner_pipeline = setModel(model_checkpoint, aggregation)
|
58 |
+
output = ner_pipeline(input_text)
|
59 |
+
|
60 |
+
df = pd.DataFrame.from_dict(output)
|
61 |
+
if aggregation != "none":
|
62 |
+
cols_to_keep = ['word','entity_group','score','start','end']
|
63 |
+
else:
|
64 |
+
cols_to_keep = ['word','entity','score','start','end']
|
65 |
+
df_final = df[cols_to_keep]
|
66 |
+
|
67 |
+
st.subheader("Recognized Entities")
|
68 |
+
st.dataframe(df_final)
|
69 |
+
|
70 |
+
st.subheader("Spacy Style Display")
|
71 |
+
spacy_display = {}
|
72 |
+
spacy_display["ents"] = []
|
73 |
+
spacy_display["text"] = input_text
|
74 |
+
spacy_display["title"] = None
|
75 |
+
|
76 |
+
for entity in output:
|
77 |
+
if aggregation != "none":
|
78 |
+
spacy_display["ents"].append({"start": entity["start"], "end": entity["end"], "label": entity["entity_group"]})
|
79 |
+
else:
|
80 |
+
spacy_display["ents"].append({"start": entity["start"], "end": entity["end"], "label": entity["entity"]})
|
81 |
+
|
82 |
+
entity_list = ["PER", "LOC", "ORG", "MISC"]
|
83 |
+
colors = {'PER': '#85DCDF', 'LOC': '#DF85DC', 'ORG': '#DCDF85', 'MISC': '#85ABDF',}
|
84 |
+
html = spacy.displacy.render(spacy_display, style="ent", minify=True, manual=True, options={"ents": entity_list, "colors": colors})
|
85 |
+
style = "<style>mark.entity { display: inline-block }</style>"
|
86 |
+
st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)
|