import streamlit as st
# Page configuration
st.set_page_config(
layout="wide",
initial_sidebar_state="auto"
)
# Custom CSS for better styling
st.markdown("""
""", unsafe_allow_html=True)
# Title
st.markdown('
Introduction to Longformer for Token & Sequence Classification
', unsafe_allow_html=True)
# Subtitle
st.markdown("""
Longformer is a transformer-based model designed to handle long documents by leveraging an attention mechanism that scales linearly with the length of the document. This makes it highly effective for tasks such as token classification and sequence classification, especially when dealing with lengthy text inputs.
""", unsafe_allow_html=True)
# Tabs for Longformer Annotators
tab1, tab2, tab3= st.tabs(["Longformer For Token Classification", "Longformer For Sequence Classification", "Longformer For Question Answering"])
# Tab 1: LongformerForTokenClassification
with tab1:
st.markdown("""
Longformer for Token Classification
Token Classification involves assigning labels to individual tokens (words or subwords) within a sentence. This is essential for tasks like Named Entity Recognition (NER), where each token is classified as a specific entity such as a person, organization, or location.
Longformer is particularly effective for token classification tasks due to its ability to handle long contexts and capture dependencies over long spans of text.
Using Longformer for token classification enables:
- Precise NER: Extract entities from lengthy documents with high accuracy.
- Efficient Contextual Understanding: Leverage Longformer's attention mechanism to model long-range dependencies.
- Scalability: Process large documents efficiently using Spark NLP.
""", unsafe_allow_html=True)
# Implementation Section
st.markdown('How to Use Longformer for Token Classification in Spark NLP
', unsafe_allow_html=True)
st.markdown("""
Below is an example of how to set up a pipeline in Spark NLP using the Longformer model for token classification, specifically for Named Entity Recognition (NER).
""", unsafe_allow_html=True)
st.code('''
from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
from pyspark.sql.functions import col, expr
document_assembler = DocumentAssembler() \\
.setInputCol('text') \\
.setOutputCol('document')
tokenizer = Tokenizer() \\
.setInputCols(['document']) \\
.setOutputCol('token')
tokenClassifier = LongformerForTokenClassification \\
.pretrained('longformer_base_token_classifier_conll03', 'en') \\
.setInputCols(['token', 'document']) \\
.setOutputCol('ner') \\
.setCaseSensitive(True) \\
.setMaxSentenceLength(512)
ner_converter = NerConverter() \\
.setInputCols(['document', 'token', 'ner']) \\
.setOutputCol('entities')
pipeline = Pipeline(stages=[
document_assembler,
tokenizer,
tokenClassifier,
ner_converter
])
text = "Facebook is a social networking service launched as TheFacebook on February 4, 2004. It was founded by Mark Zuckerberg with his college roommates and fellow Harvard University students Eduardo Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes. The website's membership was initially limited by the founders to Harvard students, but was expanded to other colleges in the Boston area, the Ivy League, and gradually most universities in the United States and Canada."
example = spark.createDataFrame([[text]]).toDF("text")
result = pipeline.fit(example).transform(example)
result.select(
expr("explode(entities) as ner_chunk")
).select(
col("ner_chunk.result").alias("chunk"),
col("ner_chunk.metadata.entity").alias("ner_label")
).show(truncate=False)
''', language='python')
# Example Output
st.text("""
+------------------+---------+
|chunk |ner_label|
+------------------+---------+
|Mark Zuckerberg |PER |
|Harvard University|ORG |
|Eduardo Saverin |PER |
|Andrew McCollum |PER |
|Dustin Moskovitz |PER |
|Chris Hughes |PER |
|Harvard |ORG |
|Boston |LOC |
|Ivy |ORG |
|League |ORG |
|United |LOC |
|States |LOC |
|Canada |LOC |
+------------------+---------+
""")
# Model Info Section
st.markdown('Choosing the Right Longformer Model
', unsafe_allow_html=True)
st.markdown("""
Spark NLP offers various Longformer models tailored for token classification tasks. Selecting the appropriate model can significantly impact performance.
Explore the available models on the Spark NLP Models Hub to find the one that fits your needs.
""", unsafe_allow_html=True)
# Tab 2: LongformerForSequenceClassification
with tab2:
st.markdown("""
Longformer for Sequence Classification
Sequence Classification involves assigning a label to an entire sequence of text, such as determining the sentiment of a review or categorizing a document into topics. Longformer’s ability to model long-range dependencies is particularly beneficial for sequence classification tasks.
Using Longformer for sequence classification enables:
- Accurate Sentiment Analysis: Determine the sentiment of long text sequences.
- Effective Document Classification: Categorize lengthy documents based on their content.
- Robust Performance: Benefit from Longformer’s attention mechanism for improved classification accuracy.
""", unsafe_allow_html=True)
# Implementation Section
st.markdown('How to Use Longformer for Sequence Classification in Spark NLP
', unsafe_allow_html=True)
st.markdown("""
The following example demonstrates how to set up a pipeline in Spark NLP using the Longformer model for sequence classification, particularly for sentiment analysis of movie reviews.
""", unsafe_allow_html=True)
st.code('''
from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
document_assembler = DocumentAssembler() \\
.setInputCol('text') \\
.setOutputCol('document')
tokenizer = Tokenizer() \\
.setInputCols(['document']) \\
.setOutputCol('token')
sequenceClassifier = LongformerForSequenceClassification \\
.pretrained('longformer_base_sequence_classifier_imdb', 'en') \\
.setInputCols(['token', 'document']) \\
.setOutputCol('class') \\
.setCaseSensitive(False) \\
.setMaxSentenceLength(1024)
pipeline = Pipeline(stages=[
document_assembler,
tokenizer,
sequenceClassifier
])
example = spark.createDataFrame([['I really liked that movie!']]).toDF("text")
result = pipeline.fit(example).transform(example)
result.select('document.result','class.result').show()
''', language='python')
# Example Output
st.text("""
+--------------------+------+
| result|result|
+--------------------+------+
|[I really liked t...| [pos]|
+--------------------+------+
""")
# Model Info Section
st.markdown('Choosing the Right Longformer Model
', unsafe_allow_html=True)
st.markdown("""
Various Longformer models are available for sequence classification in Spark NLP. Each model is fine-tuned for specific tasks, so selecting the right one is crucial for achieving optimal performance.
Explore the available models on the Spark NLP Models Hub to find the best fit for your use case.
""", unsafe_allow_html=True)
# Tab 3: LongformerForQuestionAnswering
with tab3:
st.markdown("""
Longformer for Question Answering
Question Answering is the task of identifying the correct answer to a question from a given context or passage. Longformer's ability to process long documents makes it highly suitable for question answering tasks, especially in cases where the context is lengthy.
Using Longformer for question answering enables:
- Accurate Answer Extraction: Identify precise answers within long passages.
- Contextual Understanding: Benefit from Longformer's global and local attention mechanisms to capture relevant information from context.
- Scalability: Efficiently process and handle extensive documents using Spark NLP.
""", unsafe_allow_html=True)
# Implementation Section
st.markdown('How to Use Longformer for Question Answering in Spark NLP
', unsafe_allow_html=True)
st.markdown("""
The following example demonstrates how to set up a pipeline in Spark NLP using the Longformer model for question answering, specifically tailored for SQuAD v2 dataset.
""", unsafe_allow_html=True)
st.code('''
from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
documentAssembler = MultiDocumentAssembler() \\
.setInputCols(["question", "context"]) \\
.setOutputCols(["document_question", "document_context"])
spanClassifier = LongformerForQuestionAnswering.pretrained("longformer_base_base_qa_squad2", "en") \\
.setInputCols(["document_question", "document_context"]) \\
.setOutputCol("answer")\\
.setCaseSensitive(True)
pipeline = Pipeline(stages=[documentAssembler, spanClassifier])
data = spark.createDataFrame([["What is my name?", "My name is Clara and I live in Berkeley."]]).toDF("question", "context")
result = pipeline.fit(data).transform(data)
''', language='python')
# Example Output
st.text("""
+-------+
| result|
+-------+
|[Clara]|
+-------+
""")
# Model Info Section
st.markdown('Choosing the Right Longformer Model
', unsafe_allow_html=True)
st.markdown("""
Various Longformer models are available for question answering in Spark NLP. Each model is fine-tuned for specific tasks, so selecting the right one is crucial for achieving optimal performance.
Explore the available models on the Spark NLP Models Hub to find the best fit for your use case.
""", unsafe_allow_html=True)
# Footer
st.markdown('Community & Support
', unsafe_allow_html=True)
st.markdown("""
- Official Website: Documentation and examples
- Slack: Live discussion with the community and team
- GitHub: Bug reports, feature requests, and contributions
- Medium: Spark NLP articles
- YouTube: Video tutorials
""", unsafe_allow_html=True)
st.markdown('Quick Links
', unsafe_allow_html=True)
st.markdown("""
""", unsafe_allow_html=True)