Upload folder using huggingface_hub
Browse files- app/prompts.py +11 -5
- app/state.py +83 -74
app/prompts.py
CHANGED
|
@@ -79,11 +79,17 @@ TASK_INSTRUCTIONS: Dict[str, str] = {
|
|
| 79 |
"CRITICAL: The SURAH INFORMATION block is the ONLY authoritative source."
|
| 80 |
),
|
| 81 |
"general": (
|
| 82 |
-
"The user
|
| 83 |
-
"1.
|
| 84 |
-
"2.
|
| 85 |
-
"
|
| 86 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
),
|
| 88 |
}
|
| 89 |
|
|
|
|
| 79 |
"CRITICAL: The SURAH INFORMATION block is the ONLY authoritative source."
|
| 80 |
),
|
| 81 |
"general": (
|
| 82 |
+
"The user may be asking a general Islamic question OR pasting text to look up. Steps:\n"
|
| 83 |
+
"1. Check ALL retrieved results from EVERY source (Quran AND Hadith).\n"
|
| 84 |
+
"2. For EACH result that matches the user's text or question, state WHERE it appears:\n"
|
| 85 |
+
" β’ For Quran: Surah name (Arabic & English), Surah number, and Ayah number from the [REF] line.\n"
|
| 86 |
+
" β’ For Hadith: collection name, book/chapter, hadith number, and grade.\n"
|
| 87 |
+
"3. If the same text appears in MULTIPLE places, list ALL of them explicitly.\n"
|
| 88 |
+
" Example: βΩΨ°Ψ§ Ψ§ΩΩΨ΅ Ψ°ΩΩΩΨ± ΩΩ Ψ³ΩΨ±Ψ© Ψ§ΩΨ¨ΩΨ±Ψ© (Ψ’ΩΨ© Ω’Ω₯Ω₯) ΩΨ’Ω ΨΉΩ
Ψ±Ψ§Ω (Ψ’ΩΨ© Ω’)β\n"
|
| 89 |
+
"4. Quote the Arabic text and English translation EXACTLY from the results.\n"
|
| 90 |
+
"5. Provide brief context or explanation of the text.\n"
|
| 91 |
+
"6. Answer the user's specific question if one was asked.\n"
|
| 92 |
+
"CRITICAL: Do NOT give a generic answer. Always mention the exact sources from the results."
|
| 93 |
),
|
| 94 |
}
|
| 95 |
|
app/state.py
CHANGED
|
@@ -41,84 +41,93 @@ logger = logging.getLogger("qmodel.state")
|
|
| 41 |
# SURAH INFO PROGRAMMATIC FALLBACK
|
| 42 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
def _results_fallback(results: list, lang: str) -> str:
|
| 44 |
-
"""Generate a direct answer from search results when LLM returns empty.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if not results:
|
| 46 |
return not_found_answer(lang)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
f"
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
if tafsir:
|
| 71 |
-
lines.append(f"\n{tafsir}")
|
| 72 |
-
lines.append("\nΩΨ§ΩΩΩ Ψ£ΨΉΩΩ
.")
|
| 73 |
-
return "\n".join(lines)
|
| 74 |
-
else:
|
| 75 |
-
lines = [
|
| 76 |
-
f"This noble verse is from Surah {surah_en} ({surah_ar}), verse {verse_num}.",
|
| 77 |
-
"",
|
| 78 |
-
f"βββββββββββββββββββββββββββββββββββββββββββββββ",
|
| 79 |
-
f"β β {ar_text} β",
|
| 80 |
-
f"β π Translation: {en_text}",
|
| 81 |
-
f"β π Source: Surah {surah_en} ({surah_ar}) | Surah Number: {surah_num} | Verse: {verse_num}",
|
| 82 |
-
f"βββββββββββββββββββββββββββββββββββββββββββββββ",
|
| 83 |
-
]
|
| 84 |
-
if tafsir:
|
| 85 |
-
lines.append(f"\n{tafsir}")
|
| 86 |
-
lines.append("\nAnd Allah knows best.")
|
| 87 |
-
return "\n".join(lines)
|
| 88 |
-
|
| 89 |
-
elif item_type == "hadith":
|
| 90 |
-
ar_text = top.get("arabic", "")
|
| 91 |
-
en_text = top.get("english", "")
|
| 92 |
-
source = top.get("source") or top.get("reference", "")
|
| 93 |
-
grade = top.get("grade", "")
|
| 94 |
-
grade_str = f" [{grade}]" if grade else ""
|
| 95 |
-
|
| 96 |
-
if lang == "arabic":
|
| 97 |
-
lines = [
|
| 98 |
-
f"Ψ§ΩΨΨ―ΩΨ« Ψ§ΩΨ΄Ψ±ΩΩ{grade_str}:",
|
| 99 |
-
"",
|
| 100 |
-
f"βββββββββββββββββββββββββββββββββββββββββββββββ",
|
| 101 |
-
f"β β {ar_text} β",
|
| 102 |
-
f"β π Translation: {en_text}",
|
| 103 |
-
f"β π Source: {source}",
|
| 104 |
-
f"βββββββββββββββββββββββββββββββββββββββββββββββ",
|
| 105 |
-
"\nΩΨ§ΩΩΩ Ψ£ΨΉΩΩ
.",
|
| 106 |
-
]
|
| 107 |
-
return "\n".join(lines)
|
| 108 |
else:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
f"
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
|
| 124 |
def _surah_info_fallback(info: dict, lang: str) -> str:
|
|
|
|
| 41 |
# SURAH INFO PROGRAMMATIC FALLBACK
|
| 42 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
def _results_fallback(results: list, lang: str) -> str:
|
| 44 |
+
"""Generate a direct answer from search results when LLM returns empty.
|
| 45 |
+
|
| 46 |
+
Presents ALL high-scoring results so the user sees every place
|
| 47 |
+
a text is mentioned (e.g. same verse in multiple surahs).
|
| 48 |
+
"""
|
| 49 |
if not results:
|
| 50 |
return not_found_answer(lang)
|
| 51 |
|
| 52 |
+
# Include all results whose score is at least 50% of the top score,
|
| 53 |
+
# or score >= 2.0 (strong match), whichever captures more.
|
| 54 |
+
top_score = results[0].get("_score", 0.0)
|
| 55 |
+
threshold = max(top_score * 0.5, 2.0) if top_score >= 2.0 else 0.0
|
| 56 |
+
relevant = [r for r in results if r.get("_score", 0.0) >= threshold] or results[:1]
|
| 57 |
+
|
| 58 |
+
quran_hits = [r for r in relevant if r.get("type") == "quran"]
|
| 59 |
+
hadith_hits = [r for r in relevant if r.get("type") == "hadith"]
|
| 60 |
+
|
| 61 |
+
lines: list[str] = []
|
| 62 |
+
|
| 63 |
+
# --- Quran results ---
|
| 64 |
+
if quran_hits:
|
| 65 |
+
if len(quran_hits) == 1:
|
| 66 |
+
r = quran_hits[0]
|
| 67 |
+
surah_ar = r.get("surah_name_ar", "")
|
| 68 |
+
surah_en = r.get("surah_name_en", "")
|
| 69 |
+
verse_num = r.get("verse_number", "")
|
| 70 |
+
if lang == "arabic":
|
| 71 |
+
lines.append(f"ΩΨ°Ω Ψ§ΩΨ’ΩΨ© Ψ§ΩΩΨ±ΩΩ
Ψ© Ω
Ω Ψ³ΩΨ±Ψ© {surah_ar} ({surah_en})Ψ Ψ§ΩΨ’ΩΨ© {verse_num}.")
|
| 72 |
+
else:
|
| 73 |
+
lines.append(f"This noble verse is from Surah {surah_en} ({surah_ar}), verse {verse_num}.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
else:
|
| 75 |
+
if lang == "arabic":
|
| 76 |
+
refs = [
|
| 77 |
+
f"Ψ³ΩΨ±Ψ© {r.get('surah_name_ar', '')} ({r.get('surah_name_en', '')})Ψ Ψ§ΩΨ’ΩΨ© {r.get('verse_number', '')}"
|
| 78 |
+
for r in quran_hits
|
| 79 |
+
]
|
| 80 |
+
lines.append(f"ΩΨ°Ψ§ Ψ§ΩΩΨ΅ Ψ§ΩΩΨ±ΩΩ
Ψ°ΩΩΩΨ± ΩΩ {len(quran_hits)} Ω
ΩΨ§ΨΆΨΉ:")
|
| 81 |
+
for ref in refs:
|
| 82 |
+
lines.append(f" β’ {ref}")
|
| 83 |
+
else:
|
| 84 |
+
refs = [
|
| 85 |
+
f"Surah {r.get('surah_name_en', '')} ({r.get('surah_name_ar', '')}), verse {r.get('verse_number', '')}"
|
| 86 |
+
for r in quran_hits
|
| 87 |
+
]
|
| 88 |
+
lines.append(f"This text appears in {len(quran_hits)} places:")
|
| 89 |
+
for ref in refs:
|
| 90 |
+
lines.append(f" β’ {ref}")
|
| 91 |
+
|
| 92 |
+
for r in quran_hits:
|
| 93 |
+
ar_text = r.get("arabic", "")
|
| 94 |
+
en_text = r.get("english", "")
|
| 95 |
+
surah_ar = r.get("surah_name_ar", "")
|
| 96 |
+
surah_en = r.get("surah_name_en", "")
|
| 97 |
+
surah_num = r.get("surah_number", "")
|
| 98 |
+
verse_num = r.get("verse_number", "")
|
| 99 |
+
lines.append("")
|
| 100 |
+
lines.append("βββββββββββββββββββββββββββββββββββββββββββββββ")
|
| 101 |
+
lines.append(f"β β {ar_text} β")
|
| 102 |
+
lines.append(f"β π Translation: {en_text}")
|
| 103 |
+
lines.append(f"β π Source: Ψ³ΩΨ±Ψ© {surah_ar} ({surah_en}) | Ψ±ΩΩ
Ψ§ΩΨ³ΩΨ±Ψ©: {surah_num} | Ψ§ΩΨ’ΩΨ©: {verse_num}")
|
| 104 |
+
lines.append("βββββββββββββββββββββββββββββββββββββββββββββββ")
|
| 105 |
+
|
| 106 |
+
# --- Hadith results ---
|
| 107 |
+
if hadith_hits:
|
| 108 |
+
for r in hadith_hits:
|
| 109 |
+
ar_text = r.get("arabic", "")
|
| 110 |
+
en_text = r.get("english", "")
|
| 111 |
+
source = r.get("source") or r.get("reference", "")
|
| 112 |
+
grade = r.get("grade", "")
|
| 113 |
+
grade_str = f" [{grade}]" if grade else ""
|
| 114 |
+
if lang == "arabic":
|
| 115 |
+
lines.append(f"\nΨ§ΩΨΨ―ΩΨ« Ψ§ΩΨ΄Ψ±ΩΩ{grade_str}:")
|
| 116 |
+
else:
|
| 117 |
+
lines.append(f"\nHadith{grade_str}:")
|
| 118 |
+
lines.append("")
|
| 119 |
+
lines.append("βββββββββββββββββββββββββββββββββββββββββββββββ")
|
| 120 |
+
lines.append(f"β β {ar_text} β")
|
| 121 |
+
lines.append(f"β π Translation: {en_text}")
|
| 122 |
+
lines.append(f"β π Source: {source}")
|
| 123 |
+
lines.append("βββββββββββββββββββββββββββββββββββββββββββββββ")
|
| 124 |
+
|
| 125 |
+
if lang == "arabic":
|
| 126 |
+
lines.append("\nΩΨ§ΩΩΩ Ψ£ΨΉΩΩ
.")
|
| 127 |
+
else:
|
| 128 |
+
lines.append("\nAnd Allah knows best.")
|
| 129 |
+
|
| 130 |
+
return "\n".join(lines)
|
| 131 |
|
| 132 |
|
| 133 |
def _surah_info_fallback(info: dict, lang: str) -> str:
|