abdullahmubeen10 commited on
Commit
b5e54f7
ยท
verified ยท
1 Parent(s): 30e8958

Update pages/Workflow & Model Overview.py

Browse files
Files changed (1) hide show
  1. pages/Workflow & Model Overview.py +238 -224
pages/Workflow & Model Overview.py CHANGED
@@ -1,224 +1,238 @@
1
- import streamlit as st
2
-
3
- # Custom CSS for better styling
4
- st.markdown("""
5
- <style>
6
- .main-title {
7
- font-size: 36px;
8
- color: #4A90E2;
9
- font-weight: bold;
10
- text-align: center;
11
- }
12
- .sub-title {
13
- font-size: 24px;
14
- color: #4A90E2;
15
- margin-top: 20px;
16
- }
17
- .section {
18
- background-color: #f9f9f9;
19
- padding: 15px;
20
- border-radius: 10px;
21
- margin-top: 20px;
22
- }
23
- .section h2 {
24
- font-size: 22px;
25
- color: #4A90E2;
26
- }
27
- .section p, .section ul {
28
- color: #666666;
29
- }
30
- .link {
31
- color: #4A90E2;
32
- text-decoration: none;
33
- }
34
- </style>
35
- """, unsafe_allow_html=True)
36
-
37
- # Introduction
38
- st.markdown('<div class="main-title">Sentiment Analysis with Spark NLP</div>', unsafe_allow_html=True)
39
-
40
- st.markdown("""
41
- <div class="section">
42
- <p>Welcome to the Spark NLP Sentiment Analysis Demo App! Sentiment analysis is an automated process capable of understanding the feelings or opinions that underlie a text. This process is considered a text classification and is one of the most interesting subfields of NLP. Using Spark NLP, it is possible to analyze the sentiment in a text with high accuracy.</p>
43
- <p>This app demonstrates how to use Spark NLP's SentimentDetector to perform sentiment analysis using a rule-based approach.</p>
44
- </div>
45
- """, unsafe_allow_html=True)
46
-
47
- st.image('images/Sentiment-Analysis.jpg',caption="Difference between rule-based and machine learning based sentiment analysis applications", use_column_width='auto')
48
-
49
- # About Sentiment Analysis
50
- st.markdown('<div class="sub-title">About Sentiment Analysis</div>', unsafe_allow_html=True)
51
- st.markdown("""
52
- <div class="section">
53
- <p>Sentiment analysis studies the subjective information in an expression, such as opinions, appraisals, emotions, or attitudes towards a topic, person, or entity. Expressions can be classified as positive, negative, or neutral โ€” in some cases, even more detailed.</p>
54
- <p>Some popular sentiment analysis applications include social media monitoring, customer support management, and analyzing customer feedback.</p>
55
- </div>
56
- """, unsafe_allow_html=True)
57
-
58
- # Using SentimentDetector in Spark NLP
59
- st.markdown('<div class="sub-title">Using SentimentDetector in Spark NLP</div>', unsafe_allow_html=True)
60
- st.markdown("""
61
- <div class="section">
62
- <p>The SentimentDetector annotator in Spark NLP uses a rule-based approach to analyze the sentiment in text data. This method involves using a set of predefined rules or patterns to classify text as positive, negative, or neutral.</p>
63
- <p>Spark NLP also provides Machine Learning (ML) and Deep Learning (DL) solutions for sentiment analysis. If you are interested in those approaches, please check the <a class="link" href="https://nlp.johnsnowlabs.com/docs/en/annotators#viveknsentiment" target="_blank" rel="noopener">ViveknSentiment </a> and <a class="link" href="https://nlp.johnsnowlabs.com/docs/en/annotators#sentimentdl" target="_blank" rel="noopener">SentimentDL</a> annotators of Spark NLP.</p>
64
- </div>
65
- """, unsafe_allow_html=True)
66
-
67
- st.markdown('<h2 class="sub-title">Example Usage in Python</h2>', unsafe_allow_html=True)
68
- st.markdown('<p>Hereโ€™s how you can implement sentiment analysis using the SentimentDetector annotator in Spark NLP:</p>', unsafe_allow_html=True)
69
-
70
- # Setup Instructions
71
- st.markdown('<div class="sub-title">Setup</div>', unsafe_allow_html=True)
72
- st.markdown('<p>To install Spark NLP in Python, use your favorite package manager (conda, pip, etc.). For example:</p>', unsafe_allow_html=True)
73
- st.code("""
74
- pip install spark-nlp
75
- pip install pyspark
76
- """, language="bash")
77
-
78
- st.markdown("<p>Then, import Spark NLP and start a Spark session:</p>", unsafe_allow_html=True)
79
- st.code("""
80
- import sparknlp
81
-
82
- # Start Spark Session
83
- spark = sparknlp.start()
84
- """, language='python')
85
-
86
- # load data
87
- st.markdown('<div class="sub-title">Start by loading the Dataset, Lemmas and the Sentiment Dictionary.</div>', unsafe_allow_html=True)
88
- st.code("""
89
- !wget -N https://s3.amazonaws.com/auxdata.johnsnowlabs.com/public/resources/en/lemma-corpus-small/lemmas_small.txt -P /tmp
90
- !wget -N https://s3.amazonaws.com/auxdata.johnsnowlabs.com/public/resources/en/sentiment-corpus/default-sentiment-dict.txt -P /tmp
91
- """, language="bash")
92
-
93
- st.image('images/dataset.png', caption="First few lines of the lemmas and sentiment dictionary", use_column_width='auto')
94
-
95
- # Sentiment Analysis Example
96
- st.markdown('<div class="sub-title">Example Usage: Sentiment Analysis with SentimentDetector</div>', unsafe_allow_html=True)
97
- st.code('''
98
- from sparknlp.base import DocumentAssembler, Pipeline, Finisher
99
- from sparknlp.annotator import (
100
- SentenceDetector,
101
- Tokenizer,
102
- Lemmatizer,
103
- SentimentDetector
104
- )
105
- import pyspark.sql.functions as F
106
-
107
- # Step 1: Transforms raw texts to document annotation
108
- document_assembler = (
109
- DocumentAssembler()
110
- .setInputCol("text")
111
- .setOutputCol("document")
112
- )
113
-
114
- # Step 2: Sentence Detection
115
- sentence_detector = SentenceDetector().setInputCols(["document"]).setOutputCol("sentence")
116
-
117
- # Step 3: Tokenization
118
- tokenizer = Tokenizer().setInputCols(["sentence"]).setOutputCol("token")
119
-
120
- # Step 4: Lemmatization
121
- lemmatizer = (
122
- Lemmatizer()
123
- .setInputCols("token")
124
- .setOutputCol("lemma")
125
- .setDictionary("/tmp/lemmas_small.txt", key_delimiter="->", value_delimiter="\\t")
126
- )
127
-
128
- # Step 5: Sentiment Detection
129
- sentiment_detector = (
130
- SentimentDetector()
131
- .setInputCols(["lemma", "sentence"])
132
- .setOutputCol("sentiment_score")
133
- .setDictionary("/tmp/default-sentiment-dict.txt", ",")
134
- )
135
-
136
- # Step 6: Finisher
137
- finisher = (
138
- Finisher()
139
- .setInputCols(["sentiment_score"])
140
- .setOutputCols(["sentiment"])
141
- )
142
-
143
- # Define the pipeline
144
- pipeline = Pipeline(
145
- stages=[
146
- document_assembler,
147
- sentence_detector,
148
- tokenizer,
149
- lemmatizer,
150
- sentiment_detector,
151
- finisher,
152
- ]
153
- )
154
-
155
- # Create a spark Data Frame with an example sentence
156
- data = spark.createDataFrame(
157
- [
158
- ["The restaurant staff is really nice"]
159
- ]
160
- ).toDF("text") # use the column name `text` defined in the pipeline as input
161
-
162
- # Fit-transform to get predictions
163
- result = pipeline.fit(data).transform(data).show(truncate=50)
164
- ''', language='python')
165
-
166
- st.text("""
167
- +-----------------------------------+----------+
168
- | text| sentiment|
169
- +-----------------------------------+----------+
170
- |The restaurant staff is really nice|[positive]|
171
- +-----------------------------------+----------+
172
- """)
173
-
174
- st.markdown("""
175
- <p>The code snippet demonstrates how to set up a pipeline in Spark NLP to perform sentiment analysis on text data using the SentimentDetector annotator. The resulting DataFrame contains the sentiment predictions.</p>
176
- """, unsafe_allow_html=True)
177
-
178
- # One-liner Alternative
179
- st.markdown('<div class="sub-title">One-liner Alternative</div>', unsafe_allow_html=True)
180
- st.markdown("""
181
- <div class="section">
182
- <p>In October 2022, John Snow Labs released the open-source <code>johnsnowlabs</code> library that contains all the company products, open-source and licensed, under one common library. This simplified the workflow, especially for users working with more than one of the libraries (e.g., Spark NLP + Healthcare NLP). This new library is a wrapper on all of John Snow Labโ€™s libraries and can be installed with pip:</p>
183
- <p><code>pip install johnsnowlabs</code></p>
184
- </div>
185
- """, unsafe_allow_html=True)
186
-
187
- st.markdown('<p>To run sentiment analysis with one line of code, we can simply:</p>', unsafe_allow_html=True)
188
- st.code("""
189
- # Import the NLP module which contains Spark NLP and NLU libraries
190
- from johnsnowlabs import nlp
191
-
192
- sample_text = "The restaurant staff is really nice"
193
-
194
- # Returns a pandas DataFrame, we select the desired columns
195
- nlp.load('en.sentiment').predict(sample_text, output_level='sentence')
196
- """, language='python')
197
-
198
- st.image('images/johnsnowlabs-sentiment-output.png', use_column_width='auto')
199
-
200
- st.markdown("""
201
- <p>This approach demonstrates how to use the <code>johnsnowlabs</code> library to perform sentiment analysis with a single line of code. The resulting DataFrame contains the sentiment predictions.</p>
202
- """, unsafe_allow_html=True)
203
-
204
- # Conclusion
205
- st.markdown("""
206
- <div class="section">
207
- <h2>Conclusion</h2>
208
- <p>In this app, we demonstrated how to use Spark NLP's SentimentDetector annotator to perform sentiment analysis on text data. These powerful tools enable users to efficiently process large datasets and identify sentiment, providing deeper insights for various applications. By integrating these annotators into your NLP pipelines, you can enhance text understanding, information extraction, and customer sentiment analysis.</p>
209
- </div>
210
- """, unsafe_allow_html=True)
211
-
212
- # References and Additional Information
213
- st.markdown('<div class="sub-title">For additional information, please check the following references.</div>', unsafe_allow_html=True)
214
-
215
- st.markdown("""
216
- <div class="section">
217
- <ul>
218
- <li>Documentation :&nbsp;<a href="https://nlp.johnsnowlabs.com/docs/en/transformers#sentiment" target="_blank" rel="noopener">SentimentDetector</a></li>
219
- <li>Python Docs :&nbsp;<a href="https://nlp.johnsnowlabs.com/api/python/reference/autosummary/sparknlp/annotator/sentiment/index.html#sparknlp.annotator.sentiment.SentimentDetector" target="_blank" rel="noopener">SentimentDetector</a></li>
220
- <li>Scala Docs :&nbsp;<a href="https://nlp.johnsnowlabs.com/api/com/johnsnowlabs/nlp/annotators/sentiment/SentimentDetector.html" target="_blank" rel="noopener">SentimentDetector</a></li>
221
- <li>Example Notebook :&nbsp;<a href="https://github.com/JohnSnowLabs/spark-nlp-workshop/blob/master/jupyter/training/english/classification/Sentiment%20Analysis.ipynb" target="_blank" rel="noopener">Sentiment Analysis</a></li>
222
- </ul>
223
- </div>
224
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # Custom CSS for better styling
4
+ st.markdown("""
5
+ <style>
6
+ .main-title {
7
+ font-size: 36px;
8
+ color: #4A90E2;
9
+ font-weight: bold;
10
+ text-align: center;
11
+ }
12
+ .sub-title {
13
+ font-size: 24px;
14
+ color: #4A90E2;
15
+ margin-top: 20px;
16
+ }
17
+ .section {
18
+ background-color: #f9f9f9;
19
+ padding: 15px;
20
+ border-radius: 10px;
21
+ margin-top: 20px;
22
+ }
23
+ .section h2 {
24
+ font-size: 22px;
25
+ color: #4A90E2;
26
+ }
27
+ .section p, .section ul {
28
+ color: #666666;
29
+ }
30
+ .link {
31
+ color: #4A90E2;
32
+ text-decoration: none;
33
+ }
34
+ </style>
35
+ """, unsafe_allow_html=True)
36
+
37
+ # Introduction
38
+ st.markdown('<div class="main-title">Sentiment Analysis with Spark NLP</div>', unsafe_allow_html=True)
39
+
40
+ st.markdown("""
41
+ <div class="section">
42
+ <p>Welcome to the Spark NLP Sentiment Analysis Demo App! Sentiment analysis is an automated process capable of understanding the feelings or opinions that underlie a text. This process is considered a text classification and is one of the most interesting subfields of NLP. Using Spark NLP, it is possible to analyze the sentiment in a text with high accuracy.</p>
43
+ <p>This app demonstrates how to use Spark NLP's SentimentDetector to perform sentiment analysis using a rule-based approach.</p>
44
+ </div>
45
+ """, unsafe_allow_html=True)
46
+
47
+ st.image('images/Sentiment-Analysis.jpg',caption="Difference between rule-based and machine learning based sentiment analysis applications", use_column_width='auto')
48
+
49
+ # About Sentiment Analysis
50
+ st.markdown('<div class="sub-title">About Sentiment Analysis</div>', unsafe_allow_html=True)
51
+ st.markdown("""
52
+ <div class="section">
53
+ <p>Sentiment analysis studies the subjective information in an expression, such as opinions, appraisals, emotions, or attitudes towards a topic, person, or entity. Expressions can be classified as positive, negative, or neutral โ€” in some cases, even more detailed.</p>
54
+ <p>Some popular sentiment analysis applications include social media monitoring, customer support management, and analyzing customer feedback.</p>
55
+ </div>
56
+ """, unsafe_allow_html=True)
57
+
58
+ # Using SentimentDetector in Spark NLP
59
+ st.markdown('<div class="sub-title">Using SentimentDetector in Spark NLP</div>', unsafe_allow_html=True)
60
+ st.markdown("""
61
+ <div class="section">
62
+ <p>The SentimentDetector annotator in Spark NLP uses a rule-based approach to analyze the sentiment in text data. This method involves using a set of predefined rules or patterns to classify text as positive, negative, or neutral.</p>
63
+ <p>Spark NLP also provides Machine Learning (ML) and Deep Learning (DL) solutions for sentiment analysis. If you are interested in those approaches, please check the <a class="link" href="https://nlp.johnsnowlabs.com/docs/en/annotators#viveknsentiment" target="_blank" rel="noopener">ViveknSentiment </a> and <a class="link" href="https://nlp.johnsnowlabs.com/docs/en/annotators#sentimentdl" target="_blank" rel="noopener">SentimentDL</a> annotators of Spark NLP.</p>
64
+ </div>
65
+ """, unsafe_allow_html=True)
66
+
67
+ st.markdown('<h2 class="sub-title">Example Usage in Python</h2>', unsafe_allow_html=True)
68
+ st.markdown('<p>Hereโ€™s how you can implement sentiment analysis using the SentimentDetector annotator in Spark NLP:</p>', unsafe_allow_html=True)
69
+
70
+ # Setup Instructions
71
+ st.markdown('<div class="sub-title">Setup</div>', unsafe_allow_html=True)
72
+ st.markdown('<p>To install Spark NLP in Python, use your favorite package manager (conda, pip, etc.). For example:</p>', unsafe_allow_html=True)
73
+ st.code("""
74
+ pip install spark-nlp
75
+ pip install pyspark
76
+ """, language="bash")
77
+
78
+ st.markdown("<p>Then, import Spark NLP and start a Spark session:</p>", unsafe_allow_html=True)
79
+ st.code("""
80
+ import sparknlp
81
+
82
+ # Start Spark Session
83
+ spark = sparknlp.start()
84
+ """, language='python')
85
+
86
+ # load data
87
+ st.markdown('<div class="sub-title">Start by loading the Dataset, Lemmas and the Sentiment Dictionary.</div>', unsafe_allow_html=True)
88
+ st.code("""
89
+ !wget -N https://s3.amazonaws.com/auxdata.johnsnowlabs.com/public/resources/en/lemma-corpus-small/lemmas_small.txt -P /tmp
90
+ !wget -N https://s3.amazonaws.com/auxdata.johnsnowlabs.com/public/resources/en/sentiment-corpus/default-sentiment-dict.txt -P /tmp
91
+ """, language="bash")
92
+
93
+ st.image('images/dataset.png', caption="First few lines of the lemmas and sentiment dictionary", use_column_width='auto')
94
+
95
+ # Sentiment Analysis Example
96
+ st.markdown('<div class="sub-title">Example Usage: Sentiment Analysis with SentimentDetector</div>', unsafe_allow_html=True)
97
+ st.code('''
98
+ from sparknlp.base import DocumentAssembler, Pipeline, Finisher
99
+ from sparknlp.annotator import (
100
+ SentenceDetector,
101
+ Tokenizer,
102
+ Lemmatizer,
103
+ SentimentDetector
104
+ )
105
+ import pyspark.sql.functions as F
106
+
107
+ # Step 1: Transforms raw texts to document annotation
108
+ document_assembler = (
109
+ DocumentAssembler()
110
+ .setInputCol("text")
111
+ .setOutputCol("document")
112
+ )
113
+
114
+ # Step 2: Sentence Detection
115
+ sentence_detector = SentenceDetector().setInputCols(["document"]).setOutputCol("sentence")
116
+
117
+ # Step 3: Tokenization
118
+ tokenizer = Tokenizer().setInputCols(["sentence"]).setOutputCol("token")
119
+
120
+ # Step 4: Lemmatization
121
+ lemmatizer = (
122
+ Lemmatizer()
123
+ .setInputCols("token")
124
+ .setOutputCol("lemma")
125
+ .setDictionary("/tmp/lemmas_small.txt", key_delimiter="->", value_delimiter="\\t")
126
+ )
127
+
128
+ # Step 5: Sentiment Detection
129
+ sentiment_detector = (
130
+ SentimentDetector()
131
+ .setInputCols(["lemma", "sentence"])
132
+ .setOutputCol("sentiment_score")
133
+ .setDictionary("/tmp/default-sentiment-dict.txt", ",")
134
+ )
135
+
136
+ # Step 6: Finisher
137
+ finisher = (
138
+ Finisher()
139
+ .setInputCols(["sentiment_score"])
140
+ .setOutputCols(["sentiment"])
141
+ )
142
+
143
+ # Define the pipeline
144
+ pipeline = Pipeline(
145
+ stages=[
146
+ document_assembler,
147
+ sentence_detector,
148
+ tokenizer,
149
+ lemmatizer,
150
+ sentiment_detector,
151
+ finisher,
152
+ ]
153
+ )
154
+
155
+ # Create a spark Data Frame with an example sentence
156
+ data = spark.createDataFrame(
157
+ [
158
+ ["The restaurant staff is really nice"]
159
+ ]
160
+ ).toDF("text") # use the column name `text` defined in the pipeline as input
161
+
162
+ # Fit-transform to get predictions
163
+ result = pipeline.fit(data).transform(data).show(truncate=50)
164
+ ''', language='python')
165
+
166
+ st.text("""
167
+ +-----------------------------------+----------+
168
+ | text| sentiment|
169
+ +-----------------------------------+----------+
170
+ |The restaurant staff is really nice|[positive]|
171
+ +-----------------------------------+----------+
172
+ """)
173
+
174
+ st.markdown("""
175
+ <p>The code snippet demonstrates how to set up a pipeline in Spark NLP to perform sentiment analysis on text data using the SentimentDetector annotator. The resulting DataFrame contains the sentiment predictions.</p>
176
+ """, unsafe_allow_html=True)
177
+
178
+ # One-liner Alternative
179
+ st.markdown('<div class="sub-title">One-liner Alternative</div>', unsafe_allow_html=True)
180
+ st.markdown("""
181
+ <div class="section">
182
+ <p>In October 2022, John Snow Labs released the open-source <code>johnsnowlabs</code> library that contains all the company products, open-source and licensed, under one common library. This simplified the workflow, especially for users working with more than one of the libraries (e.g., Spark NLP + Healthcare NLP). This new library is a wrapper on all of John Snow Labโ€™s libraries and can be installed with pip:</p>
183
+ <p><code>pip install johnsnowlabs</code></p>
184
+ </div>
185
+ """, unsafe_allow_html=True)
186
+
187
+ st.markdown('<p>To run sentiment analysis with one line of code, we can simply:</p>', unsafe_allow_html=True)
188
+ st.code("""
189
+ # Import the NLP module which contains Spark NLP and NLU libraries
190
+ from johnsnowlabs import nlp
191
+
192
+ sample_text = "The restaurant staff is really nice"
193
+
194
+ # Returns a pandas DataFrame, we select the desired columns
195
+ nlp.load('en.sentiment').predict(sample_text, output_level='sentence')
196
+ """, language='python')
197
+
198
+ st.image('images/johnsnowlabs-sentiment-output.png', use_column_width='auto')
199
+
200
+ st.markdown("""
201
+ <p>This approach demonstrates how to use the <code>johnsnowlabs</code> library to perform sentiment analysis with a single line of code. The resulting DataFrame contains the sentiment predictions.</p>
202
+ """, unsafe_allow_html=True)
203
+
204
+ # Conclusion
205
+ st.markdown("""
206
+ <div class="section">
207
+ <h2>Conclusion</h2>
208
+ <p>In this app, we demonstrated how to use Spark NLP's SentimentDetector annotator to perform sentiment analysis on text data. These powerful tools enable users to efficiently process large datasets and identify sentiment, providing deeper insights for various applications. By integrating these annotators into your NLP pipelines, you can enhance text understanding, information extraction, and customer sentiment analysis.</p>
209
+ </div>
210
+ """, unsafe_allow_html=True)
211
+
212
+ # References and Additional Information
213
+ st.markdown('<div class="sub-title">For additional information, please check the following references.</div>', unsafe_allow_html=True)
214
+
215
+ st.markdown("""
216
+ <div class="section">
217
+ <ul>
218
+ <li>Documentation: <a href="https://nlp.johnsnowlabs.com/docs/en/annotators#sentimentdetector" target="_blank" rel="noopener">SentimentDetector</a></li>
219
+ <li>Python Docs : <a href="https://nlp.johnsnowlabs.com/api/python/reference/autosummary/sparknlp/annotator/sentiment/sentiment_detector/index.html" target="_blank" rel="noopener">SentimentDetector</a></li>
220
+ <li>Scala Docs: <a href="https://nlp.johnsnowlabs.com/api/com/johnsnowlabs/nlp/annotators/sda/pragmatic/SentimentDetector.html" target="_blank" rel="noopener">SentimentDetector</a></li>
221
+ <li>Example Notebook :&nbsp;<a href="https://github.com/JohnSnowLabs/spark-nlp-workshop/blob/master/jupyter/training/english/classification/Sentiment%20Analysis.ipynb" target="_blank" rel="noopener">Sentiment Analysis</a></li>
222
+ <li>For extended examples of usage, see the <a href="https://github.com/JohnSnowLabs/spark-nlp-workshop/tree/master/tutorials/Certification_Trainings/Public" target="_blank" rel="noopener">Spark NLP Workshop repository</a>.</li>
223
+ </ul>
224
+ </div>
225
+ """, unsafe_allow_html=True)
226
+
227
+ st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
228
+ st.markdown("""
229
+ <div class="section">
230
+ <ul>
231
+ <li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
232
+ <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>
233
+ <li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li>
234
+ <li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li>
235
+ <li><a class="link" href="https://www.youtube.com/channel/UCmFOjlpYEhxf_wJUDuz6xxQ/videos" target="_blank">YouTube</a>: Video tutorials</li>
236
+ </ul>
237
+ </div>
238
+ """, unsafe_allow_html=True)