import streamlit as st from transformers import pipeline # --- PAGE SETUP --- st.set_page_config(page_title="Hugging Face Sentiment AI", page_icon="🤗") # --- MODEL LOADING (Cached) --- @st.cache_resource def load_sentiment_model(): # This uses the default 'distilbert-base-uncased-finetuned-sst-2-english' return pipeline("sentiment-analysis") classifier = load_sentiment_model() # --- UI ELEMENTS --- st.title("🤗 AI Sentiment Analyzer") st.write("This app uses a Hugging Face Transformer model to detect sentiment.") user_input = st.text_area("Enter text to analyze:", placeholder="I am so excited to build this app!") if st.button("Analyze Sentiment"): if user_input.strip(): with st.spinner("Analyzing..."): # Run prediction results = classifier(user_input) # Extract data label = results[0]['label'] score = results[0]['score'] # --- DISPLAY RESULTS --- st.divider() if label == "POSITIVE": st.success(f"### {label} 😊") else: st.error(f"### {label} 😡") st.metric(label="Confidence Score", value=f"{score:.2%}") else: st.warning("Please enter some text first!") st.caption("Running on Hugging Face Spaces with Streamlit")