KeenWoo commited on
Commit
f651cbc
·
verified ·
1 Parent(s): 41a3c55

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -1226
app.py DELETED
@@ -1,1226 +0,0 @@
1
- import os
2
- import json
3
- import shutil
4
- import gradio as gr
5
- import tempfile
6
- from datetime import datetime
7
- from typing import List, Dict, Any, Optional
8
- from pytube import YouTube
9
- from pathlib import Path
10
- import re
11
- import pandas as pd
12
-
13
- # --- Agent Imports ---
14
- try:
15
- from alz_companion.agent import (
16
- bootstrap_vectorstore, make_rag_chain, answer_query, synthesize_tts,
17
- transcribe_audio, detect_tags_from_query, describe_image, build_or_load_vectorstore,
18
- _default_embeddings, route_query_type, call_llm,
19
- texts_from_jsonl # <-- ADD THIS LINE
20
- )
21
- from alz_companion.prompts import (
22
- BEHAVIOUR_TAGS, EMOTION_STYLES, FAITHFULNESS_JUDGE_PROMPT
23
- )
24
- from langchain.schema import Document
25
- from langchain_community.vectorstores import FAISS
26
- AGENT_OK = True
27
- except Exception as e:
28
- AGENT_OK = False
29
- class Document:
30
- def __init__(self, page_content, metadata): self.page_content, self.metadata = page_content, metadata
31
- class FAISS:
32
- def __init__(self):
33
- self.docstore = type('obj', (object,), {'_dict': {}})()
34
- def add_documents(self, docs):
35
- start_idx = len(self.docstore._dict)
36
- for i, d in enumerate(docs, start_idx):
37
- self.docstore._dict[i] = d
38
- def save_local(self, path): pass
39
- @classmethod
40
- def from_documents(cls, docs, embeddings=None):
41
- inst = cls()
42
- inst.add_documents(docs)
43
- return inst
44
- def build_or_load_vectorstore(docs, index_path, is_personal=False): return FAISS.from_documents(docs or [], embeddings=None)
45
- def bootstrap_vectorstore(sample_paths=None, index_path="data/"): return object()
46
- def make_rag_chain(vs_general, vs_personal, **kwargs): return lambda q, **k: {"answer": f"(Demo) You asked: {q}", "sources": []}
47
- def answer_query(chain, q, **kwargs): return chain(q, **kwargs)
48
- def synthesize_tts(text: str, lang: str = "en"): return None
49
- def transcribe_audio(filepath: str, lang: str = "en"): return "This is a transcribed message."
50
- def detect_tags_from_query(*args, **kwargs): return {"detected_behavior": "None", "detected_emotion": "None"}
51
- def describe_image(image_path: str): return "This is a description of an image."
52
- def _default_embeddings(): return None
53
- def route_query_type(query: str): return "general_conversation"
54
- def call_llm(messages, **kwargs): return "Cannot call LLM in fallback mode."
55
- def texts_from_jsonl(path): return [] # <-- ADD THIS LINE
56
- BEHAVIOUR_TAGS, EMOTION_STYLES, FAITHFULNESS_JUDGE_PROMPT = {"None": []}, {"None": {}}, ""
57
- print(f"WARNING: Could not import from alz_companion ({e}). Running in UI-only demo mode.")
58
-
59
-
60
- # --- NEW: Import for Evaluation Logic ---
61
- try:
62
- from evaluate import load_test_fixtures, run_comprehensive_evaluation
63
- except ImportError:
64
- # Fallback if evaluate.py is not found
65
- def load_test_fixtures(): print("WARNING: evaluate.py not found.")
66
- def run_comprehensive_evaluation(*args, **kwargs): return "Evaluation module not found.", []
67
-
68
-
69
- # --- Centralized Configuration ---
70
- CONFIG = {
71
- "themes": ["All", "The Father", "Still Alice", "Away from Her", "Alive Inside", "General Caregiving"],
72
- "roles": ["patient", "caregiver"],
73
- "disease_stages": ["Default: Mild Stage", "Moderate Stage", "Advanced Stage"],
74
- "behavior_tags": ["None"] + list(BEHAVIOUR_TAGS.keys()),
75
- "emotion_tags": ["None"] + list(EMOTION_STYLES.keys()),
76
- "topic_tags": ["None", "caregiving_advice", "medical_fact", "personal_story", "research_update", "treatment_option:home_safety", "treatment_option:long_term_care", "treatment_option:music_therapy", "treatment_option:reassurance", "treatment_option:routine_structuring", "treatment_option:validation_therapy"],
77
- "context_tags": ["None", "disease_stage_mild", "disease_stage_moderate", "disease_stage_advanced", "disease_stage_unspecified", "interaction_mode_one_to_one", "interaction_mode_small_group", "interaction_mode_group_activity", "relationship_family", "relationship_spouse", "relationship_staff_or_caregiver", "relationship_unspecified", "setting_home_or_community", "setting_care_home", "setting_clinic_or_hospital"],
78
- "languages": {"English": "en", "Chinese": "zh", "Cantonese": "zh-yue", "Korean": "ko", "Japanese": "ja", "Malay": "ms", "French": "fr", "Spanish": "es", "Hindi": "hi", "Arabic": "ar"},
79
- "tones": ["warm", "empathetic", "caring", "reassuring", "calm", "optimistic", "motivating", "neutral", "formal", "humorous"],
80
- # --- ADD THIS NEW KEY AND LIST ---
81
- "music_moods": [
82
- "Confusion or Disorientation",
83
- "Reminiscence and Connection",
84
- "Sundowning or Restlessness",
85
- "Sadness or Longing",
86
- "Anxiety or Fear",
87
- "Agitation or Anger",
88
- "Joy or Affection"
89
- ]
90
- # --- END OF ADDITION ---
91
- }
92
-
93
- # --- File Management & Vector Store Logic ---
94
- def _storage_root() -> Path:
95
- for p in [Path(os.getenv("SPACE_STORAGE", "")), Path("/data"), Path.home() / ".cache" / "alz_companion"]:
96
- if not p: continue
97
- try:
98
- p.mkdir(parents=True, exist_ok=True)
99
- (p / ".write_test").write_text("ok")
100
- (p / ".write_test").unlink(missing_ok=True)
101
- return p
102
- except Exception: continue
103
- tmp = Path(tempfile.gettempdir()) / "alz_companion"
104
- tmp.mkdir(parents=True, exist_ok=True)
105
- return tmp
106
-
107
-
108
- STORAGE_ROOT = _storage_root()
109
- INDEX_BASE = STORAGE_ROOT / "index"
110
-
111
- # --- START: CORRECTED PLAYBOOK PATH DEFINITION ---
112
- # Construct an absolute path to the data file relative to this script's location
113
- # to prevent "file not found" errors in different environments.
114
- _SCRIPT_DIR = Path(__file__).parent
115
- PLAYBOOK_SOURCE_PATH = str(_SCRIPT_DIR / "caregiving_playbook.jsonl")
116
- PLAYBOOK_INDEX_PATH = str(INDEX_BASE / "playbook_faiss_index")
117
- # --- END: CORRECTED PLAYBOOK PATH DEFINITION ---
118
-
119
- # Define other paths
120
- PERSISTENT_MEMORY_PATH = Path(__file__).parent / "Personal Memory Bank"
121
- PERSONAL_DATA_BASE = STORAGE_ROOT / "personal"
122
- UPLOADS_BASE = INDEX_BASE / "uploads"
123
- PERSONAL_INDEX_PATH = str(PERSONAL_DATA_BASE / "personal_faiss_index")
124
- NLU_EXAMPLES_INDEX_PATH = str(INDEX_BASE / "nlu_examples_faiss_index")
125
- PLAYBOOK_INDEX_PATH = str(INDEX_BASE / "playbook_faiss_index") # <-- ADD THIS LINE
126
- THEME_PATHS = {t: str(INDEX_BASE / f"faiss_index_{t.replace(' ', '').lower()}") for t in CONFIG["themes"]}
127
-
128
- os.makedirs(UPLOADS_BASE, exist_ok=True)
129
- os.makedirs(PERSONAL_DATA_BASE, exist_ok=True)
130
- os.makedirs(PERSISTENT_MEMORY_PATH, exist_ok=True)
131
-
132
-
133
- for p in THEME_PATHS.values(): os.makedirs(p, exist_ok=True)
134
- vectorstores = {}
135
- personal_vectorstore = None
136
- nlu_vectorstore = None
137
- playbook_vectorstore = None
138
-
139
- try:
140
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
141
- except Exception:
142
- personal_vectorstore = None
143
- def bootstrap_nlu_vectorstore(example_file: str, index_path: str) -> FAISS:
144
- if not os.path.exists(example_file):
145
- print(f"WARNING: NLU example file not found at {example_file}. NLU will be less accurate.")
146
- return build_or_load_vectorstore([], index_path)
147
- docs = []
148
- with open(example_file, "r", encoding="utf-8") as f:
149
- for line in f:
150
- try:
151
- data = json.loads(line)
152
- doc = Document(page_content=data["query"], metadata=data)
153
- docs.append(doc)
154
- except (json.JSONDecodeError, KeyError): continue
155
- print(f"Found and loaded {len(docs)} NLU training examples.")
156
- if os.path.exists(index_path): shutil.rmtree(index_path)
157
- return build_or_load_vectorstore(docs, index_path)
158
-
159
-
160
- # In app.py, near the other path definitions
161
- PERSONAL_MUSIC_BASE = PERSONAL_DATA_BASE / "music"
162
- os.makedirs(PERSONAL_MUSIC_BASE, exist_ok=True)
163
-
164
- # In app.py, replace your existing versions of these three functions with the code below.
165
- # --- Function 1: Auto-loads non-music memories from the 'Personal Memory Bank' folder ---
166
- def load_personal_files_from_folder():
167
- """
168
- Scans the 'Personal Memory Bank' folder and loads new multi-modal files
169
- (text, audio, video, images) into the personal vectorstore.
170
- """
171
- global personal_vectorstore
172
- print("Scanning 'Personal Memory Bank' folder for new files...")
173
- if not os.path.exists(PERSISTENT_MEMORY_PATH):
174
- return
175
-
176
- # Define supported file extensions
177
- TEXT_EXTENSIONS = (".txt",)
178
- AUDIO_EXTENSIONS = (".mp3", ".wav", ".m4a", ".flac")
179
- VIDEO_EXTENSIONS = (".mp4", ".mov", ".avi", ".mkv")
180
- IMAGE_EXTENSIONS = (".jpg", ".jpeg", ".png", ".gif", ".bmp")
181
-
182
- # Get a list of sources already in the vectorstore to avoid re-processing files
183
- existing_sources = set()
184
- if personal_vectorstore and hasattr(personal_vectorstore.docstore, '_dict'):
185
- for doc in personal_vectorstore.docstore._dict.values():
186
- existing_sources.add(doc.metadata.get("source"))
187
-
188
- docs_to_add = []
189
- for filename in os.listdir(PERSISTENT_MEMORY_PATH):
190
- if filename in existing_sources:
191
- continue
192
-
193
- filepath = PERSISTENT_MEMORY_PATH / filename
194
- content_to_process = ""
195
-
196
- file_lower = filename.lower()
197
-
198
- if file_lower.endswith(TEXT_EXTENSIONS):
199
- print(f" - Found new text file to load: {filename}")
200
- with open(filepath, "r", encoding="utf-8") as f:
201
- content_to_process = f.read()
202
-
203
- elif file_lower.endswith(AUDIO_EXTENSIONS) or file_lower.endswith(VIDEO_EXTENSIONS):
204
- media_type = "Audio" if file_lower.endswith(AUDIO_EXTENSIONS) else "Video"
205
- print(f" - Found new {media_type} file to transcribe: {filename}")
206
- try:
207
- transcribed_text = transcribe_audio(str(filepath))
208
- title = os.path.splitext(filename)[0].replace('_', ' ').replace('-', ' ')
209
- content_to_process = f"Title: {title}\n\nContent: {transcribed_text}"
210
- except Exception as e:
211
- print(f" - ERROR: Failed to transcribe {filename}. Reason: {e}")
212
- continue
213
-
214
- elif file_lower.endswith(IMAGE_EXTENSIONS):
215
- print(f" - Found new Image file to describe: {filename}")
216
- try:
217
- description = describe_image(str(filepath))
218
- title = os.path.splitext(filename)[0].replace('_', ' ').replace('-', ' ')
219
- content_to_process = f"Title: {title}\n\nContent: {description}"
220
- except Exception as e:
221
- print(f" - ERROR: Failed to describe {filename}. Reason: {e}")
222
- continue
223
-
224
- if content_to_process:
225
- docs_to_add.extend(parse_and_tag_entries(content_to_process, source=filename, settings={}))
226
-
227
- if docs_to_add:
228
- if personal_vectorstore is None:
229
- personal_vectorstore = build_or_load_vectorstore(docs_to_add, PERSONAL_INDEX_PATH, is_personal=True)
230
- else:
231
- personal_vectorstore.add_documents(docs_to_add)
232
-
233
- personal_vectorstore.save_local(PERSONAL_INDEX_PATH)
234
- print(f"Successfully added {len(docs_to_add)} new document(s) from the folder.")
235
-
236
-
237
- # --- Function 2: Auto-syncs music from the 'Music Library' folder (Hybrid Approach) ---
238
- def sync_music_library_from_folder():
239
- """Scans 'Music Library' folder, syncs manifest for playback, and adds lyrics to vectorstore."""
240
- global personal_vectorstore
241
- music_library_path = PERSISTENT_MEMORY_PATH / "Music Library"
242
- os.makedirs(music_library_path, exist_ok=True)
243
-
244
- manifest_path = PERSONAL_MUSIC_BASE / "music_manifest.json"
245
- manifest = {}
246
- if manifest_path.exists():
247
- with open(manifest_path, "r") as f: manifest = json.load(f)
248
-
249
- existing_sources = set()
250
- if personal_vectorstore and hasattr(personal_vectorstore.docstore, '_dict'):
251
- for doc in personal_vectorstore.docstore._dict.values():
252
- existing_sources.add(doc.metadata.get("source"))
253
-
254
- print("Scanning 'Music Library' folder for new songs...")
255
- filename_pattern = re.compile(r'^(.*?) - (.*?) - (.*?)\.(mp3|wav|m4a|ogg|flac)$', re.IGNORECASE)
256
-
257
- synced_count = 0
258
- docs_to_add = []
259
- for filename in os.listdir(music_library_path):
260
- song_id = filename.replace(" ", "_").lower()
261
- if song_id in manifest and filename in existing_sources:
262
- continue
263
-
264
- match = filename_pattern.match(filename)
265
- if match:
266
- print(f" - Found new song to sync: {filename}")
267
- title, artist, tag = match.groups()[:3]
268
-
269
- source_path = music_library_path / filename
270
- dest_path = PERSONAL_MUSIC_BASE / filename
271
- if not os.path.exists(dest_path):
272
- shutil.copy2(str(source_path), str(dest_path))
273
-
274
- # Add to manifest for playback system
275
- song_metadata = {"title": title.strip(), "artist": artist.strip(), "moods": [tag.strip().lower()], "filepath": str(dest_path)}
276
- manifest[song_id] = song_metadata
277
-
278
- # --- NEW HYBRID LOGIC: Transcribe and prep for vectorstore ---
279
- # Transcribe and prep for semantic memory system (vectorstore)
280
- if filename not in existing_sources:
281
- try:
282
- print(f" - Transcribing '{title}' for memory bank...")
283
- lyrics = transcribe_audio(str(dest_path))
284
- content_for_rag = (
285
- f"Title: Song - {song_metadata['title']}\n"
286
- f"Artist: {song_metadata['artist']}\n"
287
- f"Moods: {', '.join(song_metadata['moods'])}\n\n"
288
- f"Lyrics:\n{lyrics}"
289
- )
290
- docs_to_add.extend(parse_and_tag_entries(content_for_rag, source=filename, settings={}))
291
- except Exception as e:
292
- print(f" - WARNING: Failed to transcribe {filename} for memory bank. Error: {e}")
293
- # --- END OF NEW HYBRID LOGIC ---
294
- synced_count += 1
295
-
296
- if synced_count > 0:
297
- with open(manifest_path, "w") as f: json.dump(manifest, f, indent=2)
298
- print(f"Successfully synced {synced_count} new song(s) to the music manifest.")
299
-
300
- if docs_to_add:
301
- if personal_vectorstore is None:
302
- personal_vectorstore = build_or_load_vectorstore(docs_to_add, PERSONAL_INDEX_PATH, is_personal=True)
303
- else:
304
- personal_vectorstore.add_documents(docs_to_add)
305
- personal_vectorstore.save_local(PERSONAL_INDEX_PATH)
306
- print(f"Successfully added lyrics for {len(docs_to_add)} song(s) to the personal vectorstore.")
307
-
308
-
309
- def canonical_theme(tk: str) -> str: return tk if tk in CONFIG["themes"] else "All"
310
- def theme_upload_dir(theme: str) -> str:
311
- p = UPLOADS_BASE / f"theme_{canonical_theme(theme).replace(' ', '').lower()}"
312
- p.mkdir(exist_ok=True)
313
- return str(p)
314
- def load_manifest(theme: str) -> Dict[str, Any]:
315
- p = os.path.join(theme_upload_dir(theme), "manifest.json")
316
- if os.path.exists(p):
317
- try:
318
- with open(p, "r", encoding="utf-8") as f: return json.load(f)
319
- except Exception: pass
320
- return {"files": {}}
321
- def save_manifest(theme: str, man: Dict[str, Any]):
322
- with open(os.path.join(theme_upload_dir(theme), "manifest.json"), "w", encoding="utf-8") as f: json.dump(man, f, indent=2)
323
- def list_theme_files(theme: str) -> List[tuple[str, bool]]:
324
- man = load_manifest(theme)
325
- base = theme_upload_dir(theme)
326
- found = [(n, bool(e)) for n, e in man.get("files", {}).items() if os.path.exists(os.path.join(base, n))]
327
- existing = {n for n, e in found}
328
- for name in sorted(os.listdir(base)):
329
- if name not in existing and os.path.isfile(os.path.join(base, name)): found.append((name, False))
330
- man["files"] = dict(found)
331
- save_manifest(theme, man)
332
- return found
333
- def copy_into_theme(theme: str, src_path: str) -> str:
334
- fname = os.path.basename(src_path)
335
- dest = os.path.join(theme_upload_dir(theme), fname)
336
- shutil.copy2(src_path, dest)
337
- return dest
338
- def seed_files_into_theme(theme: str):
339
- SEED_FILES = [("sample_data/caregiving_tips.txt", True), ("sample_data/the_father_segments_enriched_harmonized_plus.jsonl", True), ("sample_data/still_alice_enriched_harmonized_plus.jsonl", True), ("sample_data/away_from_her_enriched_harmonized_plus.jsonl", True), ("sample_data/alive_inside_enriched_harmonized.jsonl", True)]
340
- man, changed = load_manifest(theme), False
341
- for path, enable in SEED_FILES:
342
- if not os.path.exists(path): continue
343
- fname = os.path.basename(path)
344
- if not os.path.exists(os.path.join(theme_upload_dir(theme), fname)):
345
- copy_into_theme(theme, path)
346
- man["files"][fname] = bool(enable)
347
- changed = True
348
- if changed: save_manifest(theme, man)
349
- #def ensure_index(theme='All'):
350
- # theme = canonical_theme(theme)
351
- # if theme in vectorstores: return vectorstores[theme]
352
- # upload_dir = theme_upload_dir(theme)
353
- # enabled_files = [os.path.join(upload_dir, n) for n, enabled in list_theme_files(theme) if enabled]
354
- # index_path = THEME_PATHS.get(theme)
355
- # vectorstores[theme] = bootstrap_vectorstore(sample_paths=enabled_files, index_path=index_path)
356
- # return vectorstores[theme]
357
-
358
-
359
- # NEW Fix Check the cache FIRST. If a valid index is already loaded, return it immediately.
360
- def ensure_index(theme='All'):
361
- """
362
- Ensures a vector store for a given theme is loaded and ready.
363
- This version has corrected caching and logging logic.
364
- """
365
- theme = canonical_theme(theme)
366
-
367
- # --- START: DEFINITIVE FIX ---
368
- # 1. Check the cache FIRST. If a valid index is already loaded, return it immediately.
369
- if theme in vectorstores and vectorstores[theme] and hasattr(vectorstores[theme].docstore, '_dict') and len(vectorstores[theme].docstore._dict) > 1:
370
- return vectorstores[theme]
371
- # --- END: DEFINITIVE FIX ---
372
-
373
- print(f"DEBUG (ensure_index): Building or rebuilding index for theme '{theme}'.")
374
- upload_dir = theme_upload_dir(theme)
375
- enabled_files = [os.path.join(upload_dir, n) for n, enabled in list_theme_files(theme) if enabled]
376
- index_path = THEME_PATHS.get(theme)
377
-
378
- try:
379
- # 2. Bootstrap the vector store from the files on disk.
380
- vs = bootstrap_vectorstore(sample_paths=enabled_files, index_path=index_path)
381
-
382
- # 3. Perform the status check and logging AFTER the bootstrap is complete.
383
- if vs and hasattr(vs.docstore, '_dict'):
384
- doc_count = len(vs.docstore._dict)
385
- if doc_count > 1:
386
- print(f" - ✅ SUCCESS: Index for theme '{theme}' loaded with {doc_count} documents.")
387
- elif doc_count == 1 and list(vs.docstore._dict.values())[0].metadata.get("source") != "placeholder":
388
- print(f" - ✅ SUCCESS: Index for theme '{theme}' loaded with 1 document.")
389
- else:
390
- print(f" - ⚠️ WARNING: Index for theme '{theme}' is empty or contains only a placeholder.")
391
-
392
- # 4. Save the newly built index to the cache.
393
- vectorstores[theme] = vs
394
- return vectorstores[theme]
395
-
396
- except Exception as e:
397
- print(f" - ❌ CRITICAL FAILURE in ensure_index for theme '{theme}': {e}")
398
- return build_or_load_vectorstore([], index_path, is_personal=False)
399
-
400
-
401
- # --- Gradio Callbacks ---
402
- # In app.py, modify the collect_settings function
403
-
404
- def collect_settings(*args):
405
- keys = ["role", "patient_name", "caregiver_name", "tone", "language", "tts_lang", "temperature",
406
- # --- ADD "disease_stage" to this list ---
407
- "disease_stage",
408
- "behaviour_tag", "emotion_tag", "topic_tag", "active_theme", "tts_on", "debug_mode"]
409
- return dict(zip(keys, args))
410
-
411
-
412
- # In app.py, replace the entire parse_and_tag_entries function.
413
- def parse_and_tag_entries(text_content: str, source: str, settings: dict = None) -> List[Document]:
414
- docs_to_add = []
415
- # This logic correctly handles both simple text and complex journal entries
416
- entries = re.split(r'\n(?:---|--|-|-\*-|-\.-)\n', text_content)
417
- if len(entries) == 1 and "title:" not in entries[0].lower() and "content:" not in entries[0].lower():
418
- entries = [text_content] # Treat simple text as a single entry
419
-
420
- for entry in entries:
421
- if not entry.strip(): continue
422
-
423
- lines = entry.strip().split('\n')
424
- title_line = lines[0].split(':', 1)
425
- title = title_line[1].strip() if len(title_line) > 1 and "title:" in lines[0].lower() else "Untitled Text Entry"
426
- content_part = "\n".join(lines[1:])
427
- content = content_part.split(':', 1)[1].strip() if "content:" in content_part.lower() else content_part.strip() or entry.strip()
428
-
429
- if not content: continue
430
-
431
- full_content = f"Title: {title}\n\nContent: {content}"
432
-
433
- detected_tags = detect_tags_from_query(
434
- content, nlu_vectorstore=nlu_vectorstore,
435
- behavior_options=CONFIG["behavior_tags"], emotion_options=CONFIG["emotion_tags"],
436
- topic_options=CONFIG["topic_tags"], context_options=CONFIG["context_tags"],
437
- settings=settings
438
- )
439
-
440
- metadata = {"source": source, "title": title}
441
-
442
- # --- START: CORRECTED METADATA ASSIGNMENT ---
443
- if detected_tags.get("detected_behaviors"):
444
- metadata["behaviors"] = [b.lower() for b in detected_tags["detected_behaviors"]]
445
- detected_emotion = detected_tags.get("detected_emotion")
446
- if detected_emotion and detected_emotion != "None":
447
- metadata["emotion"] = detected_emotion.lower()
448
-
449
- # Correctly handle the plural "detected_topics" key and list value
450
- detected_topics = detected_tags.get("detected_topics")
451
- if detected_topics:
452
- metadata["topic_tags"] = [t.lower() for t in detected_topics]
453
-
454
- if detected_tags.get("detected_contexts"):
455
- metadata["context_tags"] = [c.lower() for c in detected_tags["detected_contexts"]]
456
- # --- END: CORRECTED METADATA ASSIGNMENT ---
457
-
458
- docs_to_add.append(Document(page_content=full_content, metadata=metadata))
459
-
460
- return docs_to_add
461
-
462
-
463
- def handle_add_knowledge(title, text_input, file_input, image_input, yt_url, settings):
464
- global personal_vectorstore
465
- docs_to_add = []
466
- source, content = "Unknown", ""
467
- if text_input and text_input.strip():
468
- source, content = "Text Input", f"Title: {title or 'Untitled'}\n\nContent: {text_input}"
469
- elif file_input:
470
- source = os.path.basename(file_input.name)
471
- if file_input.name.lower().endswith('.txt'):
472
- with open(file_input.name, 'r', encoding='utf-8') as f: content = f.read()
473
- else:
474
- transcribed = transcribe_audio(file_input.name)
475
- content = f"Title: {title or 'Audio/Video Note'}\n\nContent: {transcribed}"
476
- elif image_input:
477
- source, description = "Image Input", describe_image(image_input)
478
- content = f"Title: {title or 'Image Note'}\n\nContent: {description}"
479
- elif yt_url and ("youtube.com" in yt_url or "youtu.be" in yt_url):
480
- try:
481
- yt = YouTube(yt_url)
482
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_audio_file:
483
- yt.streams.get_audio_only().download(filename=temp_audio_file.name)
484
- transcribed = transcribe_audio(temp_audio_file.name)
485
- os.remove(temp_audio_file.name)
486
- source, content = f"YouTube: {yt.title}", f"Title: {title or yt.title}\n\nContent: {transcribed}"
487
- except Exception as e:
488
- return f"Error processing YouTube link: {e}"
489
- else:
490
- return "Please provide content to add."
491
- if content:
492
- docs_to_add = parse_and_tag_entries(content, source, settings=settings)
493
- if not docs_to_add: return "No processable content found to add."
494
- if personal_vectorstore is None:
495
- personal_vectorstore = build_or_load_vectorstore(docs_to_add, PERSONAL_INDEX_PATH, is_personal=True)
496
- else:
497
- personal_vectorstore.add_documents(docs_to_add)
498
- personal_vectorstore.save_local(PERSONAL_INDEX_PATH)
499
- return f"Successfully added {len(docs_to_add)} new memory/memories."
500
-
501
-
502
- # In app.py, add this new handler function
503
-
504
- def handle_add_music(file, title, artist, mood):
505
- if not all([file, title, artist, mood]):
506
- return "Please fill out all fields."
507
-
508
- # Save the audio file
509
- filename = os.path.basename(file.name)
510
- dest_path = PERSONAL_MUSIC_BASE / filename
511
- shutil.copy2(file.name, str(dest_path))
512
-
513
- # Save the metadata to a manifest file
514
- manifest_path = PERSONAL_MUSIC_BASE / "music_manifest.json"
515
- manifest = {}
516
- if manifest_path.exists():
517
- with open(manifest_path, "r") as f:
518
- manifest = json.load(f)
519
-
520
- song_id = filename.replace(" ", "_").lower()
521
- manifest[song_id] = {
522
- "title": title.strip(),
523
- "artist": artist.strip(),
524
- # "moods": [m.strip().lower() for m in mood.split(",")],
525
- "moods": [m.lower() for m in mood], # Correctly handles the list from the dropdown
526
- "filepath": str(dest_path) # Store the full path for backend access
527
- }
528
-
529
- with open(manifest_path, "w") as f:
530
- json.dump(manifest, f, indent=2)
531
-
532
- return f"Successfully added '{title}' to the music library."
533
-
534
- # In app.py, add these two new functions (e.g., after the handle_add_music function)
535
-
536
- def list_music_library():
537
- """Loads the music manifest and formats it for the Gradio UI."""
538
- manifest_path = PERSONAL_MUSIC_BASE / "music_manifest.json"
539
- if not manifest_path.exists():
540
- return gr.update(value=[["Library is empty", "", ""]]), gr.update(choices=[], value=None)
541
-
542
- with open(manifest_path, "r") as f:
543
- manifest = json.load(f)
544
-
545
- if not manifest:
546
- return gr.update(value=[["Library is empty", "", ""]]), gr.update(choices=[], value=None)
547
-
548
- display_data = [[data['title'], data['artist'], ", ".join(data['moods'])] for data in manifest.values()]
549
-
550
- # Use the song's unique ID (the key in the manifest) for the delete dropdown
551
- delete_choices = list(manifest.keys())
552
-
553
- return gr.update(value=display_data), gr.update(choices=delete_choices, value=None)
554
-
555
- def delete_music_from_library(song_id_to_delete):
556
- """Deletes a song from the manifest, the audio file, and the vectorstore."""
557
- global personal_vectorstore
558
- if not song_id_to_delete:
559
- return "No music selected to delete."
560
-
561
- # 1. Remove from manifest and delete audio file
562
- manifest_path = PERSONAL_MUSIC_BASE / "music_manifest.json"
563
- if not manifest_path.exists(): return "Error: Music manifest not found."
564
-
565
- with open(manifest_path, "r") as f: manifest = json.load(f)
566
-
567
- song_to_delete = manifest.pop(song_id_to_delete, None)
568
- if not song_to_delete: return f"Error: Could not find song ID {song_id_to_delete} in manifest."
569
-
570
- with open(manifest_path, "w") as f: json.dump(manifest, f, indent=2)
571
-
572
- try:
573
- os.remove(song_to_delete['filepath'])
574
- except OSError as e:
575
- print(f"Error deleting audio file {song_to_delete['filepath']}: {e}")
576
-
577
- # 2. Remove lyrics from the personal vectorstore
578
- if personal_vectorstore and hasattr(personal_vectorstore.docstore, '_dict'):
579
- filename_to_delete = os.path.basename(song_to_delete['filepath'])
580
- all_docs = list(personal_vectorstore.docstore._dict.values())
581
-
582
- # Find the document whose source matches the audio filename
583
- docs_to_keep = [d for d in all_docs if d.metadata.get("source") != filename_to_delete]
584
-
585
- if len(all_docs) > len(docs_to_keep):
586
- if not docs_to_keep: # If it was the last doc
587
- if os.path.isdir(PERSONAL_INDEX_PATH): shutil.rmtree(PERSONAL_INDEX_PATH)
588
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
589
- else:
590
- new_vs = FAISS.from_documents(docs_to_keep, _default_embeddings())
591
- new_vs.save_local(PERSONAL_INDEX_PATH)
592
- personal_vectorstore = new_vs
593
- return f"Successfully deleted '{song_to_delete['title']}' from the library and memory bank."
594
-
595
- return f"Successfully deleted '{song_to_delete['title']}' from the music library."
596
-
597
-
598
- def chat_fn(user_text, audio_file, settings, chat_history):
599
-
600
- # --- ADD THIS DEBUG BLOCK AT THE TOP ---
601
- print("\n" + "="*50)
602
- print(f"[DEBUG app.py] chat_fn received settings: {settings}")
603
- print("="*50 + "\n")
604
- # --- END OF ADDITION ---
605
-
606
- global personal_vectorstore
607
- question = (user_text or "").strip()
608
- if audio_file and not question:
609
- try:
610
- question = transcribe_audio(audio_file, lang=CONFIG["languages"].get(settings.get("tts_lang", "English"), "en"))
611
- except Exception as e:
612
- err_msg = f"Audio Error: {e}" if settings.get("debug_mode") else "Sorry, I couldn't understand the audio."
613
- chat_history.append({"role": "assistant", "content": err_msg})
614
- return "", None, chat_history
615
-
616
- if not question:
617
- return "", None, chat_history
618
-
619
- # --- START FIX 1: Correctly process the incoming chat_history (list of dicts) ---
620
- # The incoming chat_history is already in the desired format for the API,
621
- # we just need to filter out our special system messages (like sources).
622
- api_chat_history = [
623
- msg for msg in chat_history
624
- if msg.get("content") and not msg["content"].strip().startswith("*(")
625
- ]
626
-
627
- # Append the new user question to the history that will be displayed in the UI
628
- chat_history.append({"role": "user", "content": question})
629
- # --- END FIX 1 ---
630
-
631
- # NEW
632
- query_type = route_query_type(question, severity=settings.get("disease_stage", "Default: Mild Stage"))
633
- # query_type = route_query_type(question)
634
- # --- ADD THIS DEBUG PRINT ---
635
- print(f"[DEBUG] Router classified query as: {query_type}")
636
- # --- END OF ADDITION ---
637
-
638
-
639
- final_tags = { "scenario_tag": None, "emotion_tag": None, "topic_tag": None, "context_tags": [] }
640
- manual_behavior = settings.get("behaviour_tag", "None")
641
- manual_emotion = settings.get("emotion_tag", "None")
642
- manual_topic = settings.get("topic_tag", "None")
643
-
644
- auto_detected_context = ""
645
- if not all(m == "None" for m in [manual_behavior, manual_emotion, manual_topic]):
646
- # --- ADD THIS DEBUG PRINT ---
647
- print(f"[DEBUG app.py] Manual override DETECTED. Behavior='{manual_behavior}', Emotion='{manual_emotion}', Topic='{manual_topic}'")
648
- # --- END OF ADDITION ---
649
-
650
- final_tags["scenario_tag"] = manual_behavior if manual_behavior != "None" else None
651
- final_tags["emotion_tag"] = manual_emotion if manual_emotion != "None" else None
652
- final_tags["topic_tag"] = manual_topic if manual_topic != "None" else None
653
-
654
- # NEW: Expand detecting emotions and behaviors for caregiving to music playing
655
- # whenever a request to play music, the system will first analyze their query to detect an underlying emotion or behavior
656
- elif "caregiving_scenario" in query_type or "play_music_request" in query_type:
657
-
658
- # --- NEW DEBUG BLOCK: Print inputs before calling NLU ---
659
- print("\n--- [DEBUG app.py] Preparing to call NLU ---")
660
- print(f" - Query to Analyze: '{question}'")
661
- print(f" - NLU Vectorstore Loaded: {nlu_vectorstore is not None}")
662
- print(f" - Current Settings Passed: {settings}")
663
- print("------------------------------------------")
664
- # --- END OF NEW DEBUG BLOCK ---
665
-
666
- detected_tags = detect_tags_from_query(
667
- question, nlu_vectorstore=nlu_vectorstore, behavior_options=CONFIG["behavior_tags"],
668
- emotion_options=CONFIG["emotion_tags"], topic_options=CONFIG["topic_tags"],
669
- context_options=CONFIG["context_tags"], settings=settings)
670
-
671
- # --- ADD THIS DEBUG PRINT ---
672
- print(f"[DEBUG app.py] Raw NLU output: {detected_tags}")
673
- # --- END OF ADDITION ---
674
-
675
- behaviors = detected_tags.get("detected_behaviors")
676
- final_tags["scenario_tag"] = behaviors[0] if behaviors else None
677
- final_tags["emotion_tag"] = detected_tags.get("detected_emotion")
678
- final_tags["topic_tag"] = detected_tags.get("detected_topic")
679
- final_tags["context_tags"] = detected_tags.get("detected_contexts", [])
680
-
681
- # --- ADD THIS DEBUG PRINT ---
682
- print(f"[DEBUG] NLU detected tags: {final_tags}")
683
- # --- END OF ADDITION ---
684
-
685
- detected_parts = [f"{k.split('_')[1]}=`{v}`" for k, v in final_tags.items() if v and v != "None" and v != []]
686
- if detected_parts:
687
- auto_detected_context = f"*(Auto-detected context: {', '.join(detected_parts)})*"
688
-
689
- vs_general = ensure_index(settings.get("active_theme", "All"))
690
- if personal_vectorstore is None:
691
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
692
-
693
- # OLD rag_settings = {k: settings.get(k) for k in ["role", "temperature", "language", "patient_name", "caregiver_name", "tone"]}
694
- # NEW add "disease_stage"
695
- # rag_settings = {k: settings.get(k) for k in ["role", "temperature", "language", "patient_name", "caregiver_name", "tone", "disease_stage"]}
696
-
697
- # First, construct the path to the manifest file.
698
- manifest_path_str = str(PERSONAL_MUSIC_BASE / "music_manifest.json")
699
-
700
- # Then, gather all the settings from the UI into the dictionary.
701
- rag_settings = {k: settings.get(k) for k in ["role", "temperature", "language", "patient_name", "caregiver_name", "tone", "disease_stage"]}
702
-
703
- # Finally, add the special manifest path to that same dictionary.
704
- rag_settings["music_manifest_path"] = manifest_path_str
705
-
706
- # chain = make_rag_chain(vs_general, personal_vectorstore, playbook_vectorstore, **rag_settings)
707
- # NEW <-- Pass the playbook vectorstore here
708
- chain = make_rag_chain(vs_general, personal_vectorstore, playbook_vectorstore, **rag_settings)
709
-
710
- response = answer_query(chain, question, query_type=query_type, chat_history=api_chat_history, **final_tags)
711
-
712
- # --- MUSIC PLAYBACK LOGIC START ---
713
-
714
- # 1. Extract the text answer and the potential music file path from the agent's response.
715
- answer = response.get("answer", "[No answer found]")
716
- audio_playback_url = response.get("audio_playback_url")
717
-
718
- # 2. Append the text part of the response to the chat history so the user sees it.
719
- chat_history.append({"role": "assistant", "content": answer})
720
-
721
- if auto_detected_context:
722
- chat_history.append({"role": "assistant", "content": auto_detected_context})
723
- if response.get("sources"):
724
- chat_history.append({"role": "assistant", "content": f"*(Sources used: {', '.join(response['sources'])})*"})
725
-
726
- # 3. Decide what to play in the audio component: music takes priority over TTS.
727
- audio_out_update = None
728
- if audio_playback_url:
729
- # If a music URL was returned, update the audio component to play that music file.
730
- song_title = os.path.basename(audio_playback_url)
731
- audio_out_update = gr.update(value=audio_playback_url, visible=True, label=f"Now Playing: {song_title}", autoplay=True)
732
- elif settings.get("tts_on") and answer:
733
- # Otherwise, if no music is playing and TTS is on, fall back to reading the text answer aloud.
734
- tts_file = synthesize_tts(answer, lang=CONFIG["languages"].get(settings.get("tts_lang"), "en"))
735
- audio_out_update = gr.update(value=tts_file, visible=bool(tts_file), label="Response Audio", autoplay=True)
736
-
737
- # 4. Return all the updates for the Gradio UI.
738
- return "", audio_out_update, chat_history
739
-
740
- # --- MUSIC PLAYBACK LOGIC END ---
741
-
742
-
743
- # The save_chat_to_memory function incorrectly assumes the history is
744
- # a list of tuples, like [(True, "..."), (False, "...")]
745
- # However, The chat_fn function correctly builds the chat_history as
746
- # a list of dictionaries, like this:
747
- # [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]
748
- # To correctly parse the list of dictionaries.
749
- def save_chat_to_memory(chat_history):
750
- if not chat_history:
751
- return "Nothing to save."
752
-
753
- # --- START: MODIFIED LOGIC ---
754
- # Correctly processes the list of dictionaries from the chatbot
755
- formatted_chat = [
756
- f"{msg.get('role', 'assistant').capitalize()}: {msg.get('content', '').strip()}"
757
- for msg in chat_history
758
- if isinstance(msg, dict) and msg.get('content') and not msg.get('content', '').strip().startswith("*(")
759
- ]
760
- # --- END: MODIFIED LOGIC ---
761
-
762
- if not formatted_chat:
763
- return "No conversation to save."
764
-
765
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
766
- title = f"Conversation from {timestamp}"
767
- full_content = f"Title: {title}\n\nContent:\n" + "\n".join(formatted_chat)
768
- doc = Document(page_content=full_content, metadata={"source": "Saved Chat", "title": title})
769
-
770
- global personal_vectorstore
771
- if personal_vectorstore is None:
772
- personal_vectorstore = build_or_load_vectorstore([doc], PERSONAL_INDEX_PATH, is_personal=True)
773
- else:
774
- personal_vectorstore.add_documents([doc])
775
-
776
- personal_vectorstore.save_local(PERSONAL_INDEX_PATH)
777
- return f"Conversation from {timestamp} saved."
778
-
779
-
780
- def list_personal_memories():
781
- global personal_vectorstore
782
- if personal_vectorstore is None or not hasattr(personal_vectorstore.docstore, '_dict') or not personal_vectorstore.docstore._dict:
783
- return gr.update(value=[["No memories", "", ""]]), gr.update(choices=[], value=None)
784
- docs = list(personal_vectorstore.docstore._dict.values())
785
- return gr.update(value=[[d.metadata.get('title', '...'), d.metadata.get('source', '...'), d.page_content] for d in docs]), gr.update(choices=[d.page_content for d in docs])
786
- def delete_personal_memory(memory_to_delete):
787
- global personal_vectorstore
788
- if personal_vectorstore is None or not memory_to_delete: return "No memory selected."
789
- all_docs = list(personal_vectorstore.docstore._dict.values())
790
- docs_to_keep = [d for d in all_docs if d.page_content != memory_to_delete]
791
- if len(all_docs) == len(docs_to_keep): return "Error: Could not find memory."
792
- if not docs_to_keep:
793
- if os.path.isdir(PERSONAL_INDEX_PATH): shutil.rmtree(PERSONAL_INDEX_PATH)
794
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
795
- else:
796
- new_vs = FAISS.from_documents(docs_to_keep, _default_embeddings())
797
- new_vs.save_local(PERSONAL_INDEX_PATH)
798
- personal_vectorstore = new_vs
799
- return "Successfully deleted memory."
800
-
801
- # --- EVALUATION FUNCTIONS: move them into evaluate.py
802
- # def evaluate_nlu_tags(expected: Dict[str, Any], actual: Dict[str, Any], tag_key: str, expected_key_override: str = None) -> Dict[str, float]:
803
- # def _parse_judge_json(raw_str: str) -> dict | None:
804
- # def run_comprehensive_evaluation():
805
-
806
- def upload_knowledge(files, theme):
807
- for f in files: copy_into_theme(theme, f.name)
808
- if theme in vectorstores: del vectorstores[theme]
809
- return f"Uploaded {len(files)} file(s)."
810
- def save_file_selection(theme, enabled):
811
- man = load_manifest(theme)
812
- for fname in man['files']: man['files'][fname] = fname in enabled
813
- save_manifest(theme, man)
814
- if theme in vectorstores: del vectorstores[theme]
815
- return f"Settings saved for theme '{theme}'."
816
- def refresh_file_list_ui(theme):
817
- files = list_theme_files(theme)
818
- return gr.update(choices=[f for f, _ in files], value=[f for f, en in files if en]), f"Found {len(files)} file(s)."
819
-
820
- def auto_setup_on_load(theme):
821
- if not os.listdir(theme_upload_dir(theme)): seed_files_into_theme(theme)
822
-
823
- # --- START: DEFINITIVE FIX ---
824
- # This now provides the correct number and order of default settings.
825
- settings = collect_settings(
826
- "patient", # role
827
- "", # patient_name
828
- "", # caregiver_name
829
- "warm", # tone
830
- "English", # language
831
- "English", # tts_lang
832
- 0.7, # temperature
833
- "Default: Mild Stage", # disease_stage <-- Correctly set
834
- "None", # behaviour_tag
835
- "None", # emotion_tag
836
- "None", # topic_tag
837
- "All", # active_theme <-- Correctly set
838
- True, # tts_on
839
- False # debug_mode
840
- )
841
- # --- END: DEFINITIVE FIX ---
842
-
843
- files_ui, status = refresh_file_list_ui(theme)
844
- return settings, files_ui, status
845
-
846
- def test_save_file():
847
- try:
848
- path = PERSONAL_DATA_BASE / "persistence_test.txt"
849
- path.write_text(f"File saved at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
850
- return f"✅ Success! Wrote test file to: {path}"
851
- except Exception as e: return f"❌ Error! Failed to write file: {e}"
852
- def check_test_file():
853
- path = PERSONAL_DATA_BASE / "persistence_test.txt"
854
- if path.exists(): return f"✅ Success! Found test file. Contents: '{path.read_text()}'"
855
- return f"❌ Failure. Test file not found at: {path}"
856
-
857
-
858
- # --- START: DEFINITIVE FIX ---
859
- def get_current_vectorstore_and_evaluate(vs_general_stale, nlu_vs_stale, playbook_vs_stale, config, storage_path):
860
- """
861
- This helper function ensures we get the LATEST version of ALL global
862
- vectorstores right before running the evaluation.
863
- """
864
- # Re-fetch the CURRENT versions of the global variables, ignoring the stale
865
- # ones passed in from the gr.State UI components.
866
- global personal_vectorstore, nlu_vectorstore, playbook_vectorstore, vectorstores
867
-
868
- # --- START: DEFINITIVE FIX ---
869
- # Fetch the CURRENT, fully-loaded 'All' theme index from the global dictionary,
870
- # ignoring the stale one passed into this function.
871
- vs_general_current = vectorstores.get("All")
872
- # --- END: DEFINITIVE FIX ---
873
-
874
- # Debug block to prove the fix
875
- print("\n" + "="*20 + " PRE-EVALUATION CHECK (from app.py) " + "="*20)
876
- print("Fetching CURRENT state of all vector stores...")
877
-
878
- # --- MODIFIED DEBUG PRINT ---
879
- if vs_general_current and hasattr(vs_general_current.docstore, '_dict'):
880
- print(f" - ✅ Current vs_general has {len(vs_general_current.docstore._dict)} documents.")
881
- else:
882
- print(" - ❌ Current vs_general is None or invalid.")
883
- # --- END MODIFICATION ---
884
-
885
- if personal_vectorstore and hasattr(personal_vectorstore.docstore, '_dict'):
886
- print(f" - ✅ Current personal_vectorstore has {len(personal_vectorstore.docstore._dict)} documents.")
887
- else:
888
- print(" - ❌ Current personal_vectorstore is None or invalid.")
889
-
890
- if nlu_vectorstore and hasattr(nlu_vectorstore.docstore, '_dict'):
891
- print(f" - ✅ Current nlu_vectorstore has {len(nlu_vectorstore.docstore._dict)} documents.")
892
- else:
893
- print(" - ❌ Current nlu_vectorstore is None or invalid.")
894
-
895
- if playbook_vectorstore and hasattr(playbook_vectorstore.docstore, '_dict'):
896
- print(f" - ✅ Current playbook_vectorstore has {len(playbook_vectorstore.docstore._dict)} documents.")
897
- else:
898
- print(" - ❌ Current playbook_vectorstore is None or invalid.")
899
-
900
- print("="*78 + "\n")
901
-
902
- # Now call the evaluation function with the CURRENT, fully-loaded vector stores
903
- return run_comprehensive_evaluation(
904
- vs_general=vs_general_current, # <-- Use the correct, current version
905
- vs_personal=personal_vectorstore,
906
- nlu_vectorstore=nlu_vectorstore,
907
- vs_playbook=playbook_vectorstore,
908
- config=config,
909
- storage_path=storage_path
910
- )
911
-
912
- # --- UI Definition ---
913
- CSS = """
914
- .gradio-container { font-size: 14px; }
915
- #chatbot { min-height: 400px; }
916
- #audio_in audio, #audio_out audio { max-height: 40px; }
917
- #audio_in .waveform, #audio_out .waveform { display: none !important; }
918
- #audio_in, #audio_out { min-height: 0px !important; }
919
- """
920
-
921
- # OLD: add allowed_paths so the UI can access the music files
922
- # with gr.Blocks(theme=gr.themes.Soft(), css=CSS, allowed_paths=[str(PERSONAL_MUSIC_BASE)]) as demo:
923
- with gr.Blocks(theme=gr.themes.Soft(), css=CSS) as demo:
924
- settings_state = gr.State({})
925
- with gr.Tab("Chat"):
926
- with gr.Row():
927
- user_text = gr.Textbox(show_label=False, placeholder="Type your message here...", scale=7)
928
- submit_btn = gr.Button("Send", variant="primary", scale=1)
929
- with gr.Row():
930
- audio_in = gr.Audio(sources=["microphone"], type="filepath", label="Voice Input", elem_id="audio_in")
931
- audio_out = gr.Audio(label="Response Audio", autoplay=True, visible=True, elem_id="audio_out")
932
-
933
- chatbot = gr.Chatbot(elem_id="chatbot", label="Conversation", type="messages")
934
- chat_status = gr.Markdown()
935
- with gr.Row():
936
- clear_btn = gr.Button("Clear")
937
- save_btn = gr.Button("Save to Memory")
938
-
939
- with gr.Tab("Personalize"):
940
- gr.Markdown("### **Upload Personal Memory**")
941
- with gr.Accordion("Add Multimodal Data to Personal Memory Bank", open=True):
942
- personal_title = gr.Textbox(label="Title")
943
- personal_text = gr.Textbox(lines=5, label="Text Content")
944
- with gr.Row():
945
- personal_file = gr.File(label="Upload Audio/Video/Text File")
946
- personal_image = gr.Image(type="filepath", label="Upload Image")
947
- personal_yt_url = gr.Textbox(label="Or, provide a YouTube URL")
948
- personal_add_btn = gr.Button("Add Knowledge", variant="primary")
949
- personal_status = gr.Markdown()
950
-
951
- # In app.py, within the "Personalize" Tab
952
- gr.Markdown("### **Upload Personal Music Library**")
953
- with gr.Accordion("Add Music to Personal Memory Bank", open=False):
954
- music_file = gr.File(label="Upload Audio File (.mp3, .wav)", file_types=["audio"])
955
- music_title = gr.Textbox(label="Song Title (e.g., My Way)")
956
- music_artist = gr.Textbox(label="Artist (e.g., Frank Sinatra)")
957
- # music_mood = gr.Textbox(label="Mood Tags (comma-separated, e.g., calm, happy, nostalgic)")
958
- # NEW: Add a dropdown menu music tag selection based on emotion and behavior tags
959
- music_mood = gr.Dropdown(
960
- CONFIG["music_moods"],
961
- label="Select Moods/Contexts for this Song",
962
- multiselect=True
963
- )
964
- music_add_btn = gr.Button("Add Music", variant="primary")
965
- music_status = gr.Markdown()
966
-
967
- gr.Markdown("### **Manage Personal Memory Bank**")
968
- with gr.Accordion("View/Hide Details", open=False):
969
- personal_memory_display = gr.DataFrame(headers=["Title", "Source", "Content"], label="Saved Memories", row_count=(5, "dynamic"))
970
- personal_refresh_btn = gr.Button("Refresh Memories")
971
- personal_delete_selector = gr.Dropdown(label="Select memory to delete", scale=3, interactive=True)
972
- personal_delete_btn = gr.Button("Delete Selected", variant="stop", scale=1)
973
- personal_delete_status = gr.Markdown()
974
-
975
- # --- NEW UI FOR MUSIC MANAGEMENT ---
976
- gr.Markdown("### **Manage Music Library**")
977
- with gr.Accordion("View/Hide Music Details", open=False):
978
- music_library_display = gr.DataFrame(
979
- headers=["Title", "Artist", "Moods"],
980
- label="Music Library",
981
- row_count=(5, "dynamic")
982
- )
983
- music_refresh_btn = gr.Button("Refresh Music List")
984
- music_delete_selector = gr.Dropdown(
985
- label="Select music to delete",
986
- scale=3,
987
- interactive=True
988
- )
989
- music_delete_btn = gr.Button("Delete Selected Music", variant="stop", scale=1)
990
- music_delete_status = gr.Markdown()
991
- # --- END OF NEW UI ---
992
-
993
- with gr.Tab("Settings"):
994
- with gr.Group():
995
- gr.Markdown("## Conversation & Persona Settings")
996
- with gr.Row():
997
- role = gr.Radio(CONFIG["roles"], value="patient", label="Your Role")
998
- patient_name = gr.Textbox(label="Patient's Name")
999
- caregiver_name = gr.Textbox(label="Caregiver's Name")
1000
- with gr.Row():
1001
- temperature = gr.Slider(0.0, 1.2, value=0.7, step=0.1, label="Creativity")
1002
- tone = gr.Dropdown(CONFIG["tones"], value="warm", label="Response Tone")
1003
- with gr.Row():
1004
- # --- ADD THIS NEW DROPDOWN ---
1005
- # disease_stage = gr.Dropdown(CONFIG["disease_stages"], value="Normal / Unspecified", label="Assumed Disease Stage")
1006
- disease_stage = gr.Dropdown(CONFIG["disease_stages"], value="Default: Mild Stage", label="Assumed Disease Stage")
1007
- # --- END OF ADDITION ---
1008
- behaviour_tag = gr.Dropdown(CONFIG["behavior_tags"], value="None", label="Behaviour Filter (Manual)")
1009
- emotion_tag = gr.Dropdown(CONFIG["emotion_tags"], value="None", label="Emotion Filter (Manual)")
1010
- topic_tag = gr.Dropdown(CONFIG["topic_tags"], value="None", label="Topic Tag Filter (Manual)")
1011
- with gr.Accordion("Language, Voice & Debugging", open=False):
1012
- language = gr.Dropdown(list(CONFIG["languages"].keys()), value="English", label="Response Language")
1013
- tts_lang = gr.Dropdown(list(CONFIG["languages"].keys()), value="English", label="Voice Language")
1014
- tts_on = gr.Checkbox(True, label="Enable Voice Response")
1015
- debug_mode = gr.Checkbox(False, label="Show Debug Info")
1016
- gr.Markdown("--- \n ## General Knowledge Base Management")
1017
- with gr.Row():
1018
- with gr.Column(scale=1):
1019
- files_in = gr.File(file_count="multiple", file_types=[".jsonl", ".txt"], label="Upload Knowledge Files")
1020
- upload_btn = gr.Button("Upload to Theme")
1021
- seed_btn = gr.Button("Import Sample Data")
1022
- mgmt_status = gr.Markdown()
1023
- with gr.Column(scale=2):
1024
- active_theme = gr.Radio(CONFIG["themes"], value="All", label="Active Knowledge Theme")
1025
- files_box = gr.CheckboxGroup(choices=[], label="Enable Files for Selected Theme")
1026
- with gr.Row():
1027
- save_files_btn = gr.Button("Save Selection", variant="primary")
1028
- refresh_btn = gr.Button("Refresh List")
1029
- with gr.Accordion("Persistence Test", open=False):
1030
- test_save_btn = gr.Button("1. Run Persistence Test (Save File)")
1031
- check_save_btn = gr.Button("3. Check for Test File")
1032
- test_status = gr.Markdown()
1033
-
1034
- # --- UPDATED TESTING TAB ---
1035
- with gr.Tab("Testing"):
1036
- gr.Markdown("## Comprehensive Performance Evaluation")
1037
- gr.Markdown("Click the button below to run a full evaluation on all test fixtures. This will test NLU (Routing & Tagging) and generate RAG responses for manual review.")
1038
-
1039
- run_comprehensive_btn = gr.Button("Run Comprehensive Evaluation", variant="primary")
1040
-
1041
- batch_summary_md = gr.Markdown("### Evaluation Summary: Not yet run.")
1042
-
1043
- comprehensive_results_df = gr.DataFrame(
1044
- label="Detailed Evaluation Results",
1045
- elem_id="comprehensive_results_df",
1046
- headers=[
1047
- "Test ID","Title","Route Correct?","Expected Route","Actual Route",
1048
- "Behavior F1","Emotion F1","Topic F1","Context F1",
1049
- "Generated Answer","Sources","Source Count","Latency (ms)", "Faithfulness"
1050
- ],
1051
- interactive=False
1052
- )
1053
-
1054
-
1055
- # --- Event Wiring ---
1056
- all_settings = [
1057
- # Chat Tab Settings
1058
- role, patient_name, caregiver_name, tone, language, tts_lang, temperature,
1059
- # Disease Stage & Manual Filters
1060
- disease_stage, behaviour_tag, emotion_tag, topic_tag,
1061
- # Knowledge Base & Debug
1062
- active_theme, tts_on, debug_mode
1063
- ]
1064
- settings_state = gr.State({})
1065
-
1066
- # In app.py, replace the event wiring loop right after the all_settings list
1067
-
1068
- for component in all_settings:
1069
- component.change(fn=collect_settings, inputs=all_settings, outputs=settings_state)
1070
-
1071
- submit_btn.click(fn=chat_fn, inputs=[user_text, audio_in, settings_state, chatbot], outputs=[user_text, audio_out, chatbot])
1072
-
1073
- # for c in all_settings: c.change(fn=collect_settings, inputs=all_settings, outputs=settings_state)
1074
- # submit_btn.click(fn=chat_fn, inputs=[user_text, audio_in, settings_state, chatbot], outputs=[user_text, audio_out, chatbot])
1075
-
1076
- save_btn.click(fn=save_chat_to_memory, inputs=[chatbot], outputs=[chat_status])
1077
- clear_btn.click(lambda: (None, None, [], None, "", ""), outputs=[user_text, audio_out, chatbot, audio_in, user_text, chat_status])
1078
-
1079
- personal_add_btn.click(fn=handle_add_knowledge, inputs=[personal_title, personal_text, personal_file, personal_image, personal_yt_url, settings_state], outputs=[personal_status]).then(lambda: (None, None, None, None, None), outputs=[personal_title, personal_text, personal_file, personal_image, personal_yt_url])
1080
- # Wire the button to the function in the UI event wiring section
1081
- music_add_btn.click(
1082
- fn=handle_add_music,
1083
- inputs=[music_file, music_title, music_artist, music_mood],
1084
- outputs=[music_status]
1085
- )
1086
- # --- NEW EVENT WIRING FOR MUSIC MANAGEMENT ---
1087
- music_refresh_btn.click(
1088
- fn=list_music_library,
1089
- inputs=None,
1090
- outputs=[music_library_display, music_delete_selector]
1091
- )
1092
- music_delete_btn.click(
1093
- fn=delete_music_from_library,
1094
- inputs=[music_delete_selector],
1095
- outputs=[music_delete_status]
1096
- ).then(
1097
- fn=list_music_library,
1098
- inputs=None,
1099
- outputs=[music_library_display, music_delete_selector]
1100
- )
1101
- # --- END OF NEW WIRING ---
1102
-
1103
- personal_refresh_btn.click(fn=list_personal_memories, inputs=None, outputs=[personal_memory_display, personal_delete_selector])
1104
- personal_delete_btn.click(fn=delete_personal_memory, inputs=[personal_delete_selector], outputs=[personal_delete_status]).then(fn=list_personal_memories, inputs=None, outputs=[personal_memory_display, personal_delete_selector])
1105
-
1106
- upload_btn.click(upload_knowledge, inputs=[files_in, active_theme], outputs=[mgmt_status]).then(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
1107
- save_files_btn.click(save_file_selection, inputs=[active_theme, files_box], outputs=[mgmt_status])
1108
- seed_btn.click(seed_files_into_theme, inputs=[active_theme]).then(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
1109
- refresh_btn.click(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
1110
- active_theme.change(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
1111
-
1112
- # NEW .click() event handler
1113
- run_comprehensive_btn.click(
1114
- fn=get_current_vectorstore_and_evaluate,
1115
- inputs=[
1116
- gr.State(ensure_index("All")),
1117
- gr.State(nlu_vectorstore),
1118
- gr.State(playbook_vectorstore),
1119
- gr.State(CONFIG),
1120
- gr.State(STORAGE_ROOT)
1121
- ],
1122
- outputs=[batch_summary_md, comprehensive_results_df, comprehensive_results_df]
1123
- )
1124
-
1125
- demo.load(auto_setup_on_load, inputs=[active_theme], outputs=[settings_state, files_box, mgmt_status])
1126
- demo.load(load_test_fixtures)
1127
- test_save_btn.click(fn=test_save_file, inputs=None, outputs=[test_status])
1128
- check_save_btn.click(fn=check_test_file, inputs=None, outputs=[test_status])
1129
-
1130
- # --- Startup Logic ---
1131
- # --- Function 3: The Startup Orchestrator ---
1132
- def pre_load_indexes():
1133
- """Loads all data sources and runs the auto-loading functions at startup."""
1134
- load_test_fixtures()
1135
-
1136
- print("--- Checking and seeding knowledge files for all themes ---")
1137
- for theme in CONFIG["themes"]:
1138
- theme_dir = theme_upload_dir(theme)
1139
- if not any(f.endswith(('.jsonl', '.txt')) for f in os.listdir(theme_dir)):
1140
- print(f" - Theme '{theme}' has no knowledge files. Seeding with sample data...")
1141
- seed_files_into_theme(theme)
1142
-
1143
- # --- START: DEFINITIVE FIX ---
1144
- # After seeding, we MUST clear any cached (and likely empty)
1145
- # vector store for this theme to force a reload.
1146
- if theme in vectorstores:
1147
- del vectorstores[theme]
1148
- # --- END: DEFINITIVE FIX ---
1149
-
1150
- else:
1151
- print(f" - Theme '{theme}' already contains knowledge files. Skipping seed.")
1152
-
1153
- global personal_vectorstore, nlu_vectorstore, playbook_vectorstore # <-- Add playbook_vectorstore here
1154
- print("Pre-loading all indexes at startup...")
1155
- print(" - Loading NLU examples index...")
1156
- nlu_vectorstore = bootstrap_nlu_vectorstore("nlu_training_examples.jsonl", NLU_EXAMPLES_INDEX_PATH)
1157
- print(f" ...NLU index loaded.")
1158
- for theme in CONFIG["themes"]:
1159
- print(f" - Loading general index for theme: '{theme}'")
1160
- try:
1161
- ensure_index(theme)
1162
- print(f" ...'{theme}' theme loaded.")
1163
- except Exception as e:
1164
- print(f" ...Error loading theme '{theme}': {e}")
1165
-
1166
- print(" - Loading personal knowledge index...")
1167
- try:
1168
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
1169
- print(" ...Personal knowledge loaded.")
1170
- except Exception as e:
1171
- print(f" ...Error loading personal knowledge: {e}")
1172
-
1173
- # --- START: ADD PLAYBOOK LOADING LOGIC ---
1174
- print(" - Loading Caregiving Playbook index...")
1175
- try:
1176
- if os.path.exists(PLAYBOOK_SOURCE_PATH):
1177
- playbook_vectorstore = bootstrap_vectorstore(
1178
- sample_paths=[PLAYBOOK_SOURCE_PATH],
1179
- index_path=PLAYBOOK_INDEX_PATH
1180
- )
1181
- print(" ...Caregiving Playbook loaded.")
1182
- else:
1183
- playbook_vectorstore = bootstrap_vectorstore([], index_path=PLAYBOOK_INDEX_PATH)
1184
- print(f" ...WARNING: Playbook source not found at '{PLAYBOOK_SOURCE_PATH}'. Created empty index.")
1185
- except Exception as e:
1186
- print(f" ...Error loading Caregiving Playbook: {e}")
1187
- # --- END: ADD PLAYBOOK LOADING LOGIC ---
1188
-
1189
- load_personal_files_from_folder()
1190
- sync_music_library_from_folder()
1191
-
1192
- print("All indexes and personal files loaded. Application is ready.")
1193
-
1194
-
1195
- # --- START: NEW HELPER FUNCTION TO ORCHESTRATE STARTUP ---
1196
- def run_startup_sequence():
1197
- """
1198
- Ensures all data is loaded in the correct order before the UI is launched.
1199
- """
1200
- # --- START: DEFINITIVE FIX ---
1201
- # We REMOVE the redundant seeding call from here. The loop inside
1202
- # pre_load_indexes() will now handle seeding for ALL themes consistently.
1203
- # print("--- [1/4] Seeding general knowledge files...")
1204
- # seed_files_into_theme('All')
1205
-
1206
- print("--- [1/4] Initializing vector store indexes...")
1207
- pre_load_indexes()
1208
-
1209
- # CRITICAL STEP: This was previously happening too late.
1210
- print("--- [2/4] Loading all personal memories from folder into vector store...")
1211
- load_personal_files_from_folder()
1212
- sync_music_library_from_folder()
1213
-
1214
- print("--- [3/4] Loading test fixtures for evaluation...")
1215
- load_test_fixtures()
1216
- print("\n--- STARTUP COMPLETE. LAUNCHING UI. ---\n")
1217
- # --- END: NEW HELPER FUNCTION ---
1218
-
1219
-
1220
- if __name__ == "__main__":
1221
- # Run the complete, ordered startup sequence
1222
- run_startup_sequence()
1223
-
1224
- # Now, launch the Gradio application, which will have access to all loaded data.
1225
- demo.queue().launch(debug=True, allowed_paths=[str(PERSONAL_MUSIC_BASE)])
1226
-