Update pages/Sentiment Analysis.py
Browse files- pages/Sentiment Analysis.py +122 -117
pages/Sentiment Analysis.py
CHANGED
@@ -1,117 +1,122 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import sparknlp
|
3 |
-
import os
|
4 |
-
|
5 |
-
|
6 |
-
from sparknlp.
|
7 |
-
from
|
8 |
-
from
|
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 |
-
st.write(
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import sparknlp
|
3 |
+
import os
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
from sparknlp.base import *
|
7 |
+
from sparknlp.annotator import *
|
8 |
+
from pyspark.ml import Pipeline
|
9 |
+
from sparknlp.pretrained import PretrainedPipeline
|
10 |
+
|
11 |
+
# Page configuration
|
12 |
+
st.set_page_config(
|
13 |
+
layout="wide",
|
14 |
+
page_title="Spark NLP Demos App",
|
15 |
+
initial_sidebar_state="auto"
|
16 |
+
)
|
17 |
+
|
18 |
+
# CSS for styling
|
19 |
+
st.markdown("""
|
20 |
+
<style>
|
21 |
+
.main-title {
|
22 |
+
font-size: 36px;
|
23 |
+
color: #4A90E2;
|
24 |
+
font-weight: bold;
|
25 |
+
text-align: center;
|
26 |
+
}
|
27 |
+
.section p, .section ul {
|
28 |
+
color: #666666;
|
29 |
+
}
|
30 |
+
</style>
|
31 |
+
""", unsafe_allow_html=True)
|
32 |
+
|
33 |
+
@st.cache_resource
|
34 |
+
def init_spark():
|
35 |
+
return sparknlp.start()
|
36 |
+
|
37 |
+
@st.cache_resource
|
38 |
+
def create_pipeline(model):
|
39 |
+
documentAssembler = DocumentAssembler()\
|
40 |
+
.setInputCol("text")\
|
41 |
+
.setOutputCol("document")
|
42 |
+
|
43 |
+
use = UniversalSentenceEncoder.pretrained("tfhub_use", "en")\
|
44 |
+
.setInputCols(["document"])\
|
45 |
+
.setOutputCol("sentence_embeddings")
|
46 |
+
|
47 |
+
|
48 |
+
sentimentdl = SentimentDLModel.pretrained(model, "en")\
|
49 |
+
.setInputCols(["sentence_embeddings"])\
|
50 |
+
.setOutputCol("sentiment")
|
51 |
+
|
52 |
+
nlpPipeline = Pipeline(stages=[documentAssembler, use, sentimentdl])
|
53 |
+
|
54 |
+
return nlpPipeline
|
55 |
+
|
56 |
+
def fit_data(pipeline, data):
|
57 |
+
empty_df = spark.createDataFrame([['']]).toDF('text')
|
58 |
+
pipeline_model = pipeline.fit(empty_df)
|
59 |
+
model = LightPipeline(pipeline_model)
|
60 |
+
results = model.fullAnnotate(data)[0]
|
61 |
+
|
62 |
+
return results['sentiment'][0].result
|
63 |
+
|
64 |
+
# Set up the page layout
|
65 |
+
st.markdown('<div class="main-title">State-of-the-Art Sentiment Detection with Spark NLP</div>', unsafe_allow_html=True)
|
66 |
+
|
67 |
+
# Sidebar content
|
68 |
+
model = st.sidebar.selectbox(
|
69 |
+
"Choose the pretrained model",
|
70 |
+
["sentimentdl_use_imdb", "sentimentdl_use_twitter"],
|
71 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
72 |
+
)
|
73 |
+
|
74 |
+
# Reference notebook link in sidebar
|
75 |
+
link = """
|
76 |
+
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/SENTIMENT_EN.ipynb">
|
77 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
78 |
+
</a>
|
79 |
+
"""
|
80 |
+
st.sidebar.markdown('Reference notebook:')
|
81 |
+
st.sidebar.markdown(link, unsafe_allow_html=True)
|
82 |
+
|
83 |
+
# Load examples
|
84 |
+
folder_path = f"inputs/{model}"
|
85 |
+
examples = [
|
86 |
+
lines[1].strip()
|
87 |
+
for filename in os.listdir(folder_path)
|
88 |
+
if filename.endswith('.txt')
|
89 |
+
for lines in [open(os.path.join(folder_path, filename), 'r', encoding='utf-8').readlines()]
|
90 |
+
if len(lines) >= 2
|
91 |
+
]
|
92 |
+
|
93 |
+
selected_text = None
|
94 |
+
result_type = 'tweet'
|
95 |
+
if 'imdb' in model.lower() or 't5' in model.lower():
|
96 |
+
selected_text = st.selectbox("Select a sample IMDB review", examples)
|
97 |
+
result_type = 'review'
|
98 |
+
else:
|
99 |
+
selected_text = st.selectbox("Select a sample Tweet", examples)
|
100 |
+
|
101 |
+
custom_input = st.text_input("Try it for yourself!")
|
102 |
+
|
103 |
+
if custom_input:
|
104 |
+
selected_text = custom_input
|
105 |
+
elif selected_text:
|
106 |
+
selected_text = selected_text
|
107 |
+
|
108 |
+
st.write('Selected Text')
|
109 |
+
st.write(selected_text)
|
110 |
+
|
111 |
+
# Initialize Spark and create pipeline
|
112 |
+
spark = init_spark()
|
113 |
+
pipeline = create_pipeline(model)
|
114 |
+
output = fit_data(pipeline, selected_text)
|
115 |
+
|
116 |
+
# Display output sentence
|
117 |
+
if output in ['pos', 'positive', 'POSITIVE']:
|
118 |
+
st.markdown("""<h3>This seems like a <span style="color: green">{}</span> {}. <span style="font-size:35px;">😃</span></h3>""".format('positive', result_type), unsafe_allow_html=True)
|
119 |
+
elif output in ['neg', 'negative', 'NEGATIVE']:
|
120 |
+
st.markdown("""<h3>This seems like a <span style="color: red">{}</span> {}. <span style="font-size:35px;">😠</span?</h3>""".format('negative', result_type), unsafe_allow_html=True)
|
121 |
+
|
122 |
+
|