KeenWoo commited on
Commit
ab3b9d5
Β·
verified Β·
1 Parent(s): 436f5c7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +529 -0
app.py ADDED
@@ -0,0 +1,529 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 # <-- Add this import at the top of your file with the other imports
10
+ import re
11
+
12
+ # --- Agent Imports & Safe Fallbacks ---
13
+ try:
14
+ from alz_companion.agent import (
15
+ bootstrap_vectorstore, make_rag_chain, answer_query, synthesize_tts,
16
+ transcribe_audio, detect_tags_from_query, describe_image, build_or_load_vectorstore,
17
+ _default_embeddings
18
+ )
19
+ from alz_companion.prompts import BEHAVIOUR_TAGS, EMOTION_STYLES
20
+ from langchain.schema import Document
21
+ from langchain_community.vectorstores import FAISS
22
+ AGENT_OK = True
23
+ except Exception as e:
24
+ AGENT_OK = False
25
+ # Define all fallback functions and classes
26
+ def bootstrap_vectorstore(sample_paths=None, index_path="data/"): return object()
27
+ def build_or_load_vectorstore(docs, index_path, is_personal=False): return object()
28
+ def make_rag_chain(vs_general, vs_personal, **kwargs): return lambda q, **k: {"answer": f"(Demo) You asked: {q}", "sources": []}
29
+ def answer_query(chain, q, **kwargs): return chain(q, **kwargs)
30
+ def synthesize_tts(text: str, lang: str = "en"): return None
31
+ def transcribe_audio(filepath: str, lang: str = "en"): return "This is a transcribed message."
32
+ def detect_tags_from_query(query: str, behavior_options: list, emotion_options: list): return {"detected_behavior": "None", "detected_emotion": "None"}
33
+ def describe_image(image_path: str): return "This is a description of an image."
34
+ def _default_embeddings(): return None
35
+ class Document:
36
+ def __init__(self, page_content, metadata):
37
+ self.page_content = page_content
38
+ self.metadata = metadata
39
+ class FAISS:
40
+ def __init__(self):
41
+ self.docstore = type('obj', (object,), {'_dict': {}})()
42
+ BEHAVIOUR_TAGS = {"None": []}
43
+ EMOTION_STYLES = {"None": {}}
44
+ print(f"WARNING: Could not import from alz_companion ({e}). Running in UI-only demo mode.")
45
+
46
+ # --- Centralized Configuration ---
47
+ CONFIG = {
48
+ "themes": ["All", "The Father", "Still Alice", "Away from Her", "General Caregiving"],
49
+ "roles": ["patient", "caregiver"],
50
+ "behavior_tags": ["None"] + list(BEHAVIOUR_TAGS.keys()),
51
+ "emotion_tags": ["None"] + list(EMOTION_STYLES.keys()),
52
+ "languages": {"English": "en", "Chinese": "zh", "Malay": "ms", "French": "fr", "Spanish": "es"},
53
+ "tones": ["warm", "neutral", "formal", "playful"]
54
+ }
55
+
56
+ # --- File Management & Vector Store Logic ---
57
+ INDEX_BASE = os.getenv('INDEX_BASE', 'data')
58
+ UPLOADS_BASE = os.path.join(INDEX_BASE, "uploads")
59
+
60
+ # OPTION A: --- CHANGE THIS LINE ---
61
+ # PERSONAL_INDEX_PATH = os.path.join(str(Path.home()), "AlzCompanionData", "personal_faiss_index")
62
+ PERSONAL_INDEX_PATH = os.path.join(str(Path.home()), "AlzPersonalData", "personal_faiss_index")
63
+ # changing it from the absolute path (designed for a single local computer) to relative path (perfect for Hugging Face Spaces):
64
+ # Does NOT work -> PERSONAL_INDEX_PATH = os.path.join(INDEX_BASE, "personal_faiss_index")
65
+ # OPTION A: --- END CHANGE ---
66
+ # old code PERSONAL_INDEX_PATH = os.path.join(INDEX_BASE, "personal_faiss_index")
67
+ os.makedirs(UPLOADS_BASE, exist_ok=True)
68
+ # OPTION A: Also create the parent directory for the personal index
69
+ os.makedirs(os.path.dirname(PERSONAL_INDEX_PATH), exist_ok=True)
70
+
71
+ # OPTION B: --- Example for macOS or Linux ---
72
+ # OPTION B: PERSONAL_INDEX_PATH = "/Users/YourUsername/AlzCompanionData/personal_faiss_index"
73
+ # OPTION B: Make sure to create the directory
74
+ # OPTION B: os.makedirs(os.path.dirname(PERSONAL_INDEX_PATH), exist_ok=True)
75
+
76
+ THEME_PATHS = {t: os.path.join(INDEX_BASE, f"faiss_index_{t.replace(' ', '').lower()}") for t in CONFIG["themes"]}
77
+ vectorstores = {}
78
+ personal_vectorstore = None
79
+
80
+
81
+ def canonical_theme(tk: str) -> str: return tk if tk in CONFIG["themes"] else "All"
82
+ def theme_upload_dir(theme: str) -> str:
83
+ p = os.path.join(UPLOADS_BASE, f"theme_{canonical_theme(theme).replace(' ', '').lower()}")
84
+ os.makedirs(p, exist_ok=True)
85
+ return p
86
+ def load_manifest(theme: str) -> Dict[str, Any]:
87
+ p = os.path.join(theme_upload_dir(theme), "manifest.json")
88
+ if os.path.exists(p):
89
+ try:
90
+ with open(p, "r", encoding="utf-8") as f: return json.load(f)
91
+ except Exception: pass
92
+ return {"files": {}}
93
+ def save_manifest(theme: str, man: Dict[str, Any]):
94
+ with open(os.path.join(theme_upload_dir(theme), "manifest.json"), "w", encoding="utf-8") as f: json.dump(man, f, indent=2)
95
+ def list_theme_files(theme: str) -> List[tuple[str, bool]]:
96
+ man = load_manifest(theme)
97
+ base = theme_upload_dir(theme)
98
+ found = [(n, bool(e)) for n, e in man.get("files", {}).items() if os.path.exists(os.path.join(base, n))]
99
+ existing = {n for n, e in found}
100
+ for name in sorted(os.listdir(base)):
101
+ if name not in existing and os.path.isfile(os.path.join(base, name)): found.append((name, False))
102
+ man["files"] = dict(found)
103
+ save_manifest(theme, man)
104
+ return found
105
+ def copy_into_theme(theme: str, src_path: str) -> str:
106
+ fname = os.path.basename(src_path)
107
+ dest = os.path.join(theme_upload_dir(theme), fname)
108
+ shutil.copy2(src_path, dest)
109
+ return dest
110
+ def seed_files_into_theme(theme: str):
111
+ SEED_FILES = [
112
+ ("sample_data/caregiving_tips.txt", True),
113
+ ("sample_data/the_father_segments_tagged_with_emotion_hybrid.jsonl", True),
114
+ ("sample_data/still_alice_segments_tagged_with_emotion_hybrid.jsonl", True),
115
+ ("sample_data/away_from_her_segments_tagged_with_emotion_hybrid.jsonl", True)
116
+ ]
117
+ man, changed = load_manifest(theme), False
118
+ for path, enable in SEED_FILES:
119
+ if not os.path.exists(path): continue
120
+ fname = os.path.basename(path)
121
+ if not os.path.exists(os.path.join(theme_upload_dir(theme), fname)):
122
+ copy_into_theme(theme, path)
123
+ man["files"][fname] = bool(enable)
124
+ changed = True
125
+ if changed: save_manifest(theme, man)
126
+
127
+ def ensure_index(theme='All'):
128
+ theme = canonical_theme(theme)
129
+ if theme in vectorstores: return vectorstores[theme]
130
+ upload_dir = theme_upload_dir(theme)
131
+ enabled_files = [os.path.join(upload_dir, n) for n, enabled in list_theme_files(theme) if enabled]
132
+ index_path = THEME_PATHS.get(theme)
133
+ vectorstores[theme] = bootstrap_vectorstore(sample_paths=enabled_files, index_path=index_path)
134
+ return vectorstores[theme]
135
+
136
+ # --- Gradio Callbacks ---
137
+ def collect_settings(*args):
138
+ keys = ["role", "patient_name", "caregiver_name", "tone", "language", "tts_lang", "temperature", "behaviour_tag", "emotion_tag", "active_theme", "tts_on", "debug_mode"]
139
+ return dict(zip(keys, args))
140
+
141
+ def parse_and_tag_entries(text_content: str, source: str) -> List[Document]:
142
+ # Correct separator for the user's file format
143
+ # entries = text_content.split('\nβ€”\n')
144
+
145
+ # --- CHANGE START ---
146
+ # Define a regular expression pattern that matches any of the desired separators on their own line.
147
+ # Corrected pattern with '--' added, ordered from longest to shortest
148
+ separator_pattern = r'\n(?:---|--|-|-\.\.-|-\.-)\n'
149
+
150
+ # Use re.split() to split the text based on the pattern
151
+ entries = re.split(separator_pattern, text_content)
152
+ # --- CHANGE END ---
153
+
154
+ docs_to_add = []
155
+
156
+ for entry in entries:
157
+ if not entry.strip():
158
+ continue
159
+
160
+ title = "Untitled Text Entry"
161
+ content = entry.strip()
162
+
163
+ # Improved parsing logic
164
+ lines = entry.strip().split('\n')
165
+ if lines and "title:" in lines[0].lower():
166
+ title_line = lines[0].split(':', 1)
167
+ title = title_line[1].strip() if len(title_line) > 1 else "Untitled"
168
+
169
+ content_part = "\n".join(lines[1:])
170
+ if "content:" in content_part.lower():
171
+ content = content_part.split(':', 1)[1].strip()
172
+ else:
173
+ content = content_part.strip()
174
+
175
+ full_content = f"Title: {title}\n\nContent: {content}"
176
+
177
+ print(f" - Parsing entry: '{title}'")
178
+ behavior_options = CONFIG.get("behavior_tags", [])
179
+ emotion_options = CONFIG.get("emotion_tags", [])
180
+ detected_tags = detect_tags_from_query(content, behavior_options=behavior_options, emotion_options=emotion_options)
181
+
182
+ metadata = {"source": source, "title": title}
183
+ if detected_tags.get("detected_behavior") != "None": metadata["behaviors"] = [detected_tags.get("detected_behavior").lower()]
184
+ if detected_tags.get("detected_emotion") != "None": metadata["emotion"] = detected_tags.get("detected_emotion").lower()
185
+
186
+ docs_to_add.append(Document(page_content=full_content, metadata=metadata))
187
+
188
+ return docs_to_add
189
+
190
+ def handle_add_knowledge(title, text_input, file_input, image_input, yt_url):
191
+ global personal_vectorstore
192
+ docs_to_add = []
193
+
194
+ # Corrected prioritization of inputs
195
+ if text_input and text_input.strip():
196
+ # Handle manual text input first
197
+ docs_to_add = parse_and_tag_entries(f"Title: {title}\n\nContent: {text_input}", "Text Input")
198
+ elif file_input:
199
+ content_source = os.path.basename(file_input)
200
+ if file_input.lower().endswith('.txt'):
201
+ with open(file_input, 'r', encoding='utf-8') as f:
202
+ file_content = f.read()
203
+ docs_to_add = parse_and_tag_entries(file_content, content_source)
204
+ else: # Handle audio/video
205
+ final_title = title.strip() if title and title.strip() else "Audio/Video Note"
206
+ content_text = transcribe_audio(file_input)
207
+ full_content = f"Title: {final_title}\n\nContent: {content_text}"
208
+ docs_to_add = parse_and_tag_entries(full_content, content_source)
209
+ elif image_input:
210
+ final_title = title.strip() if title and title.strip() else "Image Note"
211
+ content_text = describe_image(image_input)
212
+ full_content = f"Title: {final_title}\n\nContent: {content_text}"
213
+ docs_to_add = parse_and_tag_entries(full_content, "Image Input")
214
+ elif yt_url and ("youtube.com" in yt_url or "youtu.be" in yt_url):
215
+ try:
216
+ yt = YouTube(yt_url)
217
+ video_title = yt.title
218
+ final_title = title.strip() if title and title.strip() else video_title
219
+ audio_stream = yt.streams.get_audio_only()
220
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_audio_file:
221
+ audio_stream.download(filename=temp_audio_file.name)
222
+ temp_audio_path = temp_audio_file.name
223
+ content_text = transcribe_audio(temp_audio_path)
224
+ content_source = f"YouTube: {video_title}"
225
+ os.remove(temp_audio_path)
226
+ full_content = f"Title: {final_title}\n\nContent: {content_text}"
227
+ docs_to_add = parse_and_tag_entries(full_content, content_source)
228
+ except Exception as e:
229
+ return f"Error processing YouTube link: {e}"
230
+ else:
231
+ return "Please provide a title and content, or another input source."
232
+
233
+ if not docs_to_add:
234
+ return "No processable content found to add."
235
+
236
+ if personal_vectorstore is None:
237
+ personal_vectorstore = build_or_load_vectorstore(docs_to_add, PERSONAL_INDEX_PATH, is_personal=True)
238
+ else:
239
+ personal_vectorstore.add_documents(docs_to_add)
240
+
241
+ personal_vectorstore.save_local(PERSONAL_INDEX_PATH)
242
+ return f"Successfully added {len(docs_to_add)} new memory/memories."
243
+
244
+ def save_chat_to_memory(chat_history):
245
+ global personal_vectorstore
246
+ if not chat_history: return "Nothing to save."
247
+ formatted_chat = []
248
+ for message in chat_history:
249
+ role = "User" if message["role"] == "user" else "Assistant"
250
+ content = message["content"].strip()
251
+ if content.startswith("*(Auto-detected context:"): continue
252
+ formatted_chat.append(f"{role}: {content}")
253
+ conversation_text = "\n".join(formatted_chat)
254
+ if not conversation_text: return "No conversation content to save."
255
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
256
+ title = f"Conversation from {timestamp}"
257
+ full_content = f"Title: {title}\n\nContent:\n{conversation_text}"
258
+ doc_to_add = Document(page_content=full_content, metadata={"source": "Saved Chat", "title": title})
259
+ if personal_vectorstore is None:
260
+ personal_vectorstore = build_or_load_vectorstore([doc_to_add], PERSONAL_INDEX_PATH, is_personal=True)
261
+ else:
262
+ personal_vectorstore.add_documents([doc_to_add])
263
+ personal_vectorstore.save_local(PERSONAL_INDEX_PATH)
264
+ print(f"Saved conversation to long-term memory.")
265
+ return f"Conversation from {timestamp} saved successfully!"
266
+
267
+ def list_personal_memories():
268
+ global personal_vectorstore
269
+ if personal_vectorstore is None or not hasattr(personal_vectorstore.docstore, '_dict') or not personal_vectorstore.docstore._dict:
270
+ return gr.update(value=[["No memories to display", "", ""]]), gr.update(choices=["No memories to select"], value=None)
271
+ docs = list(personal_vectorstore.docstore._dict.values())
272
+ dataframe_data = [[doc.metadata.get('title', 'Untitled'), doc.metadata.get('source', 'Unknown'), doc.page_content] for doc in docs]
273
+ dropdown_choices = [doc.page_content for doc in docs]
274
+ return gr.update(value=dataframe_data), gr.update(choices=dropdown_choices)
275
+
276
+ def delete_personal_memory(memory_to_delete):
277
+ global personal_vectorstore
278
+ if personal_vectorstore is None or not memory_to_delete:
279
+ return "Knowledge base is empty or no memory selected."
280
+ all_docs = list(personal_vectorstore.docstore._dict.values())
281
+ docs_to_keep = [doc for doc in all_docs if doc.page_content != memory_to_delete]
282
+ if len(all_docs) == len(docs_to_keep):
283
+ return "Error: Could not find the selected memory to delete."
284
+ print(f"Deleting memory. {len(docs_to_keep)} memories remaining.")
285
+ if not docs_to_keep:
286
+ if os.path.isdir(PERSONAL_INDEX_PATH):
287
+ shutil.rmtree(PERSONAL_INDEX_PATH)
288
+ personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
289
+ else:
290
+ new_vs = FAISS.from_documents(docs_to_keep, _default_embeddings())
291
+ new_vs.save_local(PERSONAL_INDEX_PATH)
292
+ personal_vectorstore = new_vs
293
+ return "Successfully deleted memory. The list will now refresh."
294
+
295
+ def chat_fn(user_text, audio_file, settings, chat_history):
296
+ global personal_vectorstore
297
+ question = (user_text or "").strip()
298
+ if audio_file and not question:
299
+ try:
300
+ voice_lang_name = settings.get("tts_lang", "English")
301
+ voice_lang_code = CONFIG["languages"].get(voice_lang_name, "en")
302
+ question = transcribe_audio(audio_file, lang=voice_lang_code)
303
+ except Exception as e:
304
+ err_msg = f"Audio Error: {e}" if settings.get("debug_mode") else "Sorry, I couldn't understand the audio."
305
+ chat_history.append({"role": "assistant", "content": err_msg})
306
+ return "", None, chat_history
307
+ if not question:
308
+ return "", None, chat_history
309
+ chat_history.append({"role": "user", "content": question})
310
+ manual_behavior_tag = settings.get("behaviour_tag")
311
+ manual_emotion_tag = settings.get("emotion_tag")
312
+ if manual_behavior_tag not in [None, "None"] or manual_emotion_tag not in [None, "None"]:
313
+ scenario_tag, emotion_tag = manual_behavior_tag, manual_emotion_tag
314
+ else:
315
+ behavior_options = CONFIG.get("behavior_tags", [])
316
+ emotion_options = CONFIG.get("emotion_tags", [])
317
+ detected_tags = detect_tags_from_query(question, behavior_options=behavior_options, emotion_options=emotion_options)
318
+ scenario_tag, emotion_tag = detected_tags.get("detected_behavior"), detected_tags.get("detected_emotion")
319
+ if (scenario_tag and scenario_tag != "None") or (emotion_tag and emotion_tag != "None"):
320
+ detected_msg = f"*(Auto-detected context: Behavior=`{scenario_tag}`, Emotion=`{emotion_tag}`)*"
321
+ chat_history.append({"role": "assistant", "content": detected_msg})
322
+ active_theme = settings.get("active_theme", "All")
323
+ vs_general = ensure_index(active_theme)
324
+ if personal_vectorstore is None:
325
+ personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
326
+ rag_chain_settings = {"role": settings.get("role"), "temperature": settings.get("temperature"), "language": settings.get("language"), "patient_name": settings.get("patient_name"), "caregiver_name": settings.get("caregiver_name"), "tone": settings.get("tone"),}
327
+ chain = make_rag_chain(vs_general, personal_vectorstore, **rag_chain_settings)
328
+ if scenario_tag == "None": scenario_tag = None
329
+ if emotion_tag == "None": emotion_tag = None
330
+ simple_history = chat_history[:-1]
331
+ response = answer_query(chain, question, chat_history=simple_history, scenario_tag=scenario_tag, emotion_tag=emotion_tag)
332
+ answer = response.get("answer", "[No answer found]")
333
+ chat_history.append({"role": "assistant", "content": answer})
334
+ audio_out = None
335
+ if settings.get("tts_on") and answer:
336
+ tts_lang_code = CONFIG["languages"].get(settings.get("tts_lang"), "en")
337
+ audio_out = synthesize_tts(answer, lang=tts_lang_code)
338
+ from gradio import update
339
+ return "", (update(value=audio_out, visible=bool(audio_out))), chat_history
340
+
341
+ def upload_knowledge(files, current_theme):
342
+ if not files: return "No files were selected to upload."
343
+ added = 0
344
+ for f in files:
345
+ try:
346
+ copy_into_theme(current_theme, f.name); added += 1
347
+ except Exception as e: print(f"Error uploading file {f.name}: {e}")
348
+ if added > 0 and current_theme in vectorstores: del vectorstores[current_theme]
349
+ return f"Uploaded {added} file(s). Refreshing file list..."
350
+ def save_file_selection(current_theme, enabled_files):
351
+ man = load_manifest(current_theme)
352
+ for fname in man['files']: man['files'][fname] = fname in enabled_files
353
+ save_manifest(current_theme, man)
354
+ if current_theme in vectorstores: del vectorstores[current_theme]
355
+ return f"Settings saved. Index for theme '{current_theme}' will rebuild on the next query."
356
+ def refresh_file_list_ui(current_theme):
357
+ files = list_theme_files(current_theme)
358
+ enabled = [f for f, en in files if en]
359
+ msg = f"Found {len(files)} file(s). {len(enabled)} enabled."
360
+ return gr.update(choices=[f for f, _ in files], value=enabled), msg
361
+ def auto_setup_on_load(current_theme):
362
+ theme_dir = theme_upload_dir(current_theme)
363
+ if not os.listdir(theme_dir):
364
+ print("First-time setup: Auto-seeding sample data...")
365
+ seed_files_into_theme(current_theme)
366
+ all_settings = collect_settings("patient", "", "", "warm", "English", "English", 0.7, "None", "None", "All", True, False)
367
+ files_ui, status_msg = refresh_file_list_ui(current_theme)
368
+ return all_settings, files_ui, status_msg
369
+
370
+ # In app.py, inside the Gradio Callbacks section for debugging
371
+ def test_save_file():
372
+ """A simple function to test if we can write a file to the persistent storage."""
373
+ try:
374
+ # Get the directory where the personal index is supposed to be stored
375
+ storage_dir = os.path.dirname(PERSONAL_INDEX_PATH)
376
+ test_file_path = os.path.join(storage_dir, "persistence_test.txt")
377
+
378
+ # Write the current time to the file
379
+ current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
380
+ content = f"File saved successfully at: {current_time}"
381
+
382
+ with open(test_file_path, "w", encoding="utf-8") as f:
383
+ f.write(content)
384
+
385
+ return f"βœ… Success! Wrote test file to: {test_file_path}"
386
+ except Exception as e:
387
+ return f"❌ Error! Failed to write file. Reason: {e}"
388
+
389
+ def check_test_file():
390
+ """A simple function to check if the test file from a previous session exists."""
391
+ try:
392
+ storage_dir = os.path.dirname(PERSONAL_INDEX_PATH)
393
+ test_file_path = os.path.join(storage_dir, "persistence_test.txt")
394
+
395
+ if os.path.exists(test_file_path):
396
+ with open(test_file_path, "r", encoding="utf-8") as f:
397
+ content = f.read()
398
+ return f"βœ… Success! Found test file. Contents: '{content}'"
399
+ else:
400
+ return f"❌ Failure. Test file not found at: {test_file_path}"
401
+ except Exception as e:
402
+ return f"❌ Error! Failed to check for file. Reason: {e}"
403
+
404
+
405
+
406
+ # --- UI Definition ---
407
+ CSS = ".gradio-container { font-size: 14px; } #chatbot { min-height: 250px; } #audio_out audio { max-height: 40px; } #audio_in audio { max-height: 40px; padding: 0; }"
408
+
409
+ with gr.Blocks(theme=gr.themes.Soft(), css=CSS) as demo:
410
+ settings_state = gr.State({})
411
+ with gr.Tab("Chat"):
412
+ user_text = gr.Textbox(show_label=False, placeholder="Type your message here...")
413
+ audio_in = gr.Audio(sources=["microphone"], type="filepath", label="Voice Input", elem_id="audio_in")
414
+ with gr.Row():
415
+ submit_btn = gr.Button("Send", variant="primary")
416
+ save_btn = gr.Button("Save to Memory")
417
+ clear_btn = gr.Button("Clear")
418
+ chat_status = gr.Markdown()
419
+ audio_out = gr.Audio(label="Response Audio", autoplay=True, visible=True, elem_id="audio_out")
420
+ chatbot = gr.Chatbot(elem_id="chatbot", label="Conversation", type="messages")
421
+ with gr.Tab("Personalize"):
422
+ with gr.Accordion("Add to Personal Knowledge Base", open=True):
423
+ gr.Markdown("Add personal notes, memories, or descriptions. A descriptive title helps the AI find memories more accurately.")
424
+ personal_title = gr.Textbox(label="Title / Entry Name", placeholder="e.g., 'Dad's favorite songs'")
425
+ personal_text = gr.Textbox(lines=5, label="Text Content (or use file upload)", placeholder="Type or paste text here. Use 'β€”' on a new line to separate multiple entries.")
426
+ personal_file = gr.File(label="Upload Audio/Video/Text File")
427
+ personal_image = gr.Image(type="filepath", label="Upload Image")
428
+ personal_yt_url = gr.Textbox(label="Or, provide a YouTube URL", placeholder="Paste a YouTube link here...")
429
+ with gr.Row():
430
+ personal_add_btn = gr.Button("Add Knowledge to Memory", variant="primary")
431
+ personal_status = gr.Markdown()
432
+ with gr.Accordion("Manage Personal Knowledge", open=False):
433
+ personal_memory_display = gr.DataFrame(headers=["Title", "Source", "Content"], label="Saved Personal Memories", interactive=False, row_count=(5, "dynamic"))
434
+ with gr.Row():
435
+ personal_refresh_btn = gr.Button("Refresh Memories")
436
+ with gr.Row():
437
+ personal_delete_selector = gr.Dropdown(label="Select a memory to delete (by its full content)", scale=3, interactive=True)
438
+ personal_delete_btn = gr.Button("Delete Selected Memory", variant="stop", scale=1)
439
+ personal_delete_status = gr.Markdown()
440
+ with gr.Tab("Settings"):
441
+ with gr.Group():
442
+ gr.Markdown("## Conversation & Persona Settings")
443
+ with gr.Row():
444
+ role = gr.Radio(CONFIG["roles"], value="caregiver", label="Your Role")
445
+ temperature = gr.Slider(0.0, 1.2, value=0.7, step=0.1, label="Creativity")
446
+ tone = gr.Dropdown(CONFIG["tones"], value="warm", label="Response Tone")
447
+ with gr.Row():
448
+ patient_name = gr.Textbox(label="Patient's Name", placeholder="e.g., 'Dad' or 'John'")
449
+ caregiver_name = gr.Textbox(label="Caregiver's Name", placeholder="e.g., 'me' or 'Jane'")
450
+ behaviour_tag = gr.Dropdown(CONFIG["behavior_tags"], value="None", label="Behaviour Filter (Manual Override)")
451
+ emotion_tag = gr.Dropdown(CONFIG["emotion_tags"], value="None", label="Emotion Filter (Manual Override)")
452
+ with gr.Accordion("Language, Voice & Debugging", open=False):
453
+ language = gr.Dropdown(list(CONFIG["languages"].keys()), value="English", label="Response Language")
454
+ tts_lang = gr.Dropdown(list(CONFIG["languages"].keys()), value="English", label="Voice Language")
455
+ tts_on = gr.Checkbox(True, label="Enable Voice Response (TTS)")
456
+ debug_mode = gr.Checkbox(False, label="Show Debug Info")
457
+ gr.Markdown("--- \n ## General Knowledge Base Management")
458
+ active_theme = gr.Radio(CONFIG["themes"], value="All", label="Active Knowledge Theme")
459
+ with gr.Row():
460
+ with gr.Column(scale=1):
461
+ files_in = gr.File(file_count="multiple", file_types=[".jsonl", ".txt"], label="Upload Knowledge Files")
462
+ upload_btn = gr.Button("Upload to Theme", variant="secondary")
463
+ seed_btn = gr.Button("Import Sample Data", variant="secondary")
464
+ with gr.Column(scale=2):
465
+ mgmt_status = gr.Markdown()
466
+ files_box = gr.CheckboxGroup(choices=[], label="Enable Files for the Selected Theme")
467
+ with gr.Row():
468
+ save_files_btn = gr.Button("Save Selection", variant="primary")
469
+ refresh_btn = gr.Button("Refresh List")
470
+ # --- ADD THE NEW DIAGNOSTIC TOOL AT THE BOTTOM ---
471
+ with gr.Accordion("Persistence Test", open=False):
472
+ gr.Markdown("Use this tool to verify that the Hugging Face persistent storage is working correctly. \n1. Click 'Run Test'. \n2. Manually restart the Space. \n3. Click 'Check for File'.")
473
+ with gr.Row():
474
+ test_save_btn = gr.Button("1. Run Persistence Test (Save File)")
475
+ check_save_btn = gr.Button("3. Check for Test File")
476
+ test_status = gr.Markdown()
477
+
478
+ # --- Event Wiring ---
479
+ all_settings_components = [role, patient_name, caregiver_name, tone, language, tts_lang, temperature, behaviour_tag, emotion_tag, active_theme, tts_on, debug_mode]
480
+ for component in all_settings_components:
481
+ component.change(fn=collect_settings, inputs=all_settings_components, outputs=settings_state)
482
+
483
+ submit_btn.click(fn=chat_fn, inputs=[user_text, audio_in, settings_state, chatbot], outputs=[user_text, audio_out, chatbot])
484
+ save_btn.click(fn=save_chat_to_memory, inputs=[chatbot], outputs=[chat_status])
485
+ clear_btn.click(lambda: (None, None, [], None, "", ""), outputs=[user_text, audio_out, chatbot, audio_in, user_text, chat_status])
486
+
487
+ personal_add_btn.click(
488
+ fn=handle_add_knowledge,
489
+ inputs=[personal_title, personal_text, personal_file, personal_image, personal_yt_url],
490
+ outputs=[personal_status]
491
+ ).then(
492
+ lambda: (None, None, None, None, None),
493
+ outputs=[personal_title, personal_text, personal_file, personal_image, personal_yt_url]
494
+ )
495
+
496
+ personal_refresh_btn.click(fn=list_personal_memories, inputs=None, outputs=[personal_memory_display, personal_delete_selector])
497
+ 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])
498
+
499
+ 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])
500
+ save_files_btn.click(save_file_selection, inputs=[active_theme, files_box], outputs=[mgmt_status])
501
+ seed_btn.click(seed_files_into_theme, inputs=[active_theme]).then(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
502
+ refresh_btn.click(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
503
+ active_theme.change(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
504
+ demo.load(auto_setup_on_load, inputs=[active_theme], outputs=[settings_state, files_box, mgmt_status])
505
+ test_save_btn.click(fn=test_save_file, inputs=None, outputs=[test_status])
506
+ check_save_btn.click(fn=check_test_file, inputs=None, outputs=[test_status])
507
+
508
+ # --- Startup Logic ---
509
+ def pre_load_indexes():
510
+ global personal_vectorstore
511
+ print("Pre-loading all knowledge base indexes at startup...")
512
+ for theme in CONFIG["themes"]:
513
+ print(f" - Loading general index for theme: '{theme}'")
514
+ try:
515
+ ensure_index(theme)
516
+ print(f" ...'{theme}' theme loaded successfully.")
517
+ except Exception as e:
518
+ print(f" ...Error loading theme '{theme}': {e}")
519
+ print(" - Loading personal knowledge index...")
520
+ try:
521
+ personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
522
+ print(" ...Personal knowledge loaded successfully.")
523
+ except Exception as e:
524
+ print(f" ...Error loading personal knowledge: {e}")
525
+ print("All indexes loaded. Application is ready.")
526
+
527
+ if __name__ == "__main__":
528
+ pre_load_indexes()
529
+ demo.queue().launch(debug=True)