Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import transformers | |
# Define sentiment analysis models | |
models = { | |
"DistilBERT": transformers.pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english"), | |
"BERT": transformers.pipeline("sentiment-analysis", model="bert-base-uncased-finetuned-sst-2-english"), | |
"RoBERTa": transformers.pipeline("sentiment-analysis", model="roberta-base-openai-detector"), | |
} | |
# Define function to analyze sentiment using selected model | |
def analyze_sentiment(text, model_name): | |
model = models[model_name] | |
result = model(text)[0] | |
return result['label'], result['score'] | |
# Define Streamlit app | |
def app(): | |
st.title("Sentiment Analysis App") | |
# User input | |
text = st.text_area("Enter text to analyze", max_chars=1024) | |
# Sentiment analysis | |
if st.button("Analyze"): | |
st.write("Analyzing sentiment...") | |
with st.spinner("Wait for it..."): | |
results = [] | |
for model_name in models: | |
label, score = analyze_sentiment(text, model_name) | |
results.append((model_name, label, score)) | |
st.success("Sentiment analysis complete!") | |
st.write("Results:") | |
df = pd.DataFrame(results, columns=["Model", "Sentiment", "Score"]) | |
st.write(df) | |
# Plot results | |
sns.set_style("whitegrid") | |
fig, ax = plt.subplots() | |
sns.barplot(x="Model", y="Score", hue="Sentiment", data=df, ax=ax) | |
ax.set_title("Sentiment Analysis Results") | |
st.pyplot(fig) | |
# Run Streamlit app | |
if __name__ == "__main__": | |
app() | |