Kurkur99's picture
Update app.py
49ab3c7
raw
history blame
2.17 kB
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from sentiment_labeling import add_sentiment_column
from keras.models import load_model
import pickle
# Load the model and tokenizer
model = load_model('model.h5')
with open('tokenizer.pkl', 'rb') as f:
tokenizer = pickle.load(f)
def predict_sentiment(text):
# Tokenize and pad the input text
seq = tokenizer.texts_to_sequences([text])
padded_seq = pad_sequences(seq, maxlen=MAX_LENGTH)
# Predict using the model
prediction = model.predict(padded_seq)
return np.argmax(prediction)
# Streamlit app
st.title("Thread Review Sentiment Analysis")
# Upload CSV file
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if uploaded_file:
data = pd.read_csv(uploaded_file)
st.write("Data Loaded Successfully!")
# Display raw data
if st.checkbox("Show raw data"):
st.write(data)
# Add sentiment column
data = add_sentiment_column(data)
# Distribution of sentiments
st.subheader("Distribution of Sentiments")
sentiment_counts = data['sentiment'].value_counts()
fig, ax = plt.subplots()
sentiment_counts.plot(kind='bar', ax=ax)
ax.set_title('Distribution of Sentiments')
ax.set_xlabel('Sentiment')
ax.set_ylabel('Count')
st.pyplot(fig)
# Word cloud for each sentiment
st.subheader("Word Clouds for Sentiments")
sentiments = ['positive', 'neutral', 'negative']
for sentiment in sentiments:
st.write(f"Word Cloud for {sentiment}")
subset = data[data['sentiment'] == sentiment]
text = " ".join(review for review in subset['review'])
wordcloud = WordCloud(max_words=100, background_color="white").generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
st.pyplot()
# Individual review prediction
user_input = st.text_area("Type a review here to predict its sentiment:")
if user_input:
sentiment_pred = predict_sentiment(user_input)
st.write(f"The predicted sentiment is: {sentiment_pred}")