File size: 2,257 Bytes
5ea964d
886afc7
 
 
 
5ea964d
886afc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
import numpy as np
from scipy.special import softmax
from collections import Counter

# Load model and tokenizer
MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
config = AutoConfig.from_pretrained(MODEL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL)

def analyze_sentiment_for_sentences(sentences):
    sentiments = []
    for sentence in sentences:
        encoded_input = tokenizer(sentence, return_tensors='pt')
        output = model(**encoded_input)
        scores = output[0][0].detach().numpy()
        scores = softmax(scores)
        highest_sentiment_idx = np.argmax(scores)
        highest_sentiment_label = config.id2label[highest_sentiment_idx]
        sentiments.append(highest_sentiment_label)
    return sentiments

def calculate_sentiment_percentages(sentiments):
    sentiment_counts = Counter(sentiments)
    total_sentiments = len(sentiments)
    sentiment_percentages = {"😊 positive": 0, "😐 neutral": 0, "πŸ˜” negative": 0}
    for sentiment, count in sentiment_counts.items():
        if sentiment == "positive":
            sentiment_percentages["😊 positive"] = (count / total_sentiments) * 100
        elif sentiment == "neutral":
            sentiment_percentages["😐 neutral"] = (count / total_sentiments) * 100
        else:
            sentiment_percentages["πŸ˜” negative"] = (count / total_sentiments) * 100
    return sentiment_percentages

# Streamlit UI
st.title("✨ Sentiment Analysis Web App")
st.write("Enter sentences below to analyze their sentiment.")

# User input
user_input = st.text_area("Enter sentences (one per line):")
if st.button("Analyze Sentiment"):
    if user_input.strip():
        sentences = user_input.split("\n")
        sentences = [s.strip() for s in sentences if s.strip()]
        sentiments = analyze_sentiment_for_sentences(sentences)
        sentiment_percentages = calculate_sentiment_percentages(sentiments)
        
        st.subheader("πŸ“Š Sentiment Analysis Results")
        st.write(sentiment_percentages)
    else:
        st.warning("⚠️ Please enter at least one sentence.")