CoolFace
Apppublic

rd448/sentiment-analysis-streamlit

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py33 linesDownload Raw Back to root
1import streamlit as st2from transformers import pipeline3 4# title5st.title("Sentiment Analysis App")6 7# subtitle8st.markdown("Enter a text and select a pretrained model to get the sentiment analysis:")9 10st.markdown("Link to the app - [sentiment-analysis-streamlit on 🤗 Spaces](https://huggingface.co/spaces/rd448/sentiment-analysis-streamlit)")11 12# text input13text = st.text_input("Enter text here", "I love you")14 15# Model selection dropdown16model_names = ["distilbert-base-uncased-finetuned-sst-2-english"]17model = st.selectbox("Select a pretrained model", model_names)18 19# Sentiment analysis function20def analyze_sentiment(text, model):21    if model == "distilbert-base-uncased-finetuned-sst-2-english":22        classifier = pipeline("sentiment-analysis", model=model)23        result = classifier(text)[0]24        sentiment = result['label']25        score = result['score']26    return sentiment, score27 28if st.button("Analyze"):29    sentiment, score = analyze_sentiment(text, model)30    st.write(f"Sentiment: {sentiment}")31    if score is not None:32        st.write(f"Score: {score}")33