Spaces:
Sleeping
Sleeping
File size: 1,189 Bytes
00601c4 a93aec3 00601c4 |
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 |
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!") |