suhailshafi007 commited on
Commit
f79cb8d
·
1 Parent(s): ad3ee4f

fix(daily-hadith): enforce complete narration translation with zero cuts, preserving narrator, tone, and full length

Browse files
app/hadith.py CHANGED
@@ -5324,6 +5324,8 @@ async def translate_daily_content(req: DailyTranslationRequest, request: Request
5324
 
5325
  # 4. Cache MISS: Must resolve canonical Arabic and generate via LLM
5326
  canonical_trans = {}
 
 
5327
  try:
5328
  if c_type == "ayah":
5329
  if not canonical_arabic or not content_version:
@@ -5338,6 +5340,8 @@ async def translate_daily_content(req: DailyTranslationRequest, request: Request
5338
  canonical_arabic = auth_hadith.get("text_arabic") or ""
5339
  content_version = auth_hadith.get("content_version") or ""
5340
  canonical_trans = auth_hadith.get("canonical_translations") or {}
 
 
5341
  except Exception as db_err:
5342
  logger.warning(f"Could not resolve hadith {collection} #{hadith_id} from DB: {db_err}")
5343
  if req.text and req.text.strip():
@@ -5439,6 +5443,8 @@ async def translate_daily_content(req: DailyTranslationRequest, request: Request
5439
  target_lang_name=target_lang,
5440
  target_lang_code=target_code,
5441
  reference=req.reference or f"Prophetic Hadith ({collection})",
 
 
5442
  )
5443
 
5444
  translated = await generate_chat_completion(
 
5324
 
5325
  # 4. Cache MISS: Must resolve canonical Arabic and generate via LLM
5326
  canonical_trans = {}
5327
+ english_reference = None
5328
+ urdu_reference = None
5329
  try:
5330
  if c_type == "ayah":
5331
  if not canonical_arabic or not content_version:
 
5340
  canonical_arabic = auth_hadith.get("text_arabic") or ""
5341
  content_version = auth_hadith.get("content_version") or ""
5342
  canonical_trans = auth_hadith.get("canonical_translations") or {}
5343
+ english_reference = auth_hadith.get("text_english")
5344
+ urdu_reference = auth_hadith.get("text_urdu")
5345
  except Exception as db_err:
5346
  logger.warning(f"Could not resolve hadith {collection} #{hadith_id} from DB: {db_err}")
5347
  if req.text and req.text.strip():
 
5443
  target_lang_name=target_lang,
5444
  target_lang_code=target_code,
5445
  reference=req.reference or f"Prophetic Hadith ({collection})",
5446
+ english_reference=english_reference or (req.text if req.text and not re.search(r'[؀-ۿ]', req.text) else None),
5447
+ urdu_reference=urdu_reference,
5448
  )
5449
 
5450
  translated = await generate_chat_completion(
app/services/daily_translation_pipeline.py CHANGED
@@ -211,13 +211,15 @@ def build_hadith_translation_prompt(
211
  target_lang_code: str = "en",
212
  reference: str = "",
213
  source_text: Optional[str] = None,
 
 
214
  ) -> List[Dict[str, str]]:
215
- """Builds a high-fidelity prompt for translating a prophetic Hadith."""
216
  roman_urdu_rule = ""
217
- if target_lang_code == "hinglish" or target_lang_name.lower() == "roman urdu":
218
  roman_urdu_rule = (
219
- "9. ROMAN URDU SCRIPT ENFORCEMENT: Write the translation STRICTLY in the English/Latin alphabet "
220
- "using standard, phonetically accurate Roman Urdu (e.g., 'Rasool Allah (SAW) ne farmaya...', 'Aamaal ka daromadar...'). "
221
  "NEVER use Hindi or Devanagari script (देवनागरी) under any circumstances.\n"
222
  )
223
 
@@ -228,13 +230,11 @@ def build_hadith_translation_prompt(
228
  f"of the Prophet Muhammad (peace be upon him) into {target_lang_name}.\n\n"
229
  f"CRITICAL THEOLOGICAL RULES:\n"
230
  f"1. MEANING & INTENT: Preserve the authentic meaning and intent of the prophetic narration with 100% fidelity.\n"
231
- f"2. TONE & REGISTER: Maintain the reverence, formality, dignified narrative style, and expressive character of the prophetic tradition.\n"
232
- f"3. RHETORICAL INTEGRITY: Preserve meaningful emphasis, questions, commands, warnings, supplications, and reported speech.\n"
233
- f"4. PROPHETIC SPEECH VS NARRATOR: Translate the core wisdom, teaching, and statement (matn). Do NOT invent or alter narrator chains.\n"
234
- f"5. NO SANAD IN BODY: Do NOT prepend narrator chains (e.g., 'Narrated Abu Huraira:'). Translate ONLY the substantive statement and wisdom.\n"
235
- f"6. ZERO COMMENTARY OR GRADING: Do not add personal explanations, scholarly commentary, jurisprudential rulings, or grading judgments into the translated text.\n"
236
- f"7. NO ADDITIONS OR OMISSIONS: Do not invent words, context, or claims. Do not omit any part of the prophetic statement.\n"
237
- f"8. OUTPUT FORMAT: Output ONLY the translated Hadith text. Do NOT include quotation marks, markdown wrappers, greetings, or prefixes.\n"
238
  f"{roman_urdu_rule}"
239
  )
240
 
@@ -242,12 +242,20 @@ def build_hadith_translation_prompt(
242
  if not primary_text or not re.search(r'[\u0600-\u06FF]', primary_text):
243
  if source_text and re.search(r'[\u0600-\u06FF]', source_text):
244
  primary_text = source_text.strip()
245
- user_prompt = f"Reference: {reference}\nSacred Prophetic Hadith (Canonical Arabic):\n{primary_text}\n\nTranslate strictly into {target_lang_name}:"
246
-
247
  else:
248
  raise SourceContentMissingError("Hadith canonical Arabic text is required for translation generation.")
249
- else:
250
- user_prompt = f"Reference: {reference}\nSacred Prophetic Hadith (Canonical Arabic):\n{primary_text}\n\nTranslate strictly into {target_lang_name}:"
 
 
 
 
 
 
 
 
 
 
251
 
252
  return [
253
  {"role": "system", "content": system_prompt},
@@ -664,6 +672,8 @@ async def execute_translation_job(
664
  target_lang_name=lang_name,
665
  target_lang_code=lang_code,
666
  reference=content_data["reference"],
 
 
667
  )
668
 
669
  # Retry loop: up to 3 attempts with exponential backoff
@@ -1494,7 +1504,19 @@ async def get_active_or_rotated_daily_ayah(
1494
  "text_arabic": parsed.get("arabic", ""),
1495
  "text_urdu": parsed.get("urdu", ""),
1496
  "text_english": parsed.get("english", ""),
1497
- "text_hinglish": parsed.get("translations", {}).get("hinglish", ""),
 
 
 
 
 
 
 
 
 
 
 
 
1498
  "reference": parsed.get("reference", ""),
1499
  "translations": parsed.get("translations", {}),
1500
  "content_version": active_ayah_ver,
@@ -1525,7 +1547,17 @@ async def get_active_or_rotated_daily_ayah(
1525
  "text_arabic": active_doc.get("text_arabic", ""),
1526
  "text_urdu": active_doc.get("text_urdu", ""),
1527
  "text_english": active_doc.get("text_english", ""),
1528
- "text_hinglish": active_doc.get("translations", {}).get("hinglish", ""),
 
 
 
 
 
 
 
 
 
 
1529
  "reference": active_doc.get("reference", ""),
1530
  "translations": active_doc.get("translations", {}),
1531
  "content_version": c_ver,
 
211
  target_lang_code: str = "en",
212
  reference: str = "",
213
  source_text: Optional[str] = None,
214
+ english_reference: Optional[str] = None,
215
+ urdu_reference: Optional[str] = None,
216
  ) -> List[Dict[str, str]]:
217
+ """Builds a high-fidelity prompt for translating a prophetic Hadith with zero cuts, preserving full narration."""
218
  roman_urdu_rule = ""
219
+ if target_lang_code in ("hinglish", "roman_urdu") or target_lang_name.lower() in ("roman urdu", "hinglish"):
220
  roman_urdu_rule = (
221
+ "7. ROMAN URDU SCRIPT ENFORCEMENT: Write the complete narration STRICTLY in the English/Latin alphabet "
222
+ "using natural, phonetically accurate Roman Urdu (e.g., 'Abdullah se riwayat hai ke Nabi (SAW) ye dua karte the: \"...\"'). "
223
  "NEVER use Hindi or Devanagari script (देवनागरी) under any circumstances.\n"
224
  )
225
 
 
230
  f"of the Prophet Muhammad (peace be upon him) into {target_lang_name}.\n\n"
231
  f"CRITICAL THEOLOGICAL RULES:\n"
232
  f"1. MEANING & INTENT: Preserve the authentic meaning and intent of the prophetic narration with 100% fidelity.\n"
233
+ f"2. NO CUTS OR TRUNCATION: Do NOT cut, shorten, truncate, or omit any part of the narration. The tone, expression, specific words, spiritual intent, and length must remain fully preserved and matching the authoritative narration.\n"
234
+ f"3. NARRATIVE FRAMING: Translate the complete narration faithfully, including the reporting Companion / narrator context (e.g. 'Abdullah narrated that the Prophet (ﷺ) used to supplicate: ...'), the prophetic speech, the context, and any supplications or teachings.\n"
235
+ f"4. TONE & REGISTER: Maintain the reverence, formality, dignified narrative style, and expressive character of the prophetic tradition.\n"
236
+ f"5. ZERO COMMENTARY OR GRADING: Do not add personal explanations, scholarly commentary, jurisprudential rulings, or grading judgments into the translated text.\n"
237
+ f"6. OUTPUT FORMAT: Output ONLY the translated Hadith text. Do NOT include quotation marks around the entire output, markdown wrappers, greetings, or prefixes like 'Translation:'.\n"
 
 
238
  f"{roman_urdu_rule}"
239
  )
240
 
 
242
  if not primary_text or not re.search(r'[\u0600-\u06FF]', primary_text):
243
  if source_text and re.search(r'[\u0600-\u06FF]', source_text):
244
  primary_text = source_text.strip()
 
 
245
  else:
246
  raise SourceContentMissingError("Hadith canonical Arabic text is required for translation generation.")
247
+
248
+ ref_block = ""
249
+ auth_ref = (english_reference or source_text or urdu_reference or "").strip()
250
+ if auth_ref:
251
+ ref_block = f"\n\nAuthoritative Reference Translation (Full Scope):\n{auth_ref}"
252
+
253
+ user_prompt = (
254
+ f"Reference: {reference}\n"
255
+ f"Sacred Prophetic Hadith (Canonical Arabic):\n{primary_text}"
256
+ f"{ref_block}\n\n"
257
+ f"Translate the complete Hadith narration into {target_lang_name} with exact words, no cuts, preserving the reporting narrator, full tone, expression, words, intent, and length:"
258
+ )
259
 
260
  return [
261
  {"role": "system", "content": system_prompt},
 
672
  target_lang_name=lang_name,
673
  target_lang_code=lang_code,
674
  reference=content_data["reference"],
675
+ english_reference=content_data.get("text_english"),
676
+ urdu_reference=content_data.get("text_urdu"),
677
  )
678
 
679
  # Retry loop: up to 3 attempts with exponential backoff
 
1504
  "text_arabic": parsed.get("arabic", ""),
1505
  "text_urdu": parsed.get("urdu", ""),
1506
  "text_english": parsed.get("english", ""),
1507
+ "text_hinglish": (
1508
+
1509
+ parsed.get("translations", {}).get("roman_urdu")
1510
+
1511
+ or parsed.get("translations", {}).get("hinglish")
1512
+
1513
+ or parsed.get("text_hinglish")
1514
+
1515
+ or meta.get("text_hinglish")
1516
+
1517
+ or ""
1518
+
1519
+ ),
1520
  "reference": parsed.get("reference", ""),
1521
  "translations": parsed.get("translations", {}),
1522
  "content_version": active_ayah_ver,
 
1547
  "text_arabic": active_doc.get("text_arabic", ""),
1548
  "text_urdu": active_doc.get("text_urdu", ""),
1549
  "text_english": active_doc.get("text_english", ""),
1550
+ "text_hinglish": (
1551
+
1552
+ active_doc.get("translations", {}).get("roman_urdu")
1553
+
1554
+ or active_doc.get("translations", {}).get("hinglish")
1555
+
1556
+ or active_doc.get("text_hinglish")
1557
+
1558
+ or ""
1559
+
1560
+ ),
1561
  "reference": active_doc.get("reference", ""),
1562
  "translations": active_doc.get("translations", {}),
1563
  "content_version": c_ver,