Spaces:
Sleeping
Sleeping
Update Demo.py
Browse files
Demo.py
CHANGED
|
@@ -1,109 +1,109 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import sparknlp
|
| 3 |
-
|
| 4 |
-
from sparknlp.base import *
|
| 5 |
-
from sparknlp.annotator import *
|
| 6 |
-
from pyspark.ml import Pipeline
|
| 7 |
-
|
| 8 |
-
# Page configuration
|
| 9 |
-
st.set_page_config(
|
| 10 |
-
layout="wide",
|
| 11 |
-
initial_sidebar_state="auto"
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
# CSS for styling
|
| 15 |
-
st.markdown("""
|
| 16 |
-
<style>
|
| 17 |
-
.main-title {
|
| 18 |
-
font-size: 36px;
|
| 19 |
-
color: #4A90E2;
|
| 20 |
-
font-weight: bold;
|
| 21 |
-
text-align: center;
|
| 22 |
-
}
|
| 23 |
-
.section {
|
| 24 |
-
background-color: #f9f9f9;
|
| 25 |
-
padding: 10px;
|
| 26 |
-
border-radius: 10px;
|
| 27 |
-
margin-top: 10px;
|
| 28 |
-
}
|
| 29 |
-
.section p, .section ul {
|
| 30 |
-
color: #666666;
|
| 31 |
-
}
|
| 32 |
-
</style>
|
| 33 |
-
""", unsafe_allow_html=True)
|
| 34 |
-
|
| 35 |
-
@st.cache_resource
|
| 36 |
-
def init_spark():
|
| 37 |
-
return sparknlp.start()
|
| 38 |
-
|
| 39 |
-
@st.cache_resource
|
| 40 |
-
def create_pipeline():
|
| 41 |
-
documentAssembler = DocumentAssembler() \
|
| 42 |
-
.setInputCol("text") \
|
| 43 |
-
.setOutputCol("documents")
|
| 44 |
-
|
| 45 |
-
t5 = T5Transformer.pretrained("t5_grammar_error_corrector") \
|
| 46 |
-
.setTask("gec:") \
|
| 47 |
-
.setInputCols(["documents"])\
|
| 48 |
-
.setMaxOutputLength(200)\
|
| 49 |
-
.setOutputCol("corrections")
|
| 50 |
-
|
| 51 |
-
pipeline = Pipeline().setStages([documentAssembler, t5])
|
| 52 |
-
return pipeline
|
| 53 |
-
|
| 54 |
-
def fit_data(pipeline, data):
|
| 55 |
-
df = spark.createDataFrame([[data]]).toDF("text")
|
| 56 |
-
result = pipeline.fit(df).transform(df)
|
| 57 |
-
return result.select('corrections.result').collect()
|
| 58 |
-
|
| 59 |
-
# Sidebar content
|
| 60 |
-
model = st.sidebar.selectbox(
|
| 61 |
-
"Choose the pretrained model",
|
| 62 |
-
['t5_grammar_error_corrector'],
|
| 63 |
-
help="For more info about the models visit: https://sparknlp.org/models"
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# Set up the page layout
|
| 67 |
-
title = "Correct Sentences Grammar"
|
| 68 |
-
sub_title = "This demo uses a text-to-text model fine-tuned to correct grammatical errors when the task is set to “gec:”. It is based on Prithiviraj Damodaran’s Gramformer model."
|
| 69 |
-
|
| 70 |
-
st.markdown(f'<div class="main-title">{title}</div>', unsafe_allow_html=True)
|
| 71 |
-
st.markdown(f'<div style="text-align: center; color: #666666;">{sub_title}</div>', unsafe_allow_html=True)
|
| 72 |
-
|
| 73 |
-
# Reference notebook link in sidebar
|
| 74 |
-
link = """
|
| 75 |
-
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/T5_LINGUISTIC.ipynb#scrollTo=QAZ3vOX_SW7B">
|
| 76 |
-
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
| 77 |
-
</a>
|
| 78 |
-
"""
|
| 79 |
-
st.sidebar.markdown('Reference notebook:')
|
| 80 |
-
st.sidebar.markdown(link, unsafe_allow_html=True)
|
| 81 |
-
|
| 82 |
-
# Define the exampless
|
| 83 |
-
examples = [
|
| 84 |
-
"She don't knows nothing about what's happening in the office.",
|
| 85 |
-
"They was playing soccer yesterday when it start raining heavily.",
|
| 86 |
-
"This car are more faster than that one, but it costed less money.",
|
| 87 |
-
"I seen him go to the store, but he don't buy nothing from there.",
|
| 88 |
-
"We was going to the park but it start raining before we could leave."
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
# Text selection and analysis
|
| 92 |
-
selected_text = st.selectbox("Select an example", examples)
|
| 93 |
-
custom_input = st.text_input("Try it with your own sentence!")
|
| 94 |
-
|
| 95 |
-
text_to_analyze = custom_input if custom_input else selected_text
|
| 96 |
-
|
| 97 |
-
st.write('Text to
|
| 98 |
-
HTML_WRAPPER = """<div class="scroll entities" style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem; white-space:pre-wrap">{}</div>"""
|
| 99 |
-
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True)
|
| 100 |
-
|
| 101 |
-
# Initialize Spark and create pipeline
|
| 102 |
-
spark = init_spark()
|
| 103 |
-
pipeline = create_pipeline()
|
| 104 |
-
output = fit_data(pipeline, text_to_analyze)
|
| 105 |
-
|
| 106 |
-
# Display transformed sentence
|
| 107 |
-
st.write("Predicted Sentence:")
|
| 108 |
-
output_text = "".join(output[0][0])
|
| 109 |
st.markdown(f'<div class="scroll">{output_text}</div>', unsafe_allow_html=True)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sparknlp
|
| 3 |
+
|
| 4 |
+
from sparknlp.base import *
|
| 5 |
+
from sparknlp.annotator import *
|
| 6 |
+
from pyspark.ml import Pipeline
|
| 7 |
+
|
| 8 |
+
# Page configuration
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
layout="wide",
|
| 11 |
+
initial_sidebar_state="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# CSS for styling
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.main-title {
|
| 18 |
+
font-size: 36px;
|
| 19 |
+
color: #4A90E2;
|
| 20 |
+
font-weight: bold;
|
| 21 |
+
text-align: center;
|
| 22 |
+
}
|
| 23 |
+
.section {
|
| 24 |
+
background-color: #f9f9f9;
|
| 25 |
+
padding: 10px;
|
| 26 |
+
border-radius: 10px;
|
| 27 |
+
margin-top: 10px;
|
| 28 |
+
}
|
| 29 |
+
.section p, .section ul {
|
| 30 |
+
color: #666666;
|
| 31 |
+
}
|
| 32 |
+
</style>
|
| 33 |
+
""", unsafe_allow_html=True)
|
| 34 |
+
|
| 35 |
+
@st.cache_resource
|
| 36 |
+
def init_spark():
|
| 37 |
+
return sparknlp.start()
|
| 38 |
+
|
| 39 |
+
@st.cache_resource
|
| 40 |
+
def create_pipeline():
|
| 41 |
+
documentAssembler = DocumentAssembler() \
|
| 42 |
+
.setInputCol("text") \
|
| 43 |
+
.setOutputCol("documents")
|
| 44 |
+
|
| 45 |
+
t5 = T5Transformer.pretrained("t5_grammar_error_corrector") \
|
| 46 |
+
.setTask("gec:") \
|
| 47 |
+
.setInputCols(["documents"])\
|
| 48 |
+
.setMaxOutputLength(200)\
|
| 49 |
+
.setOutputCol("corrections")
|
| 50 |
+
|
| 51 |
+
pipeline = Pipeline().setStages([documentAssembler, t5])
|
| 52 |
+
return pipeline
|
| 53 |
+
|
| 54 |
+
def fit_data(pipeline, data):
|
| 55 |
+
df = spark.createDataFrame([[data]]).toDF("text")
|
| 56 |
+
result = pipeline.fit(df).transform(df)
|
| 57 |
+
return result.select('corrections.result').collect()
|
| 58 |
+
|
| 59 |
+
# Sidebar content
|
| 60 |
+
model = st.sidebar.selectbox(
|
| 61 |
+
"Choose the pretrained model",
|
| 62 |
+
['t5_grammar_error_corrector'],
|
| 63 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Set up the page layout
|
| 67 |
+
title = "Correct Sentences Grammar"
|
| 68 |
+
sub_title = "This demo uses a text-to-text model fine-tuned to correct grammatical errors when the task is set to “gec:”. It is based on Prithiviraj Damodaran’s Gramformer model."
|
| 69 |
+
|
| 70 |
+
st.markdown(f'<div class="main-title">{title}</div>', unsafe_allow_html=True)
|
| 71 |
+
st.markdown(f'<div style="text-align: center; color: #666666;">{sub_title}</div>', unsafe_allow_html=True)
|
| 72 |
+
|
| 73 |
+
# Reference notebook link in sidebar
|
| 74 |
+
link = """
|
| 75 |
+
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/T5_LINGUISTIC.ipynb#scrollTo=QAZ3vOX_SW7B">
|
| 76 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
| 77 |
+
</a>
|
| 78 |
+
"""
|
| 79 |
+
st.sidebar.markdown('Reference notebook:')
|
| 80 |
+
st.sidebar.markdown(link, unsafe_allow_html=True)
|
| 81 |
+
|
| 82 |
+
# Define the exampless
|
| 83 |
+
examples = [
|
| 84 |
+
"She don't knows nothing about what's happening in the office.",
|
| 85 |
+
"They was playing soccer yesterday when it start raining heavily.",
|
| 86 |
+
"This car are more faster than that one, but it costed less money.",
|
| 87 |
+
"I seen him go to the store, but he don't buy nothing from there.",
|
| 88 |
+
"We was going to the park but it start raining before we could leave."
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
# Text selection and analysis
|
| 92 |
+
selected_text = st.selectbox("Select an example", examples)
|
| 93 |
+
custom_input = st.text_input("Try it with your own sentence!")
|
| 94 |
+
|
| 95 |
+
text_to_analyze = custom_input if custom_input else selected_text
|
| 96 |
+
|
| 97 |
+
st.write('Text to analyze:')
|
| 98 |
+
HTML_WRAPPER = """<div class="scroll entities" style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem; white-space:pre-wrap">{}</div>"""
|
| 99 |
+
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True)
|
| 100 |
+
|
| 101 |
+
# Initialize Spark and create pipeline
|
| 102 |
+
spark = init_spark()
|
| 103 |
+
pipeline = create_pipeline()
|
| 104 |
+
output = fit_data(pipeline, text_to_analyze)
|
| 105 |
+
|
| 106 |
+
# Display transformed sentence
|
| 107 |
+
st.write("Predicted Sentence:")
|
| 108 |
+
output_text = "".join(output[0][0])
|
| 109 |
st.markdown(f'<div class="scroll">{output_text}</div>', unsafe_allow_html=True)
|