test / app.py
imostafizur's picture
Update app.py
7496c4d verified
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()