Spaces:
Sleeping
Sleeping
Commit
·
07f735f
1
Parent(s):
a7ef914
Force clean EN output with keyword fallback
Browse files- app/rag_system.py +51 -13
app/rag_system.py
CHANGED
|
@@ -64,6 +64,41 @@ def _looks_azerbaijani(s: str) -> bool:
|
|
| 64 |
non_ascii_ratio = sum(ord(c) > 127 for c in s) / max(1, len(s))
|
| 65 |
return has_az or non_ascii_ratio > 0.15
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
class SimpleRAG:
|
| 68 |
def __init__(
|
| 69 |
self,
|
|
@@ -162,22 +197,21 @@ class SimpleRAG:
|
|
| 162 |
if not contexts:
|
| 163 |
return "No relevant context found. Please upload a PDF or ask a more specific question."
|
| 164 |
|
| 165 |
-
# 1) Clean top contexts
|
| 166 |
cleaned_contexts = [_clean_for_summary(c) for c in contexts[:5]]
|
| 167 |
cleaned_contexts = [c for c in cleaned_contexts if len(c) > 40]
|
| 168 |
if not cleaned_contexts:
|
| 169 |
return "The document appears largely tabular/numeric; couldn't extract readable sentences."
|
| 170 |
|
| 171 |
-
# 2)
|
| 172 |
if OUTPUT_LANG == "en":
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
pass
|
| 177 |
|
| 178 |
-
# 3) Split into
|
| 179 |
candidates: List[str] = []
|
| 180 |
-
for para in
|
| 181 |
for s in _split_sentences(para):
|
| 182 |
w = s.split()
|
| 183 |
if not (8 <= len(w) <= 35):
|
|
@@ -186,16 +220,18 @@ class SimpleRAG:
|
|
| 186 |
continue
|
| 187 |
candidates.append(" ".join(w))
|
| 188 |
|
|
|
|
| 189 |
if not candidates:
|
| 190 |
-
|
|
|
|
| 191 |
|
| 192 |
-
#
|
| 193 |
q_emb = self.model.encode([question], convert_to_numpy=True, normalize_embeddings=True).astype(np.float32)
|
| 194 |
cand_emb = self.model.encode(candidates, convert_to_numpy=True, normalize_embeddings=True).astype(np.float32)
|
| 195 |
scores = (cand_emb @ q_emb.T).ravel()
|
| 196 |
order = np.argsort(-scores)
|
| 197 |
|
| 198 |
-
#
|
| 199 |
selected: List[str] = []
|
| 200 |
for i in order:
|
| 201 |
s = candidates[i].strip()
|
|
@@ -205,8 +241,10 @@ class SimpleRAG:
|
|
| 205 |
if len(selected) >= max_sentences:
|
| 206 |
break
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
| 210 |
|
| 211 |
bullets = "\n".join(f"- {s}" for s in selected)
|
| 212 |
return f"Answer (based on document context):\n{bullets}"
|
|
|
|
| 64 |
non_ascii_ratio = sum(ord(c) > 127 for c in s) / max(1, len(s))
|
| 65 |
return has_az or non_ascii_ratio > 0.15
|
| 66 |
|
| 67 |
+
def _non_ascii_ratio(s: str) -> float:
|
| 68 |
+
return sum(ord(c) > 127 for c in s) / max(1, len(s))
|
| 69 |
+
|
| 70 |
+
def _keyword_summary_en(contexts: List[str]) -> List[str]:
|
| 71 |
+
"""English fallback: infer main items from keywords."""
|
| 72 |
+
text = " ".join(contexts).lower()
|
| 73 |
+
bullets: List[str] = []
|
| 74 |
+
def add(b: str):
|
| 75 |
+
if b not in bullets:
|
| 76 |
+
bullets.append(b)
|
| 77 |
+
|
| 78 |
+
if ("şüşə" in text) or ("ara kəsm" in text) or ("s/q" in text):
|
| 79 |
+
add("Removal and re-installation of glass partitions in sanitary areas.")
|
| 80 |
+
if "divar kağız" in text:
|
| 81 |
+
add("Wallpaper repair or replacement; in some areas replaced with plaster and paint.")
|
| 82 |
+
if ("alçı boya" in text) or ("boya işi" in text) or ("plaster" in text) or ("boya" in text):
|
| 83 |
+
add("Wall plastering and painting works.")
|
| 84 |
+
if "seramik" in text:
|
| 85 |
+
add("Ceramic tiling works (including grouting).")
|
| 86 |
+
if ("dilatasyon" in text) or ("ar 153" in text) or ("ar153" in text):
|
| 87 |
+
add("Installation of AR 153–050 floor expansion joint profile with required accessories and insulation.")
|
| 88 |
+
if "daş yunu" in text:
|
| 89 |
+
add("Rock wool insulation installed where required.")
|
| 90 |
+
if ("sütunlarda" in text) or ("üzlüyün" in text):
|
| 91 |
+
add("Repair of wall cladding on columns.")
|
| 92 |
+
if ("m²" in text) or ("ədəd" in text) or ("azn" in text):
|
| 93 |
+
add("Bill of quantities style lines with unit prices and measures (m², pcs).")
|
| 94 |
+
|
| 95 |
+
if not bullets:
|
| 96 |
+
bullets = [
|
| 97 |
+
"The document appears to be a bill of quantities for renovation works.",
|
| 98 |
+
"Scope includes demolition/reinstallation, finishing (plaster & paint), tiling, and profiles.",
|
| 99 |
+
]
|
| 100 |
+
return bullets[:5]
|
| 101 |
+
|
| 102 |
class SimpleRAG:
|
| 103 |
def __init__(
|
| 104 |
self,
|
|
|
|
| 197 |
if not contexts:
|
| 198 |
return "No relevant context found. Please upload a PDF or ask a more specific question."
|
| 199 |
|
| 200 |
+
# 1) Clean & keep top contexts
|
| 201 |
cleaned_contexts = [_clean_for_summary(c) for c in contexts[:5]]
|
| 202 |
cleaned_contexts = [c for c in cleaned_contexts if len(c) > 40]
|
| 203 |
if not cleaned_contexts:
|
| 204 |
return "The document appears largely tabular/numeric; couldn't extract readable sentences."
|
| 205 |
|
| 206 |
+
# 2) Try to pre-translate paragraphs to EN
|
| 207 |
if OUTPUT_LANG == "en":
|
| 208 |
+
translated = self._translate_to_en(cleaned_contexts)
|
| 209 |
+
else:
|
| 210 |
+
translated = cleaned_contexts
|
|
|
|
| 211 |
|
| 212 |
+
# 3) Split paragraphs into candidate sentences and filter
|
| 213 |
candidates: List[str] = []
|
| 214 |
+
for para in translated:
|
| 215 |
for s in _split_sentences(para):
|
| 216 |
w = s.split()
|
| 217 |
if not (8 <= len(w) <= 35):
|
|
|
|
| 220 |
continue
|
| 221 |
candidates.append(" ".join(w))
|
| 222 |
|
| 223 |
+
# 4) If we still don't have good EN sentences, fallback to keyword summary
|
| 224 |
if not candidates:
|
| 225 |
+
bullets = _keyword_summary_en(cleaned_contexts)
|
| 226 |
+
return "Answer (based on document context):\n" + "\n".join(f"- {b}" for b in bullets)
|
| 227 |
|
| 228 |
+
# 5) Rank by similarity
|
| 229 |
q_emb = self.model.encode([question], convert_to_numpy=True, normalize_embeddings=True).astype(np.float32)
|
| 230 |
cand_emb = self.model.encode(candidates, convert_to_numpy=True, normalize_embeddings=True).astype(np.float32)
|
| 231 |
scores = (cand_emb @ q_emb.T).ravel()
|
| 232 |
order = np.argsort(-scores)
|
| 233 |
|
| 234 |
+
# 6) Deduplicate (aggressive)
|
| 235 |
selected: List[str] = []
|
| 236 |
for i in order:
|
| 237 |
s = candidates[i].strip()
|
|
|
|
| 241 |
if len(selected) >= max_sentences:
|
| 242 |
break
|
| 243 |
|
| 244 |
+
# 7) If selected lines still look non-English, use keyword fallback
|
| 245 |
+
if not selected or (sum(_non_ascii_ratio(s) for s in selected) / len(selected) > 0.10):
|
| 246 |
+
bullets = _keyword_summary_en(cleaned_contexts)
|
| 247 |
+
return "Answer (based on document context):\n" + "\n".join(f"- {b}" for b in bullets)
|
| 248 |
|
| 249 |
bullets = "\n".join(f"- {s}" for s in selected)
|
| 250 |
return f"Answer (based on document context):\n{bullets}"
|