File size: 2,134 Bytes
2ed4404
 
 
 
 
 
 
 
5b9c9a7
2ed4404
 
 
 
 
 
 
d0a49c9
2ed4404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b9c9a7
 
 
2ed4404
5b9c9a7
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
import streamlit as st
import pandas as pd
import time
import plotly.express as px
from utils.file_handler import validate_file
from models.sentiment_model import load_model

# Constants
MAX_FILE_SIZE_MB = 200

# Load sentiment analysis pipeline
sentiment_pipeline = load_model()

st.title("📊 Sentiment Analysis App")

# File upload
uploaded_file = st.file_uploader("Upload a CSV file (Max: 200MB)", type=["csv"])

if uploaded_file is not None:
    if validate_file(uploaded_file, MAX_FILE_SIZE_MB):
        df = pd.read_csv(uploaded_file)
        
        # Check for 'text' column or ask user for correct column
        if "text" not in df.columns:
            text_column = st.selectbox("Select the column containing text values", df.columns)
        else:
            text_column = "text"

        if st.button("Analyze Sentiment"):
            st.write("Processing sentiment analysis...")
            progress_bar = st.progress(0)

            sentiments = []
            for i, text in enumerate(df[text_column].dropna()):
                result = sentiment_pipeline(text)
                sentiments.append(result[0]["label"])
                progress_bar.progress((i + 1) / len(df))
                time.sleep(0.1)

            df["Sentiment"] = sentiments

            # Display results
            st.write("Sentiment Analysis Results:")
            st.dataframe(df[[text_column, "Sentiment"]])

            # Create pie chart
            sentiment_counts = df["Sentiment"].value_counts().reset_index()
            sentiment_counts.columns = ["Sentiment", "Count"]
            fig = px.pie(sentiment_counts, names="Sentiment", values="Count", title="Sentiment Distribution")
            st.plotly_chart(fig)

            # Allow CSV download
            # st.download_button("Download Results", df.to_csv(index=False), "sentiment_results.csv", "text/csv")
            # Download option
            st.download_button("Download Results", df.to_csv(index=False).encode('utf-8'), "sentiment_results.csv", "text/csv")
    else:
        st.error("File exceeds the maximum allowed size of 200MB. Please upload a smaller file.")