import sentencepiece import streamlit as st import pandas as pd import spacy import roner example_list = [ "Ana merge în București.", """Ana merge în București. Ana merge în București. Ana merge în București. Ana merge în București. Ana merge în București. Ana merge în București.""" ] st.set_page_config(layout="wide") st.title("Demo for Romanian NER") model_list = ['dumitrescustefan/bert-base-romanian-ner'] st.sidebar.header("Select NER Model") model_checkpoint = st.sidebar.radio("", model_list) st.sidebar.write("For details of models: 'https://huggingface.co/dumitrescustefan/") st.sidebar.write("") xlm_agg_strategy_info = "'aggregation_strategy' can be selected as 'simple' or 'none' for 'xlm-roberta' because of the RoBERTa model's tokenization approach." st.sidebar.header("Select Aggregation Strategy Type") if model_checkpoint == "akdeniz27/xlm-roberta-base-turkish-ner": aggregation = st.sidebar.radio("", ('simple', 'none')) st.sidebar.write(xlm_agg_strategy_info) elif model_checkpoint == "xlm-roberta-large-finetuned-conll03-english": aggregation = st.sidebar.radio("", ('simple', 'none')) st.sidebar.write(xlm_agg_strategy_info) st.sidebar.write("") st.sidebar.write("This English NER model is included just to show the zero-shot transfer learning capability of XLM-Roberta.") else: aggregation = st.sidebar.radio("", ('first', 'simple', 'average', 'max', 'none')) st.sidebar.write("Please refer 'https://huggingface.co/transformers/_modules/transformers/pipelines/token_classification.html' for entity grouping with aggregation_strategy parameter.") st.subheader("Select Text Input Method") input_method = st.radio("", ('Select from Examples', 'Write or Paste New Text')) if input_method == 'Select from Examples': selected_text = st.selectbox('Select Text from List', example_list, index=0, key=1) st.subheader("Text to Run") input_text = st.text_area("Selected Text", selected_text, height=128, max_chars=None, key=2) elif input_method == "Write or Paste New Text": st.subheader("Text to Run") input_text = st.text_area('Write or Paste Text Below', value="", height=128, max_chars=None, key=2) @st.cache(allow_output_mutation=True) def setModel(named_persons_only): ner = roner.NER(named_persons_only=named_persons_only) return ner @st.cache(allow_output_mutation=True) def get_html(html: str): WRAPPER = """
{}
""" html = html.replace("\n", " ") return WRAPPER.format(html) Run_Button = st.button("Run", key=None) if Run_Button == True: ner = setModel(named_persons_only = False) output = ner(input_text)[0] # only one sentence # tabular form data = [] for word in output["words"]: if word["tag"]!="O": data.append({ "word": word["text"], "tag": word["tag"], "start_char": word["start_char"], "end_char": word["end_char"], "span_after": word["span_after"], }) df = pd.DataFrame.from_dict(data) st.subheader("Recognized Entities") st.dataframe(df) st.subheader("Spacy Style Display") spacy_display = {} spacy_display["ents"] = [] spacy_display["text"] = output["text"] spacy_display["title"] = None for word in output["words"]: #spacy_display["ents"].append({"start": entity["start"], "end": entity["end"], "label": entity["entity_group"]}) spacy_display["ents"].append({"start": word["start_char"], "end": word["end_char"], "label": word["tag"]}) entity_list = ['O', 'PERSON', 'ORG', 'GPE', 'LOC', 'NAT_REL_POL', 'EVENT', 'LANGUAGE', 'WORK_OF_ART', 'DATETIME', 'PERIOD', 'MONEY', 'QUANTITY', 'NUMERIC', 'ORDINAL', 'FACILITY'] colors = { 'O': '#FFF', 'PERSON': '#F00', 'ORG': '#F00', 'GPE': '#F00', 'LOC': '#F00', 'NAT_REL_POL': '#F00', 'EVENT': '#F00', 'LANGUAGE': '#F00', 'WORK_OF_ART': '#F00', 'DATETIME': '#F00', 'PERIOD': '#F00', 'MONEY': '#F00', 'QUANTITY': '#F00', 'NUMERIC': '#F00', 'ORDINAL': '#F00', 'FACILITY': '#F00', } html = spacy.displacy.render(spacy_display, style="ent", minify=True, manual=True, options={"ents": entity_list, "colors": colors}) style = "" st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)