File size: 1,674 Bytes
54cebbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f78ac3
54cebbd
 
 
6f78ac3
 
 
 
 
 
 
 
54cebbd
7496c4d
 
 
 
ef010b7
 
 
 
c79673f
ef010b7
 
 
 
 
54cebbd
 
 
 
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
import streamlit as st
from transformers import pipeline

# Load the sentiment classifier model
distilled_student_sentiment_classifier = pipeline(
    model="lxyuan/distilbert-base-multilingual-cased-sentiments-student",
    return_all_scores=True
)

# Define the Streamlit app
def main():
    # Add a title to the app
    st.title("DistilBERT Sentiment Analysis")

    # Add a text input field for user input
    user_input = st.text_area("Enter text:", height=100)

    # Perform sentiment analysis when the user submits input
    if st.button("Analyze"):
        # Check if the input text is empty
        if not user_input.strip():
            st.error("Please enter some text.")
        else:
            # Display a loading message while performing analysis
            with st.spinner("Analyzing..."):
                # Perform sentiment analysis on the input text
                result = distilled_student_sentiment_classifier(user_input)

            # Print the type and content of the result for debugging
            st.write("Result Type:", type(result))
            st.write("Result Content:", result)

            # Check if the result is a dictionary
            if isinstance(result, dict):
                # Access the 'scores' dictionary and check if sentiment is negative
                negative = result.get('scores', {}).get('label') == 'negative'

                # If sentiment is negative, print "Negative"
                if negative:
                    st.write("Sentiment Analysis Result: Negative")
            else:
                st.error("Unexpected data format in result.")

# Run the Streamlit app
if __name__ == "__main__":
    main()