Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -320,7 +320,9 @@ def clean_value(value: Any, max_chars: int = 220) -> str:
|
|
| 320 |
text = ", ".join(clean_value(item, max_chars=max_chars) for item in value)
|
| 321 |
else:
|
| 322 |
text = str(value)
|
| 323 |
-
text =
|
|
|
|
|
|
|
| 324 |
if not text or text.lower() in {"none", "null", "nan", "not provided"}:
|
| 325 |
return ""
|
| 326 |
if text.startswith(("http://", "https://")):
|
|
@@ -533,6 +535,14 @@ def sentence_join(lines: list[str]) -> str:
|
|
| 533 |
return "\n".join(line for line in lines if line)
|
| 534 |
|
| 535 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
def build_grounded_answer(req: AnswerRequest, lang: str) -> str:
|
| 537 |
rows = req.context or req.rows or []
|
| 538 |
if not rows:
|
|
@@ -618,7 +628,7 @@ def simplify_grounded_answer(generator: IdemGenerator, req: AnswerRequest, lang:
|
|
| 618 |
simplified = first_reformulation(raw_simplified, grounded_answer, lang)
|
| 619 |
if not simplified:
|
| 620 |
simplified = grounded_answer
|
| 621 |
-
simplified = clean_value(simplified, max_chars=3000)
|
| 622 |
if introduces_unsupported_ownership(grounded_answer, simplified):
|
| 623 |
simplified = grounded_answer
|
| 624 |
if not simplified or not text_matches_language(simplified, lang):
|
|
@@ -646,7 +656,7 @@ def clean_idem_output(raw: str, original_input: str) -> str:
|
|
| 646 |
if cleaned:
|
| 647 |
lines.append(cleaned)
|
| 648 |
|
| 649 |
-
return "\n".join(lines).strip() or raw.strip()
|
| 650 |
|
| 651 |
|
| 652 |
def first_reformulation(output_text: str, original_text: str, lang: str) -> str:
|
|
@@ -736,4 +746,4 @@ def answer(req: AnswerRequest):
|
|
| 736 |
if not result:
|
| 737 |
result = FALLBACKS[lang]
|
| 738 |
|
| 739 |
-
return {"answer": result, "source": source}
|
|
|
|
| 320 |
text = ", ".join(clean_value(item, max_chars=max_chars) for item in value)
|
| 321 |
else:
|
| 322 |
text = str(value)
|
| 323 |
+
text = text.replace("\\r\\n", "\n").replace("\\n", "\n").replace("\\t", " ")
|
| 324 |
+
text = re.sub(r"[ \t]+", " ", text)
|
| 325 |
+
text = re.sub(r"\n{3,}", "\n\n", text).strip()
|
| 326 |
if not text or text.lower() in {"none", "null", "nan", "not provided"}:
|
| 327 |
return ""
|
| 328 |
if text.startswith(("http://", "https://")):
|
|
|
|
| 535 |
return "\n".join(line for line in lines if line)
|
| 536 |
|
| 537 |
|
| 538 |
+
def normalize_answer_text(text: str) -> str:
|
| 539 |
+
text = str(text or "").replace("\\r\\n", "\n").replace("\\n", "\n").replace("\\t", " ")
|
| 540 |
+
text = re.sub(r"[ \t]+", " ", text)
|
| 541 |
+
text = re.sub(r" *\n *", "\n", text)
|
| 542 |
+
text = re.sub(r"\n{3,}", "\n\n", text)
|
| 543 |
+
return text.strip()
|
| 544 |
+
|
| 545 |
+
|
| 546 |
def build_grounded_answer(req: AnswerRequest, lang: str) -> str:
|
| 547 |
rows = req.context or req.rows or []
|
| 548 |
if not rows:
|
|
|
|
| 628 |
simplified = first_reformulation(raw_simplified, grounded_answer, lang)
|
| 629 |
if not simplified:
|
| 630 |
simplified = grounded_answer
|
| 631 |
+
simplified = normalize_answer_text(clean_value(simplified, max_chars=3000))
|
| 632 |
if introduces_unsupported_ownership(grounded_answer, simplified):
|
| 633 |
simplified = grounded_answer
|
| 634 |
if not simplified or not text_matches_language(simplified, lang):
|
|
|
|
| 656 |
if cleaned:
|
| 657 |
lines.append(cleaned)
|
| 658 |
|
| 659 |
+
return normalize_answer_text("\n".join(lines).strip() or raw.strip())
|
| 660 |
|
| 661 |
|
| 662 |
def first_reformulation(output_text: str, original_text: str, lang: str) -> str:
|
|
|
|
| 746 |
if not result:
|
| 747 |
result = FALLBACKS[lang]
|
| 748 |
|
| 749 |
+
return {"answer": normalize_answer_text(result), "source": source}
|