KeenWoo commited on
Commit
323448c
·
verified ·
1 Parent(s): b6d7fe1

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -617
app.py DELETED
@@ -1,617 +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
- )
20
- from alz_companion.prompts import (
21
- BEHAVIOUR_TAGS, EMOTION_STYLES, FAITHFULNESS_JUDGE_PROMPT
22
- )
23
- from langchain.schema import Document
24
- from langchain_community.vectorstores import FAISS
25
- AGENT_OK = True
26
- except Exception as e:
27
- AGENT_OK = False
28
- class Document:
29
- def __init__(self, page_content, metadata): self.page_content, self.metadata = page_content, metadata
30
- class FAISS:
31
- def __init__(self):
32
- self.docstore = type('obj', (object,), {'_dict': {}})()
33
- def add_documents(self, docs):
34
- start_idx = len(self.docstore._dict)
35
- for i, d in enumerate(docs, start_idx):
36
- self.docstore._dict[i] = d
37
- def save_local(self, path): pass
38
- @classmethod
39
- def from_documents(cls, docs, embeddings=None):
40
- inst = cls()
41
- inst.add_documents(docs)
42
- return inst
43
- def build_or_load_vectorstore(docs, index_path, is_personal=False): return FAISS.from_documents(docs or [], embeddings=None)
44
- def bootstrap_vectorstore(sample_paths=None, index_path="data/"): return object()
45
- def make_rag_chain(vs_general, vs_personal, **kwargs): return lambda q, **k: {"answer": f"(Demo) You asked: {q}", "sources": []}
46
- def answer_query(chain, q, **kwargs): return chain(q, **kwargs)
47
- def synthesize_tts(text: str, lang: str = "en"): return None
48
- def transcribe_audio(filepath: str, lang: str = "en"): return "This is a transcribed message."
49
- def detect_tags_from_query(*args, **kwargs): return {"detected_behavior": "None", "detected_emotion": "None"}
50
- def describe_image(image_path: str): return "This is a description of an image."
51
- def _default_embeddings(): return None
52
- def route_query_type(query: str): return "general_conversation"
53
- def call_llm(messages, **kwargs): return "Cannot call LLM in fallback mode."
54
- BEHAVIOUR_TAGS, EMOTION_STYLES, FAITHFULNESS_JUDGE_PROMPT = {"None": []}, {"None": {}}, ""
55
- print(f"WARNING: Could not import from alz_companion ({e}). Running in UI-only demo mode.")
56
-
57
-
58
- # --- NEW: Import for Evaluation Logic ---
59
- try:
60
- from evaluate import load_test_fixtures, run_comprehensive_evaluation
61
- except ImportError:
62
- # Fallback if evaluate.py is not found
63
- def load_test_fixtures(): print("WARNING: evaluate.py not found.")
64
- def run_comprehensive_evaluation(*args, **kwargs): return "Evaluation module not found.", []
65
-
66
-
67
- # --- Centralized Configuration ---
68
- CONFIG = {
69
- "themes": ["All", "The Father", "Still Alice", "Away from Her", "Alive Inside", "General Caregiving"],
70
- "roles": ["patient", "caregiver"],
71
- "disease_stages": ["Default: Mild Stage", "Moderate Stage", "Advanced Stage"],
72
- "behavior_tags": ["None"] + list(BEHAVIOUR_TAGS.keys()),
73
- "emotion_tags": ["None"] + list(EMOTION_STYLES.keys()),
74
- "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"],
75
- "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"],
76
- "languages": {"English": "en", "Chinese": "zh", "Cantonese": "zh-yue", "Korean": "ko", "Japanese": "ja", "Malay": "ms", "French": "fr", "Spanish": "es", "Hindi": "hi", "Arabic": "ar"},
77
- "tones": ["warm", "empathetic", "caring", "reassuring", "calm", "optimistic", "motivating", "neutral", "formal", "humorous"]
78
- }
79
-
80
- # --- File Management & Vector Store Logic ---
81
- def _storage_root() -> Path:
82
- for p in [Path(os.getenv("SPACE_STORAGE", "")), Path("/data"), Path.home() / ".cache" / "alz_companion"]:
83
- if not p: continue
84
- try:
85
- p.mkdir(parents=True, exist_ok=True)
86
- (p / ".write_test").write_text("ok")
87
- (p / ".write_test").unlink(missing_ok=True)
88
- return p
89
- except Exception: continue
90
- tmp = Path(tempfile.gettempdir()) / "alz_companion"
91
- tmp.mkdir(parents=True, exist_ok=True)
92
- return tmp
93
- STORAGE_ROOT = _storage_root()
94
- INDEX_BASE = STORAGE_ROOT / "index"
95
- PERSONAL_DATA_BASE = STORAGE_ROOT / "personal"
96
- UPLOADS_BASE = INDEX_BASE / "uploads"
97
- PERSONAL_INDEX_PATH = str(PERSONAL_DATA_BASE / "personal_faiss_index")
98
- NLU_EXAMPLES_INDEX_PATH = str(INDEX_BASE / "nlu_examples_faiss_index")
99
- THEME_PATHS = {t: str(INDEX_BASE / f"faiss_index_{t.replace(' ', '').lower()}") for t in CONFIG["themes"]}
100
- os.makedirs(UPLOADS_BASE, exist_ok=True)
101
- os.makedirs(PERSONAL_DATA_BASE, exist_ok=True)
102
- for p in THEME_PATHS.values(): os.makedirs(p, exist_ok=True)
103
- vectorstores = {}
104
- personal_vectorstore = None
105
- nlu_vectorstore = None
106
-
107
- try:
108
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
109
- except Exception:
110
- personal_vectorstore = None
111
- def bootstrap_nlu_vectorstore(example_file: str, index_path: str) -> FAISS:
112
- if not os.path.exists(example_file):
113
- print(f"WARNING: NLU example file not found at {example_file}. NLU will be less accurate.")
114
- return build_or_load_vectorstore([], index_path)
115
- docs = []
116
- with open(example_file, "r", encoding="utf-8") as f:
117
- for line in f:
118
- try:
119
- data = json.loads(line)
120
- doc = Document(page_content=data["query"], metadata=data)
121
- docs.append(doc)
122
- except (json.JSONDecodeError, KeyError): continue
123
- print(f"Found and loaded {len(docs)} NLU training examples.")
124
- if os.path.exists(index_path): shutil.rmtree(index_path)
125
- return build_or_load_vectorstore(docs, index_path)
126
- def canonical_theme(tk: str) -> str: return tk if tk in CONFIG["themes"] else "All"
127
- def theme_upload_dir(theme: str) -> str:
128
- p = UPLOADS_BASE / f"theme_{canonical_theme(theme).replace(' ', '').lower()}"
129
- p.mkdir(exist_ok=True)
130
- return str(p)
131
- def load_manifest(theme: str) -> Dict[str, Any]:
132
- p = os.path.join(theme_upload_dir(theme), "manifest.json")
133
- if os.path.exists(p):
134
- try:
135
- with open(p, "r", encoding="utf-8") as f: return json.load(f)
136
- except Exception: pass
137
- return {"files": {}}
138
- def save_manifest(theme: str, man: Dict[str, Any]):
139
- with open(os.path.join(theme_upload_dir(theme), "manifest.json"), "w", encoding="utf-8") as f: json.dump(man, f, indent=2)
140
- def list_theme_files(theme: str) -> List[tuple[str, bool]]:
141
- man = load_manifest(theme)
142
- base = theme_upload_dir(theme)
143
- found = [(n, bool(e)) for n, e in man.get("files", {}).items() if os.path.exists(os.path.join(base, n))]
144
- existing = {n for n, e in found}
145
- for name in sorted(os.listdir(base)):
146
- if name not in existing and os.path.isfile(os.path.join(base, name)): found.append((name, False))
147
- man["files"] = dict(found)
148
- save_manifest(theme, man)
149
- return found
150
- def copy_into_theme(theme: str, src_path: str) -> str:
151
- fname = os.path.basename(src_path)
152
- dest = os.path.join(theme_upload_dir(theme), fname)
153
- shutil.copy2(src_path, dest)
154
- return dest
155
- def seed_files_into_theme(theme: str):
156
- 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)]
157
- man, changed = load_manifest(theme), False
158
- for path, enable in SEED_FILES:
159
- if not os.path.exists(path): continue
160
- fname = os.path.basename(path)
161
- if not os.path.exists(os.path.join(theme_upload_dir(theme), fname)):
162
- copy_into_theme(theme, path)
163
- man["files"][fname] = bool(enable)
164
- changed = True
165
- if changed: save_manifest(theme, man)
166
- def ensure_index(theme='All'):
167
- theme = canonical_theme(theme)
168
- if theme in vectorstores: return vectorstores[theme]
169
- upload_dir = theme_upload_dir(theme)
170
- enabled_files = [os.path.join(upload_dir, n) for n, enabled in list_theme_files(theme) if enabled]
171
- index_path = THEME_PATHS.get(theme)
172
- vectorstores[theme] = bootstrap_vectorstore(sample_paths=enabled_files, index_path=index_path)
173
- return vectorstores[theme]
174
-
175
- # --- Gradio Callbacks ---
176
- # In app.py, modify the collect_settings function
177
-
178
- def collect_settings(*args):
179
- keys = ["role", "patient_name", "caregiver_name", "tone", "language", "tts_lang", "temperature",
180
- # --- ADD "disease_stage" to this list ---
181
- "disease_stage",
182
- "behaviour_tag", "emotion_tag", "topic_tag", "active_theme", "tts_on", "debug_mode"]
183
- return dict(zip(keys, args))
184
-
185
-
186
- # In app.py, replace the entire parse_and_tag_entries function.
187
- def parse_and_tag_entries(text_content: str, source: str, settings: dict = None) -> List[Document]:
188
- docs_to_add = []
189
- # This logic correctly handles both simple text and complex journal entries
190
- entries = re.split(r'\n(?:---|--|-|-\*-|-\.-)\n', text_content)
191
- if len(entries) == 1 and "title:" not in entries[0].lower() and "content:" not in entries[0].lower():
192
- entries = [text_content] # Treat simple text as a single entry
193
-
194
- for entry in entries:
195
- if not entry.strip(): continue
196
-
197
- lines = entry.strip().split('\n')
198
- title_line = lines[0].split(':', 1)
199
- title = title_line[1].strip() if len(title_line) > 1 and "title:" in lines[0].lower() else "Untitled Text Entry"
200
- content_part = "\n".join(lines[1:])
201
- content = content_part.split(':', 1)[1].strip() if "content:" in content_part.lower() else content_part.strip() or entry.strip()
202
-
203
- if not content: continue
204
-
205
- full_content = f"Title: {title}\n\nContent: {content}"
206
-
207
- detected_tags = detect_tags_from_query(
208
- content, nlu_vectorstore=nlu_vectorstore,
209
- behavior_options=CONFIG["behavior_tags"], emotion_options=CONFIG["emotion_tags"],
210
- topic_options=CONFIG["topic_tags"], context_options=CONFIG["context_tags"],
211
- settings=settings
212
- )
213
-
214
- metadata = {"source": source, "title": title}
215
-
216
- # --- START: CORRECTED METADATA ASSIGNMENT ---
217
- if detected_tags.get("detected_behaviors"):
218
- metadata["behaviors"] = [b.lower() for b in detected_tags["detected_behaviors"]]
219
- detected_emotion = detected_tags.get("detected_emotion")
220
- if detected_emotion and detected_emotion != "None":
221
- metadata["emotion"] = detected_emotion.lower()
222
-
223
- # Correctly handle the plural "detected_topics" key and list value
224
- detected_topics = detected_tags.get("detected_topics")
225
- if detected_topics:
226
- metadata["topic_tags"] = [t.lower() for t in detected_topics]
227
-
228
- if detected_tags.get("detected_contexts"):
229
- metadata["context_tags"] = [c.lower() for c in detected_tags["detected_contexts"]]
230
- # --- END: CORRECTED METADATA ASSIGNMENT ---
231
-
232
- docs_to_add.append(Document(page_content=full_content, metadata=metadata))
233
-
234
- return docs_to_add
235
-
236
-
237
- def handle_add_knowledge(title, text_input, file_input, image_input, yt_url, settings):
238
- global personal_vectorstore
239
- docs_to_add = []
240
- source, content = "Unknown", ""
241
- if text_input and text_input.strip():
242
- source, content = "Text Input", f"Title: {title or 'Untitled'}\n\nContent: {text_input}"
243
- elif file_input:
244
- source = os.path.basename(file_input.name)
245
- if file_input.name.lower().endswith('.txt'):
246
- with open(file_input.name, 'r', encoding='utf-8') as f: content = f.read()
247
- else:
248
- transcribed = transcribe_audio(file_input.name)
249
- content = f"Title: {title or 'Audio/Video Note'}\n\nContent: {transcribed}"
250
- elif image_input:
251
- source, description = "Image Input", describe_image(image_input)
252
- content = f"Title: {title or 'Image Note'}\n\nContent: {description}"
253
- elif yt_url and ("youtube.com" in yt_url or "youtu.be" in yt_url):
254
- try:
255
- yt = YouTube(yt_url)
256
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_audio_file:
257
- yt.streams.get_audio_only().download(filename=temp_audio_file.name)
258
- transcribed = transcribe_audio(temp_audio_file.name)
259
- os.remove(temp_audio_file.name)
260
- source, content = f"YouTube: {yt.title}", f"Title: {title or yt.title}\n\nContent: {transcribed}"
261
- except Exception as e:
262
- return f"Error processing YouTube link: {e}"
263
- else:
264
- return "Please provide content to add."
265
- if content:
266
- docs_to_add = parse_and_tag_entries(content, source, settings=settings)
267
- if not docs_to_add: return "No processable content found to add."
268
- if personal_vectorstore is None:
269
- personal_vectorstore = build_or_load_vectorstore(docs_to_add, PERSONAL_INDEX_PATH, is_personal=True)
270
- else:
271
- personal_vectorstore.add_documents(docs_to_add)
272
- personal_vectorstore.save_local(PERSONAL_INDEX_PATH)
273
- return f"Successfully added {len(docs_to_add)} new memory/memories."
274
-
275
-
276
- # REPLACE your entire old chat_fn function with this new one
277
-
278
- def chat_fn(user_text, audio_file, settings, chat_history):
279
- global personal_vectorstore
280
- question = (user_text or "").strip()
281
- if audio_file and not question:
282
- try:
283
- question = transcribe_audio(audio_file, lang=CONFIG["languages"].get(settings.get("tts_lang", "English"), "en"))
284
- except Exception as e:
285
- err_msg = f"Audio Error: {e}" if settings.get("debug_mode") else "Sorry, I couldn't understand the audio."
286
- chat_history.append({"role": "assistant", "content": err_msg})
287
- return "", None, chat_history
288
-
289
- if not question:
290
- return "", None, chat_history
291
-
292
- # --- START FIX 1: Correctly process the incoming chat_history (list of dicts) ---
293
- # The incoming chat_history is already in the desired format for the API,
294
- # we just need to filter out our special system messages (like sources).
295
- api_chat_history = [
296
- msg for msg in chat_history
297
- if msg.get("content") and not msg["content"].strip().startswith("*(")
298
- ]
299
-
300
- # Append the new user question to the history that will be displayed in the UI
301
- chat_history.append({"role": "user", "content": question})
302
- # --- END FIX 1 ---
303
-
304
- query_type = route_query_type(question)
305
- final_tags = { "scenario_tag": None, "emotion_tag": None, "topic_tag": None, "context_tags": [] }
306
- manual_behavior = settings.get("behaviour_tag", "None")
307
- manual_emotion = settings.get("emotion_tag", "None")
308
- manual_topic = settings.get("topic_tag", "None")
309
-
310
- auto_detected_context = ""
311
- if not all(m == "None" for m in [manual_behavior, manual_emotion, manual_topic]):
312
- final_tags["scenario_tag"] = manual_behavior if manual_behavior != "None" else None
313
- final_tags["emotion_tag"] = manual_emotion if manual_emotion != "None" else None
314
- final_tags["topic_tag"] = manual_topic if manual_topic != "None" else None
315
- elif "caregiving_scenario" in query_type:
316
- detected_tags = detect_tags_from_query(
317
- question, nlu_vectorstore=nlu_vectorstore, behavior_options=CONFIG["behavior_tags"],
318
- emotion_options=CONFIG["emotion_tags"], topic_options=CONFIG["topic_tags"],
319
- context_options=CONFIG["context_tags"], settings=settings)
320
- behaviors = detected_tags.get("detected_behaviors")
321
- final_tags["scenario_tag"] = behaviors[0] if behaviors else None
322
- final_tags["emotion_tag"] = detected_tags.get("detected_emotion")
323
- final_tags["topic_tag"] = detected_tags.get("detected_topic")
324
- final_tags["context_tags"] = detected_tags.get("detected_contexts", [])
325
- detected_parts = [f"{k.split('_')[1]}=`{v}`" for k, v in final_tags.items() if v and v != "None" and v != []]
326
- if detected_parts:
327
- auto_detected_context = f"*(Auto-detected context: {', '.join(detected_parts)})*"
328
-
329
- vs_general = ensure_index(settings.get("active_theme", "All"))
330
- if personal_vectorstore is None:
331
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
332
-
333
- # OLD rag_settings = {k: settings.get(k) for k in ["role", "temperature", "language", "patient_name", "caregiver_name", "tone"]}
334
- # NEW add "disease_stage"
335
- rag_settings = {k: settings.get(k) for k in ["role", "temperature", "language", "patient_name", "caregiver_name", "tone", "disease_stage"]}
336
- chain = make_rag_chain(vs_general, personal_vectorstore, **rag_settings)
337
-
338
- response = answer_query(chain, question, query_type=query_type, chat_history=api_chat_history, **final_tags)
339
- answer = response.get("answer", "[No answer found]")
340
-
341
- # --- START FIX 2: Append new messages directly in the correct dictionary format ---
342
- # 1. The main answer is appended first to appear at the top of the new messages.
343
- chat_history.append({"role": "assistant", "content": answer})
344
-
345
- # 2. Then, append the supplementary context and source information below the answer.
346
- if auto_detected_context:
347
- chat_history.append({"role": "assistant", "content": auto_detected_context})
348
- if response.get("sources"):
349
- chat_history.append({"role": "assistant", "content": f"*(Sources used: {', '.join(response['sources'])})*"})
350
-
351
- # chat_history.append({"role": "assistant", "content": answer})
352
- # --- END FIX 2 ---
353
-
354
- audio_out = None
355
- if settings.get("tts_on") and answer:
356
- audio_out = synthesize_tts(answer, lang=CONFIG["languages"].get(settings.get("tts_lang"), "en"))
357
-
358
- # --- START FIX 3: The chat_history is now already in the correct format to be returned ---
359
- return "", gr.update(value=audio_out, visible=bool(audio_out)), chat_history
360
- # --- END FIX 3 ---
361
-
362
- # The save_chat_to_memory function incorrectly assumes the history is
363
- # a list of tuples, like [(True, "..."), (False, "...")]
364
- # However, The chat_fn function correctly builds the chat_history as
365
- # a list of dictionaries, like this:
366
- # [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]
367
- # To correctly parse the list of dictionaries.
368
- def save_chat_to_memory(chat_history):
369
- if not chat_history:
370
- return "Nothing to save."
371
-
372
- # --- START: MODIFIED LOGIC ---
373
- # Correctly processes the list of dictionaries from the chatbot
374
- formatted_chat = [
375
- f"{msg.get('role', 'assistant').capitalize()}: {msg.get('content', '').strip()}"
376
- for msg in chat_history
377
- if isinstance(msg, dict) and msg.get('content') and not msg.get('content', '').strip().startswith("*(")
378
- ]
379
- # --- END: MODIFIED LOGIC ---
380
-
381
- if not formatted_chat:
382
- return "No conversation to save."
383
-
384
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
385
- title = f"Conversation from {timestamp}"
386
- full_content = f"Title: {title}\n\nContent:\n" + "\n".join(formatted_chat)
387
- doc = Document(page_content=full_content, metadata={"source": "Saved Chat", "title": title})
388
-
389
- global personal_vectorstore
390
- if personal_vectorstore is None:
391
- personal_vectorstore = build_or_load_vectorstore([doc], PERSONAL_INDEX_PATH, is_personal=True)
392
- else:
393
- personal_vectorstore.add_documents([doc])
394
-
395
- personal_vectorstore.save_local(PERSONAL_INDEX_PATH)
396
- return f"Conversation from {timestamp} saved."
397
-
398
-
399
- def list_personal_memories():
400
- global personal_vectorstore
401
- if personal_vectorstore is None or not hasattr(personal_vectorstore.docstore, '_dict') or not personal_vectorstore.docstore._dict:
402
- return gr.update(value=[["No memories", "", ""]]), gr.update(choices=[], value=None)
403
- docs = list(personal_vectorstore.docstore._dict.values())
404
- 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])
405
- def delete_personal_memory(memory_to_delete):
406
- global personal_vectorstore
407
- if personal_vectorstore is None or not memory_to_delete: return "No memory selected."
408
- all_docs = list(personal_vectorstore.docstore._dict.values())
409
- docs_to_keep = [d for d in all_docs if d.page_content != memory_to_delete]
410
- if len(all_docs) == len(docs_to_keep): return "Error: Could not find memory."
411
- if not docs_to_keep:
412
- if os.path.isdir(PERSONAL_INDEX_PATH): shutil.rmtree(PERSONAL_INDEX_PATH)
413
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
414
- else:
415
- new_vs = FAISS.from_documents(docs_to_keep, _default_embeddings())
416
- new_vs.save_local(PERSONAL_INDEX_PATH)
417
- personal_vectorstore = new_vs
418
- return "Successfully deleted memory."
419
-
420
- # --- EVALUATION FUNCTIONS: move them into evaluate.py
421
- # def evaluate_nlu_tags(expected: Dict[str, Any], actual: Dict[str, Any], tag_key: str, expected_key_override: str = None) -> Dict[str, float]:
422
- # def _parse_judge_json(raw_str: str) -> dict | None:
423
- # def run_comprehensive_evaluation():
424
-
425
- def upload_knowledge(files, theme):
426
- for f in files: copy_into_theme(theme, f.name)
427
- if theme in vectorstores: del vectorstores[theme]
428
- return f"Uploaded {len(files)} file(s)."
429
- def save_file_selection(theme, enabled):
430
- man = load_manifest(theme)
431
- for fname in man['files']: man['files'][fname] = fname in enabled
432
- save_manifest(theme, man)
433
- if theme in vectorstores: del vectorstores[theme]
434
- return f"Settings saved for theme '{theme}'."
435
- def refresh_file_list_ui(theme):
436
- files = list_theme_files(theme)
437
- return gr.update(choices=[f for f, _ in files], value=[f for f, en in files if en]), f"Found {len(files)} file(s)."
438
- def auto_setup_on_load(theme):
439
- if not os.listdir(theme_upload_dir(theme)): seed_files_into_theme(theme)
440
- settings = collect_settings("patient", "", "", "warm", "English", "English", 0.7, "None", "None", "None", "All", True, False)
441
- files_ui, status = refresh_file_list_ui(theme)
442
- return settings, files_ui, status
443
- def test_save_file():
444
- try:
445
- path = PERSONAL_DATA_BASE / "persistence_test.txt"
446
- path.write_text(f"File saved at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
447
- return f"✅ Success! Wrote test file to: {path}"
448
- except Exception as e: return f"❌ Error! Failed to write file: {e}"
449
- def check_test_file():
450
- path = PERSONAL_DATA_BASE / "persistence_test.txt"
451
- if path.exists(): return f"✅ Success! Found test file. Contents: '{path.read_text()}'"
452
- return f"❌ Failure. Test file not found at: {path}"
453
-
454
- # --- UI Definition ---
455
- CSS = """
456
- .gradio-container { font-size: 14px; }
457
- #chatbot { min-height: 400px; }
458
- #audio_in audio, #audio_out audio { max-height: 40px; }
459
- #audio_in .waveform, #audio_out .waveform { display: none !important; }
460
- #audio_in, #audio_out { min-height: 0px !important; }
461
- """
462
- with gr.Blocks(theme=gr.themes.Soft(), css=CSS) as demo:
463
- settings_state = gr.State({})
464
- with gr.Tab("Chat"):
465
- with gr.Row():
466
- user_text = gr.Textbox(show_label=False, placeholder="Type your message here...", scale=7)
467
- submit_btn = gr.Button("Send", variant="primary", scale=1)
468
- with gr.Row():
469
- audio_in = gr.Audio(sources=["microphone"], type="filepath", label="Voice Input", elem_id="audio_in")
470
- audio_out = gr.Audio(label="Response Audio", autoplay=True, visible=True, elem_id="audio_out")
471
-
472
- chatbot = gr.Chatbot(elem_id="chatbot", label="Conversation", type="messages")
473
- chat_status = gr.Markdown()
474
- with gr.Row():
475
- clear_btn = gr.Button("Clear")
476
- save_btn = gr.Button("Save to Memory")
477
-
478
- with gr.Tab("Personalize"):
479
- with gr.Accordion("Add to Personal Knowledge Base", open=True):
480
- personal_title = gr.Textbox(label="Title")
481
- personal_text = gr.Textbox(lines=5, label="Text Content")
482
- with gr.Row():
483
- personal_file = gr.File(label="Upload Audio/Video/Text File")
484
- personal_image = gr.Image(type="filepath", label="Upload Image")
485
- personal_yt_url = gr.Textbox(label="Or, provide a YouTube URL")
486
- personal_add_btn = gr.Button("Add Knowledge", variant="primary")
487
- personal_status = gr.Markdown()
488
- gr.Markdown("### **Manage Personal Knowledge**")
489
- with gr.Accordion("View/Hide Details", open=False):
490
- personal_memory_display = gr.DataFrame(headers=["Title", "Source", "Content"], label="Saved Memories", row_count=(5, "dynamic"))
491
- personal_refresh_btn = gr.Button("Refresh Memories")
492
- personal_delete_selector = gr.Dropdown(label="Select memory to delete", scale=3, interactive=True)
493
- personal_delete_btn = gr.Button("Delete Selected", variant="stop", scale=1)
494
- personal_delete_status = gr.Markdown()
495
-
496
- with gr.Tab("Settings"):
497
- with gr.Group():
498
- gr.Markdown("## Conversation & Persona Settings")
499
- with gr.Row():
500
- role = gr.Radio(CONFIG["roles"], value="patient", label="Your Role")
501
- patient_name = gr.Textbox(label="Patient's Name")
502
- caregiver_name = gr.Textbox(label="Caregiver's Name")
503
- with gr.Row():
504
- temperature = gr.Slider(0.0, 1.2, value=0.7, step=0.1, label="Creativity")
505
- tone = gr.Dropdown(CONFIG["tones"], value="warm", label="Response Tone")
506
- with gr.Row():
507
- # --- ADD THIS NEW DROPDOWN ---
508
- # disease_stage = gr.Dropdown(CONFIG["disease_stages"], value="Normal / Unspecified", label="Assumed Disease Stage")
509
- disease_stage = gr.Dropdown(CONFIG["disease_stages"], value="Default: Mild Stage", label="Assumed Disease Stage")
510
- # --- END OF ADDITION ---
511
- behaviour_tag = gr.Dropdown(CONFIG["behavior_tags"], value="None", label="Behaviour Filter (Manual)")
512
- emotion_tag = gr.Dropdown(CONFIG["emotion_tags"], value="None", label="Emotion Filter (Manual)")
513
- topic_tag = gr.Dropdown(CONFIG["topic_tags"], value="None", label="Topic Tag Filter (Manual)")
514
- with gr.Accordion("Language, Voice & Debugging", open=False):
515
- language = gr.Dropdown(list(CONFIG["languages"].keys()), value="English", label="Response Language")
516
- tts_lang = gr.Dropdown(list(CONFIG["languages"].keys()), value="English", label="Voice Language")
517
- tts_on = gr.Checkbox(True, label="Enable Voice Response")
518
- debug_mode = gr.Checkbox(False, label="Show Debug Info")
519
- gr.Markdown("--- \n ## General Knowledge Base Management")
520
- with gr.Row():
521
- with gr.Column(scale=1):
522
- files_in = gr.File(file_count="multiple", file_types=[".jsonl", ".txt"], label="Upload Knowledge Files")
523
- upload_btn = gr.Button("Upload to Theme")
524
- seed_btn = gr.Button("Import Sample Data")
525
- mgmt_status = gr.Markdown()
526
- with gr.Column(scale=2):
527
- active_theme = gr.Radio(CONFIG["themes"], value="All", label="Active Knowledge Theme")
528
- files_box = gr.CheckboxGroup(choices=[], label="Enable Files for Selected Theme")
529
- with gr.Row():
530
- save_files_btn = gr.Button("Save Selection", variant="primary")
531
- refresh_btn = gr.Button("Refresh List")
532
- with gr.Accordion("Persistence Test", open=False):
533
- test_save_btn = gr.Button("1. Run Persistence Test (Save File)")
534
- check_save_btn = gr.Button("3. Check for Test File")
535
- test_status = gr.Markdown()
536
-
537
- # --- UPDATED TESTING TAB ---
538
- with gr.Tab("Testing"):
539
- gr.Markdown("## Comprehensive Performance Evaluation")
540
- 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.")
541
-
542
- run_comprehensive_btn = gr.Button("Run Comprehensive Evaluation", variant="primary")
543
-
544
- batch_summary_md = gr.Markdown("### Evaluation Summary: Not yet run.")
545
-
546
- comprehensive_results_df = gr.DataFrame(
547
- label="Detailed Evaluation Results",
548
- elem_id="comprehensive_results_df",
549
- headers=[
550
- "Test ID","Title","Route Correct?","Expected Route","Actual Route",
551
- "Behavior F1","Emotion F1","Topic F1","Context F1",
552
- "Generated Answer","Sources","Source Count","Latency (ms)", "Faithfulness"
553
- ],
554
- interactive=False
555
- )
556
-
557
-
558
- # --- Event Wiring ---
559
- all_settings = [role, patient_name, caregiver_name, tone, language, tts_lang, temperature,
560
- disease_stage, behaviour_tag, emotion_tag, topic_tag, active_theme, tts_on, debug_mode]
561
-
562
- for c in all_settings: c.change(fn=collect_settings, inputs=all_settings, outputs=settings_state)
563
- submit_btn.click(fn=chat_fn, inputs=[user_text, audio_in, settings_state, chatbot], outputs=[user_text, audio_out, chatbot])
564
- save_btn.click(fn=save_chat_to_memory, inputs=[chatbot], outputs=[chat_status])
565
- clear_btn.click(lambda: (None, None, [], None, "", ""), outputs=[user_text, audio_out, chatbot, audio_in, user_text, chat_status])
566
- 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])
567
- personal_refresh_btn.click(fn=list_personal_memories, inputs=None, outputs=[personal_memory_display, personal_delete_selector])
568
- 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])
569
- 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])
570
- save_files_btn.click(save_file_selection, inputs=[active_theme, files_box], outputs=[mgmt_status])
571
- seed_btn.click(seed_files_into_theme, inputs=[active_theme]).then(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
572
- refresh_btn.click(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
573
- active_theme.change(refresh_file_list_ui, inputs=[active_theme], outputs=[files_box, mgmt_status])
574
-
575
- # Then update the .click() event handler
576
- run_comprehensive_btn.click(
577
- fn=lambda: run_comprehensive_evaluation(
578
- vs_general=ensure_index("All"),
579
- vs_personal=personal_vectorstore,
580
- nlu_vectorstore=nlu_vectorstore,
581
- config=CONFIG
582
- ),
583
- # The output list now has three components
584
- outputs=[batch_summary_md, comprehensive_results_df, comprehensive_results_df]
585
- )
586
-
587
- demo.load(auto_setup_on_load, inputs=[active_theme], outputs=[settings_state, files_box, mgmt_status])
588
- demo.load(load_test_fixtures)
589
- test_save_btn.click(fn=test_save_file, inputs=None, outputs=[test_status])
590
- check_save_btn.click(fn=check_test_file, inputs=None, outputs=[test_status])
591
-
592
- # --- Startup Logic ---
593
- def pre_load_indexes():
594
- global personal_vectorstore, nlu_vectorstore
595
- print("Pre-loading all indexes at startup...")
596
- print(" - Loading NLU examples index...")
597
- nlu_vectorstore = bootstrap_nlu_vectorstore("nlu_training_examples.jsonl", NLU_EXAMPLES_INDEX_PATH)
598
- print(f" ...NLU index loaded.")
599
- for theme in CONFIG["themes"]:
600
- print(f" - Loading general index for theme: '{theme}'")
601
- try:
602
- ensure_index(theme)
603
- print(f" ...'{theme}' theme loaded.")
604
- except Exception as e:
605
- print(f" ...Error loading theme '{theme}': {e}")
606
- print(" - Loading personal knowledge index...")
607
- try:
608
- personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
609
- print(" ...Personal knowledge loaded.")
610
- except Exception as e:
611
- print(f" ...Error loading personal knowledge: {e}")
612
- print("All indexes loaded. Application is ready.")
613
-
614
- if __name__ == "__main__":
615
- seed_files_into_theme('All')
616
- pre_load_indexes()
617
- demo.queue().launch(debug=True)