File size: 2,269 Bytes
b02cd71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import pandas as pd
from transformers import pipeline
import matplotlib.pyplot as plt
import time

# Load the sentiment analysis model
sentiment_model = pipeline("sentiment-analysis", model="tabularisai/multilingual-sentiment-analysis")

# Function to perform sentiment analysis
def perform_sentiment_analysis(texts):
    sentiments = sentiment_model(texts)
    return sentiments

# Function to plot the sentiment analysis results
def plot_sentiment_analysis(sentiments):
    labels = [item['label'] for item in sentiments]
    label_counts = pd.Series(labels).value_counts()

    fig, ax = plt.subplots()
    ax.pie(label_counts, labels=label_counts.index, autopct='%1.1f%%', startangle=90)
    ax.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

    st.pyplot(fig)

# Streamlit UI
st.title("Sentiment Analysis App")

# File upload
uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])

if uploaded_file is not None:
    # Read the file
    if uploaded_file.name.endswith(".csv"):
        df = pd.read_csv(uploaded_file)
    else:
        df = pd.read_excel(uploaded_file, engine='openpyxl')

    # Check if 'text' column exists
    if 'text' not in df.columns:
        st.warning("Column 'text' not found. Please enter the column name containing the text values.")
        text_column = st.text_input("Enter the column name containing the text values")
    else:
        text_column = 'text'

    if text_column in df.columns:
        # Display the first few rows of the dataframe
        st.write("First few rows of the uploaded file:")
        st.write(df.head())

        # Perform sentiment analysis
        if st.button("Run Sentiment Analysis"):
            texts = df[text_column].tolist()
            progress_bar = st.progress(0)

            # Simulate progress
            for i in range(100):
                time.sleep(0.05)
                progress_bar.progress(i + 1)

            sentiments = perform_sentiment_analysis(texts)
            st.success("Sentiment analysis completed!")

            # Plot the sentiment analysis results
            plot_sentiment_analysis(sentiments)
    else:
        st.error("The specified column does not exist in the uploaded file.")