Sentiment_Ana / app.py
Jainesh212's picture
Update app.py
8e13357
raw
history blame
No virus
1.37 kB
import streamlit as st
import transformers
import torch
# 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:")
for model_name, label, score in results:
st.write(f"- {model_name}: {label} ({score:.2f})")
# Run Streamlit app
if __name__ == "__main__":
app()