CoolFace
Apppublic

widged/named-entity-recognition

sourceHugging Faceupdated 5y agoView on Hugging Face
3likes
app.py66 linesDownload Raw Back to root
1import streamlit as st2from transformers import pipeline3import spacy4from spacy import displacy5import plotly.express as px6import numpy as np7st.set_page_config(page_title="Named Entity Recognition")8st.title("Named Entity Recognition")9st.write("_This web application is intended for educational use, please do not upload any sensitive information._")10st.write("Identifying all geopolitical entities, organizations, people, locations, or dates in a body of text.")11 12@st.cache(allow_output_mutation=True, show_spinner=False)13def Loading_NLP():14    nlp = spacy.load('en_core_web_sm')15    return nlp16@st.cache(allow_output_mutation=True)17def entRecognizer(entDict, typeEnt):18    entList = [ent for ent in entDict if entDict[ent] == typeEnt]19    return entList20def plot_result(top_topics, scores):21    top_topics = np.array(top_topics)22    scores = np.array(scores)23    scores *= 10024    fig = px.bar(x=scores, y=top_topics, orientation='h',25                 labels={'x': 'Probability', 'y': 'Category'},26                 text=scores,27                 range_x=(0,115),28                 title='Top Predictions',29                 color=np.linspace(0,1,len(scores)),30                 color_continuous_scale="Bluered")31    fig.update(layout_coloraxis_showscale=False)32    fig.update_traces(texttemplate='%{text:0.1f}%', textposition='outside')33    st.plotly_chart(fig)34 35with st.spinner(text="Please wait for the models to load. This should take approximately 60 seconds."):36    nlp = Loading_NLP()37 38text = st.text_area('Enter Text Below:', height=300)39submit = st.button('Generate')40if submit:41    entities = []42    entityLabels = []43    doc = nlp(text)44    for ent in doc.ents:45        entities.append(ent.text)46        entityLabels.append(ent.label_)47    entDict = dict(zip(entities, entityLabels))48    entOrg = entRecognizer(entDict, "ORG")49    entPerson = entRecognizer(entDict, "PERSON")50    entDate = entRecognizer(entDict, "DATE")51    entGPE = entRecognizer(entDict, "GPE")52    entLoc = entRecognizer(entDict, "LOC")53    options = {"ents": ["ORG", "GPE", "PERSON", "LOC", "DATE"]}54    HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""55 56    st.subheader("List of Named Entities:")57    st.write("Geopolitical Entities (GPE): " + str(entGPE))58    st.write("People (PERSON): " + str(entPerson))59    st.write("Organizations (ORG): " + str(entOrg))60    st.write("Dates (DATE): " + str(entDate))61    st.write("Locations (LOC): " + str(entLoc))62    st.subheader("Original Text with Entities Highlighted")63    html = displacy.render(doc, style="ent", options=options)64    html = html.replace("\n", " ")65    st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)66