abdullahmubeen10 commited on
Commit
6197660
Β·
verified Β·
1 Parent(s): a5d5930

Update Demo.py

Browse files
Files changed (1) hide show
  1. Demo.py +124 -124
Demo.py CHANGED
@@ -1,124 +1,124 @@
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_name = 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
- st.subheader("Detect the general sentiment expressed in a movie review or tweet by using our pretrained Spark NLP DL classifier.")
94
-
95
- selected_text = None
96
- result_type = 'tweet'
97
- if 'imdb' in model.lower() or 't5' in model.lower():
98
- selected_text = st.selectbox("Select a sample IMDB review", examples)
99
- result_type = 'review'
100
- else:
101
- selected_text = st.selectbox("Select a sample Tweet", examples)
102
-
103
- custom_input = st.text_input("Try it for yourself!")
104
-
105
- if custom_input:
106
- selected_text = custom_input
107
- elif selected_text:
108
- selected_text = selected_text
109
-
110
- st.subheader('Selected Text')
111
- st.write(selected_text)
112
-
113
- # Initialize Spark and create pipeline
114
- spark = init_spark()
115
- pipeline = create_pipeline(model)
116
- output = fit_data(pipeline, selected_text)
117
-
118
- # Display output sentence
119
- if output in ['pos', 'positive', 'POSITIVE']:
120
- st.markdown("""<h3>This seems like a <span style="color: green">{}</span> {}. <span style="font-size:35px;">&#128515;</span></h3>""".format('positive', result_type), unsafe_allow_html=True)
121
- elif output in ['neg', 'negative', 'NEGATIVE']:
122
- st.markdown("""<h3>This seems like a <span style="color: red">{}</span> {}. <span style="font-size:35px;">&#128544;</span?</h3>""".format('negative', result_type), unsafe_allow_html=True)
123
-
124
-
 
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
+ st.subheader("Detect the general sentiment expressed in a movie review or tweet by using our pretrained Spark NLP DL classifier.")
94
+
95
+ selected_text = None
96
+ result_type = 'tweet'
97
+ if 'imdb' in model.lower() or 't5' in model.lower():
98
+ selected_text = st.selectbox("Select a sample IMDB review", examples)
99
+ result_type = 'review'
100
+ else:
101
+ selected_text = st.selectbox("Select a sample Tweet", examples)
102
+
103
+ custom_input = st.text_input("Try it for yourself!")
104
+
105
+ if custom_input:
106
+ selected_text = custom_input
107
+ elif selected_text:
108
+ selected_text = selected_text
109
+
110
+ st.subheader('Selected Text')
111
+ st.write(selected_text)
112
+
113
+ # Initialize Spark and create pipeline
114
+ spark = init_spark()
115
+ pipeline = create_pipeline(model)
116
+ output = fit_data(pipeline, selected_text)
117
+
118
+ # Display output sentence
119
+ if output in ['pos', 'positive', 'POSITIVE']:
120
+ st.markdown("""<h3>This seems like a <span style="color: green">{}</span> {}. <span style="font-size:35px;">&#128515;</span></h3>""".format('positive', result_type), unsafe_allow_html=True)
121
+ elif output in ['neg', 'negative', 'NEGATIVE']:
122
+ st.markdown("""<h3>This seems like a <span style="color: red">{}</span> {}. <span style="font-size:35px;">&#128544;</span?</h3>""".format('negative', result_type), unsafe_allow_html=True)
123
+
124
+