File size: 10,610 Bytes
9bf7fc2
c709985
75c8d26
9bf7fc2
75c8d26
9bf7fc2
 
 
c709985
9bf7fc2
a1180f7
3279ad9
911afbf
a63ba9d
07505ba
 
75c8d26
 
 
7b33394
 
07505ba
 
 
7b33394
 
 
9bf7fc2
 
 
083e322
 
 
9bf7fc2
c709985
9bf7fc2
79b887e
9bf7fc2
083e322
9bf7fc2
75c8d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c709985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75c8d26
c709985
 
 
75c8d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c709985
 
 
 
75c8d26
 
 
c709985
 
 
 
75c8d26
 
 
 
 
 
 
c709985
 
 
 
 
3279ad9
 
8ed2dcd
3279ad9
c709985
 
 
ef93247
3279ad9
 
 
 
 
 
c709985
9bf7fc2
083e322
9bf7fc2
083e322
 
 
 
9320e6c
083e322
 
 
 
 
 
9bf7fc2
083e322
 
 
79b887e
c709985
9bf7fc2
 
083e322
 
9bf7fc2
 
 
c709985
75c8d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a00436d
75c8d26
 
faece1b
75c8d26
 
 
 
faece1b
 
75c8d26
faece1b
 
 
 
 
 
 
 
 
 
 
 
7b33394
faece1b
7b33394
faece1b
 
 
 
 
 
 
 
 
 
 
 
7b33394
faece1b
 
7b33394
faece1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f0122d
7608d93
 
 
 
 
 
 
 
 
 
 
 
 
faece1b
 
 
 
 
a63ba9d
0f0122d
cb9d3ce
a63ba9d
7608d93
 
faece1b
 
7608d93
0f0122d
a63ba9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b33394
 
 
 
 
 
a63ba9d
 
 
 
 
 
 
7b33394
a63ba9d
7b33394
 
a63ba9d
 
 
 
 
 
 
 
 
7b33394
a63ba9d
 
 
c3c8276
a63ba9d
4289ed3
 
 
 
07505ba
 
 
 
 
 
 
 
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
# server.py
import json
import random
import traceback
from typing import Optional
import uuid
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
import pycountry
from pydantic import BaseModel
from chat_utils import chat
from config import SanatanConfig
from db import SanatanDatabase
from metadata import MetadataWhereClause
from modules.audio.model import AudioRequest
from modules.audio.service import svc_get_audio_urls
from modules.quiz.answer_validator import validate_answer
from modules.quiz.models import Question
from modules.quiz.quiz_helper import generate_question
import logging

from modules.video.model import VideoRequest
from modules.video.service import svc_get_video_urls

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

router = APIRouter()

# In-memory mapping from session_id -> thread_id
# For production, you may want Redis or a DB for persistence
thread_map = {}


class Message(BaseModel):
    language: str
    text: str
    session_id: str | None = None  # Optional session ID from client


class QuizGeneratePayload(BaseModel):
    language: Optional[str] = "English"
    scripture: Optional[str] = None
    complexity: Optional[str] = None
    mode: Optional[str] = None
    session_id: Optional[str] = None  # Optional session ID from client


class QuizEvalPayload(BaseModel):
    language: Optional[str] = "English"
    q: Question
    answer: str
    session_id: Optional[str] = None  # Optional session ID from client


LANG_NATIVE_NAMES = {
    "en": "English",
    "fr": "Français",
    "es": "Español",
    "hi": "हिन्दी",
    "bn": "বাংলা",
    "te": "తెలుగు",
    "mr": "मराठी",
    "ta": "தமிழ்",
    "ur": "اردو",
    "gu": "ગુજરાતી",
    "kn": "ಕನ್ನಡ",
    "ml": "മലയാളം",
    "pa": "ਪੰਜਾਬੀ",
    "as": "অসমীয়া",
    "mai": "मैथिली",
    "sd": "سنڌي",
    "sat": "ᱥᱟᱱᱛᱟᱲᱤ",
}


@router.get("/languages")
async def handle_fetch_languages():
    supported_lang_codes = [
        "en",
        "fr",
        "es",
        "hi",
        "bn",
        "te",
        "mr",
        "ta",
        "ur",
        "gu",
        "kn",
        "ml",
        "pa",
        "as",
        "mai",
        "sd",
        "sat",
    ]

    languages = []
    for code in supported_lang_codes:
        lang = pycountry.languages.get(alpha_2=code) or pycountry.languages.get(
            alpha_3=code
        )
        if lang is None:
            continue  # skip unknown codes
        english_name = lang.name
        native_name = LANG_NATIVE_NAMES.get(code, english_name)
        languages.append(
            {
                "code": code,
                "name": english_name,
                "native_name": native_name,
            }
        )

    languages.sort(key=lambda x: x["name"])
    return languages


@router.post("/greet")
async def handle_greet(msg: Message):
    markdown = "Namaskaram 🙏 I am **bhashyam.ai** and I can help you explore the following scriptures:\n---\n"
    for scripture in SanatanConfig().scriptures:
        num_units = SanatanDatabase().count(
            collection_name=scripture["collection_name"]
        )
        markdown += f"- {scripture['title']} : `{num_units}` {scripture["unit"]}s\n"

    session_id = msg.session_id
    if not session_id:
        session_id = str(uuid.uuid4())
    return {"reply": markdown, "session_id": session_id}


@router.post("/chat")
async def handle_chat(msg: Message, request: Request):
    try:
        # Use existing session_id if provided, else generate new
        session_id = msg.session_id
        if not session_id:
            session_id = str(uuid.uuid4())
        print(session_id, ": user sent message : ", msg.text)
        # Get or create a persistent thread_id for this session
        if session_id not in thread_map:
            thread_map[session_id] = str(uuid.uuid4())
        thread_id = thread_map[session_id]

        # Call your graph/chat function
        reply_text = chat(
            debug_mode=False,
            message=msg.text,
            history=None,
            thread_id=thread_id,
            preferred_language=msg.language or "English",
        )

        # Return both reply and session_id to the client
        return {"reply": reply_text, "session_id": session_id}

    except Exception as e:
        traceback.print_exc()
        return JSONResponse(status_code=500, content={"reply": f"Error: {e}"})


@router.post("/quiz/generate")
async def handle_quiz_generate(payload: QuizGeneratePayload, request: Request):
    q = generate_question(
        collection=payload.scripture
        or random.choice(
            [
                s["collection_name"]
                for s in SanatanConfig.scriptures
                if s["collection_name"] != "yt_metadata"
            ]
        ),
        complexity=payload.complexity
        or random.choice(["beginner", "intermediate", "advanced"]),
        mode=payload.mode or random.choice(["mcq", "open"]),
        preferred_lamguage=payload.language or "English",
    )
    print(q.model_dump_json(indent=1))
    return q.model_dump()


@router.post("/quiz/eval")
async def handle_quiz_eval(payload: QuizEvalPayload, request: Request):
    result = validate_answer(
        payload.q, payload.answer, preferred_language=payload.language or "English"
    )
    print(result.model_dump_json(indent=1))
    return result


@router.get("/scriptures")
async def handle_get_scriptures():
    return_values = {}
    for scripture in SanatanConfig().scriptures:
        if scripture["collection_name"] != "yt_metadata":
            return_values[scripture["collection_name"]] = scripture["title"]
    return return_values


class ScriptureRequest(BaseModel):
    scripture_name: str
    unit_index: int


@router.post("/scripture")
async def get_scripture(req: ScriptureRequest):
    """
    Return a scripture unit (page or verse, based on config),
    including all metadata fields separately.
    used for page view to fetch by global index.
    """
    logger.info("get_scripture: received request to fetch scripture: %s", req)

    # find config entry for the scripture
    config = next(
        (s for s in SanatanConfig().scriptures if s["name"] == req.scripture_name), None
    )
    if not config:
        return {"error": f"Scripture '{req.scripture_name}' not found"}

    # fetch the raw document from DB
    raw_doc = SanatanDatabase().fetch_document_by_index(
        collection_name=config["collection_name"],
        index=req.unit_index,
        # unit_name=config.get("unit_field", config.get("unit")),
    )

    if not raw_doc or isinstance(raw_doc, str) or "error" in raw_doc:
        return {"error": f"No data available for unit {req.unit_index}"}

    # canonicalize it
    canonical_doc = SanatanConfig().canonicalize_document(
        scripture_name=req.scripture_name,
        document_text=raw_doc.get("document", ""),
        metadata_doc=raw_doc,
    )

    # add unit index & total units (so Flutter can paginate)
    canonical_doc["total"] = SanatanDatabase().count(config["collection_name"])

    print("canonical_doc = ", canonical_doc)
    return canonical_doc


@router.get("/scripture_configs")
async def get_scripture_configs():
    scriptures = []
    for s in SanatanConfig().scriptures:
        num_units = SanatanDatabase().count(collection_name=s["collection_name"])

        # Deep copy metadata_fields so we don’t mutate the original config
        metadata_fields = []
        for f in s.get("metadata_fields", []):
            f_copy = dict(f)
            lov = f_copy.get("lov")
            if callable(lov):  # evaluate the function
                try:
                    f_copy["lov"] = lov()
                except Exception as e:
                    f_copy["lov"] = []
            metadata_fields.append(f_copy)

        scriptures.append(
            {
                "name": s["name"],  # e.g. "bhagavad_gita"
                "title": s["title"],  # e.g. "Bhagavad Gita"
                "unit": s["unit"],  # e.g. "verse" or "page"
                "unit_field": s.get("unit_field", s.get("unit")),
                "total": num_units,
                "enabled": "field_mapping" in s,
                "source": s.get("source", ""),
                "credits": s.get("credits", f"{s.get('source','')}"),
                "metadata_fields": metadata_fields,
            }
        )

    return {"scriptures": sorted(scriptures, key=lambda s: s["title"])}


@router.post("/scripture/{scripture_name}/search")
async def search_scripture(
    scripture_name: str,
    filter_obj: Optional[MetadataWhereClause] = None,
):
    """
    Search scripture collection with optional filters.
    - `scripture_name`: Name of the collection
    - `filter_obj`: MetadataWhereClause (filters, groups, operator)
    - `n_results`: number of random results to return
    """
    try:
        db = SanatanDatabase()
        config = next(
            (s for s in SanatanConfig().scriptures if s["name"] == scripture_name), None
        )

        results = db.fetch_first_match(
            collection_name=config["collection_name"],
            metadata_where_clause=filter_obj,
        )

        print("results = ", results)
        # Flatten + canonicalize results
        formatted_results = []
        for i in range(len(results["metadatas"])):
            id = results["ids"][i]
            metadata_doc = results["metadatas"][i]
            metadata_doc["id"] = id
            print("metadata_doc = ",metadata_doc)
            document_text = (
                results["documents"][i] if results.get("documents") else None
            )

            canonical_doc = SanatanConfig().canonicalize_document(
                scripture_name, document_text, metadata_doc
            )
            formatted_results.append(canonical_doc)

        print("formatted_results = ", formatted_results)
        return {"results": formatted_results}

    except Exception as e:
        logger.error("Error while searching %s", e, exc_info=True)
        return {"error": str(e)}

@router.post("/audio")
async def generate_audio_urls(req: AudioRequest):
    logger.info("generate_audio_urls: %s", req)
    audio_urls = await svc_get_audio_urls(req)
    return audio_urls

@router.post("/video")
async def generate_audio_urls(req: VideoRequest):
    logger.info("generate_audio_urls: %s", req)
    video_urls = await svc_get_video_urls(req)
    return video_urls