File size: 1,370 Bytes
908cd92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
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")