Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,52 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import numpy as np
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
with open('
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
import pickle
|
| 5 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 6 |
+
|
| 7 |
+
# Load the model and tokenizer
|
| 8 |
+
model = load_model('sentiment_model.h5')
|
| 9 |
+
with open('tokenizer.pkl', 'rb') as file:
|
| 10 |
+
tokenizer = pickle.load(file)
|
| 11 |
+
with open('label_map.pkl', 'rb') as file:
|
| 12 |
+
label_map = pickle.load(file)
|
| 13 |
+
|
| 14 |
+
def preprocess_text(text, tokenizer, max_len):
|
| 15 |
+
sequence = tokenizer.texts_to_sequences([text])
|
| 16 |
+
padded_sequence = pad_sequences(sequence, maxlen=max_len)
|
| 17 |
+
return padded_sequence
|
| 18 |
+
|
| 19 |
+
def predict_sentiment(text, model, tokenizer, max_len, label_map):
|
| 20 |
+
processed_text = preprocess_text(text, tokenizer, max_len)
|
| 21 |
+
prediction = model.predict(processed_text)
|
| 22 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
| 23 |
+
predicted_label = label_map[predicted_class]
|
| 24 |
+
return predicted_label
|
| 25 |
+
|
| 26 |
+
# Streamlit app
|
| 27 |
+
def main():
|
| 28 |
+
st.title("Sentiment Analysis")
|
| 29 |
+
st.write("Enter a text to predict its sentiment.")
|
| 30 |
+
|
| 31 |
+
# Input text from user
|
| 32 |
+
input_text = st.text_area("Input Text", "Type your text here...")
|
| 33 |
+
|
| 34 |
+
if st.button("Predict Sentiment"):
|
| 35 |
+
if input_text:
|
| 36 |
+
max_len = 100 # Set this to the max length used during training
|
| 37 |
+
sentiment = predict_sentiment(input_text, model, tokenizer, max_len, label_map)
|
| 38 |
+
st.write(f"The predicted sentiment for the text is: **{sentiment}**")
|
| 39 |
+
else:
|
| 40 |
+
st.write("Please enter some text to analyze.")
|
| 41 |
+
|
| 42 |
+
st.header("Sample Texts")
|
| 43 |
+
st.write("<span style='color:green; font-weight:bold'>Positive:</span> Going to finish up Borderlands 2 today.", unsafe_allow_html=True)
|
| 44 |
+
st.write("<span style='color:yellow; font-weight:bold'>Neutral:</span> Check out this epic streamer", unsafe_allow_html=True)
|
| 45 |
+
st.write("<span style='color:red; font-weight:bold'>Negative:</span> The biggest disappointment of my life came a year ago.", unsafe_allow_html=True)
|
| 46 |
+
st.write("<span style='color:cyan; font-weight:bold'>Irrelevant:</span> Stupid 19-year-olds who write bad poetry need to get away from the computer and talk to real people who don't believe in vampires.", unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
main()
|