create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
import plotly.express as px
|
5 |
+
import plotly.io as pio
|
6 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
7 |
+
from PIL import Image
|
8 |
+
import base64
|
9 |
+
|
10 |
+
|
11 |
+
# Define the "How to Use" message
|
12 |
+
how_to_use = """
|
13 |
+
**How to Use**
|
14 |
+
1. Select a model from the dropdown menu
|
15 |
+
2. Enter text in the text area
|
16 |
+
3. Click the 'Analyze' button to get the predicted sentiment of the text
|
17 |
+
"""
|
18 |
+
|
19 |
+
# Functions
|
20 |
+
def main():
|
21 |
+
st.title("Covid Tweets Sentiment Analysis NLP App")
|
22 |
+
st.subheader("Team Harmony Project")
|
23 |
+
|
24 |
+
# Add the cover image
|
25 |
+
#st.markdown(f'<img src="data:image/jpeg;base64,{image_base64}" alt="Cover Image">', unsafe_allow_html=True)
|
26 |
+
|
27 |
+
st.image("Cover_image.jpg")
|
28 |
+
|
29 |
+
# Define the available models
|
30 |
+
models = {
|
31 |
+
"ROBERTA": "Abubakari/finetuned-Sentiment-classfication-ROBERTA-model",
|
32 |
+
"BERT": "Abubakari/finetuned-Sentiment-classfication-BERT-model",
|
33 |
+
"DISTILBERT": "Abubakari/finetuned-Sentiment-classfication-DISTILBERT-model"
|
34 |
+
}
|
35 |
+
|
36 |
+
menu = ["Home", "About"]
|
37 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
38 |
+
|
39 |
+
# Add the "How to Use" message to the sidebar
|
40 |
+
st.sidebar.markdown(how_to_use)
|
41 |
+
|
42 |
+
if choice == "Home":
|
43 |
+
st.subheader("Home")
|
44 |
+
|
45 |
+
# Add a dropdown menu to select the model
|
46 |
+
model_name = st.selectbox("Select a model", list(models.keys()))
|
47 |
+
|
48 |
+
with st.form(key="nlpForm"):
|
49 |
+
raw_text = st.text_area("Enter Text Here")
|
50 |
+
submit_button = st.form_submit_button(label="Analyze")
|
51 |
+
|
52 |
+
# Layout
|
53 |
+
col1, col2 = st.columns(2)
|
54 |
+
if submit_button:
|
55 |
+
# Display balloons
|
56 |
+
st.balloons()
|
57 |
+
with col1:
|
58 |
+
st.info("Results")
|
59 |
+
tokenizer = AutoTokenizer.from_pretrained(models[model_name])
|
60 |
+
model = AutoModelForSequenceClassification.from_pretrained(models[model_name])
|
61 |
+
|
62 |
+
# Tokenize the input text
|
63 |
+
inputs = tokenizer(raw_text, return_tensors="pt")
|
64 |
+
|
65 |
+
# Make a forward pass through the model
|
66 |
+
outputs = model(**inputs)
|
67 |
+
|
68 |
+
# Get the predicted class and associated score
|
69 |
+
predicted_class = outputs.logits.argmax().item()
|
70 |
+
score = outputs.logits.softmax(dim=1)[0][predicted_class].item()
|
71 |
+
|
72 |
+
# Compute the scores for all sentiments
|
73 |
+
positive_score = outputs.logits.softmax(dim=1)[0][2].item()
|
74 |
+
negative_score = outputs.logits.softmax(dim=1)[0][0].item()
|
75 |
+
neutral_score = outputs.logits.softmax(dim=1)[0][1].item()
|
76 |
+
|
77 |
+
# Compute the confidence level
|
78 |
+
confidence_level = np.max(outputs.logits.detach().numpy())
|
79 |
+
|
80 |
+
# Print the predicted class and associated score
|
81 |
+
st.write(f"Predicted class: {predicted_class}, Score: {score:.3f}, Confidence Level: {confidence_level:.2f}")
|
82 |
+
|
83 |
+
# Emoji
|
84 |
+
if predicted_class == 2:
|
85 |
+
st.markdown("Sentiment: Positive :smiley:")
|
86 |
+
st.image("Positive_sentiment.jpg")
|
87 |
+
elif predicted_class == 1:
|
88 |
+
st.markdown("Sentiment: Neutral :π:")
|
89 |
+
st.image("Neutral_sentiment.jpg")
|
90 |
+
else:
|
91 |
+
st.markdown("Sentiment: Negative :angry:")
|
92 |
+
st.image("Negative_sentiment2.png")
|
93 |
+
|
94 |
+
# Create the results DataFrame
|
95 |
+
# Define an empty DataFrame with columns
|
96 |
+
|
97 |
+
results_df = pd.DataFrame(columns=["Sentiment Class", "Score"])
|
98 |
+
|
99 |
+
# Create a DataFrame with scores for all sentiments
|
100 |
+
all_scores_df = pd.DataFrame({
|
101 |
+
'Sentiment Class': ['Positive', 'Negative', 'Neutral'],
|
102 |
+
'Score': [positive_score, negative_score, neutral_score]
|
103 |
+
})
|
104 |
+
|
105 |
+
# Concatenate the two DataFrames
|
106 |
+
|
107 |
+
results_df = pd.concat([results_df, all_scores_df], ignore_index=True)
|
108 |
+
|
109 |
+
#results_df = pd.DataFrame({
|
110 |
+
# "Sentiment Class": [predicted_class],
|
111 |
+
# "Score": [score]
|
112 |
+
#})
|
113 |
+
|
114 |
+
# Create the Plotly figure
|
115 |
+
fig = px.bar(results_df, x="Sentiment Class", y="Score", color="Sentiment Class")
|
116 |
+
|
117 |
+
with col2:
|
118 |
+
st.plotly_chart(fig, 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()
|