Sentiment_Ana / app.py
Jainesh212's picture
Update app.py
214f718
raw
history blame contribute delete
No virus
1.42 kB
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import transformers
models = {
"DistilBERT": transformers.pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english"),
"RoBERTa": transformers.pipeline("sentiment-analysis", model="roberta-base-openai-detector"),
}
def analyze_sentiment(text, model_name):
model = models[model_name]
result = model(text)[0]
return result['label'], result['score']
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)
if __name__ == "__main__":
app()