Spaces:
Runtime error
Runtime error
Update app.py
Browse files4 unmodified lines56789101112131415161718192021222324252627282930313233343536373839402 unmodified lines4344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811824 unmodified linesimport pandas as pdfrom datetime import datetime, timedeltaimport plotly.express as pxfrom sklearn.linear_model import LinearRegressionfrom wordcloud import WordCloudimport base64from io import BytesIOimport nltkfrom textblob import TextBlob# Download NLTK datanltk.download('punkt')# Initialize sentiment modelsbert_sentiment = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")vader_analyzer = SentimentIntensityAnalyzer()# Generate sample past sentiment data (kept from original for demo purposes)dates = [datetime.today() - timedelta(days=i) for i in range(14)]sentiment_scores = np.random.uniform(-1, 1, len(dates))df = pd.DataFrame({"Date": dates, "Sentiment Score": sentiment_scores})# Train a regression model for predictionsX = np.array(range(len(df))).reshape(-1, 1)y = df["Sentiment Score"]model = LinearRegression()model.fit(X, y)# Predict for next 7 daysfuture_dates = [datetime.today() + timedelta(days=i) for i in range(1, 8)]X_future = np.array(range(len(df), len(df) + 7)).reshape(-1, 1)predictions = model.predict(X_future)future_df = pd.DataFrame({"Date": future_dates, "Predicted Sentiment": predictions})# Generate Word Clouddef generate_wordcloud(text):2 unmodified lineswordcloud.to_image().save(img, format='PNG')return base64.b64encode(img.getvalue()).decode()# Helper function to convert BERT labels to numerical scoresdef bert_score(result):label = result['label']if label == '1 star':return -1elif label == '2 stars':return -0.5elif label == '3 stars':return 0elif label == '4 stars':return 0.5elif label == '5 stars':return 1return 0# Get overall sentiment score based on selected modeldef get_overall_score(text, model_choice):if model_choice == "VADER":return vader_analyzer.polarity_scores(text)['compound']elif model_choice == "BERT":result = bert_sentiment(text)[0]return bert_score(result)elif model_choice == "TextBlob":return TextBlob(text).sentiment.polarity# Streamlit app setupst.title("๐ Advanced Sentiment Analysis Dashboard")# Sidebar for user input and controlsst.sidebar.header("๐ Sentiment Analysis Controls")analysis_mode = st.sidebar.radio("Analysis Mode", ["Single Text", "Compare Two Texts", "Analyze CSV File"])if analysis_mode == "Single Text":user_input = st.sidebar.text_area("Enter text for sentiment analysis")elif analysis_mode == "Compare Two Texts":user_input_a = st.sidebar.text_area("Enter first text")user_input_b = st.sidebar.text_area("Enter second text")elif analysis_mode == "Analyze CSV File":uploaded_file = st.sidebar.file_uploader("Upload a CSV file with 'text' column", type=["csv"])model_choice = st.sidebar.selectbox("Choose Sentiment Model", ["VADER", "BERT", "TextBlob"])# Analyze button handlerif st.sidebar.button("Analyze Sentiment"):if analysis_mode == "Single Text":if not user_input.strip():st.error("Please enter some text for analysis.")elif not any(c.isalpha() for c in user_input):st.error("Input should contain at least one alphabetic character.")else:with st.spinner("Analyzing text..."):overall_score = get_overall_score(user_input, model_choice)st.subheader("๐ Overall Sentiment Analysis")st.write(f"**Sentiment Score ({model_choice})**: {overall_score:.2f}")# Sentence-level analysissentences = nltk.sent_tokenize(user_input)if model_choice == "VADER":sentence_scores = [vader_analyzer.polarity_scores(s)['compound'] for s in sentences]elif model_choice == "BERT":sentence_scores = [bert_score(bert_sentiment(s)[0]) for s in sentences]elif model_choice == "TextBlob":sentence_scores = [TextBlob(s).sentiment.polarity for s in sentences]sentiment_df = pd.DataFrame({"Sentence": sentences, "Sentiment Score": sentence_scores})st.subheader("๐ Sentence-Level Sentiment")st.write(sentiment_df)fig = px.bar(sentiment_df, x="Sentence", y="Sentiment Score", title="Sentiment per Sentence")st.plotly_chart(fig)# Word cloudst.subheader("โ๏ธ Word Cloud")wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'st.image(wordcloud_img, use_column_width=True)# Download results@st.cache_datadef convert_df_to_csv(df):return df.to_csv(index=False).encode('utf-8')csv = convert_df_to_csv(sentiment_df)st.download_button(label="Download Sentiment Data",data=csv,file_name='sentiment_data.csv',mime='text/csv',)elif analysis_mode == "Compare Two Texts":if not user_input_a.strip() or not user_input_b.strip():st.error("Please enter both texts for comparison.")elif not any(c.isalpha() for c in user_input_a) or not any(c.isalpha() for c in user_input_b):st.error("Both inputs should contain at least one alphabetic character.")else:with st.spinner("Analyzing texts..."):overall_score_a = get_overall_score(user_input_a, model_choice)overall_score_b = get_overall_score(user_input_b, model_choice)col1, col2 = st.columns(2)with col1:st.subheader("Text A")st.write(f"**Sentiment Score ({model_choice})**: {overall_score_a:.2f}")with col2:st.subheader("Text B")st.write(f"**Sentiment Score ({model_choice})**: {overall_score_b:.2f}")comparison_df = pd.DataFrame({"Text": ["Text A", "Text B"],"Sentiment Score": [overall_score_a, overall_score_b]})fig = px.bar(comparison_df, x="Text", y="Sentiment Score", title="Sentiment Comparison")st.plotly_chart(fig)elif analysis_mode == "Analyze CSV File":if uploaded_file is not None:df_uploaded = pd.read_csv(uploaded_file)if "text" not in df_uploaded.columns:st.error("CSV file must contain a 'text' column.")else:with st.spinner("Analyzing uploaded texts..."):df_uploaded['sentiment'] = df_uploaded['text'].apply(lambda x: get_overall_score(x, model_choice))st.subheader("Uploaded Data Sentiment Analysis")st.write(df_uploaded)fig = px.histogram(df_uploaded, x='sentiment', title='Sentiment Distribution')st.plotly_chart(fig)else:st.error("Please upload a CSV file.")# Past sentiment trends (kept from original)st.subheader("๐ Past Sentiment Trends (Last 14 Days)")fig1 = px.line(df, x='Date', y='Sentiment Score', title='Sentiment Over Time', markers=True, line_shape='spline')st.plotly_chart(fig1)# Future sentiment predictions (kept from original)st.subheader("๐ฎ Sentiment Prediction for Next 7 Days")fig2 = px.line(future_df, x='Date', y='Predicted Sentiment', title='Predicted Sentiment Trend', markers=True, line_shape='spline')st.plotly_chart(fig2)# Reset buttonif st.sidebar.button('๐ Reset Analysis'):4 unmodified lines56789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596972 unmodified lines1001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612624 unmodified linesimport pandas as pdfrom datetime import datetime, timedeltaimport plotly.express as pxfrom sklearn.linear_model import Ridgefrom wordcloud import WordCloudimport base64from io import BytesIOimport nltkfrom textblob import TextBlobimport prawfrom googleapiclient.discovery import build# Download NLTK datanltk.download('punkt')nltk.download('stopwords')# Initialize sentiment modelsbert_sentiment = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")vader_analyzer = SentimentIntensityAnalyzer()# Reddit API setup with your credentialsREDDIT_CLIENT_ID = "S7pTXhj5JDFGDb3-_zrJEA"REDDIT_CLIENT_SECRET = "QP3NYN4lrAKVLrBamzLGrpFywiVg8w"REDDIT_USER_AGENT = "SoundaryaR_Bot/1.0"reddit = praw.Reddit(client_id=REDDIT_CLIENT_ID,client_secret=REDDIT_CLIENT_SECRET,user_agent=REDDIT_USER_AGENT)# YouTube API setup with your API keyYOUTUBE_API_KEY = "AIzaSyAChqXPaiNE9hKhApkgjgonzdgiCCOo"youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)# Helper function to convert BERT label to numerical scoredef bert_score(result):label = result['label']if label == '1 star': return -1elif label == '2 stars': return -0.5elif label == '3 stars': return 0elif label == '4 stars': return 0.5elif label == '5 stars': return 1return 0# Analyze sentiment and return scores from all modelsdef analyze_text(text):vader_score = vader_analyzer.polarity_scores(text)['compound']bert_result = bert_sentiment(text)[0]bert_num = bert_score(bert_result)textblob_score = TextBlob(text).sentiment.polarityreturn vader_score, bert_num, textblob_score# Fetch live Reddit data@st.cache_datadef fetch_reddit_data(keyword):try:subreddit = reddit.subreddit("all")posts = subreddit.search(keyword, limit=100)data = []for post in posts:data.append({'date': datetime.fromtimestamp(post.created_utc),'text': post.title + " " + post.selftext,'source': 'Reddit'})return pd.DataFrame(data)except Exception as e:st.error(f"Error fetching Reddit data: {e}")return pd.DataFrame()# Fetch live YouTube data@st.cache_datadef fetch_youtube_data(keyword):try:request = youtube.search().list(q=keyword, part="snippet", maxResults=50, type="video")response = request.execute()data = []for item in response['items']:title = item['snippet']['title']description = item['snippet']['description']published_at = datetime.strptime(item['snippet']['publishedAt'], '%Y-%m-%dT%H:%M:%SZ')data.append({'date': published_at,'text': title + " " + description,'source': 'YouTube'})return pd.DataFrame(data)except Exception as e:st.error(f"Error fetching YouTube data: {e}")return pd.DataFrame()# Generate Word Clouddef generate_wordcloud(text):2 unmodified lineswordcloud.to_image().save(img, format='PNG')return base64.b64encode(img.getvalue()).decode()# Streamlit app setupst.title("๐ SentimentSync: Live Sentiment Analysis Dashboard")# Sidebar for user input and keywordst.sidebar.header("๐ Sentiment Analysis Controls")analysis_mode = st.sidebar.radio("Analysis Mode", ["Manual Text", "Live Data (Reddit & YouTube)"])if analysis_mode == "Manual Text":user_input = st.sidebar.text_area("Enter text for sentiment analysis", height=200)else:keyword = st.sidebar.text_input("Enter keyword for live data (e.g., 'Tesla')")# Display sentiment analysis resultsdef display_sentiment_analysis(vader_score, bert_result, textblob_score, df=None):st.subheader("๐ Sentiment Analysis Results")st.write(f"**VADER Sentiment Score**: {vader_score:.2f}")st.write(f"**BERT Sentiment**: {bert_result['label']} ({bert_result['score']:.2f})")st.write(f"**TextBlob Sentiment Polarity**: {textblob_score:.2f}")sentiment_data = {'Positive': max(0, vader_score),'Negative': min(0, vader_score),'Neutral': 1 - abs(vader_score)}sentiment_df_overall = pd.DataFrame(list(sentiment_data.items()), columns=["Sentiment", "Score"])st.bar_chart(sentiment_df_overall.set_index("Sentiment"))if df is not None and not df.empty:st.subheader("๐ Detailed Sentiment Data")st.write(df[['date', 'text', 'VADER', 'BERT', 'TextBlob', 'Average']])# Process and analyzeif st.sidebar.button("Analyze Sentiment"):if analysis_mode == "Manual Text" and (not user_input or not any(c.isalpha() for c in user_input)):st.warning("โ ๏ธ Please enter text with at least one alphabetic character.")elif analysis_mode == "Live Data (Reddit & YouTube)" and not keyword:st.warning("โ ๏ธ Please enter a keyword for live data analysis.")else:with st.spinner("Analyzing..."):if analysis_mode == "Manual Text":# Overall sentiment for manual inputvader_score, bert_num, textblob_score = analyze_text(user_input)bert_result = bert_sentiment(user_input)[0]# Sentence-level analysissentences = nltk.sent_tokenize(user_input)if len(sentences) > 1:dates = [datetime.today() - timedelta(days=len(sentences) - 1 - i) for i in range(len(sentences))]sentence_data = [analyze_text(s) for s in sentences]df = pd.DataFrame({"date": dates,"text": sentences,"VADER": [d[0] for d in sentence_data],"BERT": [d[1] for d in sentence_data],"TextBlob": [d[2] for d in sentence_data]})df["Average"] = df[["VADER", "BERT", "TextBlob"]].mean(axis=1)else:df = pd.DataFrame()display_sentiment_analysis(vader_score, bert_result, textblob_score, df)wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'st.image(wordcloud_img, use_column_width=True, caption="Word Cloud of Input Text")elif analysis_mode == "Live Data (Reddit & YouTube)":# Fetch live data with your credentialsreddit_df = fetch_reddit_data(keyword)youtube_df = fetch_youtube_data(keyword)df = pd.concat([reddit_df, youtube_df], ignore_index=True)if not df.empty:# Analyze sentiment for each postsentiment_data = [analyze_text(row['text']) for _, row in df.iterrows()]df['VADER'] = [d[0] for d in sentiment_data]df['BERT'] = [d[1] for d in sentiment_data]df['TextBlob'] = [d[2] for d in sentiment_data]df['Average'] = df[['VADER', 'BERT', 'TextBlob']].mean(axis=1)# Overall sentiment for all fetched datacombined_text = " ".join(df['text'])vader_score, bert_num, textblob_score = analyze_text(combined_text)bert_result = bert_sentiment(combined_text)[0]display_sentiment_analysis(vader_score, bert_result, textblob_score, df)# Word cloudwordcloud_img = f'data:image/png;base64,{generate_wordcloud(combined_text)}'st.image(wordcloud_img, use_column_width=True, caption=f"Word Cloud for '{keyword}'")# Dynamic 14-day filter based on current datedf['date'] = pd.to_datetime(df['date'])current_date = datetime.today()cutoff_date = current_date - timedelta(days=14)df_recent = df[df['date'] >= cutoff_date].sort_values('date')# Past sentiment trendsif not df_recent.empty:st.subheader("๐ Past Sentiment Trends (Last 14 Days)")fig1 = px.line(df_recent, x='date', y=["VADER", "BERT", "TextBlob", "Average"],title=f'Sentiment Over Time for "{keyword}" (Last 14 Days from {current_date.date()})',markers=True, line_shape='spline')st.plotly_chart(fig1)# Sentiment prediction with Ridge Regressionst.subheader("๐ฎ Sentiment Prediction for Next 7 Days")X = np.array((df_recent['date'] - df_recent['date'].min()).dt.total_seconds() / 86400).reshape(-1, 1)future_dates = [current_date + timedelta(days=i) for i in range(1, 8)]X_future = np.array(range(int(X[-1]) + 1, int(X[-1]) + 8)).reshape(-1, 1)models = {"VADER": Ridge(alpha=1.0).fit(X, df_recent["VADER"]),"BERT": Ridge(alpha=1.0).fit(X, df_recent["BERT"]),"TextBlob": Ridge(alpha=1.0).fit(X, df_recent["TextBlob"]),"Average": Ridge(alpha=1.0).fit(X, df_recent["Average"])}predictions = {name: model.predict(X_future) for name, model in models.items()}future_df = pd.DataFrame({"Date": future_dates,"VADER": predictions["VADER"],"BERT": predictions["BERT"],"TextBlob": predictions["TextBlob"],"Average": predictions["Average"]})fig2 = px.line(future_df, x='Date', y=["VADER", "BERT", "TextBlob", "Average"],title=f'Predicted Sentiment Trend for "{keyword}" (Next 7 Days from {current_date.date()})',markers=True, line_shape='spline')st.plotly_chart(fig2)# Sentiment distributionst.subheader("๐ Sentiment Distribution")dist_values = [sum(df_recent['Average'] > 0),sum(df_recent['Average'] < 0),sum(df_recent['Average'] == 0)]fig3 = px.pie(values=dist_values, names=['Positive', 'Negative', 'Neutral'],title=f'Sentiment Distribution for "{keyword}" (Last 14 Days)', hole=0.3)st.plotly_chart(fig3)# Sentiment scatter plotst.subheader("๐ Sentiment Scatter Plot")fig4 = px.scatter(df_recent, x='date', y="Average",title=f'Sentiment Over Time for "{keyword}" (Last 14 Days)',text=df_recent["text"].str[:20] + "...", color="source")fig4.update_traces(textposition='top center')st.plotly_chart Masters(fig4)# Rolling averagest.subheader("๐ Rolling Average Sentiment")df_recent = df_recent.sort_values('date')df_recent['Rolling Avg'] = df_recent['Average'].rolling(window=min(7, len(df_recent)), min_periods=1).mean()fig5 = px.line(df_recent, x='date', y='Rolling Avg',title=f"Rolling Average Sentiment for '{keyword}' (Last 14 Days, Window: {min(7, len(df_recent))})",markers=True)st.plotly_chart(fig5)else:st.info(f"No data within the last 14 days (from {cutoff_date.date()} to {current_date.date()}) for this keyword.")else:st.error("No data fetched. Check API credentials or keyword.")# Reset buttonif st.sidebar.button('๐ Reset Analysis'):
app.py+219-139