File size: 8,549 Bytes
e8a1598 95d357c e8a1598 95d357c e8a1598 95d357c e8a1598 95d357c e8a1598 95d357c e8a1598 d6e48fd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
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('<div class="sub-title">Installation</div>', unsafe_allow_html=True)
st.code('!pip install spark-nlp', language='python')
st.markdown('<div class="sub-title">Import Libraries and Start Spark Session</div>', unsafe_allow_html=True)
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('<div class="sub-title">Define the Pipeline</div>', unsafe_allow_html=True)
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('<div class="sub-title">Create Example DataFrame</div>', unsafe_allow_html=True)
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('<div class="sub-title">Apply the Pipeline</div>', unsafe_allow_html=True)
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) |