import streamlit as st from transformers import pipeline, AutoTokenizer st.title('Sentiment Analyser App') st.write('Welcome to my sentiment analysis app!') model_options=["sentiment-analysis", "twitter-xlm-roberta-base-sentiment", "sentiment-roberta-large-english-3-classes"] form = st.form(key='sentiment-form') model_type = form.selectbox(label="Select a model", options=model_options) user_input = form.text_area(label='Enter your text to analyse', value="Hey how are you?") submit = form.form_submit_button('Submit') def classification(user_input, type): if type=="sentiment-analysis": classifier = pipeline("sentiment-analysis") elif type=="twitter-xlm-roberta-base-sentiment": path="cardiffnlp/twitter-xlm-roberta-base-sentiment" classifier = pipeline("sentiment-analysis", model=path, tokenizer=path) elif type=="sentiment-roberta-large-english-3-classes": path="j-hartmann/sentiment-roberta-large-english-3-classes" classifier = pipeline("text-classification", model=path, return_all_scores=True) result = classifier(user_input)[0] return result if submit: resultf = classification(user_input, model_type) label = resultf['label'] score = resultf['score'] if (label == 'POSITIVE') or (label =='Positive') or (label =='positive'): st.success(f'{label} sentiment (score: {score})') else: st.error(f'{label} sentiment (score: {score})')