t5 / pages /Workflow & Model Overview.py
abdullahmubeen10's picture
Upload 177 files
dcdb825 verified
raw
history blame
11.5 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: #4A90E2;
margin-top: 20px;
}
.section {
background-color: #f9f9f9;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
}
h2 {
font-size: 20px;
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('images/T5_model_diagram.jpg', caption='Diagram of the T5 model, from the original paper', use_column_width='auto')
# Detailed Tasks with T5 Model
st.markdown("""
<div class="section">
<p>The T5 model can handle a wide range of NLP tasks by setting specific task prefixes. Here’s how you can use it for various tasks:</p>
</div>
<h2>Sentence Classification - cola</h2>
<p>Classify if a sentence is grammatically correct. Use the task prefix <code>"cola:"</code>.</p>
<h2>Natural Language Inference (NLI)</h2>
<ul>
<li><b>rte:</b> Recognize whether the meaning of one text can be inferred from another. Use <code>"rte:"</code>.</li>
<li><b>mnli:</b> Classify whether a hypothesis and premise contradict or agree. Use <code>"mnli:"</code>.</li>
<li><b>qnli:</b> Classify whether the answer to a question can be inferred from a given text. Use <code>"qnli:"</code>.</li>
<li><b>cb:</b> Classify if a premise and hypothesis contradict each other (binary). Use <code>"cb:"</code>.</li>
</ul>
<h2>Coreference Resolution</h2>
<ul>
<li><b>mrpc:</b> Classify whether two sentences are re-phrasings of each other. Use <code>"mrpc:"</code>.</li>
<li><b>qqp:</b> Classify whether two questions are re-phrasings of each other. Use <code>"qqp:"</code>.</li>
</ul>
<h2>Sentiment Analysis</h2>
<ul>
<li><b>sst2:</b> Classify the sentiment of a sentence as positive or negative. Use <code>"sst2:"</code>.</li>
<li><b>stsb:</b> Measure similarity between two sentences on a scale from 0 to 5. Use <code>"stsb:"</code>.</li>
</ul>
<h2>Question Answering</h2>
<ul>
<li><b>copa:</b> Given a question, premise, and two choices, classify the correct choice (binary). Use <code>"copa:"</code>.</li>
<li><b>multirc:</b> Given a question, paragraph, and answer candidate, classify if the answer is correct (binary). Use <code>"multirc:"</code>.</li>
<li><b>squad:</b> Answer a question based on a given context. Use <code>"squad:"</code>.</li>
</ul>
<h2>Word Sense Disambiguation</h2>
<p>Classify if a word has the same meaning in two sentences. Use <code>"wic:"</code>.</p>
<h2>Text Summarization</h2>
<p>Summarize text into a shorter representation. Use <code>"summarize:"</code>.</p>
<h2>Translation</h2>
<ul>
<li><b>wmt1:</b> Translate from English to German. Use <code>"translate English to German:"</code>.</li>
<li><b>wmt2:</b> Translate from English to French. Use <code>"translate English to French:"</code>.</li>
<li><b>wmt3:</b> Translate from English to Romanian. Use <code>"translate English to Romanian:"</code>.</li>
</ul>
<p>For more details about each task, refer to the <a class="link" href="https://arxiv.org/pdf/1910.10683.pdf" target="_blank">T5 paper</a>.</p>
""", unsafe_allow_html=True)
# 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', 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)