Spaces:
Runtime error
Runtime error
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", "feeling", "feels", "always", "never", "completely", "totally", | |
"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", "hopeless", "helpless", "worthless", | |
"guilty", "ashamed", "miserable", "regret", "despair", "empty", | |
"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", | |
"hopelessness", "uninterested", "disinterested", "unmotivated", | |
"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() |