| import streamlit as st |
| import pandas as pd |
| from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer |
| from textblob import TextBlob |
| from transformers import pipeline |
| import matplotlib.pyplot as plt |
| import os |
| from wordcloud import WordCloud |
|
|
| |
| def analyze_sentiment_hf(text): |
| hf_pipeline = pipeline("sentiment-analysis", "Dmyadav2001/Sentimental-Analysis") |
| if len(text) > 512: |
| text = text[:511] |
| sentiment_result = hf_pipeline(text) |
| sentiment_label = sentiment_result[0]["label"] |
| if sentiment_label == "LABEL_1": |
| return "Positive" |
| elif sentiment_label == "LABEL_0": |
| return "Negative" |
| else: |
| return "Neutral" |
|
|
| |
| def analyze_sentiment_vader(text): |
| sentiment_analyzer = SentimentIntensityAnalyzer() |
| sentiment_score = sentiment_analyzer.polarity_scores(text)["compound"] |
| if sentiment_score > 0: |
| return "Positive" |
| elif sentiment_score == 0: |
| return "Neutral" |
| else: |
| return "Negative" |
|
|
| |
| def analyze_sentiment_textblob(text): |
| sentiment_analysis = TextBlob(text) |
| score = sentiment_analysis.sentiment.polarity |
| if score > 0: |
| return "Positive" |
| elif score == 0: |
| return "Neutral" |
| else: |
| return "Negative" |
|
|
| |
| def display_results_dataframe(data_frame): |
| st.write(data_frame) |
|
|
| |
| def create_pie_chart(data_frame, sentiment_column): |
| sentiment_distribution = data_frame[sentiment_column].value_counts() |
| fig, ax = plt.subplots() |
| ax.pie(sentiment_distribution, labels=sentiment_distribution.index, autopct='%1.1f%%', startangle=90) |
| ax.axis('equal') |
| st.pyplot(fig) |
|
|
| |
| def create_word_cloud(sentiment_data): |
| wordcloud_generator = WordCloud(width=800, height=400).generate(sentiment_data) |
| fig, ax = plt.subplots(figsize=(10, 5)) |
| ax.imshow(wordcloud_generator, interpolation='bilinear') |
| ax.axis('off') |
| st.pyplot(fig) |
|
|
| |
| st.set_page_config(page_title="Sentiment Analysis Tool", page_icon=":bar_chart:") |
| st.title("Sentiment Analysis Tool") |
|
|
| |
| st.sidebar.title("Analysis Options") |
| input_type = st.sidebar.selectbox("Choose Input Type", ["Text Input", "CSV Upload"]) |
| model_choice = st.sidebar.selectbox("Choose Sentiment Analysis Model", ["Hugging Face", "VADER", "TextBlob"]) |
| display_type = st.sidebar.selectbox("Choose Display Type", ["DataFrame", "Pie Chart", "Word Cloud"]) |
|
|
| |
| if input_type == "Text Input": |
| user_text = st.text_input("Enter text for sentiment analysis:") |
| if st.button("Analyze Sentiment"): |
| if user_text: |
| |
| if model_choice == "Hugging Face": |
| sentiment = analyze_sentiment_hf(user_text) |
| elif model_choice == "VADER": |
| sentiment = analyze_sentiment_vader(user_text) |
| else: |
| sentiment = analyze_sentiment_textblob(user_text) |
| |
| st.write("Detected Sentiment:", sentiment) |
| else: |
| st.warning("Please enter some text to analyze.") |
| elif input_type == "CSV Upload": |
| uploaded_file = st.file_uploader("Upload CSV file for analysis", type="csv") |
| if st.button("Start Analysis"): |
| if uploaded_file is not None: |
| data_frame = pd.read_csv(uploaded_file) |
| |
| if 'text' in data_frame.columns: |
| data_frame['Sentiment'] = data_frame['text'].apply(lambda x: analyze_sentiment_hf(x) if model_choice == "Hugging Face" else (analyze_sentiment_vader(x) if model_choice == "VADER" else analyze_sentiment_textblob(x))) |
| |
| if display_type == "DataFrame": |
| display_results_dataframe(data_frame) |
| elif display_type == "Pie Chart": |
| create_pie_chart(data_frame, 'Sentiment') |
| elif display_type == "Word Cloud": |
| combined_text = ' '.join(data_frame['text']) |
| create_word_cloud(combined_text) |
| else: |
| st.error("The uploaded CSV file must contain a 'text' column.") |
| else: |
| st.warning("Please upload a CSV file to proceed with analysis.") |
|
|