File size: 13,585 Bytes
fb07a8f dfd241e fb07a8f dfd241e 3f7aba3 fb07a8f a3a6185 fb07a8f a3a6185 fb07a8f a3a6185 dfd241e 56af049 fb07a8f |
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
import streamlit as st
from sentiment_model import PretrainedSentimentAnalyzer
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import pandas as pd
import re
# Create an instance of the PretrainedSentimentAnalyzer class
analyzer = PretrainedSentimentAnalyzer(None, None, None, None)
# Define the Streamlit app
def main():
st.title("Early Depression Detection System")
# Get user input
option = st.selectbox("Select an option:", ("Enter a sentence", "Upload a CSV file"))
if option == "Enter a sentence":
text = st.text_input("Enter a sentence:")
# Perform sentiment analysis
if st.button("Analyze"):
if not text or len(text) == 1:
st.write("Enter valid text")
else:
sentiment = analyzer.predict([text], inverse_transform=True)[0]
st.write("Sentiment Analysis Results:")
st.write("Sentiment:", sentiment['label'])
#st.write("Score:", sentiment['score'])
if sentiment['label'] == 'negative':
if scan(text) == "Depression Detected":
st.write("Depression Detected")
helplines_message = """
If you or your loved ones are feeling depressed, please reach out to these helplines:
- **Beyond Blue**: 1300 659 467
- **Mental Health Emergency**: 13 14 65
"""
st.error(helplines_message)
else:
st.success("No Depression Detected")
else:
st.success("No Depression Detected")
elif option == "Upload a CSV file":
file = st.file_uploader("Upload a CSV file:")
if file is not None:
# Check file format
if file.name.endswith('.csv'):
# Perform sentiment analysis on the uploaded file
df = pd.read_csv(file)
if st.button("Analyze"):
# with st.spinner("Predicting..."):
# column = df.columns[1]
# data = df[column].astype(str).tolist()
# sentiments = analyzer.predict(data, inverse_transform=True)
# df['sentiment'] = [s['label'] for s in sentiments]
spinner_placeholder = st.markdown("""
<style>
.spinner-container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.spinner {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 100px;
height: 100px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
}
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<div class="spinner-container">
<div class="spinner"></div>
<p>Predicting...</p>
</div>
""", unsafe_allow_html=True)
column = df.columns[1]
data = df[column].astype(str).tolist()
sentiments = analyzer.predict(data, inverse_transform=True)
df['Prediction'] = [s['label'] for s in sentiments]
# Clear the spinner
spinner_placeholder.empty()
st.write("Sentiment Analysis Results:")
st.write(df)
sentiment_counts = df['Prediction'].value_counts()
sentiment_dict = {'Positive': 0, 'Neutral': 0, 'Negative': 0}
for sentiment in sentiment_counts.index:
if sentiment == 'positive':
sentiment_dict['Positive'] += sentiment_counts[sentiment]
elif sentiment == 'neutral':
sentiment_dict['Neutral'] += sentiment_counts[sentiment]
elif sentiment == 'negative':
sentiment_dict['Negative'] += sentiment_counts[sentiment]
st.write("Sentiment Counts:")
for sentiment, count in sentiment_dict.items():
st.write(sentiment + ":", count)
labels = list(sentiment_dict.keys())
sizes = list(sentiment_dict.values())
colors = ['green', 'white', 'red'] # colors for Positive, Neutral, Negative
fig = go.Figure(data=[go.Pie(labels=labels, values=sizes,hole=.2, marker=dict(colors=colors))])
fig.update_layout(
title="Sentiment Analysis",
showlegend=True,
legend_title="Sentiment",
uniformtext_minsize=12,
uniformtext_mode='hide'
)
st.plotly_chart(fig)
negative_data = df[df['Prediction'] == 'negative']
# Create a df that only has the tweets column
tweets_data = negative_data['Tweets']
depressed_df=scan(tweets_data)
if depressed_df.empty:
st.success("None of the Comments in the Dataset are depressed")
else:
st.write("Depressed Comments")
st.write(depressed_df)
helplines_message = """
I found these tweets to be depressed, please reach out to the below helplines for help:
- **Beyond Blue**: 1300 659 467
- **Mental Health Emergency**: 13 14 65
"""
st.error(helplines_message)
def scan(tweets_data):
contractions_dict = { "ain't": "are not","'s":" is","aren't": "are not",
"can't": "cannot","can't've": "cannot have",
"'cause": "because","could've": "could have","couldn't": "could not",
"couldn't've": "could not have", "didn't": "did not","doesn't": "does not",
"don't": "do not","hadn't": "had not","hadn't've": "had not have",
"hasn't": "has not","haven't": "have not","he'd": "he would",
"he'd've": "he would have","he'll": "he will", "he'll've": "he will have",
"how'd": "how did","how'd'y": "how do you","how'll": "how will",
"I'd": "I would", "I'd've": "I would have","I'll": "I will",
"I'll've": "I will have","I'm": "I am","I've": "I have", "isn't": "is not",
"it'd": "it would","it'd've": "it would have","it'll": "it will",
"it'll've": "it will have", "let's": "let us","ma'am": "madam",
"mayn't": "may not","might've": "might have","mightn't": "might not",
"mightn't've": "might not have","must've": "must have","mustn't": "must not",
"mustn't've": "must not have", "needn't": "need not",
"needn't've": "need not have","o'clock": "of the clock","oughtn't": "ought not",
"oughtn't've": "ought not have","shan't": "shall not","sha'n't": "shall not",
"shan't've": "shall not have","she'd": "she would","she'd've": "she would have",
"she'll": "she will", "she'll've": "she will have","should've": "should have",
"shouldn't": "should not", "shouldn't've": "should not have","so've": "so have",
"that'd": "that would","that'd've": "that would have", "there'd": "there would",
"there'd've": "there would have", "they'd": "they would",
"they'd've": "they would have","they'll": "they will",
"they'll've": "they will have", "they're": "they are","they've": "they have",
"to've": "to have","wasn't": "was not","we'd": "we would",
"we'd've": "we would have","we'll": "we will","we'll've": "we will have",
"we're": "we are","we've": "we have", "weren't": "were not","what'll": "what will",
"what'll've": "what will have","what're": "what are", "what've": "what have",
"when've": "when have","where'd": "where did", "where've": "where have",
"who'll": "who will","who'll've": "who will have","who've": "who have",
"why've": "why have","will've": "will have","won't": "will not",
"won't've": "will not have", "would've": "would have","wouldn't": "would not",
"wouldn't've": "would not have","y'all": "you all", "y'all'd": "you all would",
"y'all'd've": "you all would have","y'all're": "you all are",
"y'all've": "you all have","you'd": "you would","you'd've": "you would have",
"you'll": "you will","you'll've": "you will have", "you're": "you are",
"you've": "you have"}
absolute_words = {
"I","me","my","mine", "feeling","life", "feels", "always",
"absolutely", "must", "should", "all", "every", "none", "nothing",
"everyone", "everything", "only", "impossible", "forever", "can not",
"cannot", "won’t", "will not", "no one", "no-one", "every time",
"low", "everytime", "difficult", "difficulty", "nightmare", "bored",
"disaster", "irritate", "broken", "hurt", "lost", "love", "alone",
"stupid", "disgusted", "stress", "hostile", "reserved", "danger",
"funeral", "respect", "vomit", "sick", "phase", "suffer", "suffering",
"betray", "poster", "grief", "safe", "home", "treat",
"confident", "peace", "lucky", "win",
"proud", "beautiful","please","pleaded", "success", "laughing","laugh", "party",
"key", "justice", "sorry", "apologize", "kill", "help", "myself",
"depress", "depressed","depression", "hopeless", "helpless", "worthless",
"guilty", "ashamed", "miserable", "regret", "despair", "empty","shit",
"numb", "withdrawn", "lonely", "fatigued","fatigue","cry", "exhausted", "overwhelmed",
"suicidal", "self-harm", "panic", "anxiety", "trauma", "ptsd",
"bipolar", "disorder", "breakup", "divorce", "loss", "unemployed",
"fired", "bullied", "abused", "neglected", "failure", "inferior",
"insecure", "ugly", "unloved", "unwanted", "loser", "pessimistic",
"pointless", "meaningless", "purposeless", "directionless", "ending",
"hopelessness", "uninterested", "disinterested", "unmotivated", "die",
"apathetic", "indifferent", "worthlessness", "guilt", "shame","crying","cry","cried","sadness","sad","sorrow"}
# Regular expression for finding contractions
contractions_re=re.compile('(%s)' % '|'.join(contractions_dict.keys()))
# Function for expanding contractions
def expand_contractions(text,contractions_dict=contractions_dict):
def replace(match):
return contractions_dict[match.group(0)]
return contractions_re.sub(replace, text)
# Preprocessing function
def preprocess_text(text):
# Convert the input to a string
text = str(text)
# Expand contractions
text = expand_contractions(text)
# Remove punctuations
text = re.sub(r'[^\w\s]', '', text)
# Convert to lowercase
text = text.lower()
# If the resulting text is empty, return None
if text == "":
return None
return text
if isinstance(tweets_data, str):
preprocessed_tweets=preprocess_text(tweets_data)
count = 0
for word in absolute_words:
if word in preprocessed_tweets:
count += 1
if count >= 2:
return("Depression Detected")
break
if count<2:
return("No Depression Detected")
else:
# Apply the preprocess_text function to the tweet df
preprocessed_tweets = tweets_data.apply(preprocess_text)
print(preprocessed_tweets)
# Check if the preprocessed tweets have at least 2 of the absolute words
Early_dep = pd.DataFrame(columns=['Tweets'])
for tweet in preprocessed_tweets:
count = 0
for word in absolute_words:
if word in tweet:
count += 1
if count >= 2:
Early_dep = pd.concat([Early_dep, pd.DataFrame({'Tweets': [tweet]})], ignore_index=True)
break
return(Early_dep)
# Run the app
if __name__ == "__main__":
main() |