Soundaryasos's picture
Update app.py
9aa9261 verified
raw
history blame
2.76 kB
import os
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
from transformers import pipeline
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from prophet import Prophet
# Load sentiment analysis models
vader = SentimentIntensityAnalyzer()
bert_sentiment = pipeline("sentiment-analysis")
# Streamlit UI setup
st.title("Sentiment Analysis & Prediction")
# User input
user_input = st.text_area("Enter text for sentiment analysis")
def analyze_sentiment(text):
"""Perform sentiment analysis using VADER and BERT."""
vader_score = vader.polarity_scores(text)['compound']
bert_result = bert_sentiment(text)[0]
return vader_score, bert_result['label'], bert_result['score']
# Process input
if user_input:
vader_score, bert_label, bert_confidence = analyze_sentiment(user_input)
# Display results
st.subheader("Sentiment Analysis Results")
st.write(f"**VADER Sentiment Score:** {vader_score}")
st.write(f"**BERT Sentiment:** {bert_label} ({bert_confidence:.2f})")
# Word Cloud
st.subheader("Word Cloud of Input Text")
wordcloud = WordCloud(width=600, height=400, background_color='white').generate(user_input)
fig, ax = plt.subplots()
ax.imshow(wordcloud, interpolation='bilinear')
ax.axis("off")
st.pyplot(fig)
# Time Series Data for Prediction
days = 30
date_range = pd.date_range(start=pd.Timestamp.today(), periods=days, freq='D')
sentiment_scores = np.cumsum(np.random.randn(days) * 0.1 + vader_score)
df = pd.DataFrame({'ds': date_range, 'y': sentiment_scores})
# Facebook Prophet Prediction
model = Prophet()
model.fit(df)
future = model.make_future_dataframe(periods=7)
forecast = model.predict(future)
# Plot predictions
st.subheader("Sentiment Prediction for Next 7 Days")
fig, ax = plt.subplots()
ax.plot(forecast['ds'], forecast['yhat'], label='Predicted Sentiment', color='blue')
ax.fill_between(forecast['ds'], forecast['yhat_lower'], forecast['yhat_upper'], alpha=0.2, color='blue')
ax.axhline(0, color='black', linestyle='dashed')
ax.set_title("Sentiment Trend Prediction")
ax.set_xlabel("Date")
ax.set_ylabel("Sentiment Score")
ax.legend()
st.pyplot(fig)
# Explanation
st.subheader("What These Results Mean")
st.write("- **VADER Score:** Measures sentiment from -1 (negative) to +1 (positive)")
st.write("- **BERT Sentiment:** Deep learning-based classification")
st.write("- **Prediction Graph:** Expected sentiment trend for the next 7 days")
st.write("\n---\nBuilt with Streamlit, VADER, BERT, and Facebook Prophet 🚀")