File size: 1,418 Bytes
31b46d4
bf43dc9
 
 
31b46d4
 
214f718
31b46d4
 
 
 
 
214f718
31b46d4
 
 
 
 
214f718
31b46d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf43dc9
 
31b46d4
bf43dc9
 
 
 
 
 
31b46d4
214f718
31b46d4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()