import os import json import tempfile import spacy import whisper import pandas as pd import librosa import numpy as np import firebase_admin from firebase_admin import credentials, firestore, storage # ================================================================ # Firebase Initialization # ================================================================ if not firebase_admin._apps: firebase_key = os.getenv("FIREBASE_SERVICE_ACCOUNT_JSON") if firebase_key: cred = credentials.Certificate(json.loads(firebase_key)) else: cred_path = r"firebase/nms-database-3fd45-firebase-adminsdk-fbsvc-31e40a9937.json" cred = credentials.Certificate(cred_path) firebase_admin.initialize_app(cred, { 'storageBucket': 'nms-database-3fd45.firebasestorage.app' }) db = firestore.client() bucket = storage.bucket() # Lazy-loaded model placeholders _whisper_model = None _nlp = None # ================================================================ # Helper Fuctions for creating transcripts from audio files (.wav) # ================================================================ def _load_whisper(): global _whisper_model if _whisper_model is None: print("Loading Whisper model...") _whisper_model = whisper.load_model("medium") return _whisper_model def _load_spacy(): global _nlp if _nlp is None: print("Loading spaCy model...") _nlp = spacy.load("en_core_web_sm") return _nlp def text_to_cha_lines(text): text = text.replace("...", " ELLIPSIS_MARKER ") sentences = [] current = "" for char in text: current += char if char in ".!?": sentences.append(current.strip()) current = "" if current.strip(): sentences.append(current.strip()) sentences = [s.replace(" ELLIPSIS_MARKER ", " ... ") for s in sentences] return sentences def generate_mor_gra_line(doc): mor_tokens = [] gra_tokens = [] for i, token in enumerate(doc): if token.is_space or token.is_punct: continue pos = token.pos_.lower() if token.pos_ != 'PROPN' else 'n:prop' lemma = token.lemma_.lower() pos_map = { 'noun': 'n', 'verb': 'v', 'adj': 'adj', 'adv': 'adv', 'pron': 'pro', 'det': 'det', 'adp': 'prep', 'conj': 'conj', 'num': 'num', 'part': 'part', 'intj': 'co', 'propn': 'n:prop' } pos_tag = pos_map.get(pos, pos) mor_tokens.append(f"{lemma}|{pos_tag}") head_idx = token.head.i + 1 dep = token.dep_.lower() gra_tokens.append(f"{head_idx}|{dep}") return " ".join(mor_tokens), " ".join(gra_tokens) # ================================================================ # Helper Functions for audio data pulling # ================================================================ def extract_audio_features(wav_path): """Extract MFCC, ZCR, RMS from a single .wav file""" try: y, sr = librosa.load(wav_path, sr=None) mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13) mfccs_mean = np.mean(mfccs, axis=1) mfccs_std = np.std(mfccs, axis=1) zcr = np.mean(librosa.feature.zero_crossing_rate(y)) rms = np.mean(librosa.feature.rms(y=y)) return np.concatenate([mfccs_mean, mfccs_std, [zcr, rms]]) except Exception as e: print(f"Audio extraction failed for {wav_path}: {e}") return None def extract_text_features(cha_path): """Extract linguistic features from .cha file""" try: with open(cha_path, 'r', encoding='utf-8') as f: lines = f.readlines() utterances = [line.strip() for line in lines if line.startswith('*')] if not utterances: return None word_count = sum(len(line.split()) for line in utterances) sentence_count = len(utterances) avg_words_per_sentence = word_count / sentence_count if sentence_count > 0 else 0 unique_words = len(set(word.lower().strip('.,!?;:') for line in utterances for word in line.split())) lexical_diversity = unique_words / word_count if word_count > 0 else 0 pause_count = sum(line.count('<') + line.count('...') for line in utterances) return np.array([word_count, sentence_count, avg_words_per_sentence, lexical_diversity, pause_count]) except Exception as e: print(f"Text extraction failed for {cha_path}: {e}") return None # ================================================================ # Data loading functions # ================================================================ def pull_audio(docID: str): """ Downloads .wav from Firebase Storage, fetches age/gender from Firestore, checks for existing .cha and .txt in Firebase Storage. If missing, generates them and uploads to storage. Then extracts features and returns structured dict. """ try: # 1. Fetch age and gender from Firestore doc_ref = db.collection("Questionnaire").document(docID) doc = doc_ref.get() if not doc.exists: print(f"Firestore document {docID} not found") return None data = doc.to_dict() age = data.get("age") gender_val = data.get("gender") if age is None or gender_val is None: print(f"Missing age or gender for {docID}") return None # Ensure gender is integer try: gender = int(gender_val) except (ValueError, TypeError): print(f"Invalid gender value '{gender_val}' for {docID} (expected 0 or 1)") return None if gender not in (0, 1): print(f"Gender must be 0 or 1, got {gender} for {docID}") return None gender_str = "male" if gender == 1 else "female" # 2. Ensure .wav exists wav_blob = bucket.blob(f"audiostuff/wav/{docID}.wav") if not wav_blob.exists(): print(f"WAV file not found in storage for {docID}") return None with tempfile.TemporaryDirectory() as tmpdir: wav_path = os.path.join(tmpdir, f"{docID}.wav") cha_path = os.path.join(tmpdir, f"{docID}.cha") meta_path = os.path.join(tmpdir, f"{docID}.txt") wav_blob.download_to_filename(wav_path) # 3. Check for existing .txt and .cha in Firebase Storage meta_blob = bucket.blob(f"audiostuff/meta/{docID}.txt") cha_blob = bucket.blob(f"audiostuff/cha/{docID}.cha") meta_exists = meta_blob.exists() cha_exists = cha_blob.exists() # 4. If meta missing → create and upload if not meta_exists: with open(meta_path, 'w') as f: f.write(f"{age}\n{gender_str}\n") meta_blob.upload_from_filename(meta_path) print(f"Uploaded meta file for {docID}") else: meta_blob.download_to_filename(meta_path) # Re-parse to ensure consistency (optional) with open(meta_path, 'r') as f: lines = f.read().strip().splitlines() age = int(lines[0]) gender_str = lines[1].lower() gender = 1 if gender_str == "male" else 0 # 5. If .cha missing → generate and upload if not cha_exists: print(f"Generating .cha for {docID}...") y, sr = librosa.load(wav_path, sr=16000) whisper_model = _load_whisper() result = whisper_model.transcribe( audio=y, language="en", suppress_tokens=[], fp16=False ) raw_text = result["text"].strip() if not raw_text: print(f"Empty transcription for {docID}") return None utterances = text_to_cha_lines(raw_text) if not utterances: return None nlp = _load_spacy() with open(cha_path, 'w', encoding='utf-8') as f: f.write("@UTF8\n") f.write("@PID:\t11312/t-00002184-1\n") f.write("@Begin\n") f.write("@Languages:\teng\n") f.write("@Participants:\tPAR Participant, INV Investigator\n") f.write(f"@ID:\teng|Pitt|PAR|??|?|Control||Participant|||\n") f.write(f"@Media:\t{docID}, audio\n") for idx, utt in enumerate(utterances): if not utt.strip(): continue clean_utt = utt.replace('"', "'").strip() speaker = "INV" if idx == 0 else "PAR" f.write(f'*{speaker}: {clean_utt}\n') doc_spacy = nlp(clean_utt) mor_line, gra_line = generate_mor_gra_line(doc_spacy) if mor_line: f.write(f"%mor: {mor_line}\n") if gra_line: f.write(f"%gra: {gra_line}\n") f.write("@End\n") cha_blob.upload_from_filename(cha_path) print(f"Uploaded .cha file for {docID}") else: cha_blob.download_to_filename(cha_path) # 6. Extract features audio_features = extract_audio_features(wav_path) text_features = extract_text_features(cha_path) if audio_features is None or text_features is None: return None meta_features = np.array([age, gender, np.nan]) full_features = np.concatenate([audio_features, meta_features, text_features]) return { 'patient_id': docID, 'age': age, 'gender': gender, 'mmse': None, 'audio_features': audio_features, 'text_features': text_features, 'full_features': full_features } except Exception as e: print(f"Error in pull_audio_features_from_firebase for {docID}: {e}") return None def pull_lifestyle(document_id: str): """Fetches lifestyle data from Firestore.""" # Just gets all the fields and there data from firebase try: doc_ref = db.collection("Questionnaire").document(document_id) doc = doc_ref.get() if doc.exists: return doc.to_dict() else: print(f"Document {document_id} not found.") return {} except Exception as e: print(f"Error fetching document {document_id}: {e}") return {}