Boltuzamaki's picture
init
cff61f9
import os
import warnings
import streamlit as st
from transformers import pipeline
# Disable TensorFlow and CUDA logs
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU usage
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" # Suppress TensorFlow warnings
# Suppress all warnings from Python
warnings.filterwarnings("ignore")
# Load sentiment-analysis pipeline from Hugging Face
@st.cache_resource
def load_model():
return pipeline("sentiment-analysis")
# App title and description
st.title("News Sentiment Classification 📰💡")
st.write(
"""
This app uses a pre-trained model from Hugging Face to classify the sentiment of news headlines or articles.
Enter your news content below, and the model will classify it as either 'POSITIVE' or 'NEGATIVE'.
"""
)
# Input from the user
news_input = st.text_area("Enter a news headline or article:", "")
# Load the model (cached to avoid reloading every time)
sentiment_classifier = load_model()
# Classify the sentiment when the button is pressed
if st.button("Classify Sentiment"):
if news_input:
result = sentiment_classifier(news_input)
# Display the sentiment and confidence score
sentiment = result[0]["label"]
confidence = result[0]["score"]
st.subheader(f"Sentiment: {sentiment}")
st.write(f"Confidence: {confidence:.2%}")
else:
st.error("Please enter a news headline or article for classification.")
st.error("Please enter a news headline or article for classification.")