import streamlit as st import torch from transformers import pipeline # Set device (GPU if available) DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Custom Streamlit Styles st.markdown(""" """, unsafe_allow_html=True) # Load Model from Hugging Face @st.cache_resource def load_model(): model_name = "distilbert-base-uncased-finetuned-sst-2-english" # Replace with your actual HF model classifier = pipeline("text-classification", model=model_name, tokenizer=model_name, device=0 if DEVICE == "cuda" else -1) return classifier # Initialize model classifier = load_model() # Streamlit UI st.markdown('

Sentiment Analysis App 💬

', unsafe_allow_html=True) st.write("Enter a review below and let AI analyze its sentiment! 🚀") # User Input text = st.text_area("Enter text:", "", height=150) if st.button("Analyze"): if text.strip(): result = classifier(text)[0] sentiment = result['label'] confidence = result['score'] # Display sentiment result st.markdown(f'

Sentiment: {sentiment} ({confidence:.2%} confidence)

', unsafe_allow_html=True) else: st.warning("⚠️ Please enter some text!")