File size: 4,856 Bytes
64c47f8 | 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 |
import streamlit as st
# st.set_option('deprecation.showPyplotGlobalUse', False)
from datetime import datetime
# import json
# import os
from textblob import TextBlob
import matplotlib.pyplot as plt
import pandas as pd
from wordcloud import WordCloud
class Journal:
def __init__(self):
self.entries = []
def add_entry(self, timestamp, entry_text):
entry = {"timestamp": timestamp, "entry_text": entry_text}
self.entries.append(entry)
def view_entries(self):
st.header("Journal Entries")
for index, entry in enumerate(self.entries, start=1):
timestamp = entry["timestamp"]
entry_text = entry["entry_text"]
st.write(f"{index}. {timestamp}\n{entry_text}\n")
st.button(f"Delete Entry {index}", key=f"delete_{index}", on_click=self.delete_entry, args=(index-1,))
st.write("------")
def delete_entry(self, index):
if 0 <= index < len(self.entries):
deleted_entry = self.entries.pop(index)
st.write("Entry deleted:")
st.write(deleted_entry)
else:
st.write("Invalid entry index.")
def sentiment_analysis(self):
positive_count = 0
negative_count = 0
neutral_count = 0
for entry in self.entries:
entry_text = entry["entry_text"]
blob = TextBlob(entry_text)
polarity = blob.sentiment.polarity
if polarity > 0.2:
positive_count += 1
elif polarity < -0.2:
negative_count += 1
else:
neutral_count += 1
return positive_count, negative_count, neutral_count
def generate_word_cloud(self):
all_text = " ".join([entry["entry_text"] for entry in self.entries])
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(all_text)
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.title("Word Cloud of Journal Entries")
st.pyplot()
def main():
# Center-aligned title
st.markdown(
"<div style='text-align: center;'>"
"<h1> Your Personal Mental Health Journaling Companion</h1>"
"</div>",
unsafe_allow_html=True
)
# Center-aligned and enlarged subtitle using HTML and CSS
st.markdown(
"<div style='text-align: center;'><h5 style='font-size: 25px;'>"
"Unveil Clarity Within:\n\n 'Discover Healing and Insight through Digital Journaling'"
"</h5></div>",
unsafe_allow_html=True
)
# Check and initialize journal in session state
if "journal" not in st.session_state:
st.session_state.journal = Journal()
# Center-aligned and enlarged text using HTML and CSS
st.markdown(
"<div style='text-align: left; font-size: 20px;'>"
"<i>Your thoughts are the foundation of your strength, and this digital journaling experience is designed to empower you every step of the way.</i>""</div>",
unsafe_allow_html=True
)
entry_text = st.text_area(
"**Take one step closer to healing and enter your daily journal here:**",
key="entry_text"
)
timestamp = datetime.now().strftime("%m-%d %H:%M")
if st.button("Add Entry"):
st.session_state.journal.add_entry(timestamp, entry_text)
st.success("Entry added!")
if st.button("View my Entries"):
st.session_state.journal.view_entries()
st.write("Saved Entries:")
entries_table = []
for index, entry in enumerate(st.session_state.journal.entries, start=1):
timestamp = entry["timestamp"]
entry_text = entry["entry_text"]
entries_table.append((index, timestamp, entry_text))
st.table(entries_table)
search_term = st.text_input("Search Entry:")
filtered_entries = [
(index, timestamp, entry_text)
for index, (index, timestamp, entry_text) in enumerate(entries_table, start=1)
if search_term.lower() in entry_text.lower()
]
if search_term:
st.write("Filtered Entries:")
st.table(filtered_entries)
if st.button("Analyze my Emotions"):
positive_count, negative_count, neutral_count = st.session_state.journal.sentiment_analysis()
sentiment_df = pd.DataFrame({
'Sentiment': ['Positive', 'Negative', 'Neutral'],
'Count': [positive_count, negative_count, neutral_count]
})
st.bar_chart(sentiment_df.set_index('Sentiment'))
if st.button("Create a Visual Representation of my Entry"):
st.session_state.journal.generate_word_cloud()
if __name__ == '__main__':
main()
|