Spaces:
Sleeping
Sleeping
Update agents.py
Browse files
agents.py
CHANGED
|
@@ -1,100 +1,81 @@
|
|
| 1 |
-
import os, json
|
| 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 |
-
The user says: "{message}"
|
| 60 |
-
Their mood is: {mood}
|
| 61 |
-
|
| 62 |
-
Offer a kind and short reflection. Suggest something gentle like a shift in perspective, or a small action.
|
| 63 |
-
Never use words like therapy, CBT, mental health, or psychology.
|
| 64 |
-
"""
|
| 65 |
-
return call_groq(prompt, model=LLAMA_MODEL)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if not os.path.exists("journal_log.json"): return []
|
| 70 |
with open("journal_log.json") as f:
|
| 71 |
logs = json.load(f)
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
Reflect back to the user with kind interpretation, help them notice something they may have missed.
|
| 89 |
-
End with a gentle affirmation or reflective nudge.
|
| 90 |
-
|
| 91 |
-
Avoid therapy talk.
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
response = call_groq(prompt, model=LLAMA_MODEL)
|
| 95 |
-
return {
|
| 96 |
-
"entry": entry,
|
| 97 |
-
"mood": mood,
|
| 98 |
-
"mode": mode,
|
| 99 |
-
"response": response
|
| 100 |
}
|
|
|
|
|
|
| 1 |
+
import os, json
|
| 2 |
+
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):
|
| 9 |
+
file = "chat_log.json"
|
| 10 |
+
logs = []
|
| 11 |
+
if os.path.exists(file):
|
| 12 |
+
with open(file) as f: logs = json.load(f)
|
| 13 |
+
logs.append({
|
| 14 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 15 |
+
"mood": mood,
|
| 16 |
+
"user": user_msg,
|
| 17 |
+
"ai": ai_reply
|
| 18 |
+
})
|
| 19 |
+
with open(file, "w") as f: json.dump(logs, f, indent=2)
|
| 20 |
+
|
| 21 |
+
def log_entry(entry_obj):
|
| 22 |
+
file = "journal_log.json"
|
| 23 |
+
logs = []
|
| 24 |
+
if os.path.exists(file):
|
| 25 |
+
with open(file) as f: logs = json.load(f)
|
| 26 |
+
logs.append({
|
| 27 |
+
**entry_obj,
|
| 28 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 29 |
+
})
|
| 30 |
+
with open(file, "w") as f: json.dump(logs, f, indent=2)
|
| 31 |
+
|
| 32 |
+
# --- Export ---
|
| 33 |
+
def export_to_pdf(entry, mood, mode):
|
| 34 |
+
pdf = FPDF()
|
| 35 |
+
pdf.add_page()
|
| 36 |
+
pdf.set_font("Arial", size=12)
|
| 37 |
+
pdf.multi_cell(0, 10, f"Journaling Mode: {mode}\nMood: {mood}\nEntry:\n{entry}")
|
| 38 |
+
path = "journal_export.pdf"
|
| 39 |
+
pdf.output(path)
|
| 40 |
+
return path
|
| 41 |
+
|
| 42 |
+
def export_to_md(entry, mood, mode):
|
| 43 |
+
text = f"### Journal Entry\n**Mode**: {mode}\n**Mood**: {mood}\n\n{entry}"
|
| 44 |
+
path = "journal_export.md"
|
| 45 |
+
with open(path, "w") as f: f.write(text)
|
| 46 |
+
return path
|
| 47 |
+
|
| 48 |
+
# --- Counselor Analytics ---
|
| 49 |
+
def get_weekly_summary():
|
| 50 |
+
if not os.path.exists("journal_log.json"):
|
| 51 |
+
return {"error": "No logs yet."}
|
| 52 |
+
with open("journal_log.json") as f:
|
| 53 |
+
logs = json.load(f)[-7:]
|
| 54 |
+
return {
|
| 55 |
+
"total_entries": len(logs),
|
| 56 |
+
"mood_trend": Counter(x["mood"] for x in logs),
|
| 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"): return None
|
|
|
|
| 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.savefig("assets/emotion_map.png")
|
| 69 |
+
return "assets/emotion_map.png"
|
| 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()])
|