File size: 32,449 Bytes
4867007
 
 
 
 
1c9da88
4867007
1c9da88
 
 
 
 
 
4867007
3f2d74a
 
 
 
 
 
 
0cf71c1
4867007
 
 
 
 
 
 
 
 
 
d69bf5b
4867007
 
 
 
 
 
 
 
 
 
 
 
d69bf5b
4867007
 
 
 
 
 
 
1c9da88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
736c9df
 
480df2d
736c9df
480df2d
04a5cd6
645be11
480df2d
645be11
736c9df
 
 
480df2d
 
645be11
04a5cd6
d2aac79
645be11
d2aac79
645be11
d2aac79
04c23ef
1c9da88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4867007
 
 
 
 
 
 
 
 
 
 
 
3f2d74a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c9da88
 
03287a3
 
 
 
 
 
 
1c9da88
 
03287a3
1c9da88
 
4867007
 
 
 
03287a3
 
 
4867007
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80b6796
9dbf137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4867007
 
 
 
1c9da88
80b6796
1c9da88
 
4867007
 
 
1c9da88
80b6796
 
 
 
1c9da88
80b6796
1c9da88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9dbf137
 
1c9da88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4867007
 
80b6796
4867007
 
 
 
80b6796
4867007
 
 
80b6796
4867007
 
 
 
 
 
80b6796
4867007
 
 
 
 
5850002
4867007
 
 
 
 
 
 
 
 
 
 
5850002
 
 
 
 
4867007
5850002
4867007
 
5850002
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4867007
 
5850002
 
 
 
4867007
 
 
 
 
5850002
 
 
4867007
 
 
5850002
 
 
 
 
 
80b6796
5850002
 
80b6796
5850002
 
 
 
 
 
 
 
80b6796
5850002
 
 
 
 
 
 
 
 
 
 
 
 
4867007
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c9da88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4867007
 
 
 
 
 
1c9da88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f2d74a
eaaa368
 
3f2d74a
eaaa368
3f2d74a
 
 
 
 
 
 
eaaa368
1c9da88
5875a95
 
 
 
 
 
 
 
 
1c9da88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4867007
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
import os
import time
import json
import requests
from dotenv import load_dotenv, find_dotenv
from flask import Flask, Blueprint, request, jsonify, current_app, send_from_directory
# Note: we avoid creating a Flask app at module import time
import uuid
from pathlib import Path
from typing import Iterable, Optional, Sequence, Union
from flask_cors import CORS
import requests
from TTS.api import TTS

# --- S3 (added) ---
try:
    import boto3
    from botocore.exceptions import NoCredentialsError, ClientError
except Exception:
    boto3 = None
    NoCredentialsError = ClientError = Exception  # fallbacks so type names exist

# RAG imports
try:
    from .rag_backend import IngestBody, ingest_documents, ingest_pdfs_from_folder
    from .rag_llm import (
        LLMBody,
        llm_generate,
        ExplainBody,
        llm_explain,
        FollowupBody,
        get_vectorstore,
        get_vectorstore_for,  # ← add this
        llm_followups,
    )
except ImportError:
    # Fallback when running as: python ragg/app.py
    from rag_backend import IngestBody, ingest_documents, ingest_pdfs_from_folder
    from rag_llm import (
        LLMBody,
        llm_generate,
        ExplainBody,
        llm_explain,
        FollowupBody,
        get_vectorstore,
        get_vectorstore_for,  # ← add this
        llm_followups,
    )

# OpenAI client (no secret logs)
import openai
from openai import OpenAI


def xtts_speak_to_file(
    text: str,
    out_file: Optional[Union[str, Path]] = None,
    reference_dir: Optional[Union[str, Path]] = "trim",
    reference_files: Optional[Sequence[Union[str, Path]]] = None,
    language: str = "en",
    patterns: Iterable[str] = ("*.wav", "*.mp3", "*.flac"),
) -> Path:
    """
    Generate a WAV using XTTS v2 with reference audios; caches the model.
    """
    speakers: list[str] = []
    if reference_files:
        speakers.extend(str(Path(p)) for p in reference_files)

    if (not speakers) and reference_dir:
        vdir = Path(reference_dir)
        for pat in patterns:
            speakers.extend(str(p) for p in vdir.glob(pat))

    speakers = list(dict.fromkeys(speakers))
    if not speakers:
        raise FileNotFoundError(
            f"No reference audio files found. Checked: "
            f"{reference_files or []} and/or {reference_dir}"
        )

    if not hasattr(xtts_speak_to_file, "_model") or xtts_speak_to_file._model is None:
        import sys, builtins, torch
        from torch.serialization import add_safe_globals
        # --- XTTS internal classes that must be allow-listed ---
        from TTS.tts.configs.xtts_config import XttsConfig
        from TTS.tts.models.xtts import XttsAudioConfig, XttsArgs
        from TTS.config.shared_configs import BaseDatasetConfig

        # Prevent interactive prompts / stdin crashes on Hugging Face
        sys.stdin = open(os.devnull)
        builtins.input = lambda *a, **kw: ""
        os.environ["COQUI_TOS_AGREED"] = "1"

        # Allowlist all required XTTS classes for PyTorch 2.6+
        add_safe_globals([XttsConfig, XttsAudioConfig, BaseDatasetConfig, XttsArgs])

        # Initialize the XTTS model safely
        xtts_speak_to_file._model = TTS(
            model_name="tts_models/multilingual/multi-dataset/xtts_v2",
            gpu=False,
            progress_bar=False,
        )
   
    tts = xtts_speak_to_file._model

    out_path = Path(out_file) if out_file else Path(f"xtts_{uuid.uuid4().hex}.wav")
    out_path.parent.mkdir(parents=True, exist_ok=True)

    try:
        tts.tts_to_file(
            text=text,
            speaker_wav=speakers,
            language=language,
            file_path=str(out_path),
        )
    except Exception as e:
        raise RuntimeError(f"XTTS synthesis failed: {e}") from e

    return out_path

# ------------------------------------------------------------
# Load environment
# ------------------------------------------------------------
load_dotenv(find_dotenv()) 
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# Optional: version log (safe), but do NOT print the API key
try:
    print(f"openai package version: {openai.__version__}")
except Exception:
    pass

# --- S3 config (added) ---
S3_BUCKET = os.getenv("S3_BUCKET", "").strip()
AWS_REGION = os.getenv("AWS_REGION", "ap-south-1").strip()
S3_PREFIX = os.getenv("S3_PREFIX", "audio/").strip()
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "").strip()
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "").strip()

_s3_client = None
if boto3 and S3_BUCKET and AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:
    try:
        _s3_client = boto3.client(
            "s3",
            region_name=AWS_REGION,
            aws_access_key_id=AWS_ACCESS_KEY_ID,
            aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
        )
    except Exception as _e:
        _s3_client = None

def _upload_to_s3(file_path: Union[str, Path]) -> Optional[str]:
    """
    Upload the file to S3 and return a presigned URL (24h).
    If S3 is not configured, returns None (caller will fallback).
    """
    if not _s3_client or not S3_BUCKET:
        return None
    try:
        file_path = str(file_path)
        key = f"{S3_PREFIX}{Path(file_path).name}"
        _s3_client.upload_file(file_path, S3_BUCKET, key)
        url = _s3_client.generate_presigned_url(
            "get_object",
            Params={"Bucket": S3_BUCKET, "Key": key},
            ExpiresIn=24 * 3600,
        )
        return url
    except (NoCredentialsError, ClientError) as e:
        try:
            current_app.logger.error(f"S3 upload failed: {e}")
        except Exception:
            print(f"S3 upload failed: {e}")
        return None

# Media and voice references

# MEDIA_ROOT = Path(os.getenv("MEDIA_ROOT", "./media"))
# AUDIO_DIR = MEDIA_ROOT / "audio"
# AUDIO_DIR.mkdir(parents=True, exist_ok=True)
# XTTS_REF_DIR = os.getenv("XTTS_REF_DIR", "./trim")  # folder with your reference audios

BASE_DIR = Path(__file__).resolve().parent.parent  # if app.py is top-level; if it's ragg/app.py use .parent.parent
MEDIA_ROOT = Path(os.getenv("MEDIA_ROOT", str(BASE_DIR / "media")))
AUDIO_DIR = MEDIA_ROOT / "audio"
AUDIO_DIR.mkdir(parents=True, exist_ok=True)
XTTS_REF_DIR = os.getenv("XTTS_REF_DIR", str(BASE_DIR / "trim"))  # reference voice files

# D-ID config (optional)
# ------------------------------------------------------------
# Blueprint (mounted at /rag by the main app)
# ------------------------------------------------------------
rag_bp = Blueprint("rag", __name__)
@rag_bp.route("/audio/<path:filename>", methods=["GET"])
def rag_serve_audio(filename: str):
    return send_from_directory(AUDIO_DIR, filename, mimetype="audio/wav", conditional=True)

# D-ID config (set in .env / HF Secrets)
DID_API_KEY = os.getenv("DID_API_KEY", "")
DID_SOURCE_IMAGE_URL = os.getenv("DID_SOURCE_IMAGE_URL", "")
DID_VOICE_ID = os.getenv("DID_VOICE_ID", "en-US-JennyNeural")

# Default folder for /ingest-pdfs
PDF_DEFAULT_FOLDER = os.getenv("RAG_PDF_DIR", "./pdfs")


# Optional: add CORS headers (the main app should still enable CORS globally)
@rag_bp.after_app_request
def add_cors_headers(resp):
    origin = request.headers.get("Origin")
    # Allow local Angular during dev; main app may add more origins
    if origin in ("http://localhost:4200", "http://127.0.0.1:4200"):
        resp.headers["Access-Control-Allow-Origin"] = origin
        resp.headers["Vary"] = "Origin"
        resp.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization, X-User"
        resp.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
    return resp


# ------------------------------------------------------------
# Helpers
# ------------------------------------------------------------
def user_to_db_level(username: str | None) -> str | None:
    if not username:
        return None
    u = username.strip().lower()
    if u == "lowergrade":
        return "low"
    if u == "midgrade":
        return "mid"
    if u == "highergrade":
        return "high"
    return None


def extract_username_from_request(req) -> str | None:
    hdr = req.headers.get("X-User")
    if hdr:
        return hdr
    data = req.get_json(silent=True) or {}
    return data.get("username")


# --- D-ID helpers ---
def _did_create_talk(text: str):
    if not DID_API_KEY:
        return None, ("DID_API_KEY not set on the server", 500)
    if not DID_SOURCE_IMAGE_URL:
        return None, ("DID_SOURCE_IMAGE_URL not set on the server", 500)

    payload = {
        "script": {
            "type": "text",
            "input": text,
            "provider": {"type": "microsoft", "voice_id": DID_VOICE_ID},
        },
        "source_url": DID_SOURCE_IMAGE_URL,
        "config": {"fluent": True, "pad_audio": 0},
    }
    try:
        r = requests.post("https://api.d-id.com/talks", json=payload, auth=(DID_API_KEY, ""))
        if r.status_code not in (200, 201):
            return None, (f"D-ID create error: {r.text}", 502)
        talk_id = r.json().get("id")
        if not talk_id:
            return None, ("D-ID did not return a talk id", 502)
        return talk_id, None
    except Exception as e:
        current_app.logger.exception("D-ID create failed: %s", e)
        return None, ("D-ID create failed", 502)


def _did_poll_talk(talk_id: str, timeout_sec: int = 60, interval_sec: float = 2.0):
    deadline = time.time() + timeout_sec
    url = f"https://api.d-id.com/talks/{talk_id}"
    try:
        while time.time() < deadline:
            r = requests.get(url, auth=(DID_API_KEY, ""))
            if r.status_code != 200:
                return None, (f"D-ID poll error: {r.text}", 502)
            data = r.json()
            status = data.get("status")
            if status == "done":
                return data.get("result_url") or data.get("result", {}).get("url"), None
            if status == "error":
                return None, (f"D-ID generation failed: {data.get('error')}", 502)
            time.sleep(interval_sec)
        return None, ("Timed out waiting for the video", 504)
    except Exception as e:
        current_app.logger.exception("D-ID poll failed: %s", e)
        return None, ("D-ID poll failed", 502)


# ------------------------------------------------------------
# Endpoints (NOTE: no "/rag" prefix here; the blueprint adds it)
# ------------------------------------------------------------
@rag_bp.route("/ingest", methods=["POST", "OPTIONS"])
def rag_ingest():
    if request.method == "OPTIONS":
        return ("", 204)
    body = IngestBody(**(request.json or {}))
    result = ingest_documents(body)
    return jsonify(result)


@rag_bp.route("/ingest-pdfs", methods=["POST", "OPTIONS"])
def rag_ingest_pdfs():
    if request.method == "OPTIONS":
        return ("", 204)
    data = request.json or {}
    folder = data.get("folder", PDF_DEFAULT_FOLDER)
    subject = data.get("subject")
    grade = data.get("grade")
    chapter = data.get("chapter")
    result = ingest_pdfs_from_folder(folder, subject=subject, grade=grade, chapter=chapter)
    return jsonify(result)


@rag_bp.route("/generate-questions", methods=["POST", "OPTIONS"])
def rag_generate_questions():
    if request.method == "OPTIONS":
        return ("", 204)
    data = request.json or {}
    username = extract_username_from_request(request)
    mapped_level = user_to_db_level(username)
    if not data.get("db_level"):
        data["db_level"] = mapped_level
    body = LLMBody(**data)
    result = llm_generate(body)
    return jsonify(result)


# @rag_bp.route("/explain-grammar", methods=["POST", "OPTIONS"])
# @rag_bp.route("/explain-grammar", methods=["POST", "OPTIONS"])
# def rag_explain_grammar():
#     if request.method == "OPTIONS":
#         return ("", 204)

#     data = request.get_json(force=True) or {}

#     # --- Extract username and db_level ---
#     username = extract_username_from_request(request)
#     db_level = user_to_db_level(username)

#     # --- MAIN BODY (your preferred structure) ---
#     body = ExplainBody(
#         question=(data.get("question") or "").strip(),
#         model=data.get("model", "gpt-4o-mini"),
#         db_level=db_level,
#         source_ids=data.get("source_ids") or []
#     )

#     # --- 1) Run LLM / RAG explanation ---
#     result_raw = llm_explain(body)

#     # --- 2) Normalize + extract answer safely ---
#     result_dict = None
#     answer_text = ""
#     try:
#         if isinstance(result_raw, dict):
#             result_dict = dict(result_raw)
#         elif hasattr(result_raw, "model_dump"):
#             result_dict = result_raw.model_dump()
#         elif hasattr(result_raw, "dict"):
#             result_dict = result_raw.dict()
#         elif isinstance(result_raw, str):
#             result_dict = {"answer": result_raw}
#         else:
#             result_dict = {"answer": str(result_raw)}

#         answer_text = (
#             result_dict.get("answer")
#             or result_dict.get("response")
#             or result_dict.get("text")
#             or ""
#         ).strip()
#     except Exception as e:
#         current_app.logger.exception("Failed to normalize llm_explain result: %s", e)
#         return jsonify({"error": "Internal error normalizing LLM response"}), 500

#     # --- 3) Optional: synthesize TTS audio ---
#     try:
#         if data.get("synthesize_audio"):
#             try:
#                 out_name = f"explain_{uuid.uuid4().hex}.wav"
#                 wav_path = xtts_speak_to_file(
#                     text=answer_text or result_dict.get("answer", ""),
#                     out_file=AUDIO_DIR / out_name,
#                     reference_dir=XTTS_REF_DIR,
#                     reference_files=None,
#                     language=data.get("language", "en"),
#                 )
#                 # Local: serve from /rag/audio/*
#                 if "localhost" in request.host_url or "127.0.0.1" in request.host_url:
#                     base = request.host_url.rstrip("/")
#                     result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
#                 else:
#                     # Deployed: try S3 first; fallback to public SPACE_URL if set
#                     s3_url = _upload_to_s3(str(wav_path))
#                     if s3_url:
#                         result_dict["audio_url"] = s3_url
#                     else:
#                         base = os.getenv("SPACE_URL", "https://pykara-py-learn-backend.hf.space")
#                         result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
#             except FileNotFoundError as e:
#                 current_app.logger.error("XTTS reference audio missing: %s", e)
#             except Exception as e:
#                 current_app.logger.exception("XTTS synthesis during explain-grammar failed: %s", e)
#     except Exception:
#         current_app.logger.exception("Unexpected error while attempting inline synthesis")

#     # --- 4) Optional: synthesize video (D-ID) ---
#     try:
#         if data.get("synthesize_video"):
#             if not DID_API_KEY or not DID_SOURCE_IMAGE_URL:
#                 current_app.logger.error("D-ID not configured for inline explain-grammar video synthesis")
#             else:
#                 try:
#                     talk_id, err = _did_create_talk(answer_text or result_dict.get("answer", ""))
#                     if err:
#                         current_app.logger.error(
#                             "D-ID create error during explain-grammar: %s",
#                             err[0] if isinstance(err, tuple) else err,
#                         )
#                     else:
#                         video_url, err = _did_poll_talk(talk_id, timeout_sec=120, interval_sec=2.0)
#                         if err:
#                             current_app.logger.error(
#                                 "D-ID poll error during explain-grammar: %s",
#                                 err[0] if isinstance(err, tuple) else err,
#                             )
#                         else:
#                             if video_url:
#                                 result_dict["video_url"] = video_url
#                 except Exception as e:
#                     current_app.logger.exception("D-ID inline synthesis failed during explain-grammar: %s", e)
#     except Exception:
#         current_app.logger.exception("Unexpected error while attempting inline video synthesis")

#     # --- Final response ---
#     return jsonify(result_dict), 200

@rag_bp.route("/explain-grammar", methods=["POST", "OPTIONS"])
def rag_explain_grammar():
    if request.method == "OPTIONS":
        return ("", 204)

    data = request.get_json(force=True) or {}

    # --- Extract username and db_level ---
    username = extract_username_from_request(request)
    db_level = user_to_db_level(username)

    # --- MAIN BODY (your preferred structure) ---
    body = ExplainBody(
        question=(data.get("question") or "").strip(),
        model=data.get("model", "gpt-4o-mini"),
        db_level=db_level,
        source_ids=data.get("source_ids") or []
    )

    # --- 1) Run LLM / RAG explanation ---
    result_raw = llm_explain(body)

    # --- 2) Normalize + extract answer safely ---
    result_dict = None
    answer_text = ""
    try:
        if isinstance(result_raw, dict):
            result_dict = dict(result_raw)
        elif hasattr(result_raw, "model_dump"):
            result_dict = result_raw.model_dump()
        elif hasattr(result_raw, "dict"):
            result_dict = result_raw.dict()
        elif isinstance(result_raw, str):
            result_dict = {"answer": result_raw}
        else:
            result_dict = {"answer": str(result_raw)}

        answer_text = (
            result_dict.get("answer")
            or result_dict.get("response")
            or result_dict.get("text")
            or ""
        ).strip()
    except Exception as e:
        current_app.logger.exception("Failed to normalize llm_explain result: %s", e)
        return jsonify({"error": "Internal error normalizing LLM response"}), 500

    # --- 3) Optional: synthesize TTS audio ---
    try:
        if data.get("synthesize_audio"):
            try:
                out_name = f"explain_{uuid.uuid4().hex}.wav"
                wav_path = xtts_speak_to_file(
                    text=answer_text or result_dict.get("answer", ""),
                    out_file=AUDIO_DIR / out_name,
                    reference_dir=XTTS_REF_DIR,
                    reference_files=None,
                    language=data.get("language", "en"),
                )
                base = request.host_url.rstrip("/")
                result_dict["audio_url"] = f"{base}/rag/audio/{wav_path.name}"
            except FileNotFoundError as e:
                current_app.logger.error("XTTS reference audio missing: %s", e)
            except Exception as e:
                current_app.logger.exception("XTTS synthesis during explain-grammar failed: %s", e)
    except Exception:
        current_app.logger.exception("Unexpected error while attempting inline synthesis")

    # --- 4) Optional: synthesize video (D-ID) ---
    try:
        if data.get("synthesize_video"):
            if not DID_API_KEY or not DID_SOURCE_IMAGE_URL:
                current_app.logger.error("D-ID not configured for inline explain-grammar video synthesis")
            else:
                try:
                    talk_id, err = _did_create_talk(answer_text or result_dict.get("answer", ""))
                    if err:
                        current_app.logger.error(
                            "D-ID create error during explain-grammar: %s",
                            err[0] if isinstance(err, tuple) else err,
                        )
                    else:
                        video_url, err = _did_poll_talk(talk_id, timeout_sec=120, interval_sec=2.0)
                        if err:
                            current_app.logger.error(
                                "D-ID poll error during explain-grammar: %s",
                                err[0] if isinstance(err, tuple) else err,
                            )
                        else:
                            if video_url:
                                result_dict["video_url"] = video_url
                except Exception as e:
                    current_app.logger.exception("D-ID inline synthesis failed during explain-grammar: %s", e)
    except Exception:
        current_app.logger.exception("Unexpected error while attempting inline video synthesis")

    # --- Final response ---
    return jsonify(result_dict), 200



# @rag_bp.route("/suggest-followups", methods=["POST", "OPTIONS"])
@rag_bp.route("/suggest-followups", methods=["POST", "OPTIONS"])
def rag_suggest_followups():
    if request.method == "OPTIONS":
        return ("", 204)

    data = request.get_json(force=True) or {}
    username = extract_username_from_request(request)
    db_level = user_to_db_level(username)

    body = FollowupBody(
        last_question=(data.get("last_question") or "").strip(),
        last_answer=(data.get("last_answer") or "").strip(),
        n=int(data.get("n", 5)),
        model=data.get("model", "gpt-4o-mini"),
        db_level=db_level,
        source_ids=data.get("source_ids") or []   # ← same addition here
    )
    result = llm_followups(body)
    return jsonify(result)


# @rag_bp.get("/_diag")
@rag_bp.get("/_diag")
def rag_diag():
    try:
        from .rag_llm import CHROMA_DIR, CHROMA_ROOT, get_vectorstore, get_vectorstore_for
    except ImportError:
        from rag_llm import CHROMA_DIR, CHROMA_ROOT, get_vectorstore, get_vectorstore_for

    import os
    from flask import jsonify

    def _count(vs):
        """Handle both LangChain and chromadb client objects."""
        if vs is None:
            return None
        # 1️⃣ chromadb.Collection (your new get_vectorstore_for)
        if hasattr(vs, "count") and callable(vs.count):
            try:
                return vs.count()
            except Exception:
                return None
        # 2️⃣ LangChain vectorstore
        if hasattr(vs, "_collection"):
            try:
                return vs._collection.count()  # type: ignore
            except Exception:
                try:
                    return vs._client.get_collection(vs._collection.name).count()  # type: ignore
                except Exception:
                    return None
        return None

    # load each level safely
    low_vs = get_vectorstore_for("low")
    mid_vs = get_vectorstore_for("mid")
    high_vs = get_vectorstore_for("high")

    info = {
        "env_seen": {
            "CHROMA_DIR": CHROMA_DIR,
            "CHROMA_ROOT": CHROMA_ROOT
        },
        "low_dir": {
            "path": os.path.join(CHROMA_ROOT, "low"),
            "exists": os.path.isdir(os.path.join(CHROMA_ROOT, "low")),
        },
        "counts_default": _count(get_vectorstore()),
        "counts_low": _count(low_vs),
        "counts_mid": _count(mid_vs),
        "counts_high": _count(high_vs),
    }
    return jsonify(info), 200

# def rag_diag():
#     # minimal imports here to avoid circulars
#     try:
#         from .rag_llm import CHROMA_DIR, CHROMA_ROOT, get_vectorstore, get_vectorstore_for
#     except ImportError:
#         from rag_llm import CHROMA_DIR, CHROMA_ROOT, get_vectorstore, get_vectorstore_for
#
#     import os
#     from flask import jsonify
#
#     def _count(vs):
#         try:
#             return vs._collection.count()
#         except Exception:
#             try:
#                 return vs._client.get_collection(vs._collection.name).count()
#             except Exception:
#                 return None
#
#     info = {
#         "env_seen": {"CHROMA_DIR": CHROMA_DIR, "CHROMA_ROOT": CHROMA_ROOT},
#         "low_dir": {
#             "path": os.path.join(CHROMA_ROOT, "low"),
#             "exists": os.path.isdir(os.path.join(CHROMA_ROOT, "low")),
#         },
#         "counts_default": _count(get_vectorstore()),
#         "counts_low": _count(get_vectorstore_for("low")),
#         "counts_mid": _count(get_vectorstore_for("mid")),
#         "counts_high": _count(get_vectorstore_for("high")),
#     }
#     return jsonify(info), 200

@rag_bp.route("/search", methods=["POST", "OPTIONS"])
def rag_search():
    if request.method == "OPTIONS":
        return ("", 204)
    data = request.json or {}
    q = (data.get("q") or "").strip()
    if not q:
        return jsonify({"results": []})

    # derive db_level from login, unless explicitly provided
    username = extract_username_from_request(request)
    mapped_level = user_to_db_level(username)
    db_level = data.get("db_level") or mapped_level

    vs = get_vectorstore_for(db_level)
    hits = vs.similarity_search_with_score(q, k=5)
    out = []
    for doc, dist in hits:
        out.append({
            "distance": float(dist),
            "snippet": doc.page_content[:200],
            "source_path": os.path.normpath(doc.metadata.get("source_path", "")),
            "page": doc.metadata.get("page_1based"),
        })
    return jsonify({"results": out})


def generate_questions_from_vectorstore():
    try:
        vectorstore = get_vectorstore()
        query_text = "important content related to grammar"
        results = vectorstore.similarity_search_with_score(query_text, k=5)
        print(f"Vectorstore query returned {len(results)} results")
        content = "\n".join([doc.page_content for doc, _ in results])
        print(f"Retrieved content: {content[:500]}...")
        if not content:
            return {"error": "No content retrieved from vectorstore. Please ingest PDFs first."}
        prompt = f"Generate 5 important questions based on the following content: {content}"
        response = openai_client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7,
            max_tokens=150,
        )
        response_text = response.choices[0].message.content.strip()
        print(f"Processed OpenAI response: {response_text}")
        return response_text
    except Exception as e:
        print(f"Error during OpenAI API call: {e}")
        return {"error": f"Failed to call OpenAI: {str(e)}"}


@rag_bp.route("/generate-questions-from-chroma", methods=["POST", "OPTIONS"])
def generate_questions_from_chroma():
    def _generate_questions_from_vectorstore():
        try:
            vectorstore = get_vectorstore()
            query_text = "important content related to grammar"
            results = vectorstore.similarity_search_with_score(query_text, k=5)
            content = "\n".join([doc.page_content for doc, _ in results])
            if not content:
                return {"error": "No content retrieved from vectorstore. Please ingest PDFs first."}
            prompt = f"Generate 5 important questions based on the following content: {content}"
            response = openai_client.chat.completions.create(
                model="gpt-4o-mini",
                messages=[{"role": "user", "content": prompt}],
                temperature=0.7,
                max_tokens=150,
            )
            return response.choices[0].message.content.strip()
        except Exception as e:
            return {"error": f"Failed to call OpenAI: {str(e)}"}

    generated = _generate_questions_from_vectorstore()
    return jsonify({"generated_questions": generated})


@rag_bp.get("/health")
def health():
    return {"status": "ok"}, 200

@rag_bp.route("/synthesize-audio", methods=["POST", "OPTIONS"])
def rag_synthesize_audio():
    """
    Synthesize text to WAV on demand using XTTS and return a public URL.
    Body: { "text": "...", "language": "en", "reference_files": ["trim/foo.wav", ...] }
    """
    if request.method == "OPTIONS":
        return ("", 204)

    data = request.get_json(force=True) or {}
    text = (data.get("text") or "").strip()
    if not text:
        return jsonify({"error": "No text provided"}), 400

    language = data.get("language", "en")
    reference_files = data.get("reference_files")  # optional list of paths

    try:
        out_name = f"synth_{uuid.uuid4().hex}.wav"
        wav_path = xtts_speak_to_file(
            text=text,
            out_file=AUDIO_DIR / out_name,
            reference_dir=XTTS_REF_DIR,
            reference_files=reference_files,
            language=language,
        )
        # Local: serve static file
        if "localhost" in request.host_url or "127.0.0.1" in request.host_url:
            base = request.host_url.rstrip("/")
            audio_url = f"{base}/rag/audio/{wav_path.name}"
        else:
            # Deployed: try S3 first; fallback to SPACE_URL
            s3_url = _upload_to_s3(str(wav_path))
            if s3_url:
                audio_url = s3_url
            else:
                base = os.getenv("SPACE_URL", "https://pykara-py-learn-backend.hf.space")
                audio_url = f"{base}/rag/audio/{wav_path.name}"

        return jsonify({"audio_url": audio_url, "file": wav_path.name}), 200
    except Exception as e:
        import traceback
        print("=== XTTS DEBUG ERROR ===")
        print(traceback.format_exc())
        print("========================")
        return jsonify({"error": "Synthesis failed", "detail": str(e)}), 500
    # except FileNotFoundError as e:
    #     current_app.logger.error("XTTS references missing: %s", e)
    #     return jsonify({"error": "XTTS reference audio files not found on server"}), 500
    except Exception as e:
        current_app.logger.exception("XTTS synthesis error: %s", e)
        return jsonify({"error": "Synthesis failed"}), 500


@rag_bp.route("/synthesize-video", methods=["POST", "OPTIONS"])
def rag_synthesize_video():
    """
    Synthesize a short video on-demand using the D-ID service and return the public video URL.
    Body: { "text": "..." }
    """
    if request.method == "OPTIONS":
        return ("", 204)

    data = request.get_json(force=True) or {}
    text = (data.get("text") or "").strip()
    if not text:
        return jsonify({"error": "No text provided"}), 400

    # Quick config check
    if not DID_API_KEY or not DID_SOURCE_IMAGE_URL:
        current_app.logger.error("D-ID not configured (DID_API_KEY or DID_SOURCE_IMAGE_URL missing)")
        return jsonify({"error": "D-ID not configured on server"}), 500

    try:
        # Create talk (calls D-ID /talks)
        talk_id, err = _did_create_talk(text)
        if err:
            # _did_create_talk returns (None, (msg, status))
            current_app.logger.error("D-ID create error: %s", err[0])
            return jsonify({"error": err[0]}), err[1]

        # Poll for result URL
        video_url, err = _did_poll_talk(talk_id, timeout_sec=120, interval_sec=2.0)
        if err:
            current_app.logger.error("D-ID poll error: %s", err[0])
            return jsonify({"error": err[0]}), err[1]

        if not video_url:
            current_app.logger.error("D-ID did not return a video URL for talk %s", talk_id)
            return jsonify({"error": "D-ID did not return a video URL"}), 502

        return jsonify({"video_url": video_url}), 200

    except Exception as e:
        current_app.logger.exception("Unexpected error generating D-ID video: %s", e)
        return jsonify({"error": "Internal server error generating video"}), 500


# ------------------------------------------------------------
# Local runner (DEV ONLY)
# ------------------------------------------------------------
if __name__ == "__main__":
    # Allow this module to run as a standalone server on port 7000 for local dev
    from flask import Flask
    from flask_cors import CORS

    app = Flask(__name__)

    # CORS for local dev (the production app sets CORS globally in verification.py)
    CORS(
        app,
        resources={r"/rag/*": {"origins": ["http://localhost:4200", "http://127.0.0.1:4200"]}},
        supports_credentials=True,
        allow_headers=["Content-Type", "Authorization", "X-User"],
        methods=["GET", "POST", "OPTIONS"],
    )

    # Ensure Chroma dir exists (use CHROMA_DIR if set)
    os.makedirs(os.getenv("CHROMA_DIR", "./chroma"), exist_ok=True)

    # Mount blueprint at /rag and run
    app.register_blueprint(rag_bp, url_prefix="/rag")
    app.run(host="0.0.0.0", port=7000, debug=True)