Upload 4 files
Browse files- app.py +79 -0
- one_hot_info_1.pkl +3 -0
- requirements.txt +0 -0
- sentiment_analysis_model.h5 +3 -0
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
5 |
+
from tensorflow.keras.preprocessing.text import one_hot
|
6 |
+
import pickle
|
7 |
+
import emoji
|
8 |
+
|
9 |
+
# Streamlit app title
|
10 |
+
st.title('Unveiling Sentiment A Deep Dive into Sentiment Analysis :koala:')
|
11 |
+
|
12 |
+
# Function to load model and predict sentiment
|
13 |
+
def predict_sentiment(custom_data):
|
14 |
+
try:
|
15 |
+
# Load the trained model
|
16 |
+
model = load_model('sentiment_analysis_model.h5')
|
17 |
+
|
18 |
+
# Load the one-hot encoding information
|
19 |
+
with open('one_hot_info_1.pkl', 'rb') as handle:
|
20 |
+
one_hot_info = pickle.load(handle)
|
21 |
+
|
22 |
+
vocab_size = one_hot_info['vocab_size']
|
23 |
+
max_len = one_hot_info['max_len']
|
24 |
+
|
25 |
+
# Define labels with emojis
|
26 |
+
labels_with_emojis = {
|
27 |
+
'Positive': 'π',
|
28 |
+
'Neutral': 'π',
|
29 |
+
'Negative': 'π'
|
30 |
+
}
|
31 |
+
|
32 |
+
# One-hot encode each tweet
|
33 |
+
one_hot_texts = [one_hot(text, vocab_size) for text in custom_data]
|
34 |
+
|
35 |
+
# Pad the sequences
|
36 |
+
padded_texts = pad_sequences(one_hot_texts, padding='pre', maxlen=max_len)
|
37 |
+
|
38 |
+
# Predict the sentiments for all tweets
|
39 |
+
predictions = model.predict(np.array(padded_texts))
|
40 |
+
|
41 |
+
# Convert predictions to class labels and probabilities
|
42 |
+
predicted_sentiments = []
|
43 |
+
for prediction in predictions:
|
44 |
+
sentiment = np.argmax(prediction)
|
45 |
+
sentiment_label = list(labels_with_emojis.keys())[sentiment]
|
46 |
+
sentiment_emoji = labels_with_emojis[sentiment_label]
|
47 |
+
sentiment_probabilities = {label: round(prob, 4) for label, prob in zip(labels_with_emojis.keys(), prediction)}
|
48 |
+
predicted_sentiments.append((sentiment_label, sentiment_emoji, sentiment_probabilities))
|
49 |
+
|
50 |
+
return predicted_sentiments
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
st.error(f"Error during prediction: {e}")
|
54 |
+
return None
|
55 |
+
|
56 |
+
# Streamlit UI
|
57 |
+
user_input = st.text_area("Please enter the tweet you'd like analyzed::whale:")
|
58 |
+
|
59 |
+
if st.button('Analyze'):
|
60 |
+
if user_input.strip(): # Check if input is not empty
|
61 |
+
# Remove emojis and replace with their description
|
62 |
+
user_input = emoji.demojize(user_input)
|
63 |
+
|
64 |
+
# Split input by newlines to handle multiple tweets
|
65 |
+
tweets = user_input.split('\n')
|
66 |
+
|
67 |
+
# Predict sentiment for custom data
|
68 |
+
predicted_sentiments = predict_sentiment(tweets)
|
69 |
+
|
70 |
+
if predicted_sentiments is not None:
|
71 |
+
# Display results for each tweet
|
72 |
+
st.write("## Predicted Sentiments:")
|
73 |
+
for i, (sentiment_label, sentiment_emoji, sentiment_probabilities) in enumerate(predicted_sentiments):
|
74 |
+
st.write(f"Tweet {i+1}: {sentiment_label} {sentiment_emoji}")
|
75 |
+
st.write("Probabilities:")
|
76 |
+
for label, prob in sentiment_probabilities.items():
|
77 |
+
st.write(f"{label}: {prob:.4f}")
|
78 |
+
else:
|
79 |
+
st.write("Please enter tweet(s) to analyze.")
|
one_hot_info_1.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1d34f42c0fb38af36bddc4c84d3c8eba4adebca82c91ab1f813fd0d351eacc20
|
3 |
+
size 44
|
requirements.txt
ADDED
Binary file (1.76 kB). View file
|
|
sentiment_analysis_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:57343b58f31bacf0b3e699ad590c3dff23e6cf50622d01a8582e282da223939b
|
3 |
+
size 55246272
|