sentiment_2 / app.py
CananD's picture
Update app.py
a93aec3 verified
import streamlit as st
from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import numpy as np
# Load sentiment analysis model
@st.cache_resource
def load_model():
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
return pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
classifier = load_model()
# Streamlit UI
st.title("Sentiment Analysis App")
st.header("Analyze Text Sentiment")
user_input = st.text_area("Enter text to analyze:", "I love studying NLP! It's awesome.")
if st.button("Analyze"):
if user_input:
result = classifier(user_input)
sentiment = result[0]['label']
confidence = result[0]['score']
st.subheader("Result:")
if sentiment == 'POSITIVE':
st.success(f"Positive sentiment (confidence: {confidence:.2%})")
else:
st.error(f"Negative sentiment (confidence: {confidence:.2%})")
else:
st.warning("Please enter some text to analyze!")