abdullahmubeen10
commited on
Upload 5 files
Browse files- .streamlit/config.toml +3 -0
- Demo.py +157 -0
- Dockerfile +70 -0
- pages/Workflow & Model Overview.py +249 -0
- requirements.txt +7 -0
.streamlit/config.toml
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
base="light"
|
3 |
+
primaryColor="#29B4E8"
|
Demo.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
initial_sidebar_state="auto"
|
16 |
+
)
|
17 |
+
|
18 |
+
# CSS for styling
|
19 |
+
st.markdown("""
|
20 |
+
<style>
|
21 |
+
.main-title {
|
22 |
+
font-size: 36px;
|
23 |
+
color: #4A90E2;
|
24 |
+
font-weight: bold;
|
25 |
+
text-align: center;
|
26 |
+
}
|
27 |
+
.section {
|
28 |
+
background-color: #f9f9f9;
|
29 |
+
padding: 10px;
|
30 |
+
border-radius: 10px;
|
31 |
+
margin-top: 10px;
|
32 |
+
}
|
33 |
+
.section p, .section ul {
|
34 |
+
color: #666666;
|
35 |
+
}
|
36 |
+
</style>
|
37 |
+
""", unsafe_allow_html=True)
|
38 |
+
|
39 |
+
@st.cache_resource
|
40 |
+
def init_spark():
|
41 |
+
return sparknlp.start()
|
42 |
+
|
43 |
+
@st.cache_resource
|
44 |
+
def create_pipeline(model):
|
45 |
+
document_assembler = DocumentAssembler() \
|
46 |
+
.setInputCol('text') \
|
47 |
+
.setOutputCol('document')
|
48 |
+
|
49 |
+
tokenizer = Tokenizer() \
|
50 |
+
.setInputCols(['document']) \
|
51 |
+
.setOutputCol('token')
|
52 |
+
|
53 |
+
tokenClassifier = CamemBertForTokenClassification() \
|
54 |
+
.pretrained(model, 'en') \
|
55 |
+
.setInputCols(['document', 'token']) \
|
56 |
+
.setOutputCol('ner') \
|
57 |
+
.setCaseSensitive(True) \
|
58 |
+
.setMaxSentenceLength(512)
|
59 |
+
|
60 |
+
# Convert NER labels to entities
|
61 |
+
ner_converter = NerConverter() \
|
62 |
+
.setInputCols(['document', 'token', 'ner']) \
|
63 |
+
.setOutputCol('ner_chunk')
|
64 |
+
|
65 |
+
pipeline = Pipeline(stages=[
|
66 |
+
document_assembler,
|
67 |
+
tokenizer,
|
68 |
+
tokenClassifier,
|
69 |
+
ner_converter
|
70 |
+
])
|
71 |
+
return pipeline
|
72 |
+
|
73 |
+
def fit_data(pipeline, data):
|
74 |
+
empty_df = spark.createDataFrame([['']]).toDF('text')
|
75 |
+
pipeline_model = pipeline.fit(empty_df)
|
76 |
+
model = LightPipeline(pipeline_model)
|
77 |
+
result = model.fullAnnotate(data)
|
78 |
+
return result
|
79 |
+
|
80 |
+
def annotate(data):
|
81 |
+
document, chunks, labels = data["Document"], data["NER Chunk"], data["NER Label"]
|
82 |
+
annotated_words = []
|
83 |
+
for chunk, label in zip(chunks, labels):
|
84 |
+
parts = document.split(chunk, 1)
|
85 |
+
if parts[0]:
|
86 |
+
annotated_words.append(parts[0])
|
87 |
+
annotated_words.append((chunk, label))
|
88 |
+
document = parts[1]
|
89 |
+
if document:
|
90 |
+
annotated_words.append(document)
|
91 |
+
annotated_text(*annotated_words)
|
92 |
+
|
93 |
+
# Set up the page layout
|
94 |
+
st.markdown('<div class="main-title">Recognize Entities with CamemBERT</div>', unsafe_allow_html=True)
|
95 |
+
st.markdown("""
|
96 |
+
<div class="section">
|
97 |
+
<p>This model performs Named Entity Recognition (NER) using CamemBERT, a powerful language model fine-tuned specifically for French. It can accurately identify entities such as locations, organizations, persons, and miscellaneous categories in texts.</p>
|
98 |
+
</div>
|
99 |
+
""", unsafe_allow_html=True)
|
100 |
+
|
101 |
+
# Sidebar content
|
102 |
+
model = st.sidebar.selectbox(
|
103 |
+
"Choose the pretrained model",
|
104 |
+
['camembert_base_token_classifier_wikiner'],
|
105 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
106 |
+
)
|
107 |
+
|
108 |
+
# Reference notebook link in sidebar
|
109 |
+
link = """
|
110 |
+
<a href="https://github.com/JohnSnowLabs/spark-nlp-workshop/blob/5cd574dd8065d3d7406816bee36b1ef56b3f9359/Spark_NLP_Udemy_MOOC/Open_Source/17.01.Transformers-based_Embeddings.ipynb#L102">
|
111 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
112 |
+
</a>
|
113 |
+
"""
|
114 |
+
st.sidebar.markdown('Reference notebook:')
|
115 |
+
st.sidebar.markdown(link, unsafe_allow_html=True)
|
116 |
+
|
117 |
+
# Load examples
|
118 |
+
# English and French text samples for testing the CamemBERT model
|
119 |
+
|
120 |
+
examples = [
|
121 |
+
"""Barack Obama was born in Hawaii and later became the 44th President of the United States. He attended Columbia University and Harvard Law School, where he served as the president of the Harvard Law Review. After graduation, he worked as a civil rights attorney and taught constitutional law at the University of Chicago Law School. Obama's presidential campaign began in 2007, and he was elected as the first African American president in 2008. During his presidency, he signed into law the Affordable Care Act, passed the Dodd-Frank Act, and ordered the military operation that resulted in the death of Osama bin Laden.""",
|
122 |
+
"""Paris is the capital of France and one of the most visited cities in the world. The Eiffel Tower, Louvre Museum, and Notre-Dame Cathedral are among its most iconic landmarks. The city is also a global center for art, fashion, gastronomy, and culture. In addition to its historical sites, Paris is known for its cafés, parks, and gardens. The River Seine runs through the city, adding to its charm and providing picturesque views. Paris has been a major hub for education, politics, and commerce for centuries.""",
|
123 |
+
"""Apple Inc. is an American multinational technology company headquartered in Cupertino, California. It was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in April 1976. Apple is known for its innovative products, including the iPhone, iPad, and Mac computers. The company has a significant presence in Paris, where it operates several retail stores and offices. Apple's commitment to design and user experience has made it one of the most valuable companies in the world. The company continues to lead the industry in technology and sustainability initiatives."""
|
124 |
+
"""Barack Obama est né à Hawaï et est ensuite devenu le 44e président des États-Unis. Il a étudié à l'Université Columbia et à la Faculté de droit de Harvard, où il a été président de la Harvard Law Review. Après avoir obtenu son diplôme, il a travaillé comme avocat spécialisé en droits civiques et a enseigné le droit constitutionnel à la Faculté de droit de l'Université de Chicago. La campagne présidentielle d'Obama a commencé en 2007, et il a été élu premier président afro-américain en 2008. Pendant sa présidence, il a promulgué la loi sur les soins abordables, fait adopter la loi Dodd-Frank, et ordonné l'opération militaire qui a conduit à la mort d'Oussama ben Laden.""",
|
125 |
+
"""Paris est la capitale de la France et l'une des villes les plus visitées au monde. La Tour Eiffel, le Musée du Louvre et la Cathédrale Notre-Dame comptent parmi ses monuments les plus emblématiques. La ville est également un centre mondial de l'art, de la mode, de la gastronomie et de la culture. En plus de ses sites historiques, Paris est connue pour ses cafés, ses parcs et ses jardins. La Seine traverse la ville, ajoutant à son charme et offrant des vues pittoresques. Paris est depuis des siècles un important centre d'éducation, de politique et de commerce.""",
|
126 |
+
"""Apple Inc. est une multinationale technologique américaine dont le siège est à Cupertino, en Californie. Elle a été fondée par Steve Jobs, Steve Wozniak et Ronald Wayne en avril 1976. Apple est connue pour ses produits innovants, notamment l'iPhone, l'iPad et les ordinateurs Mac. La société a une présence importante à Paris, où elle exploite plusieurs magasins de détail et bureaux. L'engagement d'Apple en matière de design et d'expérience utilisateur en a fait l'une des entreprises les plus précieuses au monde. La société continue de diriger l'industrie en matière de technologie et d'initiatives de durabilité."""
|
127 |
+
]
|
128 |
+
|
129 |
+
selected_text = st.selectbox("Select an example", examples)
|
130 |
+
custom_input = st.text_input("Try it with your own Sentence!")
|
131 |
+
|
132 |
+
text_to_analyze = custom_input if custom_input else selected_text
|
133 |
+
|
134 |
+
st.subheader('Full example text')
|
135 |
+
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>"""
|
136 |
+
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True)
|
137 |
+
|
138 |
+
# Initialize Spark and create pipeline
|
139 |
+
spark = init_spark()
|
140 |
+
pipeline = create_pipeline(model)
|
141 |
+
output = fit_data(pipeline, text_to_analyze)
|
142 |
+
|
143 |
+
# Display matched sentence
|
144 |
+
st.subheader("Processed output:")
|
145 |
+
|
146 |
+
results = {
|
147 |
+
'Document': output[0]['document'][0].result,
|
148 |
+
'NER Chunk': [n.result for n in output[0]['ner_chunk']],
|
149 |
+
"NER Label": [n.metadata['entity'] for n in output[0]['ner_chunk']]
|
150 |
+
}
|
151 |
+
|
152 |
+
annotate(results)
|
153 |
+
|
154 |
+
with st.expander("View DataFrame"):
|
155 |
+
df = pd.DataFrame({'NER Chunk': results['NER Chunk'], 'NER Label': results['NER Label']})
|
156 |
+
df.index += 1
|
157 |
+
st.dataframe(df)
|
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,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
# Page configuration
|
4 |
+
st.set_page_config(
|
5 |
+
layout="wide",
|
6 |
+
initial_sidebar_state="auto"
|
7 |
+
)
|
8 |
+
|
9 |
+
# Custom CSS for better styling
|
10 |
+
st.markdown("""
|
11 |
+
<style>
|
12 |
+
.main-title {
|
13 |
+
font-size: 36px;
|
14 |
+
color: #4A90E2;
|
15 |
+
font-weight: bold;
|
16 |
+
text-align: center;
|
17 |
+
}
|
18 |
+
.sub-title {
|
19 |
+
font-size: 24px;
|
20 |
+
color: #4A90E2;
|
21 |
+
margin-top: 20px;
|
22 |
+
}
|
23 |
+
.section {
|
24 |
+
background-color: #f9f9f9;
|
25 |
+
padding: 15px;
|
26 |
+
border-radius: 10px;
|
27 |
+
margin-top: 20px;
|
28 |
+
}
|
29 |
+
.section h2 {
|
30 |
+
font-size: 22px;
|
31 |
+
color: #4A90E2;
|
32 |
+
}
|
33 |
+
.section p, .section ul {
|
34 |
+
color: #666666;
|
35 |
+
}
|
36 |
+
.link {
|
37 |
+
color: #4A90E2;
|
38 |
+
text-decoration: none;
|
39 |
+
}
|
40 |
+
.benchmark-table {
|
41 |
+
width: 100%;
|
42 |
+
border-collapse: collapse;
|
43 |
+
margin-top: 20px;
|
44 |
+
}
|
45 |
+
.benchmark-table th, .benchmark-table td {
|
46 |
+
border: 1px solid #ddd;
|
47 |
+
padding: 8px;
|
48 |
+
text-align: left;
|
49 |
+
}
|
50 |
+
.benchmark-table th {
|
51 |
+
background-color: #4A90E2;
|
52 |
+
color: white;
|
53 |
+
}
|
54 |
+
.benchmark-table td {
|
55 |
+
background-color: #f2f2f2;
|
56 |
+
}
|
57 |
+
</style>
|
58 |
+
""", unsafe_allow_html=True)
|
59 |
+
|
60 |
+
# Title
|
61 |
+
st.markdown('<div class="main-title">Introduction to CamemBERT Annotators in Spark NLP</div>', unsafe_allow_html=True)
|
62 |
+
|
63 |
+
# Subtitle
|
64 |
+
st.markdown("""
|
65 |
+
<div class="section">
|
66 |
+
<p>Spark NLP offers a variety of CamemBERT-based annotators tailored for multiple natural language processing tasks. CamemBERT is a robust and versatile model designed specifically for the French language, offering state-of-the-art performance in a range of NLP applications. Below, we provide an overview of the four key CamemBERT annotators:</p>
|
67 |
+
</div>
|
68 |
+
""", unsafe_allow_html=True)
|
69 |
+
|
70 |
+
st.markdown("""
|
71 |
+
<div class="section">
|
72 |
+
<h2>CamemBERT for Token Classification</h2>
|
73 |
+
<p>The <strong>CamemBertForTokenClassification</strong> annotator is designed for Named Entity Recognition (NER) tasks using CamemBERT, a French language model derived from RoBERTa. This model efficiently handles token classification, which involves labeling tokens in a text with tags that correspond to specific entities. CamemBERT offers robust performance in French NLP tasks, making it a valuable tool for real-time applications in this language.</p>
|
74 |
+
<p>Token classification with CamemBERT enables:</p>
|
75 |
+
<ul>
|
76 |
+
<li><strong>Named Entity Recognition (NER):</strong> Identifying and classifying entities such as names, organizations, locations, and other predefined categories.</li>
|
77 |
+
<li><strong>Information Extraction:</strong> Extracting key information from unstructured text for further analysis.</li>
|
78 |
+
<li><strong>Text Categorization:</strong> Enhancing document retrieval and categorization based on entity recognition.</li>
|
79 |
+
</ul>
|
80 |
+
<p>Here is an example of how CamemBERT token classification works:</p>
|
81 |
+
<table class="benchmark-table">
|
82 |
+
<tr>
|
83 |
+
<th>Entity</th>
|
84 |
+
<th>Label</th>
|
85 |
+
</tr>
|
86 |
+
<tr>
|
87 |
+
<td>Paris</td>
|
88 |
+
<td>LOC</td>
|
89 |
+
</tr>
|
90 |
+
<tr>
|
91 |
+
<td>Emmanuel Macron</td>
|
92 |
+
<td>PER</td>
|
93 |
+
</tr>
|
94 |
+
<tr>
|
95 |
+
<td>Élysée Palace</td>
|
96 |
+
<td>ORG</td>
|
97 |
+
</tr>
|
98 |
+
</table>
|
99 |
+
</div>
|
100 |
+
""", unsafe_allow_html=True)
|
101 |
+
|
102 |
+
# CamemBERT Token Classification - French WikiNER
|
103 |
+
st.markdown('<div class="sub-title">CamemBERT Token Classification - French WikiNER</div>', unsafe_allow_html=True)
|
104 |
+
st.markdown("""
|
105 |
+
<div class="section">
|
106 |
+
<p>The <strong>camembert_base_token_classifier_wikiner</strong> is a fine-tuned CamemBERT model for token classification tasks, specifically adapted for Named Entity Recognition (NER) on the French WikiNER dataset. It is designed to recognize five types of entities: O, LOC, PER, MISC, and ORG.</p>
|
107 |
+
</div>
|
108 |
+
""", unsafe_allow_html=True)
|
109 |
+
|
110 |
+
# How to Use the Model - Token Classification
|
111 |
+
st.markdown('<div class="sub-title">How to Use the Model</div>', unsafe_allow_html=True)
|
112 |
+
st.code('''
|
113 |
+
from sparknlp.base import *
|
114 |
+
from sparknlp.annotator import *
|
115 |
+
from pyspark.ml import Pipeline
|
116 |
+
from pyspark.sql.functions import col, expr
|
117 |
+
|
118 |
+
document_assembler = DocumentAssembler() \\
|
119 |
+
.setInputCol('text') \\
|
120 |
+
.setOutputCol('document')
|
121 |
+
|
122 |
+
tokenizer = Tokenizer() \\
|
123 |
+
.setInputCols(['document']) \\
|
124 |
+
.setOutputCol('token')
|
125 |
+
|
126 |
+
tokenClassifier = CamemBertForTokenClassification \\
|
127 |
+
.pretrained('camembert_base_token_classifier_wikiner', 'en') \\
|
128 |
+
.setInputCols(['document', 'token']) \\
|
129 |
+
.setOutputCol('ner') \\
|
130 |
+
.setCaseSensitive(True) \\
|
131 |
+
.setMaxSentenceLength(512)
|
132 |
+
|
133 |
+
# Convert NER labels to entities
|
134 |
+
ner_converter = NerConverter() \\
|
135 |
+
.setInputCols(['document', 'token', 'ner']) \\
|
136 |
+
.setOutputCol('entities')
|
137 |
+
|
138 |
+
pipeline = Pipeline(stages=[
|
139 |
+
document_assembler,
|
140 |
+
tokenizer,
|
141 |
+
tokenClassifier,
|
142 |
+
ner_converter
|
143 |
+
])
|
144 |
+
|
145 |
+
data = spark.createDataFrame([["""Paris est la capitale de la France et abrite le Président Emmanuel Macron, qui réside au palais de l'Élysée. Apple Inc. a une présence significative dans la ville."""]]).toDF("text")
|
146 |
+
result = pipeline.fit(data).transform(data)
|
147 |
+
|
148 |
+
result.select(
|
149 |
+
expr("explode(entities) as ner_chunk")
|
150 |
+
).select(
|
151 |
+
col("ner_chunk.result").alias("chunk"),
|
152 |
+
col("ner_chunk.metadata.entity").alias("ner_label")
|
153 |
+
).show(truncate=False)
|
154 |
+
''', language='python')
|
155 |
+
|
156 |
+
# Results
|
157 |
+
st.text("""
|
158 |
+
+------------------+---------+
|
159 |
+
|chunk |ner_label|
|
160 |
+
+------------------+---------+
|
161 |
+
|Paris |LOC |
|
162 |
+
|France |LOC |
|
163 |
+
|Emmanuel Macron |PER |
|
164 |
+
|Élysée Palace |ORG |
|
165 |
+
|Apple Inc. |ORG |
|
166 |
+
+------------------+---------+
|
167 |
+
""")
|
168 |
+
|
169 |
+
# Performance Metrics
|
170 |
+
st.markdown('<div class="sub-title">Performance Metrics</div>', unsafe_allow_html=True)
|
171 |
+
st.markdown("""
|
172 |
+
<div class="section">
|
173 |
+
<p>Here are the detailed performance metrics for the CamemBERT token classification model:</p>
|
174 |
+
<table class="benchmark-table">
|
175 |
+
<tr>
|
176 |
+
<th>Entity</th>
|
177 |
+
<th>Precision</th>
|
178 |
+
<th>Recall</th>
|
179 |
+
<th>F1-Score</th>
|
180 |
+
</tr>
|
181 |
+
<tr>
|
182 |
+
<td>LOC</td>
|
183 |
+
<td>0.93</td>
|
184 |
+
<td>0.94</td>
|
185 |
+
<td>0.94</td>
|
186 |
+
</tr>
|
187 |
+
<tr>
|
188 |
+
<td>PER</td>
|
189 |
+
<td>0.95</td>
|
190 |
+
<td>0.95</td>
|
191 |
+
<td>0.95</td>
|
192 |
+
</tr>
|
193 |
+
<tr>
|
194 |
+
<td>ORG</td>
|
195 |
+
<td>0.92</td>
|
196 |
+
<td>0.91</td>
|
197 |
+
<td>0.91</td>
|
198 |
+
</tr>
|
199 |
+
<tr>
|
200 |
+
<td>MISC</td>
|
201 |
+
<td>0.86</td>
|
202 |
+
<td>0.85</td>
|
203 |
+
<td>0.85</td>
|
204 |
+
</tr>
|
205 |
+
<tr>
|
206 |
+
<td>O</td>
|
207 |
+
<td>0.99</td>
|
208 |
+
<td>0.99</td>
|
209 |
+
<td>0.99</td>
|
210 |
+
</tr>
|
211 |
+
<tr>
|
212 |
+
<td>Overall</td>
|
213 |
+
<td>0.97</td>
|
214 |
+
<td>0.98</td>
|
215 |
+
<td>0.98</td>
|
216 |
+
</tr>
|
217 |
+
</table>
|
218 |
+
</div>
|
219 |
+
""", unsafe_allow_html=True)
|
220 |
+
|
221 |
+
# Model Information - Token Classification
|
222 |
+
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
|
223 |
+
st.markdown("""
|
224 |
+
<div class="section">
|
225 |
+
<ul>
|
226 |
+
<li><strong>Model Name:</strong> camembert_base_token_classifier_wikiner</li>
|
227 |
+
<li><strong>Compatibility:</strong> Spark NLP 4.2.0+</li>
|
228 |
+
<li><strong>License:</strong> Open Source</li>
|
229 |
+
<li><strong>Edition:</strong> Official</li>
|
230 |
+
<li><strong>Input Labels:</strong> [token, document]</li>
|
231 |
+
<li><strong>Output Labels:</strong> [ner]</li>
|
232 |
+
<li><strong>Language:</strong> French</li>
|
233 |
+
<li><strong>Size:</strong> 412.2 MB</li>
|
234 |
+
<li><strong>Case Sensitive:</strong> Yes</li>
|
235 |
+
<li><strong>Max Sentence Length:</strong> 512</li>
|
236 |
+
</ul>
|
237 |
+
</div>
|
238 |
+
""", unsafe_allow_html=True)
|
239 |
+
|
240 |
+
# References - Token Classification
|
241 |
+
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
|
242 |
+
st.markdown("""
|
243 |
+
<div class="section">
|
244 |
+
<ul>
|
245 |
+
<li><a class="link" href="https://huggingface.co/datasets/Jean-Baptiste/wikiner_fr" target="_blank" rel="noopener">CamemBERT WikiNER Dataset</a></li>
|
246 |
+
<li><a class="link" href="https://sparknlp.org/2022/09/23/camembert_base_token_classifier_wikiner_en.html" target="_blank" rel="noopener">CamemBERT Token Classification on Spark NLP Hub</a></li>
|
247 |
+
</ul>
|
248 |
+
</div>
|
249 |
+
""", unsafe_allow_html=True)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
st-annotated-text
|
3 |
+
streamlit-tags
|
4 |
+
pandas
|
5 |
+
numpy
|
6 |
+
spark-nlp
|
7 |
+
pyspark
|