File size: 1,622 Bytes
cda6f4a
d2fcec4
cda6f4a
d2fcec4
 
 
cda6f4a
 
 
 
 
d2fcec4
 
 
cda6f4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2fcec4
 
cda6f4a
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
import pandas as pd
import streamlit as st
import altair as alt
from transformers import pipeline

def summarize_text(text):
    unmasker = pipeline("sentiment-analysis", model="stevhliu/my_awesome_model")
    sentiment_output = unmasker(text)[0]
    sentiment_label = sentiment_output["label"]
    sentiment_score = sentiment_output["score"]
    return sentiment_label, sentiment_score

def main():
    user_input = st.text_area("Input Text Here")
    if st.button("Analyze Sentiment"):
        sentiment_label, sentiment_score = summarize_text(user_input)
        if sentiment_label == "LABEL_1":
            sentiment_label = "Positive"
            pos_score = sentiment_score
            neg_score = 1 - sentiment_score
        else:
            sentiment_label = "Negative"
            neg_score = sentiment_score
            pos_score = 1 - sentiment_score

        st.write(f"Sentiment Label: {sentiment_label}")
        st.write(f"Positive Score: {pos_score:.2f}")
        st.write(f"Negative Score: {neg_score:.2f}")

        chart_data = pd.DataFrame({"Sentiment Score": [pos_score, neg_score]}, index=["Positive", "Negative"])

        chart = alt.Chart(chart_data.reset_index()).mark_bar().encode(
            x=alt.X('index:N', title=None),
            y=alt.Y('Sentiment Score:Q', title="Sentiment Score"),
            color=alt.Color('index:N', scale=alt.Scale(domain=['Positive', 'Negative'], range=['green', 'red']), legend=None)
        ).properties(
            width=300,
            height=200
        )

        st.altair_chart(chart, use_container_width=True)

if __name__ == "__main__":
    main()