Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
| import uuid | |
| from config import SanatanConfig | |
| from db import SanatanDatabase | |
| from modules.llm.summarizer.models import ScriptureRequest | |
| import logging | |
| logging.basicConfig() | |
| logger = logging.getLogger(__name__) | |
| logger.setLevel(logging.INFO) | |
| async def get_scripture_from_db(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. | |
| """ | |
| # ensure we have a valid request id (deviceId) | |
| request_id = req.request_id or f"auto-{uuid.uuid4()}" | |
| logger.info( | |
| f"get_scripture: received requestId={request_id}, scripture={req.scripture_name}, unit_index={req.unit_index}" | |
| ) | |
| # 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", | |
| "requestId": request_id, | |
| } | |
| # fetch the raw document from DB | |
| raw_doc = SanatanDatabase().fetch_document_by_index( | |
| collection_name=config["collection_name"], | |
| index=req.unit_index, | |
| ) | |
| 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}", | |
| "requestId": request_id, | |
| } | |
| # 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"]) | |
| canonical_doc["requestId"] = request_id | |
| return canonical_doc |