santit96 commited on
Commit
93109de
β€’
0 Parent(s):

Initialize project and create a first sentiment classifier prototype with streamlit and a pretrained bert model

Browse files
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ __pycache__
2
+ *.csv
3
+ .DS_Store
4
+ *.h5
README.md ADDED
@@ -0,0 +1 @@
 
 
1
+ # bert-sentiment-analysis
data/.gitkeep ADDED
File without changes
models/__init__.py ADDED
File without changes
models/models.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Module to load the project models
3
+ """
4
+ import os
5
+ import tensorflow_text
6
+ import tensorflow as tf
7
+ import tensorflow_hub as hub
8
+
9
+
10
+ CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
11
+
12
+
13
+ def load_sentiments_model():
14
+ """
15
+ Load pretrained model
16
+ """
17
+ model_path = os.path.join(CURRENT_DIR, "sentiments_bert_model.h5")
18
+ model = tf.keras.models.load_model(
19
+ model_path, custom_objects={"KerasLayer": hub.KerasLayer}, compile=False
20
+ )
21
+ return model
notebooks/classify_sentiment_with_bert.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ pandas
2
+ jupyter
3
+ numpy
4
+ tensorflow
5
+ tensorflow-text
sentiment_analysis.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Sentiment analysis streamlit webpage
3
+ """
4
+ import streamlit as st
5
+ from sentiment_classificator import classify_sentiment
6
+
7
+
8
+ def get_representative_emoji(sentiment: str) -> str:
9
+ """
10
+ From a sentiment return the representative emoji
11
+ """
12
+ if sentiment == 'positive':
13
+ return "πŸ˜ƒ"
14
+ elif sentiment == 'negative':
15
+ return "😞"
16
+ else:
17
+ return "😐"
18
+
19
+
20
+ def main() -> None:
21
+ """
22
+ Build streamlit page for sentiment analysis
23
+ """
24
+ st.title("Sentiment Classification")
25
+
26
+ # Initialize session state variables
27
+ if 'enter_pressed' not in st.session_state:
28
+ st.session_state.enter_pressed = False
29
+
30
+ # Input text box and button
31
+ input_text = st.text_input("Enter your text here:")
32
+ button_clicked = st.button("Classify Sentiment")
33
+
34
+ if button_clicked or st.session_state.enter_pressed:
35
+ # Process the input text with the sentiment classifier
36
+ sentiment = classify_sentiment(input_text)
37
+
38
+ # Get the representative emoji
39
+ emoji = get_representative_emoji(sentiment)
40
+
41
+ # Show the response and emoji
42
+ st.write(f"Sentiment: {sentiment.capitalize()} {emoji}")
43
+
44
+
45
+ if __name__ == "__main__":
46
+ main()
sentiment_classificator.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Module to classify text into positive or negative sentiments
3
+ """
4
+ import sys
5
+ import tensorflow as tf
6
+ from models.models import load_sentiments_model
7
+
8
+ sentiments_model = load_sentiments_model()
9
+ MAX_NEG = 0.4
10
+ MIN_POS = 0.6
11
+
12
+
13
+ def classify_sentiment(input_text: str) -> str:
14
+ """
15
+ Receives a string and classifies it in positive, negative or none
16
+ """
17
+ result = tf.sigmoid(sentiments_model(tf.constant([input_text])))
18
+ if result < MAX_NEG:
19
+ return "negative"
20
+ elif result > MIN_POS:
21
+ return "positive"
22
+ else:
23
+ return "-"
24
+
25
+
26
+ if __name__ == "__main__":
27
+ if len(sys.argv) < 2:
28
+ print(
29
+ f"Usage: python {sys.argv[0]} <text to classify>")
30
+ sys.exit(1)
31
+ # Get the input string from command line argument
32
+ input_text = sys.argv[1]
33
+ sentiment = classify_sentiment(input_text)
34
+ print("Sentiment of the sentence: ", sentiment)