File size: 3,793 Bytes
df3e003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
import pandas as pd
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import emoji

model_path = "ANLPRL/TBModel"
tokenizer_path = "ANLPRL/TBTokenizer"

# Load the tokenizer and model
model = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)

def predict(text):
    encoded_data = tokenizer.encode_plus(text, padding=True, truncation=True, return_tensors='pt')
    input_ids = encoded_data['input_ids']
    attention_mask = encoded_data['attention_mask']
    with torch.no_grad():
        outputs = model(input_ids, attention_mask)
    logits = outputs.logits
    probabilities = torch.softmax(logits, dim=1)
    _, predicted = torch.max(probabilities, dim=1)
    
    # Create dictionary to map numerical labels to categories
    label_dict = {0: 'Positive', 1: 'Negative', 2: 'Neutral'}
    predicted_label = label_dict[predicted.item()]
    
    return predicted_label

# Define examples as a list
examples = [
    "ChatGPT Plus uses cutting-edge AI technology to learn from customer conversations.",
    "ChatGPT can produce harmful and biased answers.",
    "Gpt dont have feelings or a personal identity, but it strive to provide informative responses.",
]

# Create the Streamlit app
emoji_dict = {
    "positive": "\U0001F60A",
    "negative": "\U0001F61E",
    "neutral": "\U0001F610"
}

st.title("CHAT-GPT SENTIMENT ANALYSIS")

# Create the form to handle user inputs
with st.form("sentiment_analysis_form"):
    # Add the dropdown list for examples
    selected_option = st.selectbox("Select an example to analyze", [""] + examples, index=0)

    # Add the text input for user input
    user_input = st.text_input("Enter your own text to analyze", "")

    # Define color codes for different sentiment classes
    positive_color = "#00C851"
    negative_color = "#ff4444"
    neutral_color = "#FFBB33"

    # Add the submit button to analyze the sentiment
    analyze_button = st.form_submit_button("Analyze")

# Handle the form submission
if analyze_button:
    if user_input.strip() != "":
        prediction = predict(user_input.strip())
        if prediction == 'Positive':
            st.write(f"<span style='color:{positive_color}; font-weight:bold;'>{emoji_dict['positive']} Positive</span>", unsafe_allow_html=True)
        elif prediction == 'Negative':
            st.write(f"<span style='color:{negative_color}; font-weight:bold;'>{emoji_dict['negative']} Negative</span>", unsafe_allow_html=True)
        else:
            st.write(f"<span style='color:{neutral_color}; font-weight:bold;'>{emoji_dict['neutral']} Neutral</span>", unsafe_allow_html=True)
    elif selected_option != "":
        prediction = predict(selected_option)
        if prediction == 'Positive':
            st.write(f"<span style='color:{positive_color}; font-weight:bold;'>{emoji_dict['positive']} Positive</span>", unsafe_allow_html=True)
        elif prediction == 'Negative':
            st.write(f"<span style='color:{negative_color}; font-weight:bold;'>{emoji_dict['negative']} Negative</span>", unsafe_allow_html=True)
        else:
            st.write(f"<span style='color:{neutral_color}; font-weight:bold;'>{emoji_dict['neutral']} Neutral</span>", unsafe_allow_html=True)
    else:
        st.write("Please enter a text or select an example to predict")


st.markdown("""---""")
st.caption("""
    Developed by Applied NLP Research Lab
School of Digital Sciences,
Kerala University of Digital Sciences, Innovation and Technology,
Technopark phase 4, Thiruvananthapuram, India | 
Email: [email protected]   
    <span style='text-align:center; display:block;'>
        https://sites.google.com/duk.ac.in/anlprl
    </span>
""", unsafe_allow_html=True)