File size: 1,469 Bytes
c2a70ce
 
 
 
 
a3d1a03
c2a70ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from transformers import pipeline
from collections import Counter

model = pipeline('text-classification', model='SentimentAnalysis')

st.title("Sentiment Analysis")
uploaded_file = st.file_uploader('Upload CSV file', type='csv')

def get_predictions(filename):
    try:
        df = pd.read_csv(filename)
        first_hu = df.head(1000)
        
        if 'text' not in df.columns:
            st.error("The uploaded CSV must contain a 'text' column.")
            return
        
        with st.spinner("Generating predictions..."):
            predictions = []
            for text in first_hu['text']:
                output = model(text)
                if output[0]["score"]>=0.50 and output[0]['score']<=0.60 and output[0]['label']=="LABEL_1":
                  predictions.append('Neutral')  
                elif output[0]['score']>0.60 and output[0]['label']=="LABEL_1":
                    predictions.append('Positive')
                else:
                    predictions.append('Negative')
        
       
        sentiment_counts = Counter(predictions)
        
        sentiment_df = pd.DataFrame.from_dict(sentiment_counts, orient='index', columns=['Count'])
        st.bar_chart(sentiment_df)
        st.success("Predictions generated successfully!")
        

    except Exception as e:
        st.error(f"An error occurred: {str(e)}")

if uploaded_file is not None:
    get_predictions(uploaded_file)