Spaces:
Runtime error
Runtime error
import streamlit as st | |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification | |
# Function to load the pre-trained model | |
def load_model(model_name): | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
sentiment_pipeline = pipeline("sentiment-analysis", tokenizer=tokenizer, model=model) | |
return sentiment_pipeline | |
# Streamlit app | |
st.title("Basic Sentiment Analysis App") | |
st.write("Enter a text and select a pre-trained model to get the sentiment analysis.") | |
# Input text | |
text = st.text_input("Enter your text:") | |
# Model selection | |
model_options = [ | |
"distilbert-base-uncased-finetuned-sst-2-english", | |
"textattack/bert-base-uncased-SST-2", | |
"cardiffnlp/twitter-roberta-base-sentiment", | |
"nlptown/bert-base-multilingual-uncased-sentiment" | |
] | |
selected_model = st.selectbox("Choose a pre-trained model:", model_options) | |
# Load the model and perform sentiment analysis | |
if st.button("Analyze"): | |
if not text: | |
st.write("Please enter a text.") | |
else: | |
with st.spinner("Analyzing sentiment..."): | |
sentiment_pipeline = load_model(selected_model) | |
result = sentiment_pipeline(text) | |
st.write(f"Sentiment: {result[0]['label']} (confidence: {result[0]['score']:.2f})") | |
else: | |
st.write("Enter a text and click 'Analyze' to perform sentiment analysis.") | |