Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- .streamlit/config.toml +3 -0
- Demo.py +152 -0
- Dockerfile +70 -0
- pages/Workflow & Model Overview.py +253 -0
- requirements.txt +6 -0
.streamlit/config.toml
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
base="light"
|
3 |
+
primaryColor="#29B4E8"
|
Demo.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import sparknlp
|
3 |
+
import os
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
from sparknlp.base import *
|
7 |
+
from sparknlp.annotator import *
|
8 |
+
from pyspark.ml import Pipeline
|
9 |
+
from sparknlp.pretrained import PretrainedPipeline
|
10 |
+
from annotated_text import annotated_text
|
11 |
+
|
12 |
+
# Page configuration
|
13 |
+
st.set_page_config(
|
14 |
+
layout="wide",
|
15 |
+
page_title="Spark NLP Demos App",
|
16 |
+
initial_sidebar_state="auto"
|
17 |
+
)
|
18 |
+
|
19 |
+
# CSS for styling
|
20 |
+
st.markdown("""
|
21 |
+
<style>
|
22 |
+
.main-title {
|
23 |
+
font-size: 36px;
|
24 |
+
color: #4A90E2;
|
25 |
+
font-weight: bold;
|
26 |
+
text-align: center;
|
27 |
+
}
|
28 |
+
.section p, .section ul {
|
29 |
+
color: #666666;
|
30 |
+
}
|
31 |
+
</style>
|
32 |
+
""", unsafe_allow_html=True)
|
33 |
+
|
34 |
+
@st.cache_resource
|
35 |
+
def init_spark():
|
36 |
+
return sparknlp.start()
|
37 |
+
|
38 |
+
@st.cache_resource
|
39 |
+
def create_pipeline(model):
|
40 |
+
document_assembler = DocumentAssembler() \
|
41 |
+
.setInputCol("text") \
|
42 |
+
.setOutputCol("document")
|
43 |
+
|
44 |
+
tokenizer = Tokenizer() \
|
45 |
+
.setInputCols(["document"]) \
|
46 |
+
.setOutputCol("token")
|
47 |
+
|
48 |
+
embeddings = WordEmbeddingsModel.pretrained('glove_840B_300', lang='xx') \
|
49 |
+
.setInputCols(["document", "token"]) \
|
50 |
+
.setOutputCol("embeddings")
|
51 |
+
|
52 |
+
ner_model = NerDLModel.pretrained(model, 'xx') \
|
53 |
+
.setInputCols(["document", "token", "embeddings"]) \
|
54 |
+
.setOutputCol("ner")
|
55 |
+
|
56 |
+
ner_converter = NerConverter() \
|
57 |
+
.setInputCols(["document", "token", "ner"]) \
|
58 |
+
.setOutputCol("ner_chunk")
|
59 |
+
|
60 |
+
pipeline = Pipeline(stages=[
|
61 |
+
document_assembler,
|
62 |
+
tokenizer,
|
63 |
+
embeddings,
|
64 |
+
ner_model,
|
65 |
+
ner_converter
|
66 |
+
])
|
67 |
+
|
68 |
+
return pipeline
|
69 |
+
|
70 |
+
def fit_data(pipeline, data):
|
71 |
+
empty_df = spark.createDataFrame([['']]).toDF('text')
|
72 |
+
pipeline_model = pipeline.fit(empty_df)
|
73 |
+
model = LightPipeline(pipeline_model)
|
74 |
+
result = model.fullAnnotate(data)
|
75 |
+
return result
|
76 |
+
|
77 |
+
def annotate(data):
|
78 |
+
document, chunks, labels = data["Document"], data["NER Chunk"], data["NER Label"]
|
79 |
+
annotated_words = []
|
80 |
+
for chunk, label in zip(chunks, labels):
|
81 |
+
parts = document.split(chunk, 1)
|
82 |
+
if parts[0]:
|
83 |
+
annotated_words.append(parts[0])
|
84 |
+
annotated_words.append((chunk, label))
|
85 |
+
document = parts[1]
|
86 |
+
if document:
|
87 |
+
annotated_words.append(document)
|
88 |
+
annotated_text(*annotated_words)
|
89 |
+
|
90 |
+
# Set up the page layout
|
91 |
+
st.markdown('<div class="main-title">Erkennen Sie Personen, Standorte, Organisationen und verschiedene Entitäten</div>', unsafe_allow_html=True)
|
92 |
+
|
93 |
+
# Sidebar content
|
94 |
+
model = st.sidebar.selectbox(
|
95 |
+
"Choose the pretrained model",
|
96 |
+
["ner_wikiner_glove_840B_300"],
|
97 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
98 |
+
)
|
99 |
+
|
100 |
+
# Reference notebook link in sidebar
|
101 |
+
link = """
|
102 |
+
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/NER_DE.ipynb">
|
103 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
104 |
+
</a>
|
105 |
+
"""
|
106 |
+
st.sidebar.markdown('Reference notebook:')
|
107 |
+
st.sidebar.markdown(link, unsafe_allow_html=True)
|
108 |
+
|
109 |
+
# Load examples
|
110 |
+
examples = [
|
111 |
+
"""William Henry Gates III (* 28. Oktober 1955 in London) ist ein US-amerikanischer Geschäftsmann, Softwareentwickler, Investor und Philanthrop. Er ist bekannt als Mitbegründer der Microsoft Corporation. Während seiner Karriere bei Microsoft war Gates Vorsitzender, Chief Executive Officer (CEO), Präsident und Chief Software Architect und bis Mai 2014 der größte Einzelaktionär. Er ist einer der bekanntesten Unternehmer und Pioniere der Mikrocomputer-Revolution der 1970er und 1980er Jahre. Gates wurde in Seattle, Washington, geboren und wuchs dort auf. 1975 gründete er Microsoft zusammen mit seinem Freund aus Kindertagen, Paul Allen, in Albuquerque, New Mexico. Es entwickelte sich zum weltweit größten Unternehmen für Personal-Computer-Software. Gates leitete das Unternehmen als Chairman und CEO, bis er im Januar 2000 als CEO zurücktrat. Er blieb jedoch Chairman und wurde Chief Software Architect. In den späten neunziger Jahren wurde Gates für seine Geschäftstaktiken kritisiert, die als wettbewerbswidrig angesehen wurden. Diese Meinung wurde durch zahlreiche Gerichtsurteile bestätigt. Im Juni 2006 gab Gates bekannt, dass er eine Teilzeitstelle bei Microsoft und eine Vollzeitstelle bei der Bill & Melinda Gates Foundation, der privaten gemeinnützigen Stiftung, die er und seine Frau Melinda Gates im Jahr 2000 gegründet haben, übernehmen wird. [ 9] Er übertrug seine Aufgaben nach und nach auf Ray Ozzie und Craig Mundie. Im Februar 2014 trat er als Vorsitzender von Microsoft zurück und übernahm eine neue Position als Technologieberater, um den neu ernannten CEO Satya Nadella zu unterstützen.""",
|
112 |
+
"""Die Mona Lisa ist ein Ölgemälde aus dem 16. Jahrhundert, das von Leonardo geschaffen wurde. Es findet im Louvre in Paris statt.""",
|
113 |
+
"""Als Sebastian Thrun 2007 bei Google anfing, an selbstfahrenden Autos zu arbeiten, nahmen ihn nur wenige Leute außerhalb des Unternehmens ernst. "Ich kann Ihnen sagen, dass sehr hochrangige CEOs großer amerikanischer Automobilunternehmen mir die Hand schütteln und sich abwenden würden, weil ich es nicht wert war, mit ihnen zu sprechen", sagte Thrun, jetzt Mitbegründer und CEO des Online-Hochschul-Startups Udacity, in einem Interview mit Recode Anfang dieser Woche.""",
|
114 |
+
"""Facebook ist ein sozialer Netzwerkdienst, der am 4. Februar 2004 als TheFacebook gestartet wurde. Er wurde von Mark Zuckerberg mit seinen Mitbewohnern und Kommilitonen der Harvard University, Eduardo Saverin, Andrew McCollum, Dustin Moskovitz und Chris Hughes, gegründet. Die Mitgliedschaft der Website wurde ursprünglich von den Gründern auf Harvard-Studenten beschränkt, aber auf andere Colleges in der Region Boston, die Ivy League und nach und nach auf die meisten Universitäten in den USA und Kanada ausgeweitet.""",
|
115 |
+
"""Die Geschichte der Verarbeitung natürlicher Sprache begann im Allgemeinen in den 1950er Jahren, obwohl Arbeiten aus früheren Perioden gefunden werden können. 1950 veröffentlichte Alan Turing einen Artikel mit dem Titel "Computing Machinery and Intelligence", in dem der heutige Turing-Test als Kriterium für die Intelligenz vorgeschlagen wurde""",
|
116 |
+
"""Geoffrey Everest Hinton ist ein englisch-kanadischer kognitiver Psychologe und Informatiker, der vor allem für seine Arbeit an künstlichen neuronalen Netzen bekannt ist. Seit 2013 arbeitet er für Google und die University of Toronto. 2017 war er Mitbegründer und wissenschaftlicher Leiter des Vector Institute in Toronto.""",
|
117 |
+
"""Als ich John sagte, dass ich nach Alaska ziehen wollte, warnte er mich, dass ich dort Probleme haben würde, einen Starbucks zu finden.""",
|
118 |
+
"""Steven Paul Jobs war ein amerikanischer Geschäftsmagnat, Industriedesigner, Investor und Medieninhaber. Er war Vorsitzender, Chief Executive Officer (CEO) und Mitbegründer von Apple Inc., Vorsitzender und Mehrheitsaktionär von Pixar, einem Mitglied des Board of Directors der Walt Disney Company nach der Übernahme von Pixar, und Gründer, Vorsitzender und CEO von NeXT. Jobs ist weithin als Pionier der PC-Revolution der 1970er und 1980er Jahre anerkannt, zusammen mit Apple-Mitbegründer Steve Wozniak. Jobs wurde in San Francisco, Kalifornien, geboren und zur Adoption freigegeben. Er wuchs in der San Francisco Bay Area auf. Er besuchte das Reed College 1972, bevor er es im selben Jahr abbrach, und reiste 1974 durch Indien, um Erleuchtung zu erlangen und den Zen-Buddhismus zu studieren.""",
|
119 |
+
"""Titanic ist ein amerikanischer epischer Romantik- und Katastrophenfilm aus dem Jahr 1997, der von James Cameron inszeniert, geschrieben, co-produziert und mitherausgegeben wurde. Es enthält sowohl historische als auch fiktive Aspekte und basiert auf Berichten über den Untergang der RMS Titanic. Die Stars Leonardo DiCaprio und Kate Winslet sind Mitglieder verschiedener sozialer Schichten, die sich während ihrer unglücklichen Jungfernfahrt an Bord des Schiffes verlieben.""",
|
120 |
+
"""John Snow ist nicht nur der König des Nordens, sondern auch ein englischer Arzt und führend in der Entwicklung von Anästhesie und medizinischer Hygiene. Er gilt als der erste, der Daten zur Heilung des Cholera-Ausbruchs im Jahr 1834 verwendet."""
|
121 |
+
]
|
122 |
+
|
123 |
+
selected_text = st.selectbox("Select an example", examples)
|
124 |
+
custom_input = st.text_input("Try it with your own Sentence!")
|
125 |
+
|
126 |
+
text_to_analyze = custom_input if custom_input else selected_text
|
127 |
+
|
128 |
+
st.subheader('Full example text')
|
129 |
+
HTML_WRAPPER = """<div class="scroll entities" style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem; white-space:pre-wrap">{}</div>"""
|
130 |
+
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True)
|
131 |
+
|
132 |
+
# Initialize Spark and create pipeline
|
133 |
+
spark = init_spark()
|
134 |
+
pipeline = create_pipeline(model)
|
135 |
+
output = fit_data(pipeline, text_to_analyze)
|
136 |
+
|
137 |
+
# Display matched sentence
|
138 |
+
st.subheader("Processed output:")
|
139 |
+
|
140 |
+
results = {
|
141 |
+
'Document': output[0]['document'][0].result,
|
142 |
+
'NER Chunk': [n.result for n in output[0]['ner_chunk']],
|
143 |
+
"NER Label": [n.metadata['entity'] for n in output[0]['ner_chunk']]
|
144 |
+
}
|
145 |
+
|
146 |
+
annotate(results)
|
147 |
+
|
148 |
+
with st.expander("View DataFrame"):
|
149 |
+
df = pd.DataFrame({'NER Chunk': results['NER Chunk'], 'NER Label': results['NER Label']})
|
150 |
+
df.index += 1
|
151 |
+
st.dataframe(df)
|
152 |
+
|
Dockerfile
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Download base image ubuntu 18.04
|
2 |
+
FROM ubuntu:18.04
|
3 |
+
|
4 |
+
# Set environment variables
|
5 |
+
ENV NB_USER jovyan
|
6 |
+
ENV NB_UID 1000
|
7 |
+
ENV HOME /home/${NB_USER}
|
8 |
+
|
9 |
+
# Install required packages
|
10 |
+
RUN apt-get update && apt-get install -y \
|
11 |
+
tar \
|
12 |
+
wget \
|
13 |
+
bash \
|
14 |
+
rsync \
|
15 |
+
gcc \
|
16 |
+
libfreetype6-dev \
|
17 |
+
libhdf5-serial-dev \
|
18 |
+
libpng-dev \
|
19 |
+
libzmq3-dev \
|
20 |
+
python3 \
|
21 |
+
python3-dev \
|
22 |
+
python3-pip \
|
23 |
+
unzip \
|
24 |
+
pkg-config \
|
25 |
+
software-properties-common \
|
26 |
+
graphviz \
|
27 |
+
openjdk-8-jdk \
|
28 |
+
ant \
|
29 |
+
ca-certificates-java \
|
30 |
+
&& apt-get clean \
|
31 |
+
&& update-ca-certificates -f;
|
32 |
+
|
33 |
+
# Install Python 3.8 and pip
|
34 |
+
RUN add-apt-repository ppa:deadsnakes/ppa \
|
35 |
+
&& apt-get update \
|
36 |
+
&& apt-get install -y python3.8 python3-pip \
|
37 |
+
&& apt-get clean;
|
38 |
+
|
39 |
+
# Set up JAVA_HOME
|
40 |
+
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
|
41 |
+
RUN mkdir -p ${HOME} \
|
42 |
+
&& echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> ${HOME}/.bashrc \
|
43 |
+
&& chown -R ${NB_UID}:${NB_UID} ${HOME}
|
44 |
+
|
45 |
+
# Create a new user named "jovyan" with user ID 1000
|
46 |
+
RUN useradd -m -u ${NB_UID} ${NB_USER}
|
47 |
+
|
48 |
+
# Switch to the "jovyan" user
|
49 |
+
USER ${NB_USER}
|
50 |
+
|
51 |
+
# Set home and path variables for the user
|
52 |
+
ENV HOME=/home/${NB_USER} \
|
53 |
+
PATH=/home/${NB_USER}/.local/bin:$PATH
|
54 |
+
|
55 |
+
# Set the working directory to the user's home directory
|
56 |
+
WORKDIR ${HOME}
|
57 |
+
|
58 |
+
# Upgrade pip and install Python dependencies
|
59 |
+
RUN python3.8 -m pip install --upgrade pip
|
60 |
+
COPY requirements.txt /tmp/requirements.txt
|
61 |
+
RUN python3.8 -m pip install -r /tmp/requirements.txt
|
62 |
+
|
63 |
+
# Copy the application code into the container at /home/jovyan
|
64 |
+
COPY --chown=${NB_USER}:${NB_USER} . ${HOME}
|
65 |
+
|
66 |
+
# Expose port for Streamlit
|
67 |
+
EXPOSE 7860
|
68 |
+
|
69 |
+
# Define the entry point for the container
|
70 |
+
ENTRYPOINT ["streamlit", "run", "Demo.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
pages/Workflow & Model Overview.py
ADDED
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
# Custom CSS for better styling
|
4 |
+
st.markdown("""
|
5 |
+
<style>
|
6 |
+
.main-title {
|
7 |
+
font-size: 36px;
|
8 |
+
color: #4A90E2;
|
9 |
+
font-weight: bold;
|
10 |
+
text-align: center;
|
11 |
+
}
|
12 |
+
.sub-title {
|
13 |
+
font-size: 24px;
|
14 |
+
color: #4A90E2;
|
15 |
+
margin-top: 20px;
|
16 |
+
}
|
17 |
+
.section {
|
18 |
+
background-color: #f9f9f9;
|
19 |
+
padding: 15px;
|
20 |
+
border-radius: 10px;
|
21 |
+
margin-top: 20px;
|
22 |
+
}
|
23 |
+
.section h2 {
|
24 |
+
font-size: 22px;
|
25 |
+
color: #4A90E2;
|
26 |
+
}
|
27 |
+
.section p, .section ul {
|
28 |
+
color: #666666;
|
29 |
+
}
|
30 |
+
.link {
|
31 |
+
color: #4A90E2;
|
32 |
+
text-decoration: none;
|
33 |
+
}
|
34 |
+
</style>
|
35 |
+
""", unsafe_allow_html=True)
|
36 |
+
|
37 |
+
# Main Title
|
38 |
+
st.markdown('<div class="main-title">State-of-the-Art Named Entity Recognition with Spark NLP (German)</div>', unsafe_allow_html=True)
|
39 |
+
|
40 |
+
# Introduction
|
41 |
+
st.markdown("""
|
42 |
+
<div class="section">
|
43 |
+
<p>Named Entity Recognition (NER) is the task of identifying important words in a text and associating them with a category. For example, we may be interested in finding all the personal names in documents, or company names in news articles. Other examples include domain-specific uses such as identifying all disease names in a clinical text, or company trading codes in financial ones.</p>
|
44 |
+
<p>NER can be implemented with many approaches. In this post, we introduce a deep learning-based method using the NerDL model. This approach leverages the scalability of Spark NLP with Python.</p>
|
45 |
+
</div>
|
46 |
+
""", unsafe_allow_html=True)
|
47 |
+
|
48 |
+
# Introduction to Spark NLP
|
49 |
+
st.markdown('<div class="sub-title">Introduction to Spark NLP</div>', unsafe_allow_html=True)
|
50 |
+
st.markdown("""
|
51 |
+
<div class="section">
|
52 |
+
<p>Spark NLP is an open-source library maintained by John Snow Labs. It is built on top of Apache Spark and Spark ML and provides simple, performant & accurate NLP annotations for machine learning pipelines that can scale easily in a distributed environment.</p>
|
53 |
+
<p>To install Spark NLP, you can simply use any package manager like conda or pip. For example, using pip you can simply run <code>pip install spark-nlp</code>. For different installation options, check the official <a href="https://nlp.johnsnowlabs.com/docs/en/install" target="_blank" class="link">documentation</a>.</p>
|
54 |
+
</div>
|
55 |
+
""", unsafe_allow_html=True)
|
56 |
+
|
57 |
+
# Using NerDL Model
|
58 |
+
st.markdown('<div class="sub-title">Using NerDL Model</div>', unsafe_allow_html=True)
|
59 |
+
st.markdown("""
|
60 |
+
<div class="section">
|
61 |
+
<p>The NerDL model in Spark NLP is a deep learning-based approach for NER tasks. It uses a Char CNNs - BiLSTM - CRF architecture that achieves state-of-the-art results in most datasets. The training data should be a labeled Spark DataFrame in the format of CoNLL 2003 IOB with annotation type columns.</p>
|
62 |
+
</div>
|
63 |
+
""", unsafe_allow_html=True)
|
64 |
+
|
65 |
+
# Setup Instructions
|
66 |
+
st.markdown('<div class="sub-title">Setup</div>', unsafe_allow_html=True)
|
67 |
+
st.markdown('<p>To install Spark NLP in Python, use your favorite package manager (conda, pip, etc.). For example:</p>', unsafe_allow_html=True)
|
68 |
+
st.code("""
|
69 |
+
pip install spark-nlp
|
70 |
+
pip install pyspark
|
71 |
+
""", language="bash")
|
72 |
+
|
73 |
+
st.markdown("<p>Then, import Spark NLP and start a Spark session:</p>", unsafe_allow_html=True)
|
74 |
+
st.code("""
|
75 |
+
import sparknlp
|
76 |
+
|
77 |
+
# Start Spark Session
|
78 |
+
spark = sparknlp.start()
|
79 |
+
""", language='python')
|
80 |
+
|
81 |
+
# Example Usage with NerDL Model in Italian
|
82 |
+
st.markdown('<div class="sub-title">Example Usage with NerDL Model in Italian</div>', unsafe_allow_html=True)
|
83 |
+
st.markdown("""
|
84 |
+
<div class="section">
|
85 |
+
<p>Below is an example of how to set up and use the NerDL model for named entity recognition in Italian:</p>
|
86 |
+
</div>
|
87 |
+
""", unsafe_allow_html=True)
|
88 |
+
st.code('''
|
89 |
+
from sparknlp.base import *
|
90 |
+
from sparknlp.annotator import *
|
91 |
+
from pyspark.ml import Pipeline
|
92 |
+
from pyspark.sql.functions import col, expr, round, concat, lit
|
93 |
+
|
94 |
+
# Document Assembler
|
95 |
+
document_assembler = DocumentAssembler() \\
|
96 |
+
.setInputCol("text") \\
|
97 |
+
.setOutputCol("document")
|
98 |
+
|
99 |
+
# Tokenizer
|
100 |
+
tokenizer = Tokenizer() \\
|
101 |
+
.setInputCols(["document"]) \\
|
102 |
+
.setOutputCol("token")
|
103 |
+
|
104 |
+
# Word Embeddings
|
105 |
+
embeddings = WordEmbeddingsModel.pretrained('glove_840B_300', lang='xx') \\
|
106 |
+
.setInputCols(["document", "token"]) \\
|
107 |
+
.setOutputCol("embeddings")
|
108 |
+
|
109 |
+
# NerDL Model
|
110 |
+
ner_model = NerDLModel.pretrained('ner_wikiner_glove_840B_300', 'xx') \\
|
111 |
+
.setInputCols(["document", "token", "embeddings"]) \\
|
112 |
+
.setOutputCol("ner")
|
113 |
+
|
114 |
+
# NER Converter
|
115 |
+
ner_converter = NerConverter() \\
|
116 |
+
.setInputCols(["document", "token", "ner"]) \\
|
117 |
+
.setOutputCol("ner_chunk")
|
118 |
+
|
119 |
+
# Pipeline
|
120 |
+
pipeline = Pipeline(stages=[
|
121 |
+
document_assembler,
|
122 |
+
tokenizer,
|
123 |
+
embeddings,
|
124 |
+
ner_model,
|
125 |
+
ner_converter
|
126 |
+
])
|
127 |
+
|
128 |
+
# Example sentence
|
129 |
+
example = """William Henry Gates III (* 28. Oktober 1955 in London) ist ein US-amerikanischer Geschäftsmann, Softwareentwickler, Investor und Philanthrop. Er ist bekannt als Mitbegründer der Microsoft Corporation. Während seiner Karriere bei Microsoft war Gates Vorsitzender, Chief Executive Officer (CEO), Präsident und Chief Software Architect und bis Mai 2014 der größte Einzelaktionär. Er ist einer der bekanntesten Unternehmer und Pioniere der Mikrocomputer-Revolution der 1970er und 1980er Jahre. Gates wurde in Seattle, Washington, geboren und wuchs dort auf. 1975 gründete er Microsoft zusammen mit seinem Freund aus Kindertagen, Paul Allen, in Albuquerque, New Mexico. Es entwickelte sich zum weltweit größten Unternehmen für Personal-Computer-Software. Gates leitete das Unternehmen als Chairman und CEO, bis er im Januar 2000 als CEO zurücktrat. Er blieb jedoch Chairman und wurde Chief Software Architect. In den späten neunziger Jahren wurde Gates für seine Geschäftstaktiken kritisiert, die als wettbewerbswidrig angesehen wurden. Diese Meinung wurde durch zahlreiche Gerichtsurteile bestätigt. Im Juni 2006 gab Gates bekannt, dass er eine Teilzeitstelle bei Microsoft und eine Vollzeitstelle bei der Bill & Melinda Gates Foundation, der privaten gemeinnützigen Stiftung, die er und seine Frau Melinda Gates im Jahr 2000 gegründet haben, übernehmen wird. [ 9] Er übertrug seine Aufgaben nach und nach auf Ray Ozzie und Craig Mundie. Im Februar 2014 trat er als Vorsitzender von Microsoft zurück und übernahm eine neue Position als Technologieberater, um den neu ernannten CEO Satya Nadella zu unterstützen."""
|
130 |
+
data = spark.createDataFrame([[example]]).toDF("text")
|
131 |
+
|
132 |
+
# Transforming data
|
133 |
+
result = pipeline.fit(data).transform(data)
|
134 |
+
|
135 |
+
# Select the result, entity, and confidence columns
|
136 |
+
result.select(
|
137 |
+
expr("explode(ner_chunk) as ner_chunk")
|
138 |
+
).select(
|
139 |
+
col("ner_chunk.result").alias("result"),
|
140 |
+
col("ner_chunk.metadata").getItem("entity").alias("entity"),
|
141 |
+
concat(
|
142 |
+
round((col("ner_chunk.metadata").getItem("confidence").cast("float") * 100), 2),
|
143 |
+
lit("%")
|
144 |
+
).alias("confidence")
|
145 |
+
).show(truncate=False)
|
146 |
+
''', language="python")
|
147 |
+
|
148 |
+
st.text("""
|
149 |
+
+-------------------------------+------+----------+
|
150 |
+
|result |entity|confidence|
|
151 |
+
+-------------------------------+------+----------+
|
152 |
+
|William Henry Gates III |PER |57.6% |
|
153 |
+
|London |LOC |91.51% |
|
154 |
+
|US-amerikanischer |MISC |99.56% |
|
155 |
+
|Microsoft Corporation |ORG |72.69% |
|
156 |
+
|Microsoft |ORG |98.13% |
|
157 |
+
|Gates |PER |93.14% |
|
158 |
+
|Gates |PER |77.17% |
|
159 |
+
|Seattle |LOC |89.12% |
|
160 |
+
|Washington |LOC |73.5% |
|
161 |
+
|Microsoft |ORG |95.15% |
|
162 |
+
|Paul Allen |PER |85.83% |
|
163 |
+
|Albuquerque |LOC |84.27% |
|
164 |
+
|New Mexico |LOC |67.22% |
|
165 |
+
|Gates |PER |97.12% |
|
166 |
+
|Gates |PER |97.68% |
|
167 |
+
|Gates |PER |98.26% |
|
168 |
+
|Microsoft |ORG |98.1% |
|
169 |
+
|Bill & Melinda Gates Foundation|MISC |42.91% |
|
170 |
+
|Melinda Gates |PER |87.0% |
|
171 |
+
|Ray Ozzie |PER |83.86% |
|
172 |
+
+-------------------------------+------+----------+
|
173 |
+
""")
|
174 |
+
|
175 |
+
# Benchmark Section
|
176 |
+
st.markdown('<div class="sub-title">Benchmark</div>', unsafe_allow_html=True)
|
177 |
+
st.markdown("""
|
178 |
+
<div class="section">
|
179 |
+
<p>Evaluating the performance of NER models is crucial to understanding their effectiveness in real-world applications. Below are the benchmark results for the "ner_wikiner_glove_840B_300" model on Italian text, focusing on various named entity categories. The metrics used include precision, recall, and F1-score, which are standard for evaluating classification models.</p>
|
180 |
+
</div>
|
181 |
+
""", unsafe_allow_html=True)
|
182 |
+
|
183 |
+
st.markdown("""
|
184 |
+
<div class="sub-title">Detailed Results</div>
|
185 |
+
|
186 |
+
| Entity Type | Precision | Recall | F1-Score | Support |
|
187 |
+
|-------------|:---------:|:------:|:--------:|--------:|
|
188 |
+
| **B-LOC** | 0.88 | 0.92 | 0.90 | 13,050 |
|
189 |
+
| **I-ORG** | 0.78 | 0.71 | 0.74 | 1,211 |
|
190 |
+
| **I-LOC** | 0.89 | 0.85 | 0.87 | 7,454 |
|
191 |
+
| **I-PER** | 0.93 | 0.94 | 0.94 | 4,539 |
|
192 |
+
| **B-ORG** | 0.88 | 0.72 | 0.79 | 2,222 |
|
193 |
+
| **B-PER** | 0.90 | 0.93 | 0.92 | 7,206 |
|
194 |
+
|
195 |
+
<div class="sub-title">Averages</div>
|
196 |
+
|
197 |
+
| Average Type | Precision | Recall | F1-Score | Support |
|
198 |
+
|--------------|:---------:|:------:|:--------:|--------:|
|
199 |
+
| **Micro** | 0.89 | 0.89 | 0.89 | 35,682 |
|
200 |
+
| **Macro** | 0.88 | 0.85 | 0.86 | 35,682 |
|
201 |
+
| **Weighted** | 0.89 | 0.89 | 0.89 | 35,682 |
|
202 |
+
|
203 |
+
<div class="sub-title">Category-Specific Performance</div>
|
204 |
+
|
205 |
+
| Category | Precision | Recall | F1-Score | Support |
|
206 |
+
|----------|:---------:|:------:|:--------:|--------:|
|
207 |
+
| **LOC** | 86.33% | 90.53% | 88.38 | 13,685 |
|
208 |
+
| **MISC** | 81.88% | 67.03% | 73.72 | 3,069 |
|
209 |
+
| **ORG** | 85.91% | 70.52% | 77.46 | 1,824 |
|
210 |
+
| **PER** | 89.54% | 92.08% | 90.79 | 7,410 |
|
211 |
+
|
212 |
+
<div class="sub-title">Additional Metrics</div>
|
213 |
+
|
214 |
+
- **Processed Tokens:** 349,242
|
215 |
+
- **Total Phrases:** 26,227
|
216 |
+
- **Found Phrases:** 25,988
|
217 |
+
- **Correct Phrases:** 22,529
|
218 |
+
- **Accuracy (non-O):** 85.99%
|
219 |
+
- **Overall Accuracy:** 98.06%
|
220 |
+
""", unsafe_allow_html=True)
|
221 |
+
|
222 |
+
# Summary
|
223 |
+
st.markdown('<div class="sub-title">Summary</div>', unsafe_allow_html=True)
|
224 |
+
st.markdown("""
|
225 |
+
<div class="section">
|
226 |
+
<p>In this article, we discussed named entity recognition using a deep learning-based method with the "wikiner_840B_300" model for Italian. We introduced how to perform the task using the open-source Spark NLP library with Python, which can be used at scale in the Spark ecosystem. These methods can be used for natural language processing applications in various fields, including finance and healthcare.</p>
|
227 |
+
</div>
|
228 |
+
""", unsafe_allow_html=True)
|
229 |
+
|
230 |
+
# References
|
231 |
+
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
|
232 |
+
st.markdown("""
|
233 |
+
<div class="section">
|
234 |
+
<ul>
|
235 |
+
<li><a class="link" href="https://sparknlp.org/api/python/reference/autosummary/sparknlp/annotator/ner/ner_dl/index.html" target="_blank" rel="noopener">NerDLModel</a> annotator documentation</li>
|
236 |
+
<li>Model Used: <a class="link" href="https://sparknlp.org/2021/07/19/ner_wikiner_glove_840B_300_xx.html" target="_blank" rel="noopener">ner_wikiner_glove_840B_300</a></li>
|
237 |
+
<li><a class="link" href="https://nlp.johnsnowlabs.com/recognize_entitie" target="_blank" rel="noopener">Visualization demos for NER in Spark NLP</a></li>
|
238 |
+
<li><a class="link" href="https://www.johnsnowlabs.com/named-entity-recognition-ner-with-bert-in-spark-nlp/">Named Entity Recognition (NER) with BERT in Spark NLP</a></li>
|
239 |
+
</ul>
|
240 |
+
</div>
|
241 |
+
""", unsafe_allow_html=True)
|
242 |
+
|
243 |
+
# Community & Support
|
244 |
+
st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
|
245 |
+
st.markdown("""
|
246 |
+
<div class="section">
|
247 |
+
<ul>
|
248 |
+
<li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
|
249 |
+
<li><a class="link" href="https://join.slack.com/t/spark-nlp/shared_invite/zt-198dipu77-L3UWNe_AJf4Rqb3DaMb-7A" target="_blank">Slack Community</a>: Connect with other Spark NLP users</li>
|
250 |
+
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub Repository</a>: Source code and issue tracker</li>
|
251 |
+
</ul>
|
252 |
+
</div>
|
253 |
+
""", unsafe_allow_html=True)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
st-annotated-text
|
3 |
+
pandas
|
4 |
+
numpy
|
5 |
+
spark-nlp
|
6 |
+
pyspark
|