|
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 |
|
|
|
|
|
|
|
analyzer = PretrainedSentimentAnalyzer(None, None, None, None) |
|
|
|
def main(): |
|
st.title("Early Depression Detection System") |
|
|
|
|
|
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:") |
|
|
|
|
|
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']) |
|
|
|
if sentiment['label'] == 'negative': |
|
st.write(scan(text)) |
|
st.write("Please call the helpline number:") |
|
st.write("Beyond Blue: 1300 659 467") |
|
st.write("Mental Health Emergency: 13 14 65") |
|
else: |
|
st.write("No Depression Detected") |
|
elif option == "Upload a CSV file": |
|
file = st.file_uploader("Upload a CSV file:") |
|
if file is not None: |
|
|
|
if file.name.endswith('.csv'): |
|
|
|
df = pd.read_csv(file) |
|
if st.button("Analyze"): |
|
|
|
|
|
|
|
|
|
|
|
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] |
|
|
|
|
|
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'] |
|
|
|
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'] |
|
|
|
tweets_data = negative_data['Tweets'] |
|
st.write("Depressed Tweets") |
|
st.write(scan(tweets_data)) |
|
st.write("Please call the helpline number:") |
|
st.write("Beyond Blue: 1300 659 467") |
|
st.write("Mental Health Emergency: 13 14 65") |
|
else: |
|
st.write("File type not supported") |
|
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", "feeling", "feels", "always", "never", "completely", "totally", |
|
"absolutely", "must", "should", "all", "every", "none", "nothing", |
|
"everyone", "everything", "only", "impossible", "forever", "can’t", |
|
"cannot", "won’t", "will not", "no one", "no-one", "every time", |
|
"low", "everytime", "difficult", "difficulty", "nightmare", "bored", |
|
"disaster", "irritate", "broken", "hurt", "lost", "book", "alone", |
|
"journal", "tool", "fight", "highway", "stupid", "disgusted", "stress", |
|
"hostile", "reserved", "danger", "funeral", "respect", "news", "toothache", |
|
"vomit", "sick", "beach", "office", "window", "phase", "plant", "hotel", |
|
"friend", "teacher", "betray", "poster", "grief", "puppy", "safe", "kiss", |
|
"home", "treat", "confident", "peace", "paper", "custom", "rain", "lucky", |
|
"win", "proud", "beauty", "city", "museum", "success", "laughter", "party", |
|
"key", "justice", "respect", "sorry", "apologize", "kill","help"] |
|
|
|
|
|
|
|
contractions_re=re.compile('(%s)' % '|'.join(contractions_dict.keys())) |
|
|
|
|
|
def expand_contractions(text,contractions_dict=contractions_dict): |
|
def replace(match): |
|
return contractions_dict[match.group(0)] |
|
return contractions_re.sub(replace, text) |
|
|
|
|
|
def preprocess_text(text): |
|
|
|
text = str(text) |
|
|
|
text = expand_contractions(text) |
|
|
|
text = re.sub(r'[^\w\s]', '', text) |
|
|
|
text = text.lower() |
|
|
|
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: |
|
break |
|
return("Depression Detected") |
|
else: |
|
|
|
preprocessed_tweets = tweets_data.apply(preprocess_text) |
|
print(preprocessed_tweets) |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
main() |