SparkNLP_NER / app.py
MaziyarPanahi's picture
Update the URL to the demos
ffcba18
raw
history blame
15.6 kB
import streamlit as st
st.set_page_config(
layout="centered", # Can be "centered" or "wide". In the future also "dashboard", etc.
initial_sidebar_state="auto", # Can be "auto", "expanded", "collapsed"
page_title='Extractive Summarization', # String or None. Strings get appended with "• Streamlit".
page_icon='./favicon.png', # String, anything supported by st.image, or None.
)
import pandas as pd
import numpy as np
import json
import os
import sys
sys.path.append(os.path.abspath('./'))
import streamlit_apps_config as config
from streamlit_ner_output import show_html2, jsl_display_annotations, get_color
import sparknlp
from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.sql import functions as F
from sparknlp_display import NerVisualizer
from pyspark.ml import Pipeline
from pyspark.sql.types import StringType
spark= sparknlp.start()
## Marking down NER Style
st.markdown(config.STYLE_CONFIG, unsafe_allow_html=True)
root_path = config.project_path
########## To Remove the Main Menu Hamburger ########
hide_menu_style = """
<style>
#MainMenu {visibility: hidden;}
</style>
"""
st.markdown(hide_menu_style, unsafe_allow_html=True)
########## Side Bar ########
## loading logo(newer version with href)
import base64
@st.cache(allow_output_mutation=True)
def get_base64_of_bin_file(bin_file):
with open(bin_file, 'rb') as f:
data = f.read()
return base64.b64encode(data).decode()
@st.cache(allow_output_mutation=True)
def get_img_with_href(local_img_path, target_url):
img_format = os.path.splitext(local_img_path)[-1].replace('.', '')
bin_str = get_base64_of_bin_file(local_img_path)
html_code = f'''
<a href="{target_url}">
<img height="90%" width="90%" src="data:image/{img_format};base64,{bin_str}" />
</a>'''
return html_code
logo_html = get_img_with_href('./jsl-logo.png', 'https://www.johnsnowlabs.com/')
st.sidebar.markdown(logo_html, unsafe_allow_html=True)
#sidebar info
model_name= ["nerdl_fewnerd_100d", "ner_conll_elmo", "ner_mit_movie_complex_distilbert_base_cased", "ner_conll_albert_large_uncased", "onto_100"]
st.sidebar.title("Pretrained model to test")
selected_model = st.sidebar.selectbox("", model_name)
######## Main Page #########
if selected_model == "nerdl_fewnerd_100d":
app_title= "Detect up to 8 entity types in general domain texts"
app_description= "Named Entity Recognition model aimed to detect up to 8 entity types from general domain texts. This model was trained on the Few-NERD/inter public dataset using Spark NLP, and it is available in Spark NLP Models hub. "
st.title(app_title)
st.markdown("<h2>"+app_description+"</h2>" , unsafe_allow_html=True)
st.markdown("**`PERSON`** **,** **`ORGANIZATION`** **,** **`LOCATION`** **,** **`ART`** **,** **`BUILDING`** **,** **`PRODUCT`** **,** **`EVENT`** **,** **`OTHER`**", unsafe_allow_html=True)
elif selected_model== "ner_conll_elmo":
app_title= "Detect up to 4 entity types in general domain texts"
app_description= "Named Entity Recognition model aimed to detect up to 4 entity types from general domain texts. This model was trained on the CoNLL 2003 text corpus using Spark NLP, and it is available in Spark NLP Models hub. "
st.title(app_title)
st.markdown("<h2>"+app_description+"</h2>" , unsafe_allow_html=True)
st.markdown("**`PER`** **,** **`LOC`** **,** **`ORG`** **,** **`MISC` **", unsafe_allow_html=True)
elif selected_model== "ner_mit_movie_complex_distilbert_base_cased":
app_title= "Detect up to 12 entity types in movie domain texts"
app_description= "Named Entity Recognition model aimed to detect up to 12 entity types from movie domain texts. This model was trained on the MIT Movie Corpus complex queries dataset to detect movie trivia using Spark NLP, and it is available in Spark NLP Models hub. "
st.title(app_title)
st.markdown("<h2>"+app_description+"</h2>" , unsafe_allow_html=True)
st.markdown("""**`ACTOR`** **,** **`AWARD`** **,** **`CHARACTER_NAME`** **,** **`DIRECTOR`** **,** **`GENRE`** **,** **`OPINION`** **,** **`ORIGIN`** **,** **`PLOT`**,
**`QUOTE`** **,** **`RELATIONSHIP`** **,** **`SOUNDTRACK`** **,** **`YEAR` **""", unsafe_allow_html=True)
elif selected_model=="ner_conll_albert_large_uncased":
app_title= "Detect up to 4 entity types in general domain texts"
app_description= "Named Entity Recognition model aimed to detect up to 4 entity types from general domain texts. This model was trained on the CoNLL 2003 text corpus using Spark NLP, and it is available in Spark NLP Models hub. "
st.title(app_title)
st.markdown("<h2>"+app_description+"</h2>" , unsafe_allow_html=True)
st.markdown("**`PER`** **,** **`LOC`** **,** **`ORG`** **,** **`MISC` **", unsafe_allow_html=True)
elif selected_model=="onto_100":
app_title= "Detect up to 18 entity types in general domain texts"
app_description= "Named Entity Recognition model aimed to detect up to 18 entity types from general domain texts. This model was trained with GloVe 100d word embeddings using Spark NLP, so be sure to use same embeddings in the pipeline. It is available in Spark NLP Models hub. "
st.title(app_title)
st.markdown("<h2>"+app_description+"</h2>" , unsafe_allow_html=True)
st.markdown("""**`CARDINAL`** **,** **`EVENT`** **,** **`WORK_OF_ART`** **,** **`ORG`** **,** **`DATE`** **,** **`GPE`** **,** **`PERSON`** **,** **`PRODUCT`**,
**`NORP`** **,** **`ORDINAL`** **,** **`MONEY`** **,** **`LOC` **, **`FAC`** **,** **`LAW`** **,** **`TIME`** **,** **`PERCENT`** **,** **`QUANTITY`** **,** **`LANGUAGE` **""", unsafe_allow_html=True)
st.subheader("")
#caching the models in the dictionary
@st.cache(allow_output_mutation=True, show_spinner=False)
def load_sparknlp_models():
ner_models_list= ["nerdl_fewnerd_100d", "ner_conll_elmo", "ner_mit_movie_complex_distilbert_base_cased",
"ner_conll_albert_large_uncased", "onto_100"]
embeddings_list= ["glove_100d", "elmo", "distilbert_base_cased", "albert_large_uncased", "glove_100d_for_onto"]
documentAssembler = DocumentAssembler()\
.setInputCol("text")\
.setOutputCol("document")
sentenceDetector= SentenceDetector()\
.setInputCols(["document"])\
.setOutputCol("sentence")
tokenizer = Tokenizer()\
.setInputCols(["sentence"])\
.setOutputCol("token")
ner_converter= NerConverter()\
.setInputCols(["document", "token", "ner"])\
.setOutputCol("ner_chunk")
model_dict= {
'documentAssembler': documentAssembler,
'sentenceDetector': sentenceDetector,
'tokenizer': tokenizer,
'ner_converter': ner_converter
}
for embeddings_name, ner_model_name in zip(embeddings_list, ner_models_list):
try:
if embeddings_name=="glove_100d":
model_dict[embeddings_name]= WordEmbeddingsModel.pretrained(embeddings_name, "en")\
.setInputCols(["sentence", "token"])\
.setOutputCol("embeddings")
elif embeddings_name=="elmo":
model_dict[embeddings_name]= ElmoEmbeddings.pretrained(embeddings_name, "en")\
.setInputCols(["token", "document"])\
.setOutputCol("embeddings")\
.setPoolingLayer("elmo")
elif embeddings_name=="distilbert_base_cased":
model_dict[embeddings_name]= DistilBertEmbeddings\
.pretrained(embeddings_name, 'en')\
.setInputCols(["token", "document"])\
.setOutputCol("embeddings")
elif embeddings_name=="albert_large_uncased":
model_dict[embeddings_name]= AlbertEmbeddings\
.pretrained(embeddings_name, 'en')\
.setInputCols(["document", "token"])\
.setOutputCol("embeddings")
elif embeddings_name=="glove_100d_for_onto":
model_dict[embeddings_name]= WordEmbeddingsModel.pretrained("glove_100d", "en")\
.setInputCols(["sentence", "token"])\
.setOutputCol("embeddings")
model_dict[ner_model_name]= NerDLModel.pretrained(ner_model_name, "en")\
.setInputCols(["document", "token", "embeddings"])\
.setOutputCol("ner")
except:
pass
return model_dict
placeholder_= st.empty()
placeholder_.info("If you are launching the app for the first time, it may take some time (approximately 1 minute) for SparkNLP models to load...")
nlp_dict= load_sparknlp_models()
placeholder_.empty()
if selected_model=="ner_conll_albert_large_uncased":
text= st.text_input("Type here your text and press enter to run:", value="Mark Knopfler was born in Glasgow, Scotland. He is a British singer-songwriter, guitarist, and record producer. He became known as the lead guitarist, singer and songwriter of the rock band Dire Straits.")
elif selected_model=="ner_mit_movie_complex_distilbert_base_cased":
text= st.text_input("Type here your text and press enter to run:", value="It's only appropriate that Solaris, Russian filmmaker Andrei Tarkovsky's psychological sci-fi classic from 1972, contains an equally original and mind-bending score. Solaris explores the inadequacies of time and memory on an enigmatic planet below a derelict space station. To reinforce the film's chilling setting, Tarkovsky commissioned composer Eduard Artemiev to construct an electronic soundscape reflecting planet Solaris' amorphous and mysterious surface")
elif selected_model=="ner_conll_elmo":
text= st.text_input("Type here your text and press enter to run: ", value="Tottenham Hotspur Football Club, commonly referred to as Tottenham or Spurs, is an English professional football club based in Tottenham, London, that competes in the Premier League, the top flight of English football.")
elif selected_model=="onto_100":
text= st.text_input("Type here your text and press enter to run: ", value="William Henry Gates III (born October 28, 1955) is an American business magnate, software developer, investor, and philanthropist. He is best known as the co-founder of Microsoft Corporation. During his career at Microsoft, Gates held the positions of chairman, chief executive officer (CEO), president and chief software architect, while also being the largest individual shareholder until May 2014. He is one of the best-known entrepreneurs and pioneers of the microcomputer revolution of the 1970s and 1980s. Born and raised in Seattle, Washington, Gates co-founded Microsoft with childhood friend Paul Allen in 1975, in Albuquerque, New Mexico; it went on to become the world's largest personal computer software company. Gates led the company as chairman and CEO until stepping down as CEO in January 2000, but he remained chairman and became chief software architect.")
else:
text= st.text_input("Type here your text and press enter to run:", value="12 Corazones ('12 Hearts') is Spanish-language dating game show produced in the United States for the television network Telemundo since January 2005, based on its namesake Argentine TV show format. The show is filmed in Los Angeles and revolves around the twelve Zodiac signs that identify each contestant. In 2008, Ho filmed a cameo in the Steven Spielberg feature film The Cloverfield Paradox, as a news pundit.")
def build_pipeline(text, model_name=selected_model):
base_pipeline= Pipeline(stages=[
nlp_dict["documentAssembler"],
nlp_dict["sentenceDetector"],
nlp_dict["tokenizer"]
])
fewnerd_pipeline= Pipeline(stages=[
base_pipeline,
nlp_dict["glove_100d"],
nlp_dict[model_name],
nlp_dict["ner_converter"]
])
elmo_pipeline= Pipeline(stages=[
base_pipeline,
nlp_dict["elmo"],
nlp_dict[model_name],
nlp_dict["ner_converter"]
])
movie_pipeline= Pipeline(stages=[
base_pipeline,
nlp_dict["distilbert_base_cased"],
nlp_dict[model_name],
nlp_dict["ner_converter"]
])
albert_pipeline= Pipeline(stages=[
base_pipeline,
nlp_dict["albert_large_uncased"],
nlp_dict[model_name],
nlp_dict["ner_converter"]
])
onto_pipeline= Pipeline(stages=[
base_pipeline,
nlp_dict["glove_100d_for_onto"],
nlp_dict[model_name],
nlp_dict["ner_converter"]
])
text_df = spark.createDataFrame([[text]]).toDF("text")
if model_name=="nerdl_fewnerd_100d":
pipeline_model= fewnerd_pipeline.fit(text_df)
elif model_name=="ner_conll_elmo":
pipeline_model= elmo_pipeline.fit(text_df)
elif model_name=="ner_mit_movie_complex_distilbert_base_cased":
pipeline_model= movie_pipeline.fit(text_df)
elif model_name=="ner_conll_albert_large_uncased":
pipeline_model= albert_pipeline.fit(text_df)
elif model_name=="onto_100":
pipeline_model= onto_pipeline.fit(text_df)
result = pipeline_model.transform(text_df).toPandas()
return result
#placeholder for warning
placeholder= st.empty()
placeholder.info("Processing...")
result= build_pipeline(text)
placeholder.empty()
df= pd.DataFrame({"ner_chunk": result["ner_chunk"].iloc[0]})
labels_set = set()
for i in df['ner_chunk'].values:
labels_set.add(i[4]['entity'])
labels_set = list(labels_set)
labels = st.sidebar.multiselect(
"NER Labels", options=labels_set, default=list(labels_set)
)
show_html2(text, df, labels, "Text annotated with identified Named Entities")
try_link="""<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/Certification_Trainings/Public/3.SparkNLP_Pretrained_Models.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/></a>"""
st.sidebar.title('')
st.sidebar.markdown("<h1> Try it yourself: </h1>" , unsafe_allow_html=True)
st.sidebar.markdown(try_link, unsafe_allow_html=True)
st.sidebar.info("""Want to see more?
- Check Spark NLP in action, including our Spark NLP for Healthcare & Spark OCR demos at [here](https://nlp.johnsnowlabs.com/demos)
- Check our 4.4K+ models available in Spark NLP Models Hub [here](https://nlp.johnsnowlabs.com/models)""")