import streamlit as st from sparknlp.base import * from sparknlp.annotator import * from pyspark.ml import Pipeline # 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('
Switch Between Informal and Formal Style
', unsafe_allow_html=True) # Introduction Section st.markdown("""

Switching between informal and formal styles is a crucial skill in effective communication. Informal style is often used in casual conversations, while formal style is reserved for professional or official contexts. Understanding how to adapt your language to different audiences can greatly enhance the clarity and impact of your message.

On this page, we explore how to implement a pipeline that automatically switches between informal and formal styles using advanced NLP models. We utilize the T5 Transformer model, fine-tuned for style transfer, to seamlessly convert sentences between these two styles.

""", unsafe_allow_html=True) # T5 Transformer Overview st.markdown('
Understanding the T5 Transformer for Style Transfer
', unsafe_allow_html=True) st.markdown("""

The T5 (Text-To-Text Transfer Transformer) model, developed by Google, is a versatile tool for a wide range of NLP tasks. For style transfer, T5 can be fine-tuned to convert text between different styles, such as informal to formal and vice versa.

By processing input sentences and applying the appropriate style transfer, T5 generates outputs that adjust the tone while preserving the original meaning. This is especially useful for applications in writing assistance, automated editing, and communication training.

""", unsafe_allow_html=True) # Performance Section st.markdown('
Performance and Use Cases
', unsafe_allow_html=True) st.markdown("""

The T5 model has demonstrated strong performance in text transformation tasks, including style transfer between informal and formal language. It consistently produces accurate and contextually appropriate results, making it a valuable tool for enhancing communication in various settings.

This capability is beneficial for writers, editors, and professionals who need to adapt text to different audiences or contexts. The T5 model’s ability to perform these transformations efficiently makes it a powerful asset for improving written communication.

""", unsafe_allow_html=True) # Implementation Section st.markdown('
Implementing Informal-Formal Style Switching
', unsafe_allow_html=True) st.markdown("""

The following example demonstrates how to implement a style transfer pipeline using Spark NLP to switch between informal and formal styles. The pipeline includes a document assembler and the T5 model for performing the transformations in both directions.

""", 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") # Informal to Formal transformation t5_informal_to_formal = T5Transformer()\\ .pretrained("t5_informal_to_formal_styletransfer")\\ .setTask("transfer Casual to Formal:")\\ .setInputCols(["documents"])\\ .setOutputCol("formal") # Formal to Informal transformation t5_formal_to_informal = T5Transformer()\\ .pretrained("t5_formal_to_informal_styletransfer")\\ .setTask("transfer Formal to Casual:")\\ .setInputCols(["documents"])\\ .setOutputCol("informal") pipeline_informal_to_formal = Pipeline().setStages([document_assembler, t5_informal_to_formal]) pipeline_formal_to_informal = Pipeline().setStages([document_assembler, t5_formal_to_informal]) # Input data examples data_informal = spark.createDataFrame([["Hey, what’s up? Wanna hang out later?"]]).toDF("text") data_formal = spark.createDataFrame([["I would like to inquire about your availability for a meeting."]]).toDF("text") # Apply the pipeline for informal to formal result_informal_to_formal = pipeline_informal_to_formal.fit(data_informal).transform(data_informal) result_informal_to_formal.select("formal.result").show(truncate=False) # Apply the pipeline for formal to informal result_formal_to_informal = pipeline_formal_to_informal.fit(data_formal).transform(data_formal) result_formal_to_informal.select("informal.result").show(truncate=False) ''', language='python') # Example Output st.text(""" +---------------------------------------------------------------+ |formal.result | +---------------------------------------------------------------+ |[I would like to know if you are available to meet later.] | +---------------------------------------------------------------+ +---------------------------------------------------------------+ |informal.result | +---------------------------------------------------------------+ |[Hey, wanna hang out later?] | +---------------------------------------------------------------+ """) # Model Info Section st.markdown('
Choosing the Right T5 Model for Style Transfer
', unsafe_allow_html=True) st.markdown("""

For switching between informal and formal styles, we use the models: "t5_informal_to_formal_styletransfer" for informal-to-formal conversion and "t5_formal_to_informal_styletransfer" for formal-to-informal conversion.

Explore other T5 models tailored for different style transfer tasks on the Spark NLP Models Hub to find the best fit for your specific needs.

""", unsafe_allow_html=True) # References Section st.markdown('
References
', unsafe_allow_html=True) st.markdown("""
""", unsafe_allow_html=True) # Community & Support Section st.markdown('
Community & Support
', unsafe_allow_html=True) st.markdown("""
""", unsafe_allow_html=True) # Quick Links Section st.markdown('
Quick Links
', unsafe_allow_html=True) st.markdown("""
""", unsafe_allow_html=True)