Update Demo.py
Browse files
Demo.py
CHANGED
@@ -1,115 +1,115 @@
|
|
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 |
-
initial_sidebar_state="auto"
|
15 |
-
)
|
16 |
-
|
17 |
-
# CSS for styling
|
18 |
-
st.markdown("""
|
19 |
-
<style>
|
20 |
-
.main-title {
|
21 |
-
font-size: 36px;
|
22 |
-
color: #4A90E2;
|
23 |
-
font-weight: bold;
|
24 |
-
text-align: center;
|
25 |
-
}
|
26 |
-
.section p, .section ul {
|
27 |
-
color: #666666;
|
28 |
-
}
|
29 |
-
</style>
|
30 |
-
""", unsafe_allow_html=True)
|
31 |
-
|
32 |
-
@st.cache_resource
|
33 |
-
def init_spark():
|
34 |
-
return sparknlp.start()
|
35 |
-
|
36 |
-
@st.cache_resource
|
37 |
-
def create_pipeline(model):
|
38 |
-
document = DocumentAssembler()\
|
39 |
-
.setInputCol("text")\
|
40 |
-
.setOutputCol("document")
|
41 |
-
|
42 |
-
embeddings = BertSentenceEmbeddings\
|
43 |
-
.pretrained('labse', 'xx') \
|
44 |
-
.setInputCols(["document"])\
|
45 |
-
.setOutputCol("sentence_embeddings")
|
46 |
-
|
47 |
-
sentimentClassifier = ClassifierDLModel.pretrained("classifierdl_bert_sentiment", "fr") \
|
48 |
-
.setInputCols(["sentence_embeddings"]) \
|
49 |
-
.setOutputCol("class_")
|
50 |
-
|
51 |
-
nlpPipeline = Pipeline(
|
52 |
-
stages=[
|
53 |
-
document,
|
54 |
-
embeddings,
|
55 |
-
sentimentClassifier])
|
56 |
-
|
57 |
-
return nlpPipeline
|
58 |
-
|
59 |
-
def fit_data(pipeline, data):
|
60 |
-
empty_df = spark.createDataFrame([['']]).toDF('text')
|
61 |
-
pipeline_model = pipeline.fit(empty_df)
|
62 |
-
model = LightPipeline(pipeline_model)
|
63 |
-
results = model.fullAnnotate(data)[0]
|
64 |
-
|
65 |
-
return results['class_'][0].result
|
66 |
-
|
67 |
-
# Set up the page layout
|
68 |
-
st.markdown('<div class="main-title">State-of-the-Art French Sentiment Detection with Spark NLP</div>', unsafe_allow_html=True)
|
69 |
-
|
70 |
-
# Sidebar content
|
71 |
-
model = st.sidebar.selectbox(
|
72 |
-
"Choose the pretrained model",
|
73 |
-
["classifierdl_bert_sentiment"],
|
74 |
-
help="For more info about the models visit: https://sparknlp.org/models"
|
75 |
-
)
|
76 |
-
|
77 |
-
# Reference notebook link in sidebar
|
78 |
-
link = """
|
79 |
-
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/CLASSIFICATION_Fr_Sentiment.ipynb">
|
80 |
-
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
81 |
-
</a>
|
82 |
-
"""
|
83 |
-
st.sidebar.markdown('Reference notebook:')
|
84 |
-
st.sidebar.markdown(link, unsafe_allow_html=True)
|
85 |
-
|
86 |
-
# Load examples
|
87 |
-
examples = [
|
88 |
-
"Jeu et championnat pas assez excitants ? Devez-vous vérifier l'arbitre vidéo maintenant? Je suis horrifié, pensa-t-il, il ne devrait répondre que s'il a fait des erreurs flagrantes. Le football n'est plus amusant.",
|
89 |
-
"J'ai raté le podcast werder hier mercredi. À quelle vitesse vous vous habituez à quelque chose et vous l'attendez avec impatience. Merci à Plainsman pour les bonnes interviews et la perspicacité dans les coulisses du werderbremen. Passez de bonnes vacances d'hiver !",
|
90 |
-
"Les scènes s'enchaînent de manière saccadée, les dialogues sont théâtraux, le jeu des acteurs ne transcende pas franchement le film. Seule la musique de Vivaldi sauve le tout. Belle déception.",
|
91 |
-
"Je n'aime pas l'arbitre parce qu'il est empoisonné !",
|
92 |
-
"ManCity Guardiola et sa bande, vous êtes des connards. Je viens de perdre une fortune à cause de ta dette envers tes Bavarois là-bas."
|
93 |
-
]
|
94 |
-
|
95 |
-
selected_text = st.selectbox("Select a sample", examples)
|
96 |
-
custom_input = st.text_input("Try it for yourself!")
|
97 |
-
|
98 |
-
if custom_input:
|
99 |
-
selected_text = custom_input
|
100 |
-
elif selected_text:
|
101 |
-
selected_text = selected_text
|
102 |
-
|
103 |
-
st.subheader('Selected Text')
|
104 |
-
st.write(selected_text)
|
105 |
-
|
106 |
-
# Initialize Spark and create pipeline
|
107 |
-
spark = init_spark()
|
108 |
-
pipeline = create_pipeline(model)
|
109 |
-
output = fit_data(pipeline, selected_text)
|
110 |
-
|
111 |
-
# Display output sentence
|
112 |
-
if output.lower() in ['pos', 'positive']:
|
113 |
-
st.markdown("""<h3>This seems like a <span style="color: green">{}</span> text. <span style="font-size:35px;">😃</span></h3>""".format('positive'), unsafe_allow_html=True)
|
114 |
-
elif
|
115 |
-
st.markdown("""<h3>This seems like a <span style="color: red">{}</span> text. <span style="font-size:35px;">😠</span?</h3>""".format('negative'), unsafe_allow_html=True)
|
|
|
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 |
+
initial_sidebar_state="auto"
|
15 |
+
)
|
16 |
+
|
17 |
+
# CSS for styling
|
18 |
+
st.markdown("""
|
19 |
+
<style>
|
20 |
+
.main-title {
|
21 |
+
font-size: 36px;
|
22 |
+
color: #4A90E2;
|
23 |
+
font-weight: bold;
|
24 |
+
text-align: center;
|
25 |
+
}
|
26 |
+
.section p, .section ul {
|
27 |
+
color: #666666;
|
28 |
+
}
|
29 |
+
</style>
|
30 |
+
""", unsafe_allow_html=True)
|
31 |
+
|
32 |
+
@st.cache_resource
|
33 |
+
def init_spark():
|
34 |
+
return sparknlp.start()
|
35 |
+
|
36 |
+
@st.cache_resource
|
37 |
+
def create_pipeline(model):
|
38 |
+
document = DocumentAssembler()\
|
39 |
+
.setInputCol("text")\
|
40 |
+
.setOutputCol("document")
|
41 |
+
|
42 |
+
embeddings = BertSentenceEmbeddings\
|
43 |
+
.pretrained('labse', 'xx') \
|
44 |
+
.setInputCols(["document"])\
|
45 |
+
.setOutputCol("sentence_embeddings")
|
46 |
+
|
47 |
+
sentimentClassifier = ClassifierDLModel.pretrained("classifierdl_bert_sentiment", "fr") \
|
48 |
+
.setInputCols(["sentence_embeddings"]) \
|
49 |
+
.setOutputCol("class_")
|
50 |
+
|
51 |
+
nlpPipeline = Pipeline(
|
52 |
+
stages=[
|
53 |
+
document,
|
54 |
+
embeddings,
|
55 |
+
sentimentClassifier])
|
56 |
+
|
57 |
+
return nlpPipeline
|
58 |
+
|
59 |
+
def fit_data(pipeline, data):
|
60 |
+
empty_df = spark.createDataFrame([['']]).toDF('text')
|
61 |
+
pipeline_model = pipeline.fit(empty_df)
|
62 |
+
model = LightPipeline(pipeline_model)
|
63 |
+
results = model.fullAnnotate(data)[0]
|
64 |
+
|
65 |
+
return results['class_'][0].result
|
66 |
+
|
67 |
+
# Set up the page layout
|
68 |
+
st.markdown('<div class="main-title">State-of-the-Art French Sentiment Detection with Spark NLP</div>', unsafe_allow_html=True)
|
69 |
+
|
70 |
+
# Sidebar content
|
71 |
+
model = st.sidebar.selectbox(
|
72 |
+
"Choose the pretrained model",
|
73 |
+
["classifierdl_bert_sentiment"],
|
74 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
75 |
+
)
|
76 |
+
|
77 |
+
# Reference notebook link in sidebar
|
78 |
+
link = """
|
79 |
+
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/CLASSIFICATION_Fr_Sentiment.ipynb">
|
80 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
81 |
+
</a>
|
82 |
+
"""
|
83 |
+
st.sidebar.markdown('Reference notebook:')
|
84 |
+
st.sidebar.markdown(link, unsafe_allow_html=True)
|
85 |
+
|
86 |
+
# Load examples
|
87 |
+
examples = [
|
88 |
+
"Jeu et championnat pas assez excitants ? Devez-vous vérifier l'arbitre vidéo maintenant? Je suis horrifié, pensa-t-il, il ne devrait répondre que s'il a fait des erreurs flagrantes. Le football n'est plus amusant.",
|
89 |
+
"J'ai raté le podcast werder hier mercredi. À quelle vitesse vous vous habituez à quelque chose et vous l'attendez avec impatience. Merci à Plainsman pour les bonnes interviews et la perspicacité dans les coulisses du werderbremen. Passez de bonnes vacances d'hiver !",
|
90 |
+
"Les scènes s'enchaînent de manière saccadée, les dialogues sont théâtraux, le jeu des acteurs ne transcende pas franchement le film. Seule la musique de Vivaldi sauve le tout. Belle déception.",
|
91 |
+
"Je n'aime pas l'arbitre parce qu'il est empoisonné !",
|
92 |
+
"ManCity Guardiola et sa bande, vous êtes des connards. Je viens de perdre une fortune à cause de ta dette envers tes Bavarois là-bas."
|
93 |
+
]
|
94 |
+
|
95 |
+
selected_text = st.selectbox("Select a sample", examples)
|
96 |
+
custom_input = st.text_input("Try it for yourself!")
|
97 |
+
|
98 |
+
if custom_input:
|
99 |
+
selected_text = custom_input
|
100 |
+
elif selected_text:
|
101 |
+
selected_text = selected_text
|
102 |
+
|
103 |
+
st.subheader('Selected Text')
|
104 |
+
st.write(selected_text)
|
105 |
+
|
106 |
+
# Initialize Spark and create pipeline
|
107 |
+
spark = init_spark()
|
108 |
+
pipeline = create_pipeline(model)
|
109 |
+
output = fit_data(pipeline, selected_text)
|
110 |
+
|
111 |
+
# Display output sentence
|
112 |
+
if output.lower() in ['pos', 'positive']:
|
113 |
+
st.markdown("""<h3>This seems like a <span style="color: green">{}</span> text. <span style="font-size:35px;">😃</span></h3>""".format('positive'), unsafe_allow_html=True)
|
114 |
+
elif output.lower() in ['neg', 'negative']:
|
115 |
+
st.markdown("""<h3>This seems like a <span style="color: red">{}</span> text. <span style="font-size:35px;">😠</span?</h3>""".format('negative'), unsafe_allow_html=True)
|