sparknlp-text-summarization / pages /Workflow & Model Overview.py
abdullahmubeen10's picture
Upload 6 files
d6e48fd verified
raw
history blame contribute delete
No virus
8.48 kB
import streamlit as st
# Custom CSS for better styling
st.markdown("""
<style>
.main-title {
font-size: 36px;
color: #4A90E2;
font-weight: bold;
text-align: center;
}
.sub-title {
font-size: 24px;
color: #333333;
margin-top: 20px;
}
.section {
background-color: #f9f9f9;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
}
.section h2 {
font-size: 22px;
color: #4A90E2;
}
.section p, .section ul {
color: #666666;
}
.link {
color: #4A90E2;
text-decoration: none;
}
</style>
""", unsafe_allow_html=True)
# Introduction
st.markdown('<div class="main-title">State-of-the-Art Text Summarization with Spark NLP</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>Welcome to the Spark NLP Demos App! In the rapidly evolving field of Natural Language Processing (NLP), the combination of powerful models and scalable frameworks is crucial. One such resource-intensive task is Text Summarization, which benefits immensely from the efficient implementation of machine learning models on distributed systems like Apache Spark.</p>
<p>Spark NLP stands out as the leading choice for enterprises building NLP solutions. This open-source library, built in Scala with a Python wrapper, offers state-of-the-art machine learning models within an easy-to-use pipeline design compatible with Spark ML.</p>
</div>
""", unsafe_allow_html=True)
# About the T5 Model
st.markdown('<div class="sub-title">About the T5 Model</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>A standout model for text summarization is the Text-to-Text Transformer (T5), introduced by Google researchers in 2019. T5 achieves remarkable results by utilizing a unique design that allows it to perform multiple NLP tasks with simple prefixes. For text summarization, the input text is prefixed with "summarize:".</p>
<p>In Spark NLP, the T5 model is available through the T5Transformer annotator. We'll show you how to use Spark NLP in Python to perform text summarization using the T5 model.</p>
</div>
""", unsafe_allow_html=True)
st.image('https://www.johnsnowlabs.com/wp-content/uploads/2023/09/img_blog_2.jpg', caption='Diagram of the T5 model, from the original paper', use_column_width='auto')
# How to Use the Model
st.markdown('<div class="sub-title">How to Use the T5 Model with Spark NLP</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>To use the T5Transformer annotator in Spark NLP to perform text summarization, we need to create a pipeline with two stages: the first transforms the input text into an annotation object, and the second stage contains the T5 model.</p>
</div>
""", unsafe_allow_html=True)
st.markdown('### Installation')
st.code('!pip install spark-nlp', language='python')
st.markdown('### Import Libraries and Start Spark Session')
st.code("""
import sparknlp
from sparknlp.base import DocumentAssembler, PipelineModel
from sparknlp.annotator import T5Transformer
# Start the Spark Session
spark = sparknlp.start()
""", language='python')
st.markdown("""
<div class="section">
<p>Now we can define the pipeline to use the T5 model. We'll use the PipelineModel object since we are using the pretrained model and don’t need to train any stage of the pipeline.</p>
</div>
""", unsafe_allow_html=True)
st.markdown('### Define the Pipeline')
st.code("""
# Transforms raw texts into `document` annotation
document_assembler = (
DocumentAssembler().setInputCol("text").setOutputCol("documents")
)
# The T5 model
t5 = (
T5Transformer.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(["documents"])
.setMaxOutputLength(200)
.setOutputCol("t5")
)
# Define the Spark pipeline
pipeline = PipelineModel(stages = [document_assembler, t5])
""", language='python')
st.markdown("""
<div class="section">
<p>To use the model, create a Spark DataFrame containing the input data. In this example, we'll work with a single sentence, but the framework can handle multiple texts for simultaneous processing. The input column from the DocumentAssembler annotator requires a column named “text.”</p>
</div>
""", unsafe_allow_html=True)
st.markdown('### Create Example DataFrame')
st.code("""
example = \"""
Transfer learning, where a model is first pre-trained on a data-rich task
before being fine-tuned on a downstream task, has emerged as a powerful
technique in natural language processing (NLP). The effectiveness of transfer
learning has given rise to a diversity of approaches, methodology, and
practice. In this paper, we explore the landscape of transfer learning
techniques for NLP by introducing a unified framework that converts all
text-based language problems into a text-to-text format.
Our systematic study compares pre-training objectives, architectures,
unlabeled data sets, transfer approaches, and other factors on dozens of
language understanding tasks. By combining the insights from our exploration
with scale and our new Colossal Clean Crawled Corpus, we achieve
state-of-the-art results on many benchmarks covering summarization,
question answering, text classification, and more. To facilitate future
work on transfer learning for NLP, we release our data set, pre-trained
models, and code.
\"""
spark_df = spark.createDataFrame([[example]])
""", language='python')
st.markdown('### Apply the Pipeline')
st.code("""
result = pipeline.transform(spark_df)
result.select("t5.result").show(truncate=False)
""", language='python')
st.markdown('<div class="sub-title">Output</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The summarization output will look something like this:</p>
<pre>transfer learning has emerged as a powerful technique in natural language processing (NLP) the effectiveness of transfer learning has given rise to a diversity of approaches, methodologies, and practice.</pre>
<p>Note: We defined the maximum output length to 200. Depending on the length of the original text, this parameter should be adapted.</p>
</div>
""", unsafe_allow_html=True)
# Additional Resources and References
st.markdown('<div class="sub-title">Additional Resources and References</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://sparknlp.org/docs/en/transformers#t5transformer" target="_blank">T5Transformer documentation page</a></li>
<li><a class="link" href="https://arxiv.org/abs/1910.10683" target="_blank">T5 paper</a></li>
<li><a class="link" href="https://sparknlp.org/docs/en/quickstart" target="_blank">Getting Started with Spark NLP</a></li>
<li><a class="link" href="https://nlp.johnsnowlabs.com/models" target="_blank">Pretrained Models</a></li>
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp/tree/master/examples/python/annotation/text/english" target="_blank">Example Notebooks</a></li>
<li><a class="link" href="https://sparknlp.org/docs/en/install" target="_blank">Installation Guide</a></li>
</ul>
</div>
""", unsafe_allow_html=True)
st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
<li><a class="link" href="https://join.slack.com/t/spark-nlp/shared_invite/zt-198dipu77-L3UWNe_AJ8xqDk0ivmih5Q" target="_blank">Slack</a>: Live discussion with the community and team</li>
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li>
<li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li>
<li><a class="link" href="https://www.youtube.com/channel/UCmFOjlpYEhxf_wJUDuz6xxQ/videos" target="_blank">YouTube</a>: Video tutorials</li>
</ul>
</div>
""", unsafe_allow_html=True)