File size: 2,174 Bytes
0e992ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import matplotlib.pyplot as plt

# Load model and tokenizer
model_name = "tabularisai/multilingual-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

def predict_sentiment(texts):
    inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True, max_length=512)
    with torch.no_grad():
        outputs = model(**inputs)
    probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
    sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
    return [sentiment_map[p] for p in torch.argmax(probabilities, dim=-1).tolist()]

# Streamlit UI
st.title("Sentiment Analysis App")
st.write("Upload an Excel file containing text data, and we'll analyze its sentiment.")

uploaded_file = st.file_uploader("Upload Excel File", type=["xlsx", "xls"])

if uploaded_file is not None:
    df = pd.read_excel(uploaded_file)
    st.write("Preview of Uploaded Data:")
    st.dataframe(df.head())
    
    text_column = st.selectbox("Select the column containing text", df.columns)
    
    if st.button("Analyze Sentiment"):
        df["Sentiment"] = predict_sentiment(df[text_column].astype(str).tolist())
        
        # Display results
        st.write("Sentiment Analysis Results:")
        st.dataframe(df[[text_column, "Sentiment"]])
        
        # Pie chart
        sentiment_counts = df["Sentiment"].value_counts()
        fig, ax = plt.subplots()
        ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%', colors=["red", "yellow", "pink", "lightgreen", "green"])
        ax.set_title("Sentiment Distribution")
        st.pyplot(fig)
        
        # Table display for sentiment analysis results
        st.write("Detailed Sentiment Table:")
        st.table(df[[text_column, "Sentiment"]])
        
        # Download option
        st.download_button("Download Results", df.to_csv(index=False).encode('utf-8'), "sentiment_results.csv", "text/csv")