Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
+
import emoji
|
6 |
+
|
7 |
+
model_path = "ANLPRL/TBModel"
|
8 |
+
tokenizer_path = "ANLPRL/TBTokenizer"
|
9 |
+
|
10 |
+
# Load the tokenizer and model
|
11 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
|
13 |
+
|
14 |
+
def predict(text):
|
15 |
+
encoded_data = tokenizer.encode_plus(text, padding=True, truncation=True, return_tensors='pt')
|
16 |
+
input_ids = encoded_data['input_ids']
|
17 |
+
attention_mask = encoded_data['attention_mask']
|
18 |
+
with torch.no_grad():
|
19 |
+
outputs = model(input_ids, attention_mask)
|
20 |
+
logits = outputs.logits
|
21 |
+
probabilities = torch.softmax(logits, dim=1)
|
22 |
+
_, predicted = torch.max(probabilities, dim=1)
|
23 |
+
|
24 |
+
# Create dictionary to map numerical labels to categories
|
25 |
+
label_dict = {0: 'Positive', 1: 'Negative', 2: 'Neutral'}
|
26 |
+
predicted_label = label_dict[predicted.item()]
|
27 |
+
|
28 |
+
return predicted_label
|
29 |
+
|
30 |
+
# Define examples as a list
|
31 |
+
examples = [
|
32 |
+
"ChatGPT Plus uses cutting-edge AI technology to learn from customer conversations.",
|
33 |
+
"ChatGPT can produce harmful and biased answers.",
|
34 |
+
"Gpt dont have feelings or a personal identity, but it strive to provide informative responses.",
|
35 |
+
]
|
36 |
+
|
37 |
+
# Create the Streamlit app
|
38 |
+
emoji_dict = {
|
39 |
+
"positive": "\U0001F60A",
|
40 |
+
"negative": "\U0001F61E",
|
41 |
+
"neutral": "\U0001F610"
|
42 |
+
}
|
43 |
+
|
44 |
+
st.title("CHAT-GPT SENTIMENT ANALYSIS")
|
45 |
+
|
46 |
+
# Create the form to handle user inputs
|
47 |
+
with st.form("sentiment_analysis_form"):
|
48 |
+
# Add the dropdown list for examples
|
49 |
+
selected_option = st.selectbox("Select an example to analyze", [""] + examples, index=0)
|
50 |
+
|
51 |
+
# Add the text input for user input
|
52 |
+
user_input = st.text_input("Enter your own text to analyze", "")
|
53 |
+
|
54 |
+
# Define color codes for different sentiment classes
|
55 |
+
positive_color = "#00C851"
|
56 |
+
negative_color = "#ff4444"
|
57 |
+
neutral_color = "#FFBB33"
|
58 |
+
|
59 |
+
# Add the submit button to analyze the sentiment
|
60 |
+
analyze_button = st.form_submit_button("Analyze")
|
61 |
+
|
62 |
+
# Handle the form submission
|
63 |
+
if analyze_button:
|
64 |
+
if user_input.strip() != "":
|
65 |
+
prediction = predict(user_input.strip())
|
66 |
+
if prediction == 'Positive':
|
67 |
+
st.write(f"<span style='color:{positive_color}; font-weight:bold;'>{emoji_dict['positive']} Positive</span>", unsafe_allow_html=True)
|
68 |
+
elif prediction == 'Negative':
|
69 |
+
st.write(f"<span style='color:{negative_color}; font-weight:bold;'>{emoji_dict['negative']} Negative</span>", unsafe_allow_html=True)
|
70 |
+
else:
|
71 |
+
st.write(f"<span style='color:{neutral_color}; font-weight:bold;'>{emoji_dict['neutral']} Neutral</span>", unsafe_allow_html=True)
|
72 |
+
elif selected_option != "":
|
73 |
+
prediction = predict(selected_option)
|
74 |
+
if prediction == 'Positive':
|
75 |
+
st.write(f"<span style='color:{positive_color}; font-weight:bold;'>{emoji_dict['positive']} Positive</span>", unsafe_allow_html=True)
|
76 |
+
elif prediction == 'Negative':
|
77 |
+
st.write(f"<span style='color:{negative_color}; font-weight:bold;'>{emoji_dict['negative']} Negative</span>", unsafe_allow_html=True)
|
78 |
+
else:
|
79 |
+
st.write(f"<span style='color:{neutral_color}; font-weight:bold;'>{emoji_dict['neutral']} Neutral</span>", unsafe_allow_html=True)
|
80 |
+
else:
|
81 |
+
st.write("Please enter a text or select an example to predict")
|
82 |
+
|
83 |
+
|
84 |
+
st.markdown("""---""")
|
85 |
+
st.caption("""
|
86 |
+
Developed by Applied NLP Research Lab
|
87 |
+
School of Digital Sciences,
|
88 |
+
Kerala University of Digital Sciences, Innovation and Technology,
|
89 |
+
Technopark phase 4, Thiruvananthapuram, India |
|
90 |
+
Email: anlprl.duk@gmail.com
|
91 |
+
<span style='text-align:center; display:block;'>
|
92 |
+
https://sites.google.com/duk.ac.in/anlprl
|
93 |
+
</span>
|
94 |
+
""", unsafe_allow_html=True)
|