Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +53 -27
hate_speech_demo.py
CHANGED
|
@@ -609,41 +609,67 @@ def get_contextual_rating(contextual_api, user_input):
|
|
| 609 |
elif "caution" in response_text.lower() or "warning" in response_text.lower():
|
| 610 |
safety_level = "warning"
|
| 611 |
|
| 612 |
-
#
|
|
|
|
|
|
|
|
|
|
| 613 |
try:
|
| 614 |
-
#
|
| 615 |
-
|
| 616 |
-
policy_rating = rating_match.group(1).strip() if rating_match else ""
|
| 617 |
|
| 618 |
-
#
|
| 619 |
-
|
| 620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
|
| 622 |
-
#
|
| 623 |
-
|
| 624 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
|
| 626 |
-
#
|
| 627 |
-
|
| 628 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
|
| 630 |
-
#
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
formatted_text += f"<strong>Policy Rating:</strong> {policy_rating}<br><br>"
|
| 634 |
-
if policy_category:
|
| 635 |
-
formatted_text += f"<strong>Policy Category:</strong> {policy_category}<br><br>"
|
| 636 |
-
if target_of_hate:
|
| 637 |
-
formatted_text += f"<strong>Target of Hate:</strong> {target_of_hate}<br><br>"
|
| 638 |
-
if explanation:
|
| 639 |
-
formatted_text += f"<strong>Explanation:</strong> {explanation}"
|
| 640 |
|
| 641 |
-
#
|
| 642 |
-
if
|
| 643 |
-
formatted_text =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
except Exception as e:
|
| 646 |
-
print(f"Error formatting rating: {e}")
|
|
|
|
| 647 |
formatted_text = response_text
|
| 648 |
|
| 649 |
return formatted_text, retrieval_text, safety_level
|
|
|
|
| 609 |
elif "caution" in response_text.lower() or "warning" in response_text.lower():
|
| 610 |
safety_level = "warning"
|
| 611 |
|
| 612 |
+
# Add simple fallback in case parsing fails
|
| 613 |
+
formatted_text = response_text
|
| 614 |
+
|
| 615 |
+
# Try to parse specific parts of the response with more robust error handling
|
| 616 |
try:
|
| 617 |
+
# Simple approach: Split by key phrases and clean up
|
| 618 |
+
parts = {}
|
|
|
|
| 619 |
|
| 620 |
+
# Extract policy rating
|
| 621 |
+
if "Policy Rating:" in response_text:
|
| 622 |
+
policy_part = response_text.split("Policy Rating:")[1]
|
| 623 |
+
if "Policy Category:" in policy_part:
|
| 624 |
+
parts["rating"] = policy_part.split("Policy Category:")[0].strip()
|
| 625 |
+
elif "Target of Hate:" in policy_part:
|
| 626 |
+
parts["rating"] = policy_part.split("Target of Hate:")[0].strip()
|
| 627 |
+
elif "Explanation:" in policy_part:
|
| 628 |
+
parts["rating"] = policy_part.split("Explanation:")[0].strip()
|
| 629 |
+
else:
|
| 630 |
+
parts["rating"] = policy_part.strip()
|
| 631 |
|
| 632 |
+
# Extract policy category
|
| 633 |
+
if "Policy Category:" in response_text:
|
| 634 |
+
category_part = response_text.split("Policy Category:")[1]
|
| 635 |
+
if "Target of Hate:" in category_part:
|
| 636 |
+
parts["category"] = category_part.split("Target of Hate:")[0].strip()
|
| 637 |
+
elif "Explanation:" in category_part:
|
| 638 |
+
parts["category"] = category_part.split("Explanation:")[0].strip()
|
| 639 |
+
else:
|
| 640 |
+
parts["category"] = category_part.strip()
|
| 641 |
|
| 642 |
+
# Extract target of hate
|
| 643 |
+
if "Target of Hate:" in response_text:
|
| 644 |
+
target_part = response_text.split("Target of Hate:")[1]
|
| 645 |
+
if "Explanation:" in target_part:
|
| 646 |
+
parts["target"] = target_part.split("Explanation:")[0].strip()
|
| 647 |
+
else:
|
| 648 |
+
parts["target"] = target_part.strip()
|
| 649 |
|
| 650 |
+
# Extract explanation
|
| 651 |
+
if "Explanation:" in response_text:
|
| 652 |
+
parts["explanation"] = response_text.split("Explanation:")[1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
|
| 654 |
+
# Build formatted output with HTML
|
| 655 |
+
if parts:
|
| 656 |
+
formatted_text = ""
|
| 657 |
+
if "rating" in parts:
|
| 658 |
+
formatted_text += f"<strong>Policy Rating:</strong> {parts['rating']}<br><br>"
|
| 659 |
+
if "category" in parts:
|
| 660 |
+
formatted_text += f"<strong>Policy Category:</strong> {parts['category']}<br><br>"
|
| 661 |
+
if "target" in parts:
|
| 662 |
+
formatted_text += f"<strong>Target of Hate:</strong> {parts['target']}<br><br>"
|
| 663 |
+
if "explanation" in parts:
|
| 664 |
+
formatted_text += f"<strong>Explanation:</strong> {parts['explanation']}"
|
| 665 |
|
| 666 |
+
# If we end up with empty formatted text, fall back to original
|
| 667 |
+
if not formatted_text.strip():
|
| 668 |
+
formatted_text = response_text
|
| 669 |
+
|
| 670 |
except Exception as e:
|
| 671 |
+
print(f"Error formatting rating (falling back to raw text): {e}")
|
| 672 |
+
# Fall back to the original text in case of any parsing error
|
| 673 |
formatted_text = response_text
|
| 674 |
|
| 675 |
return formatted_text, retrieval_text, safety_level
|