File size: 1,225 Bytes
451cf27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline

# Set page title and header
st.set_page_config(page_title="Sentiment Analysis App", page_icon="🤖")
st.title("🤖 Sentiment Analysis with Hugging Face")

st.markdown("""
This app uses a pre-trained machine learning model from Hugging Face Transformers to analyze the sentiment of your text.
""")

# Load the pipeline (cached to avoid reloading on every interaction)
@st.cache_resource
def load_sentiment_pipeline():
    return pipeline("sentiment-analysis")

classifier = load_sentiment_pipeline()

# User input
text_input = st.text_area("Enter some text here:", height=150, placeholder="I love building cool AI apps!")

if st.button("Analyze Sentiment"):
    if text_input.strip():
        with st.spinner("Analyzing..."):
            result = classifier(text_input)[0]
            label = result['label']
            score = result['score']
            
            if label == 'POSITIVE':
                st.success(f"**Sentiment:** {label} 😊")
            else:
                st.error(f"**Sentiment:** {label} 😔")
            
            st.metric("Confidence Score", f"{score:.4f}")
    else:
        st.warning("Please enter some text to analyze.")