-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1225 lines (1054 loc) · 47.2 KB
/
Copy pathapp.py
File metadata and controls
1225 lines (1054 loc) · 47.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Reflecting Pool - Journal Analytics & Search
Single unified Streamlit application for journal analysis, search, and chat.
Combines the analytics dashboard and chat interface into one app.
"""
import sys
from pathlib import Path
# Resolve project root from this file's location
ROOT = Path(__file__).resolve().parent
# Make submodules importable
sys.path.insert(0, str(ROOT / "rag"))
sys.path.insert(0, str(ROOT / "dashboard"))
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import json
import re
from datetime import datetime, timedelta
from collections import Counter
from typing import Dict, List, Tuple
# Default paths (resolved from project root)
DEFAULT_OCR_DIR = str(ROOT / "ocr" / "ocr_output")
DEFAULT_RAG_DB = str(ROOT / "rag" / "vector_db")
THEME_FILE = ROOT / ".reflecting_pool_theme.json"
# ---------------------------------------------------------------------------
# Theme persistence
# ---------------------------------------------------------------------------
_THEME_DEFAULTS = {
"body_font": "Georgia", "heading_font": "Garamond",
"font_size": 16, "line_height": 1.6,
"text_color": "#262730", "heading_color": "#0f0f23",
"link_color": "#636EFA", "metric_color": "#1a1a2e",
"bg_color": "#ffffff", "sidebar_bg": "#f8f9fa",
"content_padding": 1.0, "block_gap": 1.5,
"metric_font_size": 14, "border_radius": 8,
}
def load_theme() -> dict:
"""Load saved theme or return defaults."""
if THEME_FILE.exists():
try:
with open(THEME_FILE, "r") as f:
saved = json.load(f)
return {**_THEME_DEFAULTS, **saved}
except (json.JSONDecodeError, OSError):
pass
return dict(_THEME_DEFAULTS)
def save_theme(theme: dict):
"""Persist the current theme to disk."""
with open(THEME_FILE, "w") as f:
json.dump(theme, f, indent=2)
def _inject_theme_css(t: dict):
"""Inject the saved theme as page-wide CSS."""
st.markdown(f"""<style>
html, body, [class*="css"] {{
font-family: '{t["body_font"]}', serif !important;
font-size: {t["font_size"]}px !important;
line-height: {t["line_height"]} !important;
color: {t["text_color"]} !important;
background-color: {t["bg_color"]} !important;
}}
h1, h2, h3, h4, h5, h6,
.stMarkdown h1, .stMarkdown h2, .stMarkdown h3 {{
font-family: '{t["heading_font"]}', serif !important;
color: {t["heading_color"]} !important;
}}
a, a:visited {{ color: {t["link_color"]} !important; }}
[data-testid="stMetricValue"] {{
color: {t["metric_color"]} !important;
font-family: '{t["heading_font"]}', serif !important;
}}
[data-testid="stMetricLabel"] {{
font-size: {t["metric_font_size"]}px !important;
color: {t["text_color"]} !important; opacity: 0.75;
}}
[data-testid="stSidebar"] {{ background-color: {t["sidebar_bg"]} !important; }}
[data-testid="stSidebar"] * {{ color: {t["text_color"]} !important; }}
.block-container {{
padding-left: {t["content_padding"]}rem !important;
padding-right: {t["content_padding"]}rem !important;
}}
.element-container {{ margin-bottom: {t["block_gap"] / 2}rem !important; }}
[data-testid="stExpander"] {{ border-radius: {t["border_radius"]}px !important; }}
[data-baseweb="tab-list"] {{ font-family: '{t["body_font"]}', serif !important; }}
</style>""", unsafe_allow_html=True)
# ---------------------------------------------------------------------------
# Session logging
# ---------------------------------------------------------------------------
def _log_path() -> Path:
"""Return the session log file path (logs/ folder, timestamped per session)."""
logs_dir = ROOT / "logs"
logs_dir.mkdir(exist_ok=True)
# One log file per session, keyed by the session start time
if "log_filename" not in st.session_state:
ts = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
st.session_state.log_filename = f"session_{ts}.log"
return logs_dir / st.session_state.log_filename
def session_log(message: str):
"""Append a timestamped line to the session log if logging is active."""
if not st.session_state.get("session_logging"):
return
try:
with open(_log_path(), "a", encoding="utf-8") as f:
f.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {message}\n")
except OSError:
pass
# ---------------------------------------------------------------------------
# Page config
# ---------------------------------------------------------------------------
st.set_page_config(
page_title="Reflecting Pool",
page_icon=str(ROOT / "favicon.ico"),
layout="wide",
initial_sidebar_state="expanded",
)
# ---------------------------------------------------------------------------
# Data loading
# ---------------------------------------------------------------------------
@st.cache_data
def load_journal_data(ocr_output_dir: str) -> pd.DataFrame:
"""Load journal entries from OCR output directory."""
ocr_path = Path(ocr_output_dir)
text_dir = ocr_path / "text"
metadata_dir = ocr_path / "metadata"
if not text_dir.exists() or not metadata_dir.exists():
return pd.DataFrame()
entries = []
for text_file in text_dir.glob("*.txt"):
metadata_file = metadata_dir / f"{text_file.stem}.json"
if not metadata_file.exists():
continue
text = text_file.read_text(encoding="utf-8").strip()
with open(metadata_file, "r", encoding="utf-8") as f:
metadata = json.load(f)
if not text:
continue
entries.append({
"date": metadata["entry_date"],
"text": text,
"word_count": len(text.split()),
"char_count": len(text),
})
if not entries:
return pd.DataFrame()
df = pd.DataFrame(entries)
df["date"] = pd.to_datetime(df["date"], format="mixed")
return df.sort_values("date")
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def get_sentiment(text: str) -> float:
"""VADER sentiment score (-1 to +1)."""
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
return analyzer.polarity_scores(text)["compound"]
@st.cache_data(show_spinner="Analyzing sentiment...")
def _add_sentiment(df: pd.DataFrame) -> pd.DataFrame:
"""Compute and cache sentiment scores for a dataframe."""
if "sentiment" in df.columns:
return df
df = df.copy()
df["sentiment"] = df["text"].apply(get_sentiment)
return df
def section_header(title: str, help_text: str):
"""Section header with a collapsible help tooltip."""
st.header(title)
with st.expander("What does this show?"):
st.markdown(help_text)
def extract_common_words(texts: List[str], n_words: int = 30) -> List[Tuple[str, int]]:
"""Extract most common meaningful words, filtering stop words."""
stop_words = {
"the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for",
"of", "with", "by", "from", "as", "is", "was", "were", "been", "be",
"have", "has", "had", "do", "does", "did", "will", "would", "could",
"should", "may", "might", "must", "can", "i", "me", "my", "mine",
"you", "your", "yours", "he", "him", "his", "she", "her", "hers",
"it", "its", "we", "us", "our", "ours", "they", "them", "their",
"theirs", "this", "that", "these", "those", "am", "are", "was",
"were", "been", "being", "have", "has", "had", "having", "do",
"does", "did", "doing", "just", "so", "than", "too", "very",
"about", "after", "again", "also", "back", "been", "before",
"being", "between", "both", "came", "come", "each", "even",
"every", "first", "get", "got", "into", "know", "like", "made",
"make", "many", "more", "most", "much", "need", "never", "next",
"now", "only", "other", "over", "part", "really", "right", "same",
"some", "still", "such", "take", "tell", "then", "there", "thing",
"think", "time", "want", "well", "went", "what", "when", "where",
"which", "while", "who", "will", "with", "work", "year",
}
all_words = []
for text in texts:
words = re.findall(r"\b\w+\b", text.lower())
all_words.extend(w for w in words if w not in stop_words and len(w) > 3)
return Counter(all_words).most_common(n_words)
@st.cache_resource
def _get_rag(db_path: str):
"""Lazily import and instantiate JournalRAG (cached so embeddings load once)."""
from journal_rag import JournalRAG
return JournalRAG(db_path=db_path)
# ---------------------------------------------------------------------------
# Sidebar – OCR Watcher
# ---------------------------------------------------------------------------
def _render_ocr_watcher(ocr_output_dir: str):
"""Render the OCR Watcher controls in the sidebar.
Uses ``watchdog`` + ``JournalPhotoHandler`` from the OCR module to watch a
folder for new journal photos and process them automatically. The Observer
thread is stored in ``st.session_state`` so it survives Streamlit reruns.
"""
st.sidebar.header("OCR Watcher")
# Graceful fallback if watchdog is not installed
try:
from watchdog.observers import Observer # noqa: F401
except ImportError:
st.sidebar.info(
"Install **watchdog** to enable the folder watcher:\n\n"
"`pip install watchdog`"
)
return
# Initialise session-state keys on first run
if "ocr_watcher_running" not in st.session_state:
st.session_state.ocr_watcher_running = False
st.session_state.ocr_watcher_observer = None
st.session_state.ocr_watcher_handler = None
# Default watch directory (sibling of OCR output)
default_watch = str(ROOT / "journal_photos")
watch_dir = st.sidebar.text_input(
"Watch Folder",
value=default_watch,
help="Folder to monitor for new journal photos (e.g. your iCloud Photos folder)",
)
# Staleness check – if the observer died unexpectedly, reset state
if st.session_state.ocr_watcher_running:
obs = st.session_state.ocr_watcher_observer
if obs is None or not obs.is_alive():
st.session_state.ocr_watcher_running = False
st.session_state.ocr_watcher_observer = None
st.session_state.ocr_watcher_handler = None
st.sidebar.warning("Watcher stopped unexpectedly.")
# Status indicator
if st.session_state.ocr_watcher_running:
st.sidebar.caption(":green[● Running]")
else:
st.sidebar.caption(":gray[● Stopped]")
# Start / Stop button
col1, col2 = st.sidebar.columns(2)
if st.session_state.ocr_watcher_running:
if col1.button("Stop Watcher", key="ocr_watch_stop"):
try:
obs = st.session_state.ocr_watcher_observer
if obs is not None:
obs.stop()
obs.join(timeout=5)
except Exception:
pass
st.session_state.ocr_watcher_running = False
st.session_state.ocr_watcher_observer = None
st.session_state.ocr_watcher_handler = None
session_log("OCR Watcher stopped")
st.rerun()
else:
if col1.button("Start Watcher", type="primary", key="ocr_watch_start"):
watch_path = Path(watch_dir)
if not watch_path.is_dir():
st.sidebar.error(f"Folder not found: {watch_dir}")
else:
try:
sys.path.insert(0, str(ROOT / "ocr"))
from journal_ocr import JournalOCRPipeline
from auto_ocr_watcher import JournalPhotoHandler
from watchdog.observers import Observer as Obs
pipeline = JournalOCRPipeline(output_dir=ocr_output_dir)
handler = JournalPhotoHandler(pipeline)
observer = Obs()
observer.schedule(handler, str(watch_path), recursive=False)
observer.daemon = True
observer.start()
st.session_state.ocr_watcher_observer = observer
st.session_state.ocr_watcher_handler = handler
st.session_state.ocr_watcher_running = True
session_log(f"OCR Watcher started – watching {watch_dir}")
st.rerun()
except Exception as exc:
st.sidebar.error(f"Failed to start watcher: {exc}")
# Processed file count
handler = st.session_state.ocr_watcher_handler
if handler is not None and hasattr(handler, "processed_files"):
n = len(handler.processed_files)
if n > 0:
st.sidebar.success(f"Processed **{n}** file{'s' if n != 1 else ''} this session")
if col2.button("Refresh Data", key="ocr_watch_refresh"):
st.cache_data.clear()
session_log("Data cache cleared after OCR Watcher processing")
st.rerun()
# ---------------------------------------------------------------------------
# Sidebar
# ---------------------------------------------------------------------------
def render_sidebar(df: pd.DataFrame):
"""Render sidebar controls and return filtered dataframe + config."""
st.sidebar.header("Configuration")
ocr_output_dir = st.sidebar.text_input(
"OCR Output Directory",
value=DEFAULT_OCR_DIR,
help="Path to your OCR output directory",
)
rag_db_path = st.sidebar.text_input(
"RAG Database Path",
value=DEFAULT_RAG_DB,
help="Path to your RAG vector database",
)
# --- OCR Watcher ---
_render_ocr_watcher(ocr_output_dir)
# --- RAG sidebar search ---
st.sidebar.header("Search")
search_query = st.sidebar.text_input(
"Search Query",
placeholder="e.g., feeling anxious about work",
help="Semantic search across all journal entries",
)
if st.sidebar.button("Search", type="primary"):
if search_query:
session_log(f"Sidebar search: {search_query}")
try:
with st.spinner("Searching..."):
rag = _get_rag(rag_db_path)
results = rag.search(search_query, n_results=5)
if results:
st.sidebar.success(f"Found {len(results)} results")
for i, result in enumerate(results, 1):
with st.sidebar.expander(f"{i}. {result['date']}"):
st.text(result["text"][:200] + "...")
else:
st.sidebar.warning("No results found")
except FileNotFoundError:
st.sidebar.error("RAG database not found. Run ingestion first.")
except Exception as e:
st.sidebar.error(f"Search error: {e}")
else:
st.sidebar.warning("Please enter a search query")
if st.sidebar.button("Ingest to RAG"):
try:
with st.spinner("Ingesting journal entries into RAG database..."):
rag = _get_rag(rag_db_path)
count = rag.ingest_from_ocr(ocr_output_dir)
st.sidebar.success(f"Ingested {count} entries!")
except Exception as e:
st.sidebar.error(f"Ingestion error: {e}")
# --- Date filter ---
filtered_df = df
if not df.empty:
st.sidebar.header("Filters")
min_date = df["date"].min().date()
max_date = df["date"].max().date()
date_range = st.sidebar.date_input(
"Date Range",
value=(min_date, max_date),
min_value=min_date,
max_value=max_date,
)
if len(date_range) == 2:
mask = (df["date"].dt.date >= date_range[0]) & (df["date"].dt.date <= date_range[1])
filtered_df = df[mask]
# --- Session logging ---
st.sidebar.header("Session Log")
if "session_logging" not in st.session_state:
st.session_state.session_logging = False
logging_on = st.sidebar.toggle(
"Enable session log",
value=st.session_state.session_logging,
help=f"Writes activity to {_log_path()}",
)
if logging_on != st.session_state.session_logging:
st.session_state.session_logging = logging_on
if logging_on:
session_log("Session logging started")
else:
session_log("Session logging stopped")
if st.session_state.session_logging:
log_file = _log_path()
st.sidebar.caption(f"Logging to: {log_file}")
if log_file.exists():
if "show_log" not in st.session_state:
st.session_state.show_log = False
if st.sidebar.button("Hide log" if st.session_state.show_log else "View log"):
st.session_state.show_log = not st.session_state.show_log
st.rerun()
if st.session_state.show_log:
st.sidebar.code(log_file.read_text(encoding="utf-8")[-2000:], language="text")
return ocr_output_dir, rag_db_path, filtered_df
# ---------------------------------------------------------------------------
# Tab: Analytics
# ---------------------------------------------------------------------------
def tab_analytics(df: pd.DataFrame):
section_header("Sentiment Over Time", """
Tracks the emotional tone of each entry using VADER sentiment analysis.
Score runs from **-1** (most negative) to **+1** (most positive).
The dashed grey line marks neutral (0). Hover over a dot for the exact
date and score.
""")
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df["date"], y=df["sentiment"],
mode="lines+markers", name="Sentiment",
line=dict(color="rgb(99, 110, 250)", width=2),
marker=dict(size=6),
))
fig.add_hline(y=0, line_dash="dash", line_color="gray", opacity=0.5)
fig.update_layout(
xaxis_title="Date", yaxis_title="Sentiment Score",
yaxis_range=[-1.1, 1.1], height=400, hovermode="x unified",
)
st.plotly_chart(fig, use_container_width=True)
# --- Writing consistency ---
section_header("Writing Consistency", """
**Left**: entries per month. **Right**: word count per entry over time.
""")
col1, col2 = st.columns(2)
with col1:
monthly = df.groupby(df["date"].dt.to_period("M")).size()
monthly.index = monthly.index.astype(str)
fig_m = px.bar(x=monthly.index, y=monthly.values,
labels={"x": "Month", "y": "Entries"}, title="Entries per Month")
fig_m.update_traces(marker_color="rgb(99, 110, 250)")
st.plotly_chart(fig_m, use_container_width=True)
with col2:
fig_w = px.scatter(df, x="date", y="word_count",
title="Words per Entry",
labels={"date": "Date", "word_count": "Word Count"})
st.plotly_chart(fig_w, use_container_width=True)
# --- Heatmap ---
section_header("Writing Frequency Heatmap", """
GitHub-style calendar grid. Rows are days of the week, columns are weeks.
Darker cells mean more words written that day.
""")
date_range_full = pd.date_range(start=df["date"].min(), end=df["date"].max(), freq="D")
daily_words = df.groupby(df["date"].dt.date)["word_count"].sum()
heatmap_data = []
for d in date_range_full:
heatmap_data.append({
"date": d,
"count": daily_words.get(d.date(), 0),
"day_of_week": d.day_name(),
})
hm_df = pd.DataFrame(heatmap_data)
pivot = hm_df.pivot_table(
values="count", index="day_of_week",
columns=hm_df["date"].dt.to_period("W"), fill_value=0,
)
day_order = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
pivot = pivot.reindex(day_order)
pivot.columns = pivot.columns.astype(str)
fig_hm = px.imshow(pivot, labels=dict(x="Week", y="Day", color="Words"),
color_continuous_scale="Blues", aspect="auto")
fig_hm.update_layout(height=300)
st.plotly_chart(fig_hm, use_container_width=True)
# ---------------------------------------------------------------------------
# Tab: Words & Themes
# ---------------------------------------------------------------------------
def tab_words(df: pd.DataFrame):
section_header("Most Common Words", """
Ranked by frequency with common stop words filtered out.
""")
col1, col2 = st.columns([2, 1])
with col1:
n_words = st.slider("Number of words to show", 10, 50, 30)
common = extract_common_words(df["text"].tolist(), n_words=n_words)
if common:
words_df = pd.DataFrame(common, columns=["Word", "Count"])
fig = px.bar(words_df, x="Count", y="Word", orientation="h",
title=f"Top {n_words} Most Common Words")
fig.update_layout(height=600, yaxis={"categoryorder": "total ascending"})
st.plotly_chart(fig, use_container_width=True)
with col2:
st.subheader("Top Words")
if common:
for word, count in common[:15]:
st.text(f"{word}: {count}")
# ---------------------------------------------------------------------------
# Tab: Music
# ---------------------------------------------------------------------------
@st.cache_data(show_spinner=False, ttl=600)
def _cached_music_search(df: pd.DataFrame) -> dict:
"""Cache iTunes lookups so they don't re-run on every interaction."""
from music_extraction import extract_and_search_music
entries = [
{"date": row["date"].strftime("%Y-%m-%d"), "text": row["text"]}
for _, row in df.iterrows()
]
return extract_and_search_music(entries)
def tab_music(df: pd.DataFrame):
section_header("Music Mentioned", """
Songs and artists detected in your journal entries,
linked to Apple Music / iTunes for metadata and artwork.
""")
try:
from music_extraction import extract_and_search_music, format_duration
with st.spinner("Searching for music mentions..."):
music_data = _cached_music_search(df)
if not music_data:
st.info("No music mentions detected. Try writing about songs you're listening to!")
st.markdown("""
**Tips for detection:**
- Use quotes: *listened to "Everlong" by Foo Fighters*
- Mention artists: *listening to Radiohead*
- Use colons: *Song: "Karma Police"*
""")
return
sorted_music = sorted(music_data.values(), key=lambda x: x["count"], reverse=True)
cols_per_row = 3
for i in range(0, len(sorted_music), cols_per_row):
cols = st.columns(cols_per_row)
for j, col in enumerate(cols):
if i + j >= len(sorted_music):
break
item = sorted_music[i + j]
meta = item["metadata"]
with col:
if meta["artwork_url"]:
st.image(meta["artwork_url"], use_container_width=True)
st.markdown(f"**{meta['song_name']}**")
st.markdown(f"*{meta['artist_name']}*")
if meta["album_name"]:
st.caption(f"Album: {meta['album_name']}")
c1, c2 = st.columns(2)
with c1:
st.metric("Mentions", item["count"])
with c2:
last = max(item["dates"])
st.metric("Last", last.split("T")[0] if "T" in last else last)
if meta["duration_ms"]:
st.caption(f"Duration: {format_duration(meta['duration_ms'])}")
if meta["genre"]:
st.caption(f"Genre: {meta['genre']}")
lc1, lc2 = st.columns(2)
with lc1:
if meta["preview_url"]:
st.markdown(f"[Preview]({meta['preview_url']})")
with lc2:
if meta["itunes_url"]:
st.markdown(f"[iTunes]({meta['itunes_url']})")
st.divider()
st.caption(f"Found {len(sorted_music)} songs/artists across {len(df)} entries")
# --- Music export ---
music_rows = []
for item in sorted_music:
meta = item["metadata"]
music_rows.append({
"Song": meta["song_name"],
"Artist": meta["artist_name"],
"Album": meta.get("album_name", ""),
"Genre": meta.get("genre", ""),
"Mentions": item["count"],
"Dates Mentioned": "; ".join(sorted(item["dates"])),
"iTunes URL": meta.get("itunes_url", ""),
})
music_df = pd.DataFrame(music_rows)
st.download_button(
"Download music data (CSV)",
data=music_df.to_csv(index=False),
file_name="journal_music.csv",
mime="text/csv",
)
except ImportError:
st.error("Music extraction module not found. Ensure music_extraction.py is in the dashboard folder.")
except Exception as e:
st.error(f"Error extracting music: {e}")
# ---------------------------------------------------------------------------
# Tab: Chat
# ---------------------------------------------------------------------------
def tab_chat(rag_db_path: str):
st.header("Chat with Your Journals")
st.markdown(
"Ask questions about your journal entries. "
"The system searches semantically and can optionally use a local LLM."
)
# --- LLM toggle ---
use_llm = st.checkbox(
"Use LLM for answers",
value=False,
help="Requires Ollama installed and running locally",
)
llm_model = "llama3.3"
if use_llm:
llm_model = st.selectbox("LLM Model", ["llama3.3", "mistral", "llama2"])
# --- Check database exists ---
if not Path(rag_db_path).exists():
st.warning(
"RAG database not found. Ingest entries first using the sidebar button "
"or option 4 in Journal_System.bat."
)
return
# --- Chat history ---
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if message.get("sources"):
with st.expander("View Sources"):
for i, src in enumerate(message["sources"], 1):
st.markdown(f"**{i}. Entry from {src['date']}**")
st.text(src["text"][:300] + ("..." if len(src["text"]) > 300 else ""))
st.divider()
# --- Chat input ---
if prompt := st.chat_input("Ask a question about your journals..."):
session_log(f"Chat question: {prompt}")
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
with st.spinner("Searching your journals..."):
try:
from journal_rag import JournalRAG, OllamaLLM
rag = JournalRAG(db_path=rag_db_path)
results = rag.search(prompt, n_results=3)
if not results:
response = "I couldn't find any relevant journal entries. Try rephrasing."
sources = []
elif use_llm:
try:
with st.spinner("Generating answer with AI..."):
llm = OllamaLLM(model=llm_model)
context = [r["text"] for r in results]
response = llm.generate(prompt, context)
sources = results
except Exception as e:
response = f"LLM error: {e}\n\nShowing relevant entries instead:"
sources = results
else:
response = "Here are the most relevant journal entries:"
sources = results
st.markdown(response)
if sources:
with st.expander("View Sources", expanded=(not use_llm)):
for i, src in enumerate(sources, 1):
st.markdown(f"**{i}. Entry from {src['date']}**")
st.text(src["text"][:300] + ("..." if len(src["text"]) > 300 else ""))
st.divider()
st.session_state.messages.append({
"role": "assistant", "content": response, "sources": sources,
})
session_log(f"Chat response: {response[:200]}")
except FileNotFoundError:
msg = "RAG database not found. Please ingest your journal entries first."
st.error(msg)
st.session_state.messages.append({"role": "assistant", "content": msg, "sources": []})
except Exception as e:
msg = f"Error: {e}"
st.error(msg)
st.session_state.messages.append({"role": "assistant", "content": msg, "sources": []})
# --- Example questions ---
with st.expander("Example Questions"):
st.markdown("""
- What was I worried about last week?
- When did I last mention [person's name]?
- What made me happy this month?
- How have I been sleeping lately?
- What goals did I set recently?
""")
# --- Clear history ---
if st.button("Clear Chat History"):
st.session_state.messages = []
st.rerun()
# ---------------------------------------------------------------------------
# Tab: Entries & Stats
# ---------------------------------------------------------------------------
def tab_entries(df: pd.DataFrame):
section_header("Detailed Statistics", """
Breakdown across sentiment, writing length, and journaling streaks.
Expand any highlighted entry to preview its text.
""")
col1, col2, col3 = st.columns(3)
with col1:
st.subheader("Sentiment")
st.metric("Average", f"{df['sentiment'].mean():.2f}")
max_idx = df["sentiment"].idxmax()
max_row = df.loc[max_idx]
st.metric("Most Positive", max_row["date"].strftime("%Y-%m-%d"),
delta=f"{max_row['sentiment']:.2f}")
with st.expander("Preview"):
st.caption(f"{max_row['date'].strftime('%Y-%m-%d')} - {max_row['word_count']} words - score {max_row['sentiment']:.2f}")
st.text(max_row["text"][:300] + ("..." if len(max_row["text"]) > 300 else ""))
min_idx = df["sentiment"].idxmin()
min_row = df.loc[min_idx]
st.metric("Most Negative", min_row["date"].strftime("%Y-%m-%d"),
delta=f"{min_row['sentiment']:.2f}")
with st.expander("Preview"):
st.caption(f"{min_row['date'].strftime('%Y-%m-%d')} - {min_row['word_count']} words - score {min_row['sentiment']:.2f}")
st.text(min_row["text"][:300] + ("..." if len(min_row["text"]) > 300 else ""))
with col2:
st.subheader("Writing")
st.metric("Median Words", f"{int(df['word_count'].median()):,}")
long_idx = df["word_count"].idxmax()
long_row = df.loc[long_idx]
st.metric("Longest Entry", f"{long_row['word_count']:,} words",
delta=long_row["date"].strftime("%Y-%m-%d"))
with st.expander("Preview"):
st.caption(f"{long_row['date'].strftime('%Y-%m-%d')} - {long_row['word_count']} words")
st.text(long_row["text"][:300] + ("..." if len(long_row["text"]) > 300 else ""))
short_idx = df["word_count"].idxmin()
short_row = df.loc[short_idx]
st.metric("Shortest Entry", f"{short_row['word_count']:,} words",
delta=short_row["date"].strftime("%Y-%m-%d"))
with st.expander("Preview"):
st.caption(f"{short_row['date'].strftime('%Y-%m-%d')} - {short_row['word_count']} words")
st.text(short_row["text"][:300] + ("..." if len(short_row["text"]) > 300 else ""))
with col3:
st.subheader("Consistency")
dates_set = set(df["date"].dt.date)
sorted_dates = sorted(dates_set)
longest_streak = temp = 1
for i in range(1, len(sorted_dates)):
if (sorted_dates[i] - sorted_dates[i - 1]).days == 1:
temp += 1
else:
longest_streak = max(longest_streak, temp)
temp = 1
longest_streak = max(longest_streak, temp)
current_streak = 0
if sorted_dates:
today = datetime.now().date()
last = sorted_dates[-1]
if last == today or (today - last).days == 1:
current_streak = 1
for i in range(len(sorted_dates) - 2, -1, -1):
if (sorted_dates[i + 1] - sorted_dates[i]).days == 1:
current_streak += 1
else:
break
st.metric("Current Streak", f"{current_streak} days")
st.metric("Longest Streak", f"{longest_streak} days")
st.metric("Total Days Journaled", len(dates_set))
# --- Recent entries ---
section_header("Recent Entries", """
Your most recent journal entries, newest first.
Click to expand and read a preview.
""")
n_recent = st.slider("Entries to show", 3, 10, 5)
recent = df.tail(n_recent).sort_values("date", ascending=False)
for _, row in recent.iterrows():
label = f"{row['date'].strftime('%Y-%m-%d')} - {row['word_count']} words - Sentiment: {row['sentiment']:.2f}"
with st.expander(label):
preview = row["text"][:300]
if len(row["text"]) > 300:
preview += "..."
st.text(preview)
# --- Data export ---
st.divider()
st.subheader("Export Data")
ex1, ex2 = st.columns(2)
with ex1:
stats_df = df[["date", "word_count", "sentiment"]].copy()
stats_df["date"] = stats_df["date"].dt.strftime("%Y-%m-%d")
st.download_button(
"Download statistics (CSV)",
data=stats_df.to_csv(index=False),
file_name="journal_statistics.csv",
mime="text/csv",
)
with ex2:
full_df = df[["date", "word_count", "char_count", "sentiment", "text"]].copy()
full_df["date"] = full_df["date"].dt.strftime("%Y-%m-%d")
st.download_button(
"Download all entries (CSV)",
data=full_df.to_csv(index=False),
file_name="journal_entries.csv",
mime="text/csv",
)
# ---------------------------------------------------------------------------
# Tab: Appearance
# ---------------------------------------------------------------------------
def tab_appearance():
section_header("Appearance & Styling", """
Customise the look of this dashboard. Changes apply immediately
and are saved automatically so they persist between launches.
""")
# Load saved theme as defaults
t = load_theme()
body_fonts = ["Georgia", "Palatino Linotype", "Garamond", "Times New Roman",
"Merriweather", "Source Serif 4", "sans-serif", "monospace"]
heading_fonts = ["Georgia", "Palatino Linotype", "Garamond", "Impact",
"Trebuchet MS", "Verdana", "serif", "sans-serif"]
# --- Typography ---
st.subheader("Typography")
fc1, fc2 = st.columns(2)
with fc1:
body_font = st.selectbox("Body font", body_fonts,
index=body_fonts.index(t["body_font"]) if t["body_font"] in body_fonts else 0)
font_size = st.slider("Base font size (px)", 12, 22, t["font_size"])
with fc2:
heading_font = st.selectbox("Heading font", heading_fonts,
index=heading_fonts.index(t["heading_font"]) if t["heading_font"] in heading_fonts else 2)
line_height = st.slider("Line height", 1.2, 2.2, t["line_height"], step=0.1)
# --- Colours ---
st.subheader("Colours")
cc1, cc2, cc3 = st.columns(3)
with cc1:
text_color = st.color_picker("Body text", t["text_color"])
heading_color = st.color_picker("Headings", t["heading_color"])
with cc2:
link_color = st.color_picker("Links / accent", t["link_color"])
metric_color = st.color_picker("Metric values", t["metric_color"])
with cc3:
bg_color = st.color_picker("Page background", t["bg_color"])
sidebar_bg = st.color_picker("Sidebar background", t["sidebar_bg"])
# --- Spacing ---
st.subheader("Spacing & Layout")
sc1, sc2 = st.columns(2)
with sc1:
content_padding = st.slider("Content padding (rem)", 0.5, 4.0, t["content_padding"], step=0.25)
block_gap = st.slider("Section gap (rem)", 0.5, 4.0, t["block_gap"], step=0.25)
with sc2:
metric_font_size = st.slider("Metric label size (px)", 10, 18, t["metric_font_size"])
border_radius = st.slider("Border radius (px)", 0, 20, t["border_radius"])
# --- Presets ---
st.subheader("Quick Presets")
presets = {
"Dark Ink": {
"body_font": "Georgia", "heading_font": "Garamond",
"font_size": 16, "line_height": 1.7,
"text_color": "#e8e8e0", "heading_color": "#f5f0e8",
"link_color": "#c9a96e", "metric_color": "#c9a96e",
"bg_color": "#1a1814", "sidebar_bg": "#12100e",
"content_padding": 1.5, "block_gap": 2.0,
"metric_font_size": 13, "border_radius": 6,
},
"Parchment": {
"body_font": "Palatino Linotype", "heading_font": "Garamond",
"font_size": 17, "line_height": 1.8,
"text_color": "#3b2f1e", "heading_color": "#1e1208",
"link_color": "#7a4f2e", "metric_color": "#4a3220",
"bg_color": "#fdf6e3", "sidebar_bg": "#f5e8c8",
"content_padding": 2.0, "block_gap": 2.0,
"metric_font_size": 14, "border_radius": 4,
},
"Minimal": {
"body_font": "sans-serif", "heading_font": "sans-serif",
"font_size": 15, "line_height": 1.5,
"text_color": "#111111", "heading_color": "#000000",
"link_color": "#0066cc", "metric_color": "#0066cc",
"bg_color": "#ffffff", "sidebar_bg": "#f4f4f4",
"content_padding": 1.0, "block_gap": 1.5,
"metric_font_size": 12, "border_radius": 2,
},
"Sage": {
"body_font": "Georgia", "heading_font": "Georgia",
"font_size": 16, "line_height": 1.7,
"text_color": "#2d3a2e", "heading_color": "#1a2e1b",
"link_color": "#4a7c59", "metric_color": "#3a6648",
"bg_color": "#f4f8f4", "sidebar_bg": "#e8f0e8",
"content_padding": 1.5, "block_gap": 1.75,
"metric_font_size": 14, "border_radius": 10,
},
}
preset_cols = st.columns(len(presets))