abdullahmubeen10 commited on
Commit
3ad496b
·
verified ·
1 Parent(s): c80da3a

Upload 18 files

Browse files
.streamlit/config.toml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [theme]
2
+ base="light"
3
+ primaryColor="#29B4E8"
Demo.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_assembler = DocumentAssembler()\
39
+ .setInputCol("text")\
40
+ .setOutputCol("document")
41
+
42
+ sentence_detector = SentenceDetector() \
43
+ .setInputCols(["document"]) \
44
+ .setOutputCol("sentence")
45
+
46
+ tokenizer = Tokenizer() \
47
+ .setInputCols(["sentence"]) \
48
+ .setOutputCol("token")
49
+
50
+ word_embeddings = WordEmbeddingsModel()\
51
+ .pretrained('urduvec_140M_300d', 'ur')\
52
+ .setInputCols(["sentence",'token'])\
53
+ .setOutputCol("word_embeddings")
54
+
55
+ sentence_embeddings = SentenceEmbeddings() \
56
+ .setInputCols(["sentence", "word_embeddings"]) \
57
+ .setOutputCol("sentence_embeddings") \
58
+ .setPoolingStrategy("AVERAGE")
59
+
60
+ classifier = SentimentDLModel.pretrained('sentimentdl_urduvec_imdb', 'ur' )\
61
+ .setInputCols(['sentence_embeddings'])\
62
+ .setOutputCol('sentiment')
63
+
64
+ nlpPipeline = Pipeline(
65
+ stages=[
66
+ document_assembler,
67
+ sentence_detector,
68
+ tokenizer,
69
+ word_embeddings,
70
+ sentence_embeddings,
71
+ classifier ])
72
+
73
+ return nlpPipeline
74
+
75
+ def fit_data(pipeline, data):
76
+ empty_df = spark.createDataFrame([['']]).toDF('text')
77
+ pipeline_model = pipeline.fit(empty_df)
78
+ model = LightPipeline(pipeline_model)
79
+ results = model.fullAnnotate(data)[0]
80
+
81
+ return results['sentiment'][0].result
82
+
83
+ # Set up the page layout
84
+ st.markdown('<div class="main-title">State-of-the-Art Urdu Sentiment Detection with Spark NLP</div>', unsafe_allow_html=True)
85
+
86
+ # Sidebar content
87
+ model = st.sidebar.selectbox(
88
+ "Choose the pretrained model",
89
+ ["sentimentdl_urduvec_imdb"],
90
+ help="For more info about the models visit: https://sparknlp.org/models"
91
+ )
92
+
93
+ # Reference notebook link in sidebar
94
+ link = """
95
+ <a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/public/SENTIMENT_UR.ipynb">
96
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
97
+ </a>
98
+ """
99
+ st.sidebar.markdown('Reference notebook:')
100
+ st.sidebar.markdown(link, unsafe_allow_html=True)
101
+
102
+ # Load examples
103
+ folder_path = f"inputs/{model}"
104
+ examples = [
105
+ lines[1].strip()
106
+ for filename in os.listdir(folder_path)
107
+ if filename.endswith('.txt')
108
+ for lines in [open(os.path.join(folder_path, filename), 'r', encoding='utf-8').readlines()]
109
+ if len(lines) >= 2
110
+ ]
111
+
112
+ selected_text = st.selectbox("Select a sample", examples)
113
+ custom_input = st.text_input("Try it for yourself!")
114
+
115
+ if custom_input:
116
+ selected_text = custom_input
117
+ elif selected_text:
118
+ selected_text = selected_text
119
+
120
+ st.subheader('Selected Text')
121
+ st.write(selected_text)
122
+
123
+ # Initialize Spark and create pipeline
124
+ spark = init_spark()
125
+ pipeline = create_pipeline(model)
126
+ output = fit_data(pipeline, selected_text)
127
+
128
+ # Display output sentence
129
+ if output.lower() in ['pos', 'positive']:
130
+ st.markdown("""<h3>This seems like a <span style="color: green">{}</span> text. <span style="font-size:35px;">&#128515;</span></h3>""".format('positive'), unsafe_allow_html=True)
131
+ elif output.lower() in ['neg', 'negative']:
132
+ st.markdown("""<h3>This seems like a <span style="color: red">{}</span> text. <span style="font-size:35px;">&#128544;</span?</h3>""".format('negative'), unsafe_allow_html=True)
Dockerfile ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Download base image ubuntu 18.04
2
+ FROM ubuntu:18.04
3
+
4
+ # Set environment variables
5
+ ENV NB_USER jovyan
6
+ ENV NB_UID 1000
7
+ ENV HOME /home/${NB_USER}
8
+
9
+ # Install required packages
10
+ RUN apt-get update && apt-get install -y \
11
+ tar \
12
+ wget \
13
+ bash \
14
+ rsync \
15
+ gcc \
16
+ libfreetype6-dev \
17
+ libhdf5-serial-dev \
18
+ libpng-dev \
19
+ libzmq3-dev \
20
+ python3 \
21
+ python3-dev \
22
+ python3-pip \
23
+ unzip \
24
+ pkg-config \
25
+ software-properties-common \
26
+ graphviz \
27
+ openjdk-8-jdk \
28
+ ant \
29
+ ca-certificates-java \
30
+ && apt-get clean \
31
+ && update-ca-certificates -f;
32
+
33
+ # Install Python 3.8 and pip
34
+ RUN add-apt-repository ppa:deadsnakes/ppa \
35
+ && apt-get update \
36
+ && apt-get install -y python3.8 python3-pip \
37
+ && apt-get clean;
38
+
39
+ # Set up JAVA_HOME
40
+ ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
41
+ RUN mkdir -p ${HOME} \
42
+ && echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> ${HOME}/.bashrc \
43
+ && chown -R ${NB_UID}:${NB_UID} ${HOME}
44
+
45
+ # Create a new user named "jovyan" with user ID 1000
46
+ RUN useradd -m -u ${NB_UID} ${NB_USER}
47
+
48
+ # Switch to the "jovyan" user
49
+ USER ${NB_USER}
50
+
51
+ # Set home and path variables for the user
52
+ ENV HOME=/home/${NB_USER} \
53
+ PATH=/home/${NB_USER}/.local/bin:$PATH
54
+
55
+ # Set the working directory to the user's home directory
56
+ WORKDIR ${HOME}
57
+
58
+ # Upgrade pip and install Python dependencies
59
+ RUN python3.8 -m pip install --upgrade pip
60
+ COPY requirements.txt /tmp/requirements.txt
61
+ RUN python3.8 -m pip install -r /tmp/requirements.txt
62
+
63
+ # Copy the application code into the container at /home/jovyan
64
+ COPY --chown=${NB_USER}:${NB_USER} . ${HOME}
65
+
66
+ # Expose port for Streamlit
67
+ EXPOSE 7860
68
+
69
+ # Define the entry point for the container
70
+ ENTRYPOINT ["streamlit", "run", "Demo.py", "--server.port=7860", "--server.address=0.0.0.0"]
images/Sentiment-Analysis.jpg ADDED
images/dataset.png ADDED
images/johnsnowlabs-sentiment-output.png ADDED
inputs/sentimentdl_urduvec_imdb/Example1.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 1
2
+ یہ کردار زندہ دل اور دلچسپ تھے ، پلاٹ بہترین طور پر چل رہا تھا ، پائرو اثرات کو مہارت کے ساتھ انجام دیا گیا تھا ، اور اس میں ایک بنیادی محبت کی مثلث کی کہانی لی گئی ہے اور اس میں سائنس فکشن عنصر کو پھینک دیا گیا ہے۔ میں بہت سارے کرداروں کے ساتھ ان کی نشاندہی کرسکتا تھا اور ان کے محرکات نے کہانی کے فریم ورک میں منطقی عقلی احساس پیدا کیا تھا۔ کیمرا کام بہت اچھا تھا ، آڈیو واضح اور درست تھا ، بیک گراؤنڈ میوزک کو اثر انداز کے لئے منتخب کیا گیا تھا ، گانے والے فائر مین ایک اچھے باصلاحیت یادگار تھے۔ عجیب کیفیت ، سیٹوں نے بہت عمدہ تیار کیا ، اور ہنر مند ہنر کے ساتھ انجام دیئے گئے خاص اثرات۔ میں حیرت زدہ ہوں کہ کس طرح چین اسٹور کی پارکنگ میں پوری منی کارنیوال کو ایک ہی لیمپپوسٹ آؤٹ لیٹ کے ذریعہ تقویت مل سکتی ہے۔ کم سے کم کہنا ناممکن لگتا ہے۔ فلم کے اختتام کے قریب بھائیوں کے مابین لڑائی شاندار تھی۔ جم ورنی کو غیر جوکر والے کردار میں رکھنا بھی ایک حیرت انگیز ٹچ تھا کیونکہ اس نے ایک کارنی کا نیم سنجیدہ کردار بہت عمدہ ادا کیا تھا۔
3
+
inputs/sentimentdl_urduvec_imdb/Example10.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 10
2
+ میں اسٹورٹ بلیس کو اب تک کی بدترین فلم سمجھتا ہوں۔ اداکاری خوفناک تھی اور سازش مضحکہ خیز تھی۔ مجھے یہ حقیقت ملتی ہے کہ مرکزی کردار کی بیوی نے اسے چھوڑ کر ذہنی خرابی پھیلادی ، لیکن یہ اتنے بے وقوف اور غضبناک ہوگیا ، تھوڑی دیر کے بعد میں کسی بھی کردار کے بارے میں کم خیال رکھ سکتی تھی۔ فلم ایک ہی خیال کے بغیر کسی چیز کے آگے بڑھتی چلی جاتی ہے۔ پلاٹ میں شامل کرنے کے لئے تازہ یا حیرت انگیز. جیگر کاؤنٹر کے ساتھ پوری چیز بہت زیادہ ہوگئی جب اسٹورٹ نے اپنی دیوار کھولنا شروع کیا تو یہ دیکھنے کے لئے کہ کاؤنٹر کے پاس کچھ ہے اس کے اشارے کے بعد اس کے پیچھے کیا ہے۔ پھر اس طیارے کے ساتھ بار بار مناظر دیکھنے کو ملے ، اور وہ پریشان کن خود سے ملتا ہے۔ مجھے اندازہ کرنا چاہئے تھا کہ جب میں کسی اداکار کو نہیں پہچانتی ہوں تو یہ فلم فلاپ تھی۔ جب یہ فلم آتی ہے تو اپنے آپ پر احسان کریں ، کتاب پڑھیں! آپ بہتر ہوں گے۔
3
+
inputs/sentimentdl_urduvec_imdb/Example2.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 2
2
+ چونکہ میں نے 80 کی دہائی میں انسپکٹر گیجٹ کارٹون کو پسند کیا تھا ، لہذا میں اس فلم کو دیکھنے گیا۔ میں نے اپنا پیسہ ضائع کیا۔ پلاٹ بہت پتلا تھا۔ نیز ، فلم مجھے زیادہ دن دلچسپی نہیں بنا سکی۔ مجھے خوشی ہوئی کہ یہ ختم ہوچکا ہے۔ اگر آپ انسپکٹر گیجٹ دیکھنا چاہتے ہیں تو ، اس کے بجائے کارٹون دیکھیں۔ یہ فلم سے کہیں بہتر تھا۔
3
+
inputs/sentimentdl_urduvec_imdb/Example3.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 3
2
+ جان لیگوزامو ایک بہترین مزاح نگار اور کہانی سنانے والا ہے۔ ہر بار جب یہ HBO پر ہوتا ہے تو مجھے اسے روکنا پڑتا ہے۔ جان یہ کہانی سناتا ہے کہ وہ کیسے بڑا ہوا (شاید کچھ حقیقت اور افسانہ) اور درمیان میں مزاحیہ کہانیاں شامل کرتا ہے۔ اگر آپ جان کی کامیڈی پسند کرتے ہیں تو مجھے کہنا پڑے گا یہ ان کی بہترین کامیڈی ہے۔
3
+
inputs/sentimentdl_urduvec_imdb/Example4.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 4
2
+ یہ بہت سی بی مائنس فلموں میں سے ایک اور ہے جسے فلمی شور کے طور پر ٹیگ کیا گیا ہے تاکہ کسی ایسی چیز میں دلچسپی پیدا کی جاسکے جو اس سے عاری ہو۔ فلم کے تمام پہلوؤں - اسکرپٹ ، اداکاری ، ہدایت نامہ معمولی ہیں۔ تینوں لیڈز کے ذریعہ اداکاری لکڑی کی ہے۔ میرا اندازہ ہے کہ جان ڈال سے توقع کی جا رہی تھی کہ وہ فلمی کاروبار میں جگہ لے جائیں گے لیکن پھر کسی کو احساس ہوا کہ اس میں بہت کم صلاحیت ہے اور اسی وجہ سے وہ ٹی وی کا کام کرنا چھوڑ گیا۔ لی جے کوب جو عام طور پر لاجواب ہوتا ہے وہ خراب اسکرپٹ اور خراب سمت سے اوپر نہیں بڑھ سکتا۔ سمجھا جاتا ہے کہ جین وایاٹ ایک غیر معمولی فیتال ہے لیکن دیکھنے والوں کو راضی کرنے کے قریب کہیں نہیں آتا ہے۔ فلم میں حیرت انگیز نظر آنے والی دو کاریں ہیں جو میں نے کبھی نہیں دیکھی ہیں ، اس میں جان ڈال لی جے کوب کے بعد چل رہی ہے خاص طور پر حیرت انگیز ہے۔ ڈی وی ڈی کی منتقلی عام الفا ہے۔
3
+
inputs/sentimentdl_urduvec_imdb/Example5.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 5
2
+ اس فلم کی تشہیر مزاح کے طور پر کی گئی تھی لیکن اس سے کہیں زیادہ سنجیدہ تھی جیسے ٹریلرز نے اسے تیار کیا تھا۔ مجھے غلط مت سمجھو ، میں نے فلم کا لطف اٹھایا ، لیکن زیادہ ہنسنے کی امید کر رہا تھا۔ رابن ولیمز اور لورا لننی کی عمدہ پرفارمنس۔ دیکھنے کے قابل ہے ، لیکن فرش پر رولنگ ہونے کی توقع نہ کریں۔ فلم نے مجھے یہ سوچ کر چھوڑ دیا کہ یہ کیا ہوگا اگر رابن ولیمز کا کردار ایک حقیقی شخص ہوتا جو صدر کے لئے انتخاب لڑ رہا تھا۔ کیا ہم ایک مزاح نگار کو منتخب کریں گے؟ مجھے شک ہے ، بدقسمتی سے۔ آج کل اس طرح کی پوری ایمانداری سے بہت زیادہ کمی ہے۔ یہ ایک ایسی فلم ہے جسے میں DVD پر آتے ہی اپنی ڈی وی ڈی لائبریری میں شامل کردوں گا۔ فلم میں دل ہے۔
3
+
inputs/sentimentdl_urduvec_imdb/Example6.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 6
2
+ اتنا اچھا عمل نہیں سنسنی خیز فلم ہے کیونکہ یہ اسی پانی کو ابتدائی اسٹیون سیگل فلموں کی طرح ناکام بنا دیتا ہے کیونکہ وہاں بہت اچھا سیٹ نہیں ہوتا ہے۔ اسٹیون سیگل اسی طرح کے کردار نبھا رہے ہیں جو انہوں نے قانون کے اوپر سے ادا کیا ہے۔ میری رائے میں کیینن آئیوری وینس کی پرفارمنس ایسی اوسط فلم میں ضائع ہوتی ہے اور اس کا تعلق زیادہ بہتر فلم میں ہے۔ باب گونٹن بطور خاص بھاری ہے۔ پوری فلم میں بہترین اداکاری برائن کاکس کی ہے جو قاتل کے کردار میں بہت ڈراؤنا ہے۔ میرے پسندیدہ مناظر روسی مافیا کے ساتھ لڑائی کے مناظر ہیں۔ گلیمر مین (1996) کو دیکھنے کی فلم کی ایک وجہ خوبصورت اور والپٹ اپس نکی کاکس کے مختصر نمائش کے لئے ہے۔ یہ بہت خراب ہے کہ ان کے ساتھ اس کے ساتھ زیادہ مناظر نہیں تھے۔
3
+
inputs/sentimentdl_urduvec_imdb/Example7.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 7
2
+ نیکسٹ ایکشن اسٹار ریلیٹی ٹی وی سیریز دیکھنے کے بعد ، فاتحین کی فلم دیکھ کر مجھے بہت خوشی ہوئی۔ میں اس طرح کی نئی صلاحیتوں کی نمائش کرنے کا کام کررہا تھا ، لیکن مجھے خوشگوار حیرت اور حیرت ہوئی۔ بلی زین ، یقینا، ، اس کا معمول کا بڑا نفس تھا ، لیکن کورین اور شان نے اپنے ساتھ اپنے پاس رکھا۔ جریڈ اور جین (مقابلہ سے بھی) کو اپنے کرداروں میں دیکھ کر اچھا لگا۔ شان کا کردار ، بلی کا نہیں ، بلکہ شکار کیا گیا ہے ، اور اس کھیل میں نئے قواعد دریافت کرنے پر اس کی مایوسی اچھی طرح سے ادا کی گئی ہے۔ کورین اپنے کردار کی طرح شان کو پسند کرتی ہے اور صرف اس میں پیسوں کے ل being رہ رہی ہے۔ مجھے اچھا لگا کہ کھیل آخری سیکنڈ تک کس طرح کھیلا گیا۔ اور پھر اس سے آگے! ایک زبردست مووی نہیں ، بلکہ پوری طرح سے ایک دل لگی اور گیٹ سے باہر پہلی بار دو لوگوں کے لئے ایک عمدہ شوکیس۔
3
+
inputs/sentimentdl_urduvec_imdb/Example8.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 8
2
+ یہ مایوس کن فلم تھی۔ ایسا لگتا ہے کہ لوگوں کے پاس کوئی مادہ نہیں ہے ، مرکزی کردارنما مارٹن کاہل صفر سے چھٹکارا پانے والی اقدار کے حامل ہیں ، در حقیقت ، جون واوائٹ سمیت اس میں موجود ہر شخص آستین کی علامت ہے۔ میں کسی کو بھی اس فلم کی سفارش نہیں کروں گا۔ یہ تشدد قابل نفرت ہے ، حالانکہ یہ فن پارے سے کیا گیا ہے۔ فلم بندی کا رنگ سیاہ ہونا ہے ، کم از کم میں نے اس زمرے کے مطابق جو پرنٹ دیکھا تھا۔ ایک مایوسی۔
3
+
inputs/sentimentdl_urduvec_imdb/Example9.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Example 9
2
+ جب میں نے اس فلم کو کرائے پر لیا تھا تو میں پوری طرح کی توقع نہیں کر رہا تھا ، کیونکہ ان دنوں بہت سی آزاد فلمیں تھوڑی بہت زیادہ نظر آتی ہیں (ٹھیک ہے ، اس معاملے کے لئے ہالی ووڈ کی فلمیں بھی) لیکن یہ فلم لاجواب ، واقعی بہت اچھی تھی ، یہ بہت خراب ہے یہ ایک بہت بڑا سامعین تک نہیں پہنچا کیونکہ یہ صرف بہت عمدہ ہے۔ میں واقعی میں ایلس کے عزم کو پسند کرتا ہوں ، یہ واقعی مجھے اپنی زندگی کو ایک تحفہ کے طور پر دیکھنے پر مجبور کرتا ہے ، اور میں دیکھتا ہوں کہ مجھے صرف ایک تعلیم حاصل کرنے کا موقع ملا ہے۔ لیکن ان سب کو چھوڑ کر ، یہ فلم واقعی یہ ثابت کرتی ہے کہ اچھے فنکار اچھی کہانی سنانے کا اعزاز دے سکتے ہیں ، بجٹ کچھ بھی نہیں ہو ، یہ ایک بہترین فلم ہے اور ہر ایک کو اسے دیکھنا چاہئے ، وہ اسے پسند کریں گے اور یقینا اس سے کچھ سیکھیں گے۔ مجھے یہ جاننے کے لئے راجر ایبرٹ ہونے کی ضرورت نہیں ہے کہ میں نے ایک بہترین فلموں میں سے ایک دیکھا ہے جو میں نے سارا سال دیکھا ہے ، اور یقینا. سب سے زیادہ سچائی میں سے ایک ہے۔
3
+
pages/Workflow & Model Overview.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
225
+
226
+ st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
227
+ st.markdown("""
228
+ <div class="section">
229
+ <ul>
230
+ <li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
231
+ <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>
232
+ <li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li>
233
+ <li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li>
234
+ <li><a class="link" href="https://www.youtube.com/channel/UCmFOjlpYEhxf_wJUDuz6xxQ/videos" target="_blank">YouTube</a>: Video tutorials</li>
235
+ </ul>
236
+ </div>
237
+ """, unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ numpy
4
+ spark-nlp
5
+ pyspark