Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
| from modules.llm.summarizer.helpers.db_helper import get_scripture_from_db | |
| from modules.llm.summarizer.helpers.llm_helper import summarize_scripture_verse | |
| from modules.llm.summarizer.models import ScriptureRequest, ScriptureVerseSummary | |
| from google.cloud import firestore | |
| import os | |
| import json | |
| from google.oauth2 import service_account | |
| # Load Firebase credentials from environment variable | |
| service_account_info = os.getenv("FIREBASE_SERVICE_ACCOUNT_JSON") | |
| if not service_account_info: | |
| raise ValueError("FIREBASE_SERVICE_ACCOUNT_JSON not found in environment variables.") | |
| # Parse the JSON (it’s stored as a string in env) | |
| credentials_dict = json.loads(service_account_info) | |
| credentials = service_account.Credentials.from_service_account_info(credentials_dict) | |
| # Initialize Firestore client with these credentials | |
| db = firestore.AsyncClient(credentials=credentials, project=credentials.project_id) | |
| # or db = firestore.Client(credentials=credentials, project=credentials.project_id) | |
| async def svc_summarize_scripture_verse(req: ScriptureRequest) -> ScriptureVerseSummary: | |
| """ | |
| Summarizes a scripture verse, with Firestore caching per verse and language. | |
| Firestore structure: | |
| /scripture_summaries/{scripture_name_global_index}/meanings/{target_language} | |
| """ | |
| # Step 1: Get scripture data | |
| scripture_data = await get_scripture_from_db(req) | |
| scripture_name = scripture_data.get("scripture_name", "UnknownScripture") | |
| global_index = scripture_data.get("_global_index", -1) | |
| target_language = req.target_language.lower() | |
| # Compose the document path | |
| doc_id = f"{scripture_name}_{global_index}".replace(" ", "_") | |
| base_ref = db.collection("scripture_summaries").document(doc_id) | |
| meaning_ref = base_ref.collection("meanings").document(target_language) | |
| # Step 2: Check for cached version | |
| cached_doc = await meaning_ref.get() | |
| if cached_doc.exists: | |
| print(f"✅ Using cached summary for {scripture_name} {global_index} ({target_language})") | |
| return ScriptureVerseSummary(**cached_doc.to_dict()) | |
| print(f"🧠 Generating new summary for {scripture_name} {global_index} ({target_language})") | |
| # Step 3: Generate new summary with LLM | |
| summary = summarize_scripture_verse( | |
| scripture_data.get("text","--no lyrics available--"), | |
| scripture_data.get("translation","--no translation available--"), | |
| scripture_data.get("word_by_word_native","--no word-by-word meaning available--"), | |
| target_language, | |
| ) | |
| summary_data = summary.model_dump() | |
| # Step 4: Ensure parent document exists | |
| await base_ref.set( | |
| { | |
| "scripture_name": scripture_name, | |
| "global_index": global_index, | |
| }, | |
| merge=True, | |
| ) | |
| # Step 5: Store meaning under nested language doc | |
| await meaning_ref.set(summary_data) | |
| print(f"✅ Stored summary for {scripture_name} {global_index} in Firestore") | |
| return summary | |