Update app.py
Browse files
app.py
CHANGED
|
@@ -638,60 +638,74 @@ def run_tool(source_url, target_url, anchor_text, smart_rewrite, plain_text, sug
|
|
| 638 |
# Process original anchor suggestion
|
| 639 |
draft_html = res["best_sentence_with_anchor"]
|
| 640 |
|
| 641 |
-
#
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
else:
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
polished = gpt_validate_and_polish(final_html, anchor_text, target_url, language=language_name)
|
| 651 |
final_html = polished.get("sentence_html", final_html)
|
| 652 |
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
# ONLY show alternative if:
|
| 662 |
-
# 1. suggest_alternative_anchor is True
|
| 663 |
-
# 2. The original anchor was NOT found in the article
|
| 664 |
-
# 3. We have an alternative suggestion
|
| 665 |
-
if suggest_alternative_anchor and not res.get("keyword_in_article", True) and res.get("alternative_anchor"):
|
| 666 |
-
alt_anchor = res["alternative_anchor"]
|
| 667 |
-
alt_sentence_original = res.get("alternative_sentence_original", "")
|
| 668 |
-
alt_sentence = res.get("alternative_sentence", "")
|
| 669 |
|
| 670 |
-
#
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
alt_final = alt_g["sentence_html"]
|
| 679 |
-
else:
|
| 680 |
-
alt_final = alt_sentence
|
| 681 |
-
|
| 682 |
-
# Polish if needed
|
| 683 |
-
if not res.get("alternative_exact_match", False):
|
| 684 |
-
alt_polished = gpt_validate_and_polish(alt_final, alt_anchor, target_url, language=alt_language_name)
|
| 685 |
-
alt_final = alt_polished.get("sentence_html", alt_final)
|
| 686 |
-
|
| 687 |
-
alt_output = to_plain_text(alt_final) if plain_text else alt_final
|
| 688 |
|
| 689 |
-
#
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
|
| 696 |
return result
|
| 697 |
|
|
|
|
| 638 |
# Process original anchor suggestion
|
| 639 |
draft_html = res["best_sentence_with_anchor"]
|
| 640 |
|
| 641 |
+
# Check if anchor was already present in the article
|
| 642 |
+
anchor_was_present = res.get("anchor_was_present", False)
|
| 643 |
+
|
| 644 |
+
# Only apply GPT rewriting if anchor wasn't already present
|
| 645 |
+
# If anchor is present, we just want to show where to add the link
|
| 646 |
+
if anchor_was_present:
|
| 647 |
+
# Anchor exists - just show where to add the link
|
| 648 |
+
final_output = to_plain_text(draft_html) if plain_text else draft_html
|
| 649 |
+
result = warn + f"β
**Anchor text '{anchor_text}' found in article!**\n\n"
|
| 650 |
+
result += f"π Add link here:\n\n"
|
| 651 |
+
result += f"Original: {original_sentence}\n\n"
|
| 652 |
+
result += f"With link: {final_output}"
|
| 653 |
else:
|
| 654 |
+
# Anchor doesn't exist - need to add it to the sentence
|
| 655 |
+
# 1) Optional first-pass rewrite with language support
|
| 656 |
+
if smart_rewrite:
|
| 657 |
+
g = gpt_rewrite(draft_html, anchor_text, target_url, style="neutral", language=language_name)
|
| 658 |
+
final_html = g["sentence_html"]
|
| 659 |
+
else:
|
| 660 |
+
final_html = draft_html
|
| 661 |
+
|
| 662 |
+
# 2) QA/polish pass with language support
|
| 663 |
polished = gpt_validate_and_polish(final_html, anchor_text, target_url, language=language_name)
|
| 664 |
final_html = polished.get("sentence_html", final_html)
|
| 665 |
|
| 666 |
+
# 3) Optionally convert to plain text
|
| 667 |
+
final_output = to_plain_text(final_html) if plain_text else final_html
|
| 668 |
+
|
| 669 |
+
# Build the result for when anchor is NOT present
|
| 670 |
+
result = warn + f"β οΈ **Anchor text '{anchor_text}' not found in article**\n\n"
|
| 671 |
+
result += f"π Result 1 - Suggested placement:\n\n"
|
| 672 |
+
result += f"Original: {original_sentence}\n\n"
|
| 673 |
+
result += f"Suggested: {final_output}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
+
# ONLY show alternative if:
|
| 676 |
+
# 1. suggest_alternative_anchor is True
|
| 677 |
+
# 2. The original anchor was NOT found in the article
|
| 678 |
+
# 3. We have an alternative suggestion
|
| 679 |
+
if suggest_alternative_anchor and not res.get("keyword_in_article", True) and res.get("alternative_anchor"):
|
| 680 |
+
alt_anchor = res["alternative_anchor"]
|
| 681 |
+
alt_sentence_original = res.get("alternative_sentence_original", "")
|
| 682 |
+
alt_sentence = res.get("alternative_sentence", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
|
| 684 |
+
# Detect language for alternative sentence
|
| 685 |
+
if alt_sentence_original:
|
| 686 |
+
alt_detected_lang = detect_language(alt_sentence_original)
|
| 687 |
+
alt_language_name = get_language_name(alt_detected_lang)
|
| 688 |
+
|
| 689 |
+
# Apply GPT rewriting to alternative as well
|
| 690 |
+
if smart_rewrite and alt_sentence:
|
| 691 |
+
alt_g = gpt_rewrite(alt_sentence, alt_anchor, target_url, style="neutral", language=alt_language_name)
|
| 692 |
+
alt_final = alt_g["sentence_html"]
|
| 693 |
+
else:
|
| 694 |
+
alt_final = alt_sentence
|
| 695 |
+
|
| 696 |
+
# Polish if needed
|
| 697 |
+
if not res.get("alternative_exact_match", False):
|
| 698 |
+
alt_polished = gpt_validate_and_polish(alt_final, alt_anchor, target_url, language=alt_language_name)
|
| 699 |
+
alt_final = alt_polished.get("sentence_html", alt_final)
|
| 700 |
+
|
| 701 |
+
alt_output = to_plain_text(alt_final) if plain_text else alt_final
|
| 702 |
+
|
| 703 |
+
# Add alternative as Result 2
|
| 704 |
+
result += f"\n\n{'='*50}\n\n"
|
| 705 |
+
result += f"π Result 2 - Alternative from article:\n"
|
| 706 |
+
result += f"π‘ Alternative anchor: '{alt_anchor}'\n\n"
|
| 707 |
+
result += f"Original: {alt_sentence_original}\n\n"
|
| 708 |
+
result += f"Suggested: {alt_output}"
|
| 709 |
|
| 710 |
return result
|
| 711 |
|