Spaces:
Sleeping
Sleeping
Update journal_utils.py
Browse files- journal_utils.py +79 -14
journal_utils.py
CHANGED
|
@@ -3,6 +3,10 @@ from datetime import datetime
|
|
| 3 |
from collections import Counter
|
| 4 |
from fpdf import FPDF
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# --- Logging ---
|
| 8 |
def log_chat_interaction(user_msg, ai_reply, mood):
|
|
@@ -57,25 +61,86 @@ def get_weekly_summary():
|
|
| 57 |
"frequent_words": Counter(" ".join(x["entry"] for x in logs).split()).most_common(7)
|
| 58 |
}
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def generate_emotion_map():
|
| 61 |
-
if not os.path.exists("journal_log.json"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
with open("journal_log.json") as f:
|
| 63 |
logs = json.load(f)
|
|
|
|
| 64 |
moods = [x["mood"] for x in logs]
|
| 65 |
mood_count = Counter(moods)
|
|
|
|
|
|
|
| 66 |
plt.bar(mood_count.keys(), mood_count.values())
|
| 67 |
plt.title("Mood Trend")
|
| 68 |
-
plt.
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
def get_counselor_view():
|
| 72 |
-
if not os.path.exists("journal_log.json"): return "No logs yet."
|
| 73 |
-
with open("journal_log.json") as f:
|
| 74 |
-
logs = json.load(f)
|
| 75 |
-
insights = {
|
| 76 |
-
"Total Journals": len(logs),
|
| 77 |
-
"Mood Counts": Counter(x["mood"] for x in logs),
|
| 78 |
-
"Flags (e.g., 'hopeless')": sum("hopeless" in x["entry"].lower() for x in logs),
|
| 79 |
-
"Flags (e.g., 'tired')": sum("tired" in x["entry"].lower() for x in logs)
|
| 80 |
-
}
|
| 81 |
-
return "\n".join([f"{k}: {v}" for k, v in insights.items()])
|
|
|
|
| 3 |
from collections import Counter
|
| 4 |
from fpdf import FPDF
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
+
import os
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from collections import Counter
|
| 9 |
+
|
| 10 |
|
| 11 |
# --- Logging ---
|
| 12 |
def log_chat_interaction(user_msg, ai_reply, mood):
|
|
|
|
| 61 |
"frequent_words": Counter(" ".join(x["entry"] for x in logs).split()).most_common(7)
|
| 62 |
}
|
| 63 |
|
| 64 |
+
|
| 65 |
+
from collections import Counter
|
| 66 |
+
import json
|
| 67 |
+
import os
|
| 68 |
+
|
| 69 |
+
def get_counselor_view():
|
| 70 |
+
journal_entries = []
|
| 71 |
+
chat_entries = []
|
| 72 |
+
|
| 73 |
+
if os.path.exists("journal_log.json"):
|
| 74 |
+
with open("journal_log.json") as f:
|
| 75 |
+
journal_entries = json.load(f)
|
| 76 |
+
|
| 77 |
+
if os.path.exists("chat_log.json"):
|
| 78 |
+
with open("chat_log.json") as f:
|
| 79 |
+
chat_entries = json.load(f)
|
| 80 |
+
|
| 81 |
+
combined_texts = []
|
| 82 |
+
for j in journal_entries:
|
| 83 |
+
combined_texts.append(j["entry"])
|
| 84 |
+
combined_texts.append(j["response"])
|
| 85 |
+
|
| 86 |
+
for c in chat_entries:
|
| 87 |
+
combined_texts.append(c["user"])
|
| 88 |
+
combined_texts.append(c["ai"])
|
| 89 |
+
|
| 90 |
+
all_text = " ".join(combined_texts).lower()
|
| 91 |
+
|
| 92 |
+
keywords_of_concern = ["tired", "hopeless", "alone", "worthless", "angry", "burnout", "anxious"]
|
| 93 |
+
flags = {kw: all_text.count(kw) for kw in keywords_of_concern}
|
| 94 |
+
|
| 95 |
+
moods = [j["mood"] for j in journal_entries] + [c["mood"] for c in chat_entries]
|
| 96 |
+
mood_counts = Counter(moods)
|
| 97 |
+
|
| 98 |
+
return f"""
|
| 99 |
+
🧠 Counselor Dashboard Summary
|
| 100 |
+
|
| 101 |
+
📝 Total Journal Entries: {len(journal_entries)}
|
| 102 |
+
💬 Total Conversations: {len(chat_entries)}
|
| 103 |
+
|
| 104 |
+
📊 Mood Distribution:
|
| 105 |
+
{dict(mood_counts)}
|
| 106 |
+
|
| 107 |
+
⚠️ Flagged Terms Detected:
|
| 108 |
+
{flags}
|
| 109 |
+
|
| 110 |
+
🧩 Top Observations:
|
| 111 |
+
- Watch for spikes in fatigue, hopelessness, or anger.
|
| 112 |
+
- Consistent use of certain terms may suggest emotional load.
|
| 113 |
+
- Consider encouraging journaling, breathing, or referring to a professional if >3 flags occur.
|
| 114 |
+
|
| 115 |
+
"""
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
import os
|
| 119 |
+
import matplotlib.pyplot as plt
|
| 120 |
+
from collections import Counter
|
| 121 |
+
|
| 122 |
def generate_emotion_map():
|
| 123 |
+
if not os.path.exists("journal_log.json"):
|
| 124 |
+
return None
|
| 125 |
+
|
| 126 |
+
# ✅ Ensure the folder exists
|
| 127 |
+
os.makedirs("assets", exist_ok=True)
|
| 128 |
+
|
| 129 |
with open("journal_log.json") as f:
|
| 130 |
logs = json.load(f)
|
| 131 |
+
|
| 132 |
moods = [x["mood"] for x in logs]
|
| 133 |
mood_count = Counter(moods)
|
| 134 |
+
|
| 135 |
+
plt.figure(figsize=(6, 4))
|
| 136 |
plt.bar(mood_count.keys(), mood_count.values())
|
| 137 |
plt.title("Mood Trend")
|
| 138 |
+
plt.xlabel("Mood")
|
| 139 |
+
plt.ylabel("Frequency")
|
| 140 |
+
plt.tight_layout()
|
| 141 |
+
|
| 142 |
+
save_path = "assets/emotion_map.png"
|
| 143 |
+
plt.savefig(save_path)
|
| 144 |
+
plt.close()
|
| 145 |
+
return save_path
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|