Spaces:
Sleeping
Sleeping
import streamlit as st | |
import sparknlp | |
import os | |
import pandas as pd | |
from sparknlp.base import * | |
from sparknlp.annotator import * | |
from pyspark.ml import Pipeline | |
from sparknlp.pretrained import PretrainedPipeline | |
# Page configuration | |
st.set_page_config( | |
layout="wide", | |
page_title="Spark NLP Demos App", | |
initial_sidebar_state="auto" | |
) | |
# CSS for styling | |
st.markdown(""" | |
<style> | |
.main-title { | |
font-size: 36px; | |
color: #4A90E2; | |
font-weight: bold; | |
text-align: center; | |
} | |
.section p, .section ul { | |
color: #666666; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
def init_spark(): | |
return sparknlp.start() | |
def create_pipeline(model): | |
documentAssembler = DocumentAssembler()\ | |
.setInputCol("text")\ | |
.setOutputCol("document") | |
use = UniversalSentenceEncoder.pretrained()\ | |
.setInputCols(["document"])\ | |
.setOutputCol("sentence_embeddings") | |
sentimentdl = ClassifierDLModel.pretrained(model)\ | |
.setInputCols(["sentence_embeddings"])\ | |
.setOutputCol("sentiment") | |
nlpPipeline = Pipeline(stages = [documentAssembler, use, sentimentdl]) | |
return nlpPipeline | |
def fit_data(pipeline, data): | |
empty_df = spark.createDataFrame([['']]).toDF('text') | |
pipeline_model = pipeline.fit(empty_df) | |
model = LightPipeline(pipeline_model) | |
results = model.fullAnnotate(data)[0] | |
return results['sentiment'][0].result | |
# Set up the page layout | |
st.markdown('<div class="main-title">Detect Sarcastic Tweets with Spark NLP</div>', unsafe_allow_html=True) | |
# Sidebar content | |
model = st.sidebar.selectbox( | |
"Choose the pretrained model", | |
["classifierdl_use_sarcasm"], | |
help="For more info about the models visit: https://sparknlp.org/models" | |
) | |
# Reference notebook link in sidebar | |
link = """ | |
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/SENTIMENT_EN_SARCASM.ipynb"> | |
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/> | |
</a> | |
""" | |
st.sidebar.markdown('Reference notebook:') | |
st.sidebar.markdown(link, unsafe_allow_html=True) | |
# Load examples | |
examples = [ | |
"Love getting home from work knowing that in less than 8hours you're getting up to go back there again.", | |
"Oh my gosh! Can you imagine @JessieJ playing piano on her tour while singing a song. I would die and go to heaven. #sheisanangel", | |
"Dear Teva, thank you for waking me up every few hours by howling. Your just trying to be mother natures alarm clock.", | |
"The United States is a signatory to this international convention", | |
"If I could put into words how much I love waking up at am on Tuesdays I would", | |
"@pdomo Don't forget that Nick Foles is also the new Tom Brady. What a preseason! #toomanystudQBs #thankgodwedonthavetebow", | |
"I cant even describe how excited I am to go cook noodles for hours", | |
"@Will_Piper should move back up fella. I'm already here... On my own... Having loads of fun", | |
"Tweeting at work... Having sooooo much fun and honestly not bored at all #countdowntillfinish", | |
"I can do what I want to. I play by my own rules" | |
] | |
selected_text = st.selectbox("Select a sample", examples) | |
custom_input = st.text_input("Try it for yourself!") | |
if custom_input: | |
selected_text = custom_input | |
elif selected_text: | |
selected_text = selected_text | |
st.subheader('Selected Text') | |
st.write(selected_text) | |
st.subheader('Selected Text') | |
st.write(selected_text) | |
# Initialize Spark and create pipeline | |
spark = init_spark() | |
pipeline = create_pipeline(model) | |
output = fit_data(pipeline, selected_text) | |
# Display output sentence | |
if output in ['neutral', 'normal']: | |
st.markdown("""<h3>This seems like <span style="color: #209DDC">{}</span> news. <span style="font-size:35px;">🙂</span></h3>""".format(output), unsafe_allow_html=True) | |
elif output == 'sarcasm': | |
st.markdown("""<h3>This seems like a <span style="color: #B64434">{}</span> tweet. <span style="font-size:35px;">🙃</span></h3>""".format('sarcastic'), unsafe_allow_html=True) | |