Spaces:
Runtime error
Runtime error
Update app.py
Browse files11 unmodified lines1213141516171819202122232425262728293 unmodified lines333435363738393 unmodified lines43444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410511 unmodified linesimport nltkfrom textblob import TextBlobnltk.download('punkt')# Initialize sentiment modelsbert_sentiment = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")vader_analyzer = SentimentIntensityAnalyzer()# Generate sample past sentiment datadates = [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 modelX = np.array(range(len(df))).reshape(-1, 1)y = df["Sentiment Score"]model = LinearRegression()3 unmodified linesfuture_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 Cloud3 unmodified lineswordcloud.to_image().save(img, format='PNG')return base64.b64encode(img.getvalue()).decode()# Streamlit app setupst.title("๐ Advanced Sentiment Analysis Dashboard")# Sidebar for user inputst.sidebar.header("๐ Sentiment Analysis Controls")user_input = st.sidebar.text_area("Enter text for sentiment analysis")# Display sentiment analysis resultsdef display_sentiment_analysis(vader_score, bert_result, textblob_score):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 = pd.DataFrame(list(sentiment_data.items()), columns=["Sentiment", "Score"])st.bar_chart(sentiment_df.set_index("Sentiment"))wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'st.image(wordcloud_img, use_column_width=True)if st.sidebar.button("Analyze Sentiment"):if user_input:with st.spinner("Analyzing text..."):vader_score = vader_analyzer.polarity_scores(user_input)['compound']bert_result = bert_sentiment(user_input)[0]textblob_score = TextBlob(user_input).sentiment.polaritydisplay_sentiment_analysis(vader_score, bert_result, textblob_score)else:st.warning("โ ๏ธ Please enter some text for analysis.")# Past sentiment trendsst.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 predictionsst.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)# Sentiment distribution pie chartst.subheader("๐ Sentiment Distribution")fig3 = px.pie(values=[sum(df['Sentiment Score'] > 0), sum(df['Sentiment Score'] <= 0)], names=['Positive', 'Negative'], title='Sentiment Distribution', hole=0.3)st.plotly_chart(fig3)# Sentiment scatter plotst.subheader("๐ Sentiment Scatter Plot (Last 14 Days)")fig4 = px.scatter(df, x='Date', y='Sentiment Score', title='Sentiment Over Time')st.plotly_chart(fig4)# Rolling average sentimentst.subheader("๐ Rolling Average of Sentiment (7-Day Window)")df['Rolling Avg Sentiment'] = df['Sentiment Score'].rolling(window=7).mean()fig5 = px.line(df, x='Date', y='Rolling Avg Sentiment', title="7-Day Rolling Average Sentiment")st.plotly_chart(fig5)# Reset buttonif st.sidebar.button('๐ Reset Analysis'):st.experimental_rerun()11 unmodified lines121314151617181920212223242526272829303 unmodified lines3435363738393 unmodified lines43444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218311 unmodified linesimport 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()3 unmodified linesfuture_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 Cloud3 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'):st.experimental_rerun()
app.py+123-45