Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,97 +1,128 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
|
3 |
|
4 |
-
# Define the
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
def preprocess(text):
|
16 |
-
new_text = []
|
17 |
-
# Replace user mentions with '@user'
|
18 |
-
for t in text.split(" "):
|
19 |
-
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
20 |
-
# Replace links with 'http'
|
21 |
-
t = 'http' if t.startswith('http') else t
|
22 |
-
new_text.append(t)
|
23 |
-
# Join the preprocessed text
|
24 |
-
return " ".join(new_text)
|
25 |
-
|
26 |
-
# Define a function to perform sentiment analysis on the input text using model 1
|
27 |
-
def sentiment_analysis_model1(text):
|
28 |
-
# Preprocess the input text
|
29 |
-
text = preprocess(text)
|
30 |
-
|
31 |
-
# Tokenize the input text using the pre-trained tokenizer
|
32 |
-
encoded_input = tokenizer1(text, return_tensors='pt')
|
33 |
-
|
34 |
-
# Feed the tokenized input to the pre-trained model and obtain output
|
35 |
-
output = model1(**encoded_input)
|
36 |
-
|
37 |
-
# Obtain the prediction scores for the output
|
38 |
-
scores_ = output[0][0].detach().numpy()
|
39 |
-
|
40 |
-
# Apply softmax activation function to obtain probability distribution over the labels
|
41 |
-
scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
46 |
|
47 |
-
# Return the scores
|
48 |
-
return scores
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
# Preprocess the input text
|
53 |
-
text = preprocess(text)
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
# Obtain the prediction scores for the output
|
62 |
-
scores_ = output[0][0].detach().numpy()
|
63 |
-
|
64 |
-
# Apply softmax activation function to obtain probability distribution over the labels
|
65 |
-
scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
|
66 |
-
|
67 |
-
# Format the output dictionary with the predicted scores
|
68 |
-
labels = ['Negative', 'Neutral', 'Positive']
|
69 |
-
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
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 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
import streamlit as st
|
4 |
+
import altair as alt
|
5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
6 |
+
from PIL import Image
|
7 |
+
import base64
|
8 |
|
9 |
+
# Define the "How to Use" message
|
10 |
+
how_to_use = """
|
11 |
+
**How to Use**
|
12 |
+
1. Select a model from the dropdown menu
|
13 |
+
2. Enter text in the text area
|
14 |
+
3. Click the 'Analyze' button to get the predicted sentiment of the text
|
15 |
+
"""
|
16 |
+
image1 = Image.open("sentiment analysis.jpg")
|
17 |
+
|
18 |
+
# Functions
|
19 |
+
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
st.title("Covid Tweets Sentiment Analysis NLP App")
|
22 |
+
st.subheader("Team Harmony Project")
|
|
|
23 |
|
|
|
|
|
24 |
|
25 |
+
# Open the image file
|
26 |
+
st.image(image1)
|
|
|
|
|
27 |
|
28 |
+
# Define the available models
|
29 |
+
models= {
|
30 |
+
"RoBERTa":"saisi/finetuned-Sentiment-classfication-ROBERTA-model",
|
31 |
+
"DistilBERT":"saisi/finetuned-Sentiment-classfication-DISTILBERT-model"
|
32 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
menu = ["Home", "About"]
|
35 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
36 |
+
|
37 |
+
# Add the "How to Use" message to the sidebar
|
38 |
+
st.sidebar.markdown(how_to_use)
|
39 |
+
|
40 |
+
if choice == "Home":
|
41 |
+
st.subheader("Home")
|
42 |
+
|
43 |
+
# Add a dropdown menu to select the model
|
44 |
+
model_name = st.selectbox("Select a model", list(models.keys()))
|
45 |
+
|
46 |
+
with st.form(key="nlpForm"):
|
47 |
+
raw_text = st.text_area("Enter Text Here")
|
48 |
+
submit_button = st.form_submit_button(label="Analyze")
|
49 |
+
|
50 |
+
# Layout
|
51 |
+
col1, col2 = st.columns(2)
|
52 |
+
if submit_button:
|
53 |
+
# Display balloons
|
54 |
+
st.balloons()
|
55 |
+
with col1:
|
56 |
+
st.info("Results")
|
57 |
+
tokenizer = AutoTokenizer.from_pretrained(models[model_name])
|
58 |
+
model = AutoModelForSequenceClassification.from_pretrained(models[model_name])
|
59 |
+
|
60 |
+
# Tokenize the input text
|
61 |
+
inputs = tokenizer(raw_text, return_tensors="pt")
|
62 |
+
|
63 |
+
# Make a forward pass through the model
|
64 |
+
outputs = model(**inputs)
|
65 |
+
|
66 |
+
# Get the predicted class and associated score
|
67 |
+
predicted_class = outputs.logits.argmax().item()
|
68 |
+
score = outputs.logits.softmax(dim=1)[0][predicted_class].item()
|
69 |
+
|
70 |
+
# Compute the scores for all sentiments
|
71 |
+
positive_score = outputs.logits.softmax(dim=1)[0][2].item()
|
72 |
+
negative_score = outputs.logits.softmax(dim=1)[0][0].item()
|
73 |
+
neutral_score = outputs.logits.softmax(dim=1)[0][1].item()
|
74 |
+
|
75 |
+
# Compute the confidence level
|
76 |
+
confidence_level = np.max(outputs.logits.detach().numpy())
|
77 |
+
|
78 |
+
# Print the predicted class and associated score
|
79 |
+
st.write(f"Predicted class: {predicted_class}, Score: {score:.3f}, Confidence Level: {confidence_level:.2f}")
|
80 |
+
|
81 |
+
# Emoji
|
82 |
+
if predicted_class == 2:
|
83 |
+
st.markdown("Sentiment: Positive :smiley:")
|
84 |
+
st.image(image2)
|
85 |
+
elif predicted_class == 1:
|
86 |
+
st.markdown("Sentiment: Neutral :π:")
|
87 |
+
st.image(image3)
|
88 |
+
else:
|
89 |
+
st.markdown("Sentiment: Negative :angry:")
|
90 |
+
st.image(image4)
|
91 |
+
|
92 |
+
# Create the results DataFrame
|
93 |
+
# Define an empty DataFrame with columns
|
94 |
+
|
95 |
+
results_df = pd.DataFrame(columns=["Sentiment Class", "Score"])
|
96 |
+
|
97 |
+
# Create a DataFrame with scores for all sentiments
|
98 |
+
all_scores_df = pd.DataFrame({
|
99 |
+
'Sentiment Class': ['Positive', 'Negative', 'Neutral'],
|
100 |
+
'Score': [positive_score, negative_score, neutral_score]
|
101 |
+
})
|
102 |
+
|
103 |
+
# Concatenate the two DataFrames
|
104 |
+
|
105 |
+
results_df = pd.concat([results_df, all_scores_df], ignore_index=True)
|
106 |
+
|
107 |
+
|
108 |
+
|
109 |
+
# Create the Altair chart
|
110 |
+
chart = alt.Chart(results_df).mark_bar(width=50).encode(
|
111 |
+
x="Sentiment Class",
|
112 |
+
y="Score",
|
113 |
+
color="Sentiment Class"
|
114 |
+
)
|
115 |
+
|
116 |
+
# Display the chart
|
117 |
+
with col2:
|
118 |
+
st.altair_chart(chart, use_container_width=True)
|
119 |
+
st.write(results_df)
|
120 |
+
|
121 |
+
else:
|
122 |
+
st.subheader("About")
|
123 |
+
st.write("This is a sentiment analysis NLP app developed by Team Harmony for analyzing tweets related to Covid-19.It uses a pre-trained model to predict the sentiment of the input text. The app is part of a project to promote teamwork and collaboration among developers.")
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
if __name__ == "__main__":
|
128 |
+
main()
|