File size: 1,516 Bytes
f25443b
7f19196
f25443b
7f19196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
import streamlit as st
from transformers import pipeline

print("Loading the model...")

# Title and Description
st.title("Sentiment Analysis Web App")
st.write("""
### Powered by Hugging Face and Streamlit
This app uses a pre-trained NLP model from Hugging Face to analyze the sentiment of the text you enter. 
Try entering a sentence to see if it's positive, negative, or neutral!
""")

# Initialize Hugging Face Sentiment Analysis Pipeline
@st.cache_resource
def load_model():
    print("before load model")
    return pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

sentiment_analyzer = load_model()

# Input Text from User
user_input = st.text_area("Enter some text to analyze:", "Streamlit and Hugging Face make NLP fun!")

# Analyze Sentiment
if st.button("Analyze Sentiment"):
    print("button click")
    if user_input.strip():
        result = sentiment_analyzer(user_input)[0]
        sentiment = result['label']
        score = result['score']

        # Display the Result
        st.subheader("Sentiment Analysis Result")
        st.write(f"**Sentiment:** {sentiment}")
        st.write(f"**Confidence Score:** {score:.2f}")
    else:
        st.warning("Please enter some text to analyze!")

# Sidebar with About Information
st.sidebar.title("About")
st.sidebar.info("""
This app demonstrates the use of Hugging Face's NLP models with Streamlit. 
It uses the `distilbert-base-uncased-finetuned-sst-2-english` model for sentiment analysis.
""")
print("after")