File size: 1,086 Bytes
2cec43b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline

# title
st.title("Sentiment Analysis App")

# subtitle
st.markdown("Enter a text and select a pretrained model to get the sentiment analysis:")

st.markdown("Link to the app - [sentiment-analysis-streamlit on 🤗 Spaces](https://huggingface.co/spaces/rd448/sentiment-analysis-streamlit)")

# text input
text = st.text_input("Enter text here", "I love you")

# Model selection dropdown
model_names = ["distilbert-base-uncased-finetuned-sst-2-english"]
model = st.selectbox("Select a pretrained model", model_names)

# Sentiment analysis function
def analyze_sentiment(text, model):
    if model == "distilbert-base-uncased-finetuned-sst-2-english":
        classifier = pipeline("sentiment-analysis", model=model)
        result = classifier(text)[0]
        sentiment = result['label']
        score = result['score']
    return sentiment, score

if st.button("Analyze"):
    sentiment, score = analyze_sentiment(text, model)
    st.write(f"Sentiment: {sentiment}")
    if score is not None:
        st.write(f"Score: {score}")