Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
image2 = Image.open("download.jpg")
|
18 |
+
image3 = Image.open("download (1).jpg")
|
19 |
+
image4 = Image.open("Unhappy_Face_Emoji_Icon_ios10_large.webp")
|
20 |
+
# Functions
|
21 |
+
def main():
|
22 |
+
|
23 |
+
st.title("Covid Tweets Sentiment Analysis NLP App")
|
24 |
+
st.subheader("Team Harmony Project")
|
25 |
+
|
26 |
+
|
27 |
+
# Open the image file
|
28 |
+
st.image(image1)
|
29 |
+
|
30 |
+
# Define the available models
|
31 |
+
models= {
|
32 |
+
"BERT":"Pendo/finetuned-Sentiment-classfication-BERT-model",
|
33 |
+
"RoBERTa":"Pendo/finetuned-Sentiment-classfication-ROBERTA-Base-model",
|
34 |
+
"DistilBERT":"Pendo/finetuned-Sentiment-classfication-DISTILBERT-base-uncased-model"
|
35 |
+
}
|
36 |
+
|
37 |
+
menu = ["Home", "About"]
|
38 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
39 |
+
|
40 |
+
# Add the "How to Use" message to the sidebar
|
41 |
+
st.sidebar.markdown(how_to_use)
|
42 |
+
|
43 |
+
if choice == "Home":
|
44 |
+
st.subheader("Home")
|
45 |
+
|
46 |
+
# Add a dropdown menu to select the model
|
47 |
+
model_name = st.selectbox("Select a model", list(models.keys()))
|
48 |
+
|
49 |
+
with st.form(key="nlpForm"):
|
50 |
+
raw_text = st.text_area("Enter Text Here")
|
51 |
+
submit_button = st.form_submit_button(label="Analyze")
|
52 |
+
|
53 |
+
# Layout
|
54 |
+
col1, col2 = st.columns(2)
|
55 |
+
if submit_button:
|
56 |
+
# Display balloons
|
57 |
+
st.balloons()
|
58 |
+
with col1:
|
59 |
+
st.info("Results")
|
60 |
+
tokenizer = AutoTokenizer.from_pretrained(models[model_name])
|
61 |
+
model = AutoModelForSequenceClassification.from_pretrained(models[model_name])
|
62 |
+
|
63 |
+
# Tokenize the input text
|
64 |
+
inputs = tokenizer(raw_text, return_tensors="pt")
|
65 |
+
|
66 |
+
# Make a forward pass through the model
|
67 |
+
outputs = model(**inputs)
|
68 |
+
|
69 |
+
# Get the predicted class and associated score
|
70 |
+
predicted_class = outputs.logits.argmax().item()
|
71 |
+
score = outputs.logits.softmax(dim=1)[0][predicted_class].item()
|
72 |
+
|
73 |
+
# Compute the scores for all sentiments
|
74 |
+
positive_score = outputs.logits.softmax(dim=1)[0][2].item()
|
75 |
+
negative_score = outputs.logits.softmax(dim=1)[0][0].item()
|
76 |
+
neutral_score = outputs.logits.softmax(dim=1)[0][1].item()
|
77 |
+
|
78 |
+
# Compute the confidence level
|
79 |
+
confidence_level = np.max(outputs.logits.detach().numpy())
|
80 |
+
|
81 |
+
# Print the predicted class and associated score
|
82 |
+
st.write(f"Predicted class: {predicted_class}, Score: {score:.3f}, Confidence Level: {confidence_level:.2f}")
|
83 |
+
|
84 |
+
# Emoji
|
85 |
+
if predicted_class == 2:
|
86 |
+
st.markdown("Sentiment: Positive :smiley:")
|
87 |
+
st.image(image2)
|
88 |
+
elif predicted_class == 1:
|
89 |
+
st.markdown("Sentiment: Neutral :😐:")
|
90 |
+
st.image(image3)
|
91 |
+
else:
|
92 |
+
st.markdown("Sentiment: Negative :angry:")
|
93 |
+
st.image(image4)
|
94 |
+
|
95 |
+
# Create the results DataFrame
|
96 |
+
# Define an empty DataFrame with columns
|
97 |
+
|
98 |
+
results_df = pd.DataFrame(columns=["Sentiment Class", "Score"])
|
99 |
+
|
100 |
+
# Create a DataFrame with scores for all sentiments
|
101 |
+
all_scores_df = pd.DataFrame({
|
102 |
+
'Sentiment Class': ['Positive', 'Negative', 'Neutral'],
|
103 |
+
'Score': [positive_score, negative_score, neutral_score]
|
104 |
+
})
|
105 |
+
|
106 |
+
# Concatenate the two DataFrames
|
107 |
+
|
108 |
+
results_df = pd.concat([results_df, all_scores_df], ignore_index=True)
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
# Create the Altair chart
|
113 |
+
chart = alt.Chart(results_df).mark_bar(width=50).encode(
|
114 |
+
x="Sentiment Class",
|
115 |
+
y="Score",
|
116 |
+
color="Sentiment Class"
|
117 |
+
)
|
118 |
+
|
119 |
+
# Display the chart
|
120 |
+
with col2:
|
121 |
+
st.altair_chart(chart, use_container_width=True)
|
122 |
+
st.write(results_df)
|
123 |
+
|
124 |
+
else:
|
125 |
+
st.subheader("About")
|
126 |
+
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.")
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
+
if __name__ == "__main__":
|
131 |
+
main()
|