File size: 9,717 Bytes
d7e89a9 |
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
import streamlit as st
# Page configuration
st.set_page_config(
layout="wide",
initial_sidebar_state="auto"
)
# 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;
}
.section h2 {
font-size: 22px;
color: #4A90E2;
}
.section p, .section ul {
color: #666666;
}
.link {
color: #4A90E2;
text-decoration: none;
}
</style>
""", unsafe_allow_html=True)
# Title
st.markdown('<div class="main-title">Switch Between Active and Passive Voice</div>', unsafe_allow_html=True)
# Introduction Section
st.markdown("""
<div class="section">
<p>Switching between active and passive voice is an essential skill in writing, allowing for more versatile sentence structures and varied expression. Active voice is direct and vigorous, while passive voice can be used to emphasize the action or the recipient of the action, making it a useful tool for nuanced communication.</p>
<p>In this page, we explore how to implement a pipeline that can automatically switch between active and passive voice, and vice versa, using advanced NLP models. We use a T5 Transformer model fine-tuned for style transfer, enabling seamless conversion of sentences between these two voices.</p>
</div>
""", unsafe_allow_html=True)
# T5 Transformer Overview
st.markdown('<div class="sub-title">Understanding the T5 Transformer for Style Transfer</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The T5 (Text-To-Text Transfer Transformer) model, developed by Google, is a powerful tool capable of handling a variety of text-based tasks in a unified framework. When fine-tuned for style transfer, T5 can effectively convert sentences from active to passive voice and vice versa.</p>
<p>The model processes input sentences and, based on its training, generates a text output that switches the voice while preserving the original meaning. This is particularly useful for applications in writing assistance, automated editing, and language learning tools.</p>
</div>
""", unsafe_allow_html=True)
# Performance Section
st.markdown('<div class="sub-title">Performance and Use Cases</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The T5 model has been extensively tested on various text transformation tasks, including style transfer between active and passive voice. The model consistently produces accurate and contextually appropriate results, making it a valuable asset in both professional and educational settings.</p>
<p>This capability is especially useful for writers, editors, and educators who need to adjust sentence structures for clarity, emphasis, or stylistic variation. The T5 model's ability to perform these transformations without requiring external data sources makes it a powerful tool for on-the-fly text editing.</p>
</div>
""", unsafe_allow_html=True)
# Implementation Section
st.markdown('<div class="sub-title">Implementing Active-Passive Voice Switching</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The following example demonstrates how to implement a style transfer pipeline using Spark NLP to switch between active and passive voice and vice versa. The pipeline includes a document assembler and the T5 model to perform the transformation in both directions.</p>
</div>
""", unsafe_allow_html=True)
st.code('''
from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
# Initialize Spark NLP
spark = sparknlp.start()
# Define the pipeline stages
document_assembler = DocumentAssembler()\\
.setInputCol("text")\\
.setOutputCol("documents")
# Active to Passive transformation
t5_active_to_passive = T5Transformer()\\
.pretrained("t5_active_to_passive_styletransfer")\\
.setTask("Transfer Active to Passive:")\\
.setInputCols(["documents"])\\
.setOutputCol("passive")
# Passive to Active transformation
t5_passive_to_active = T5Transformer()\\
.pretrained("t5_passive_to_active_styletransfer")\\
.setTask("Transfer Passive to Active:")\\
.setInputCols(["documents"])\\
.setOutputCol("active")
pipeline_active_to_passive = Pipeline().setStages([document_assembler, t5_active_to_passive])
pipeline_passive_to_active = Pipeline().setStages([document_assembler, t5_passive_to_active])
# Input data example
data_active = spark.createDataFrame([["The dog chased the cat."]]).toDF("text")
data_passive = spark.createDataFrame([["The cat was chased by the dog."]]).toDF("text")
# Apply the pipeline for active to passive
result_active_to_passive = pipeline_active_to_passive.fit(data_active).transform(data_active)
result_active_to_passive.select("passive.result").show(truncate=False)
# Apply the pipeline for passive to active
result_passive_to_active = pipeline_passive_to_active.fit(data_passive).transform(data_passive)
result_passive_to_active.select("active.result").show(truncate=False)
''', language='python')
# Example Output
st.text("""
+--------------------------------+
|passive.result |
+--------------------------------+
|[The cat was chased by the dog.]|
+--------------------------------+
+---------------------------+
|active.result |
+---------------------------+
|[The dog chased the cat.] |
+---------------------------+
""")
# Model Info Section
st.markdown('<div class="sub-title">Choosing the Right T5 Model for Style Transfer</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>Several T5 models are available, each fine-tuned for different tasks. For switching between active and passive voice, two models are used: "t5_active_to_passive_styletransfer" for active-to-passive conversion and "t5_passive_to_active_styletransfer" for passive-to-active conversion.</p>
<p>Depending on your requirements, you can explore other T5 models optimized for different style transfer tasks. Check the <a class="link" href="https://sparknlp.org/models?annotator=T5Transformer" target="_blank">Spark NLP Models Hub</a> to find the most suitable model for your needs.</p>
</div>
""", unsafe_allow_html=True)
# References Section
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://ai.googleblog.com/2020/02/exploring-transfer-learning-with-t5.html" target="_blank">Google AI Blog</a>: Exploring Transfer Learning with T5</li>
<li><a class="link" href="https://sparknlp.org/models?annotator=T5Transformer" target="_blank">Spark NLP Model Hub</a>: Explore T5 models</li>
<li>Model used for Active to Passive: <a class="link" href="https://sparknlp.org/2022/05/31/t5_active_to_passive_styletransfer_en_3_0.html" target="_blank">t5_active_to_passive_styletransfer</a></li>
<li>Model used for Passive to Active: <a class="link" href="https://sparknlp.org/2022/06/01/t5_passive_to_active_styletransfer.html" target="_blank">t5_passive_to_active_styletransfer</a></li>
<li><a class="link" href="https://github.com/google-research/text-to-text-transfer-transformer" target="_blank">GitHub</a>: T5 Transformer repository</li>
<li><a class="link" href="https://arxiv.org/abs/1910.10683" target="_blank">T5 Paper</a>: Detailed insights from the developers</li>
</ul>
</div>
""", unsafe_allow_html=True)
# Community & Support Section
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)
# Quick Links Section
st.markdown('<div class="sub-title">Quick Links</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://sparknlp.org/docs/en/quickstart" target="_blank">Getting Started</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)
|