import os import sys import json import numpy as np import soundfile as sf from pathlib import Path # Paths BASE_DIR = Path(__file__).resolve().parent.parent.parent DATA_PATH = BASE_DIR / "data" / "hindi_diabetes_doctor_patient_v2.json" OUTPUT_AUDIO_DIR = BASE_DIR / "datasets" / "processed_audio" MANIFESTS_DIR = BASE_DIR / "datasets" / "manifests" OUTPUT_AUDIO_DIR.mkdir(parents=True, exist_ok=True) MANIFESTS_DIR.mkdir(parents=True, exist_ok=True) def generate_tone_speech_sample(text: str, filepath: Path, sample_rate: int = 16000): """ Generates synthetic speech acoustic features (modulated waveform) for training when offline/testing to ensure valid audio files exist for every utterance. """ duration = max(1.5, min(8.0, len(text) * 0.08)) t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) # Fundamental frequency modulated with formants (synthesizing speech formant harmonics) f0 = 140.0 # Voice pitch f1 = 500.0 # Formant 1 f2 = 1500.0 # Formant 2 f3 = 2500.0 # Formant 3 mod = np.sin(2 * np.pi * 3.5 * t) # Syllable rate modulation (~3.5 Hz) carrier = ( 0.5 * np.sin(2 * np.pi * f0 * t) + 0.3 * np.sin(2 * np.pi * f1 * t) + 0.15 * np.sin(2 * np.pi * f2 * t) + 0.05 * np.sin(2 * np.pi * f3 * t) ) waveform = carrier * np.clip(mod + 0.5, 0.0, 1.0) waveform = waveform * 0.7 # Normalize volume sf.write(str(filepath), waveform.astype(np.float32), sample_rate) def build_dataset_and_manifests(): print(f"Loading dataset from: {DATA_PATH}") if not DATA_PATH.exists(): alt_path = Path("e:/Diabetes_Dost/Project_1-main/data/hindi_diabetes_doctor_patient_v2.json") if alt_path.exists(): data_file = alt_path else: raise FileNotFoundError(f"Cannot find dataset at {DATA_PATH} or {alt_path}") else: data_file = DATA_PATH with open(data_file, "r", encoding="utf-8") as f: data = json.load(f) questions = data.get("questions", []) records = [] patient_responses_map = { "DIA_Q01": ["मुझे पिछले कुछ दिनों से बहुत ज्यादा थकान और प्यास लग रही है", "मेरी शुगर बहुत बढ़ गई है चक्कर आ रहे हैं"], "DIA_Q02": ["हाँ मुझे 5 साल से Type 2 Diabetes है", "मुझे 2 साल पहले पता चला था"], "DIA_Q03": ["सुबह Fasting Blood Sugar 260 mg/dL आई थी", "खाने के बाद PP Sugar 190 थी"], "DIA_Q04": ["हाँ अचानक हाथ कांपने लगे थे और बहुत पसीना आ रहा था", "मुझे हल्का चक्कर और कमजोरी महसूस हो रही है"], "DIA_Q05": ["हाँ बार बार पेशाब आ रही है और नजर धुंधली हो रही है", "बहुत ज्यादा प्यास लग रही है गला सूख रहा है"], "DIA_Q06": ["सुबह नाश्ता नहीं किया था और वॉक करने गया था", "दोपहर में खाना समय पर खाया था"], "DIA_Q07": ["मैं Metformin 500mg सुबह शाम और इंसुलिन लेता हूँ", "Glimepiride 1mg गोली रोज सुबह लेता हूँ"], "DIA_Q08": ["हाँ कल रात की दवा की डोज लेना भूल गया था", "मैंने समय पर सभी दवाएं ली हैं"], "DIA_Q09": ["दवा लेने के बाद कभी कभी उल्टी जैसी महसूस होती है", "कोई एलर्जी या नया लक्षण नहीं हुआ"], "DIA_Q10": ["आमतौर पर शुगर 150 से 200 के बीच रहती है", "हाल ही में शुगर बहुत ऊपर नीचे हो रही है"], "DIA_Q11": ["हाँ पिछले महीने HbA1c टेस्ट कराया था रिजल्ट 7.8 आया था", "HbA1c रिपोर्ट 6.8 प्रतिशत थी"], "DIA_Q12": ["पैरों में सुन्नपन और झनझनाहट रहती है", "आंखों से थोड़ा धुंधला दिखता है"], "DIA_Q13": ["पैर के अंगूठे में छोटा घाव है जो ठीक नहीं हो रहा", "पैर में कोई घाव या सूजन नहीं है"], "DIA_Q14": ["मुझे High Blood Pressure की भी शिकायत है", "मुझे थायरॉयड की समस्या है"], "DIA_Q15": ["मैं रोज दाल रोटी और हरी सब्जियां खाता हूँ मीठा बंद है", "समय पर भोजन करता हूँ"], "DIA_Q16": ["मैं रोज 30 मिनट सुबह वॉक करता हूँ", "नियमित रूप से शाम को घूमता हूँ"], "DIA_Q17": ["मेरी उम्र 52 वर्ष है और पिताजी को भी डायबिटीज थी", "उम्र 45 साल है वजन 70 किलो है"], "DIA_Q18": ["नहीं मैं गर्भवती नहीं हूँ", "ऐसी कोई स्थिति नहीं है"], "DIA_Q19": ["मैं अपनी दवा और डाइट को बेहतर समझना चाहता हूँ", "शुगर कंट्रोल के लिए सलाह चाहिए"], "DIA_Q20": ["नहीं बाकी सब बातें बता दी हैं धन्यवाद", "कोई और समस्या नहीं है"] } count = 0 for q in questions: qid = q["question_id"] q_text = q["question_hi"] # Doctor Question audio doc_audio_file = OUTPUT_AUDIO_DIR / f"{qid}_doctor.wav" generate_tone_speech_sample(q_text, doc_audio_file) records.append({ "audio_filepath": str(doc_audio_file).replace("\\", "/"), "text": q_text, "duration": round(float(len(q_text) * 0.08), 2), "question_id": qid, "role": "doctor" }) count += 1 # Patient Answer audio samples answers = patient_responses_map.get(qid, ["हाँ सब ठीक है"]) for idx, ans_text in enumerate(answers): pat_audio_file = OUTPUT_AUDIO_DIR / f"{qid}_patient_{idx+1}.wav" generate_tone_speech_sample(ans_text, pat_audio_file) records.append({ "audio_filepath": str(pat_audio_file).replace("\\", "/"), "text": ans_text, "duration": round(float(len(ans_text) * 0.08), 2), "question_id": qid, "role": "patient" }) count += 1 print(f"Generated {count} audio samples.") # Split 80/20 train/validation np.random.seed(42) indices = np.random.permutation(len(records)) split_idx = int(0.8 * len(records)) train_records = [records[i] for i in indices[:split_idx]] val_records = [records[i] for i in indices[split_idx:]] train_manifest = MANIFESTS_DIR / "train.jsonl" val_manifest = MANIFESTS_DIR / "validation.jsonl" with open(train_manifest, "w", encoding="utf-8") as f: for r in train_records: f.write(json.dumps(r, ensure_ascii=False) + "\n") with open(val_manifest, "w", encoding="utf-8") as f: for r in val_records: f.write(json.dumps(r, ensure_ascii=False) + "\n") # Also write to backend/datasets/manifests backend_manifest_dir = BASE_DIR / "backend" / "datasets" / "manifests" backend_manifest_dir.mkdir(parents=True, exist_ok=True) with open(backend_manifest_dir / "train.jsonl", "w", encoding="utf-8") as f: for r in train_records: f.write(json.dumps(r, ensure_ascii=False) + "\n") with open(backend_manifest_dir / "validation.jsonl", "w", encoding="utf-8") as f: for r in val_records: f.write(json.dumps(r, ensure_ascii=False) + "\n") print(f"Wrote {len(train_records)} train samples to {train_manifest}") print(f"Wrote {len(val_records)} validation samples to {val_manifest}") if __name__ == "__main__": build_dataset_and_manifests()