Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- hate_speech_demo.py +224 -279
- hatespeech/hate_speech_demo.py +224 -279
- test/.DS_Store +0 -0
- test/Hate Speech Policy.pdf +3 -0
- test/README.md +6 -0
- test/test.py +96 -0
.DS_Store
CHANGED
|
Binary files a/.DS_Store and b/.DS_Store differ
|
|
|
hate_speech_demo.py
CHANGED
|
@@ -10,11 +10,33 @@ from dotenv import load_dotenv
|
|
| 10 |
from together import Together
|
| 11 |
import openai
|
| 12 |
import json
|
|
|
|
| 13 |
|
| 14 |
# Load environment variables (for local development)
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def process_retrieval_text(retrieval_text, user_input):
|
| 19 |
"""
|
| 20 |
Process the retrieval text by identifying proper document boundaries
|
|
@@ -45,17 +67,22 @@ def process_retrieval_text(retrieval_text, user_input):
|
|
| 45 |
content_match = re.search(r'Content:\s*(.*)', doc_info, re.DOTALL)
|
| 46 |
content = content_match.group(1).strip() if content_match else "No content available"
|
| 47 |
|
| 48 |
-
# Format with clear section headers
|
| 49 |
formatted_html = f"""
|
| 50 |
<div class='doc-section'>
|
| 51 |
-
<
|
| 52 |
-
|
| 53 |
-
<
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
</div>
|
| 60 |
"""
|
| 61 |
chunks.append(formatted_html)
|
|
@@ -86,8 +113,12 @@ def process_retrieval_text(retrieval_text, user_input):
|
|
| 86 |
chunks.append(current_chunk)
|
| 87 |
|
| 88 |
# Format each chunk with better section styling
|
| 89 |
-
chunks = [f"
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Extract keywords from user input (longer than 3 chars)
|
| 93 |
keywords = re.findall(r'\b\w{4,}\b', user_input.lower())
|
|
@@ -106,7 +137,85 @@ def process_retrieval_text(retrieval_text, user_input):
|
|
| 106 |
|
| 107 |
highlighted_chunks.append(highlighted_chunk)
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# API Keys - hardcoded for convenience
|
| 112 |
# Replace these with your actual API keys
|
|
@@ -388,6 +497,55 @@ h1, h2, h3, h4, h5, h6, p, span, div, button, input, textarea, label {
|
|
| 388 |
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 389 |
transform: translateY(-2px);
|
| 390 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
"""
|
| 392 |
|
| 393 |
# Contextual API class - UPDATED WITH NEW MODEL ID
|
|
@@ -437,6 +595,7 @@ class ContextualAPIUtils:
|
|
| 437 |
return response_content, response_time, retrieval_text, error_message
|
| 438 |
|
| 439 |
# Contextual AI rating - UPDATED PROMPT FORMAT
|
|
|
|
| 440 |
def get_contextual_rating(contextual_api, user_input):
|
| 441 |
rating_prompt = f"Content to rate: {user_input}"
|
| 442 |
response_text, response_time, retrieval_text, error = contextual_api.chat(rating_prompt)
|
|
@@ -450,7 +609,38 @@ def get_contextual_rating(contextual_api, user_input):
|
|
| 450 |
elif "caution" in response_text.lower() or "warning" in response_text.lower():
|
| 451 |
safety_level = "warning"
|
| 452 |
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
# LlamaGuard rating
|
| 456 |
LLAMA_HARM_CATEGORIES = {
|
|
@@ -516,7 +706,6 @@ def get_openai_moderation(openai_client, user_input):
|
|
| 516 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 517 |
|
| 518 |
# Perspective API
|
| 519 |
-
|
| 520 |
def get_perspective_rating(api_key, user_input):
|
| 521 |
url = 'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'
|
| 522 |
params = {'key': api_key}
|
|
@@ -552,7 +741,6 @@ def get_perspective_rating(api_key, user_input):
|
|
| 552 |
except Exception as e:
|
| 553 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 554 |
|
| 555 |
-
|
| 556 |
# Updated to only require one input
|
| 557 |
def rate_user_input(user_input):
|
| 558 |
# Initialize APIs with hardcoded keys
|
|
@@ -566,7 +754,6 @@ def rate_user_input(user_input):
|
|
| 566 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
| 567 |
perspective_rating, perspective_safety = get_perspective_rating(PERSPECTIVE_API_KEY, user_input)
|
| 568 |
|
| 569 |
-
|
| 570 |
# Format responses carefully to avoid random line breaks
|
| 571 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
| 572 |
# Don't add line breaks to contextual rating
|
|
@@ -651,257 +838,15 @@ def create_gradio_app():
|
|
| 651 |
border_color_primary="#E0E0E0"
|
| 652 |
)
|
| 653 |
|
| 654 |
-
#
|
| 655 |
-
custom_css = CUSTOM_CSS
|
| 656 |
-
/* Policy preview popup styles */
|
| 657 |
-
.policy-popup {
|
| 658 |
-
display: none;
|
| 659 |
-
position: fixed;
|
| 660 |
-
top: 0;
|
| 661 |
-
left: 0;
|
| 662 |
-
width: 100%;
|
| 663 |
-
height: 100%;
|
| 664 |
-
background-color: rgba(0,0,0,0.7);
|
| 665 |
-
z-index: 1000;
|
| 666 |
-
justify-content: center;
|
| 667 |
-
align-items: center;
|
| 668 |
-
}
|
| 669 |
-
|
| 670 |
-
.policy-popup-content {
|
| 671 |
-
background-color: white;
|
| 672 |
-
width: 80%;
|
| 673 |
-
height: 80%;
|
| 674 |
-
border-radius: 8px;
|
| 675 |
-
padding: 20px;
|
| 676 |
-
position: relative;
|
| 677 |
-
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
| 678 |
-
display: flex;
|
| 679 |
-
flex-direction: column;
|
| 680 |
-
}
|
| 681 |
-
|
| 682 |
-
.policy-popup-header {
|
| 683 |
-
display: flex;
|
| 684 |
-
justify-content: space-between;
|
| 685 |
-
align-items: center;
|
| 686 |
-
margin-bottom: 15px;
|
| 687 |
-
border-bottom: 1px solid #eee;
|
| 688 |
-
padding-bottom: 10px;
|
| 689 |
-
}
|
| 690 |
-
|
| 691 |
-
.policy-popup-title {
|
| 692 |
-
font-weight: bold;
|
| 693 |
-
font-size: 18px;
|
| 694 |
-
}
|
| 695 |
-
|
| 696 |
-
.policy-popup-close {
|
| 697 |
-
background-color: #222222;
|
| 698 |
-
color: white;
|
| 699 |
-
border: none;
|
| 700 |
-
border-radius: 4px;
|
| 701 |
-
padding: 5px 10px;
|
| 702 |
-
cursor: pointer;
|
| 703 |
-
}
|
| 704 |
-
|
| 705 |
-
.policy-popup-close:hover {
|
| 706 |
-
background-color: #000000;
|
| 707 |
-
}
|
| 708 |
-
|
| 709 |
-
.policy-iframe-container {
|
| 710 |
-
flex: 1;
|
| 711 |
-
overflow: hidden;
|
| 712 |
-
}
|
| 713 |
-
|
| 714 |
-
.policy-iframe {
|
| 715 |
-
width: 100%;
|
| 716 |
-
height: 100%;
|
| 717 |
-
border: 1px solid #eee;
|
| 718 |
-
}
|
| 719 |
-
|
| 720 |
-
/* Fallback for when PDF can't be displayed in iframe */
|
| 721 |
-
.policy-fallback {
|
| 722 |
-
padding: 20px;
|
| 723 |
-
text-align: center;
|
| 724 |
-
}
|
| 725 |
-
|
| 726 |
-
.policy-fallback a {
|
| 727 |
-
display: inline-block;
|
| 728 |
-
margin-top: 15px;
|
| 729 |
-
padding: 10px 15px;
|
| 730 |
-
background-color: #FCA539;
|
| 731 |
-
color: #000000;
|
| 732 |
-
text-decoration: none;
|
| 733 |
-
border-radius: 4px;
|
| 734 |
-
font-weight: bold;
|
| 735 |
-
}
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
"""
|
| 739 |
|
| 740 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 741 |
# Add loading spinner
|
| 742 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 743 |
|
| 744 |
-
#
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
# Add policy popup HTML with reliable PDF loading that works on first click
|
| 748 |
-
policy_popup_html = """
|
| 749 |
-
<div id="policy-popup" class="policy-popup">
|
| 750 |
-
<div class="policy-popup-content">
|
| 751 |
-
<div class="policy-popup-header">
|
| 752 |
-
<div class="policy-popup-title">Hate Speech Policy</div>
|
| 753 |
-
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 754 |
-
</div>
|
| 755 |
-
<div class="policy-iframe-container">
|
| 756 |
-
<!-- PDF viewer iframe -->
|
| 757 |
-
<iframe class="policy-iframe" id="policy-iframe"></iframe>
|
| 758 |
-
|
| 759 |
-
<!-- Loading indicator while PDF loads -->
|
| 760 |
-
<div id="pdf-loading" style="display: flex; justify-content: center; align-items: center; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(255,255,255,0.8);">
|
| 761 |
-
<div style="text-align: center;">
|
| 762 |
-
<div style="width: 50px; height: 50px; border: 5px solid #f3f3f3; border-top: 5px solid #FCA539; border-radius: 50%; margin: 0 auto 15px auto; animation: spin 1s linear infinite;"></div>
|
| 763 |
-
<p>Loading policy document...</p>
|
| 764 |
-
</div>
|
| 765 |
-
</div>
|
| 766 |
-
<style>
|
| 767 |
-
@keyframes spin {
|
| 768 |
-
0% { transform: rotate(0deg); }
|
| 769 |
-
100% { transform: rotate(360deg); }
|
| 770 |
-
}
|
| 771 |
-
</style>
|
| 772 |
-
|
| 773 |
-
<!-- Fallback content if iframe fails -->
|
| 774 |
-
<div class="policy-fallback" id="policy-fallback" style="display:none;">
|
| 775 |
-
<p>The policy document couldn't be displayed in the preview.</p>
|
| 776 |
-
<a href="#" id="policy-download-link" target="_blank">Download Policy PDF</a>
|
| 777 |
-
<button onclick="retryPdfLoad()" style="display: block; margin-top: 15px; padding: 8px 15px; background: #FCA539; border: none; border-radius: 4px; cursor: pointer;">Try Again</button>
|
| 778 |
-
</div>
|
| 779 |
-
</div>
|
| 780 |
-
</div>
|
| 781 |
-
</div>
|
| 782 |
-
|
| 783 |
-
<script>
|
| 784 |
-
// Store the PDF URL globally once found
|
| 785 |
-
let pdfPolicyUrl = null;
|
| 786 |
-
let pdfLoadAttempted = false;
|
| 787 |
-
|
| 788 |
-
// Find the PDF URL as soon as possible
|
| 789 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 790 |
-
findPdfUrl();
|
| 791 |
-
});
|
| 792 |
-
|
| 793 |
-
// Function to find the PDF URL in various locations
|
| 794 |
-
function findPdfUrl() {
|
| 795 |
-
// Try to find the file component
|
| 796 |
-
const fileComponents = document.querySelectorAll("div.file-component");
|
| 797 |
-
for (const comp of fileComponents) {
|
| 798 |
-
const downloadBtn = comp.querySelector("a[download]");
|
| 799 |
-
if (downloadBtn && downloadBtn.href && downloadBtn.href.includes("file=")) {
|
| 800 |
-
pdfPolicyUrl = downloadBtn.href;
|
| 801 |
-
console.log("Found PDF URL:", pdfPolicyUrl);
|
| 802 |
-
return pdfPolicyUrl;
|
| 803 |
-
}
|
| 804 |
-
}
|
| 805 |
-
|
| 806 |
-
// Try to find any link with the PDF name
|
| 807 |
-
const allLinks = document.querySelectorAll("a");
|
| 808 |
-
for (const link of allLinks) {
|
| 809 |
-
if (link.href && (link.href.includes("Policy.pdf") ||
|
| 810 |
-
link.href.includes("file=") && link.textContent.includes("Download"))) {
|
| 811 |
-
pdfPolicyUrl = link.href;
|
| 812 |
-
console.log("Found PDF URL from links:", pdfPolicyUrl);
|
| 813 |
-
return pdfPolicyUrl;
|
| 814 |
-
}
|
| 815 |
-
}
|
| 816 |
-
|
| 817 |
-
// If we didn't find it, try again in a second
|
| 818 |
-
setTimeout(findPdfUrl, 1000);
|
| 819 |
-
return null;
|
| 820 |
-
}
|
| 821 |
-
|
| 822 |
-
// Function to actually load the PDF
|
| 823 |
-
function loadPolicyPdf(url) {
|
| 824 |
-
const iframe = document.getElementById("policy-iframe");
|
| 825 |
-
const loadingIndicator = document.getElementById("pdf-loading");
|
| 826 |
-
const fallback = document.getElementById("policy-fallback");
|
| 827 |
-
const downloadLink = document.getElementById("policy-download-link");
|
| 828 |
-
|
| 829 |
-
// Show loading, hide fallback
|
| 830 |
-
loadingIndicator.style.display = "flex";
|
| 831 |
-
fallback.style.display = "none";
|
| 832 |
-
iframe.style.display = "block";
|
| 833 |
-
|
| 834 |
-
// Set the download link
|
| 835 |
-
downloadLink.href = url;
|
| 836 |
-
|
| 837 |
-
// Set iframe source to PDF
|
| 838 |
-
iframe.src = url;
|
| 839 |
-
|
| 840 |
-
// Set a timeout to hide loading regardless of outcome
|
| 841 |
-
setTimeout(function() {
|
| 842 |
-
loadingIndicator.style.display = "none";
|
| 843 |
-
}, 3000);
|
| 844 |
-
|
| 845 |
-
// When iframe loads, hide loading indicator
|
| 846 |
-
iframe.onload = function() {
|
| 847 |
-
loadingIndicator.style.display = "none";
|
| 848 |
-
pdfLoadAttempted = true;
|
| 849 |
-
};
|
| 850 |
-
|
| 851 |
-
// Handle errors
|
| 852 |
-
iframe.onerror = function() {
|
| 853 |
-
loadingIndicator.style.display = "none";
|
| 854 |
-
fallback.style.display = "block";
|
| 855 |
-
iframe.style.display = "none";
|
| 856 |
-
pdfLoadAttempted = true;
|
| 857 |
-
};
|
| 858 |
-
}
|
| 859 |
-
|
| 860 |
-
// Function to retry loading the PDF
|
| 861 |
-
function retryPdfLoad() {
|
| 862 |
-
if (pdfPolicyUrl) {
|
| 863 |
-
loadPolicyPdf(pdfPolicyUrl);
|
| 864 |
-
} else {
|
| 865 |
-
// Try to find the URL first
|
| 866 |
-
const url = findPdfUrl();
|
| 867 |
-
if (url) {
|
| 868 |
-
loadPolicyPdf(url);
|
| 869 |
-
}
|
| 870 |
-
}
|
| 871 |
-
}
|
| 872 |
-
|
| 873 |
-
// Function to handle opening the policy popup
|
| 874 |
-
function openPolicyPopup() {
|
| 875 |
-
// Display the popup immediately
|
| 876 |
-
document.getElementById('policy-popup').style.display = 'flex';
|
| 877 |
-
|
| 878 |
-
// If we've never tried to load the PDF, or it failed previously, try loading it
|
| 879 |
-
if (!pdfLoadAttempted) {
|
| 880 |
-
if (pdfPolicyUrl) {
|
| 881 |
-
// We already have the URL, load the PDF
|
| 882 |
-
loadPolicyPdf(pdfPolicyUrl);
|
| 883 |
-
} else {
|
| 884 |
-
// Try to find the URL first
|
| 885 |
-
const url = findPdfUrl();
|
| 886 |
-
if (url) {
|
| 887 |
-
loadPolicyPdf(url);
|
| 888 |
-
} else {
|
| 889 |
-
// Show fallback if we can't find the URL
|
| 890 |
-
document.getElementById("pdf-loading").style.display = "none";
|
| 891 |
-
document.getElementById("policy-iframe").style.display = "none";
|
| 892 |
-
document.getElementById("policy-fallback").style.display = "block";
|
| 893 |
-
}
|
| 894 |
-
}
|
| 895 |
-
}
|
| 896 |
-
}
|
| 897 |
-
|
| 898 |
-
// Make functions globally available
|
| 899 |
-
window.openPolicyPopup = openPolicyPopup;
|
| 900 |
-
window.retryPdfLoad = retryPdfLoad;
|
| 901 |
-
</script>
|
| 902 |
-
"""
|
| 903 |
-
|
| 904 |
-
gr.HTML(policy_popup_html)
|
| 905 |
|
| 906 |
gr.Markdown("# Safety Oracle for Rating Hate Speech [BETA]")
|
| 907 |
gr.HTML("""
|
|
@@ -950,10 +895,10 @@ def create_gradio_app():
|
|
| 950 |
rate_btn = gr.Button("Rate Content", elem_classes=["gray-button"], scale=1)
|
| 951 |
|
| 952 |
user_input = gr.Textbox(
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
)
|
| 957 |
|
| 958 |
# 🌟 Contextual Safety Oracle
|
| 959 |
gr.HTML("""
|
|
@@ -1025,18 +970,18 @@ def create_gradio_app():
|
|
| 1025 |
|
| 1026 |
# Bind rating button with loading indicator
|
| 1027 |
rate_btn.click(
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
).then(
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
).then(
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
)
|
| 1040 |
|
| 1041 |
return app
|
| 1042 |
|
|
|
|
| 10 |
from together import Together
|
| 11 |
import openai
|
| 12 |
import json
|
| 13 |
+
from pathlib import Path
|
| 14 |
|
| 15 |
# Load environment variables (for local development)
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
+
# Google Drive Embed URL for PDF
|
| 19 |
+
PDF_EMBED_URL = "https://drive.google.com/file/d/1WZCQpIWfelbxgBr8dNiW2rqVGaDyf-Gi/preview"
|
| 20 |
+
|
| 21 |
+
# Custom PDF Viewer Modal (injected HTML)
|
| 22 |
+
PDF_MODAL_HTML = f"""
|
| 23 |
+
<div id='policy-popup' class='pdf-modal' style='display:none;'>
|
| 24 |
+
<div class='pdf-modal-content'>
|
| 25 |
+
<button class='close-btn' onclick="document.getElementById('policy-popup').style.display='none'; document.body.style.overflow='auto';">×</button>
|
| 26 |
+
<div class='pdf-container'>
|
| 27 |
+
<iframe class='pdf-embed' src='{PDF_EMBED_URL}' allow="autoplay"></iframe>
|
| 28 |
+
</div>
|
| 29 |
+
</div>
|
| 30 |
+
</div>
|
| 31 |
+
<script>
|
| 32 |
+
function openPolicyPopup() {{
|
| 33 |
+
document.getElementById('policy-popup').style.display = 'flex';
|
| 34 |
+
document.body.style.overflow = 'hidden';
|
| 35 |
+
}}
|
| 36 |
+
</script>
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
# Replace your process_retrieval_text function with this updated version
|
| 40 |
def process_retrieval_text(retrieval_text, user_input):
|
| 41 |
"""
|
| 42 |
Process the retrieval text by identifying proper document boundaries
|
|
|
|
| 67 |
content_match = re.search(r'Content:\s*(.*)', doc_info, re.DOTALL)
|
| 68 |
content = content_match.group(1).strip() if content_match else "No content available"
|
| 69 |
|
| 70 |
+
# Format with clear section headers and better spacing
|
| 71 |
formatted_html = f"""
|
| 72 |
<div class='doc-section'>
|
| 73 |
+
<h3 class="doc-number">Evidence Section {i+1}</h3>
|
| 74 |
+
|
| 75 |
+
<div class="doc-section-info">
|
| 76 |
+
<p><strong>Document Title:</strong> {doc_name}</p>
|
| 77 |
+
<div class="subsection-info">
|
| 78 |
+
<p><strong>Page Number:</strong> Page {page}</p>
|
| 79 |
+
</div>
|
| 80 |
+
</div>
|
| 81 |
+
|
| 82 |
+
<div class="doc-content-container">
|
| 83 |
+
<h4>Content:</h4>
|
| 84 |
+
<div class='doc-content'>{content}</div>
|
| 85 |
+
</div>
|
| 86 |
</div>
|
| 87 |
"""
|
| 88 |
chunks.append(formatted_html)
|
|
|
|
| 113 |
chunks.append(current_chunk)
|
| 114 |
|
| 115 |
# Format each chunk with better section styling
|
| 116 |
+
chunks = [f"""
|
| 117 |
+
<div class='doc-section'>
|
| 118 |
+
<h3 class="doc-title">Evidence Section {i+1}</h3>
|
| 119 |
+
<div class='doc-content'>{chunk.strip()}</div>
|
| 120 |
+
</div>
|
| 121 |
+
""" for i, chunk in enumerate(chunks)]
|
| 122 |
|
| 123 |
# Extract keywords from user input (longer than 3 chars)
|
| 124 |
keywords = re.findall(r'\b\w{4,}\b', user_input.lower())
|
|
|
|
| 137 |
|
| 138 |
highlighted_chunks.append(highlighted_chunk)
|
| 139 |
|
| 140 |
+
# Add some additional CSS for the knowledge sections
|
| 141 |
+
additional_css = """
|
| 142 |
+
<style>
|
| 143 |
+
.knowledge-sections {
|
| 144 |
+
border-radius: 8px;
|
| 145 |
+
background: #f9f9f9;
|
| 146 |
+
padding: 15px;
|
| 147 |
+
font-family: 'All Round Gothic Demi', 'Poppins', sans-serif !important;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
.doc-section {
|
| 151 |
+
margin-bottom: 25px;
|
| 152 |
+
padding: 15px;
|
| 153 |
+
background: white;
|
| 154 |
+
border-radius: 8px;
|
| 155 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
.doc-number, .doc-title {
|
| 159 |
+
margin-top: 0;
|
| 160 |
+
padding-bottom: 10px;
|
| 161 |
+
border-bottom: 1px solid #eee;
|
| 162 |
+
color: #222;
|
| 163 |
+
font-size: 18px;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
.doc-section-info {
|
| 167 |
+
margin: 10px 0;
|
| 168 |
+
padding: 8px;
|
| 169 |
+
background: #f5f5f5;
|
| 170 |
+
border-radius: 4px;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
.doc-section-info p {
|
| 174 |
+
margin: 5px 0;
|
| 175 |
+
font-size: 16px;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
.subsection-info {
|
| 179 |
+
margin-left: 15px;
|
| 180 |
+
padding-left: 10px;
|
| 181 |
+
border-left: 2px solid #ddd;
|
| 182 |
+
margin-top: 5px;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
.subsection-info p {
|
| 186 |
+
font-size: 14px;
|
| 187 |
+
color: #555;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
.doc-content-container {
|
| 191 |
+
margin-top: 15px;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
.doc-content-container h4 {
|
| 195 |
+
margin-bottom: 8px;
|
| 196 |
+
font-size: 16px;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
.doc-content {
|
| 200 |
+
padding: 12px;
|
| 201 |
+
background: #f9f9f9;
|
| 202 |
+
border-left: 3px solid #FCA539;
|
| 203 |
+
line-height: 1.6;
|
| 204 |
+
border-radius: 4px;
|
| 205 |
+
white-space: pre-line;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
.highlight-match {
|
| 209 |
+
background-color: #FCA539;
|
| 210 |
+
color: black;
|
| 211 |
+
font-weight: bold;
|
| 212 |
+
padding: 0 2px;
|
| 213 |
+
border-radius: 2px;
|
| 214 |
+
}
|
| 215 |
+
</style>
|
| 216 |
+
"""
|
| 217 |
+
|
| 218 |
+
return additional_css + "<div class='knowledge-sections'>" + "".join(highlighted_chunks) + "</div>"
|
| 219 |
|
| 220 |
# API Keys - hardcoded for convenience
|
| 221 |
# Replace these with your actual API keys
|
|
|
|
| 497 |
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 498 |
transform: translateY(-2px);
|
| 499 |
}
|
| 500 |
+
|
| 501 |
+
/* PDF Modal Styling */
|
| 502 |
+
.pdf-modal {
|
| 503 |
+
display: none;
|
| 504 |
+
position: fixed;
|
| 505 |
+
top: 0;
|
| 506 |
+
left: 0;
|
| 507 |
+
width: 100%;
|
| 508 |
+
height: 100%;
|
| 509 |
+
background-color: rgba(0,0,0,0.7);
|
| 510 |
+
z-index: 1000;
|
| 511 |
+
justify-content: center;
|
| 512 |
+
align-items: center;
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
.pdf-modal-content {
|
| 516 |
+
background-color: white;
|
| 517 |
+
width: 80%;
|
| 518 |
+
height: 80%;
|
| 519 |
+
border-radius: 8px;
|
| 520 |
+
padding: 20px;
|
| 521 |
+
position: relative;
|
| 522 |
+
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
.close-btn {
|
| 526 |
+
position: absolute;
|
| 527 |
+
right: 15px;
|
| 528 |
+
top: 15px;
|
| 529 |
+
font-size: 24px;
|
| 530 |
+
cursor: pointer;
|
| 531 |
+
background: #222;
|
| 532 |
+
color: white;
|
| 533 |
+
border: none;
|
| 534 |
+
border-radius: 4px;
|
| 535 |
+
padding: 5px 15px;
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
.pdf-container {
|
| 539 |
+
width: 100%;
|
| 540 |
+
height: calc(100% - 40px);
|
| 541 |
+
margin-top: 40px;
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
.pdf-embed {
|
| 545 |
+
width: 100%;
|
| 546 |
+
height: 100%;
|
| 547 |
+
border: 1px solid #eee;
|
| 548 |
+
}
|
| 549 |
"""
|
| 550 |
|
| 551 |
# Contextual API class - UPDATED WITH NEW MODEL ID
|
|
|
|
| 595 |
return response_content, response_time, retrieval_text, error_message
|
| 596 |
|
| 597 |
# Contextual AI rating - UPDATED PROMPT FORMAT
|
| 598 |
+
# Replace your get_contextual_rating function with this enhanced version
|
| 599 |
def get_contextual_rating(contextual_api, user_input):
|
| 600 |
rating_prompt = f"Content to rate: {user_input}"
|
| 601 |
response_text, response_time, retrieval_text, error = contextual_api.chat(rating_prompt)
|
|
|
|
| 609 |
elif "caution" in response_text.lower() or "warning" in response_text.lower():
|
| 610 |
safety_level = "warning"
|
| 611 |
|
| 612 |
+
# Parse specific parts of the response
|
| 613 |
+
try:
|
| 614 |
+
# Look for policy rating
|
| 615 |
+
rating_match = re.search(r'Policy Rating: (.*?)(?:Policy|$)', response_text, re.IGNORECASE)
|
| 616 |
+
policy_rating = rating_match.group(1).strip() if rating_match else ""
|
| 617 |
+
|
| 618 |
+
# Look for policy category
|
| 619 |
+
category_match = re.search(r'Policy Category: (.*?)(?:Explanation|$)', response_text, re.IGNORECASE)
|
| 620 |
+
policy_category = category_match.group(1).strip() if category_match else ""
|
| 621 |
+
|
| 622 |
+
# Look for explanation
|
| 623 |
+
explanation_match = re.search(r'Explanation: (.*)', response_text, re.IGNORECASE)
|
| 624 |
+
explanation = explanation_match.group(1).strip() if explanation_match else response_text
|
| 625 |
+
|
| 626 |
+
# Format with HTML
|
| 627 |
+
formatted_text = ""
|
| 628 |
+
if policy_rating:
|
| 629 |
+
formatted_text += f"<strong>Policy Rating:</strong> {policy_rating}<br><br>"
|
| 630 |
+
if policy_category:
|
| 631 |
+
formatted_text += f"<strong>Policy Category:</strong> {policy_category}<br><br>"
|
| 632 |
+
if explanation:
|
| 633 |
+
formatted_text += f"<strong>Explanation:</strong> {explanation}"
|
| 634 |
+
|
| 635 |
+
# If we couldn't extract anything, use the original
|
| 636 |
+
if not formatted_text:
|
| 637 |
+
formatted_text = response_text
|
| 638 |
+
|
| 639 |
+
except Exception as e:
|
| 640 |
+
print(f"Error formatting rating: {e}")
|
| 641 |
+
formatted_text = response_text
|
| 642 |
+
|
| 643 |
+
return formatted_text, retrieval_text, safety_level
|
| 644 |
|
| 645 |
# LlamaGuard rating
|
| 646 |
LLAMA_HARM_CATEGORIES = {
|
|
|
|
| 706 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 707 |
|
| 708 |
# Perspective API
|
|
|
|
| 709 |
def get_perspective_rating(api_key, user_input):
|
| 710 |
url = 'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'
|
| 711 |
params = {'key': api_key}
|
|
|
|
| 741 |
except Exception as e:
|
| 742 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 743 |
|
|
|
|
| 744 |
# Updated to only require one input
|
| 745 |
def rate_user_input(user_input):
|
| 746 |
# Initialize APIs with hardcoded keys
|
|
|
|
| 754 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
| 755 |
perspective_rating, perspective_safety = get_perspective_rating(PERSPECTIVE_API_KEY, user_input)
|
| 756 |
|
|
|
|
| 757 |
# Format responses carefully to avoid random line breaks
|
| 758 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
| 759 |
# Don't add line breaks to contextual rating
|
|
|
|
| 838 |
border_color_primary="#E0E0E0"
|
| 839 |
)
|
| 840 |
|
| 841 |
+
# Use the custom CSS with PDF modal styling
|
| 842 |
+
custom_css = CUSTOM_CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
|
| 844 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 845 |
# Add loading spinner
|
| 846 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 847 |
|
| 848 |
+
# Add the PDF modal HTML directly (defined at the top of the file)
|
| 849 |
+
gr.HTML(PDF_MODAL_HTML)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 850 |
|
| 851 |
gr.Markdown("# Safety Oracle for Rating Hate Speech [BETA]")
|
| 852 |
gr.HTML("""
|
|
|
|
| 895 |
rate_btn = gr.Button("Rate Content", elem_classes=["gray-button"], scale=1)
|
| 896 |
|
| 897 |
user_input = gr.Textbox(
|
| 898 |
+
placeholder="Type content to evaluate here...",
|
| 899 |
+
lines=6,
|
| 900 |
+
label=""
|
| 901 |
+
)
|
| 902 |
|
| 903 |
# 🌟 Contextual Safety Oracle
|
| 904 |
gr.HTML("""
|
|
|
|
| 970 |
|
| 971 |
# Bind rating button with loading indicator
|
| 972 |
rate_btn.click(
|
| 973 |
+
show_loading,
|
| 974 |
+
inputs=None,
|
| 975 |
+
outputs=loading_spinner
|
| 976 |
+
).then(
|
| 977 |
+
rate_user_input,
|
| 978 |
+
inputs=[user_input],
|
| 979 |
+
outputs=[contextual_results, llama_results, openai_results, perspective_results]
|
| 980 |
+
).then(
|
| 981 |
+
hide_loading,
|
| 982 |
+
inputs=None,
|
| 983 |
+
outputs=loading_spinner
|
| 984 |
+
)
|
| 985 |
|
| 986 |
return app
|
| 987 |
|
hatespeech/hate_speech_demo.py
CHANGED
|
@@ -10,11 +10,33 @@ from dotenv import load_dotenv
|
|
| 10 |
from together import Together
|
| 11 |
import openai
|
| 12 |
import json
|
|
|
|
| 13 |
|
| 14 |
# Load environment variables (for local development)
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def process_retrieval_text(retrieval_text, user_input):
|
| 19 |
"""
|
| 20 |
Process the retrieval text by identifying proper document boundaries
|
|
@@ -45,17 +67,22 @@ def process_retrieval_text(retrieval_text, user_input):
|
|
| 45 |
content_match = re.search(r'Content:\s*(.*)', doc_info, re.DOTALL)
|
| 46 |
content = content_match.group(1).strip() if content_match else "No content available"
|
| 47 |
|
| 48 |
-
# Format with clear section headers
|
| 49 |
formatted_html = f"""
|
| 50 |
<div class='doc-section'>
|
| 51 |
-
<
|
| 52 |
-
|
| 53 |
-
<
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
</div>
|
| 60 |
"""
|
| 61 |
chunks.append(formatted_html)
|
|
@@ -86,8 +113,12 @@ def process_retrieval_text(retrieval_text, user_input):
|
|
| 86 |
chunks.append(current_chunk)
|
| 87 |
|
| 88 |
# Format each chunk with better section styling
|
| 89 |
-
chunks = [f"
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Extract keywords from user input (longer than 3 chars)
|
| 93 |
keywords = re.findall(r'\b\w{4,}\b', user_input.lower())
|
|
@@ -106,7 +137,85 @@ def process_retrieval_text(retrieval_text, user_input):
|
|
| 106 |
|
| 107 |
highlighted_chunks.append(highlighted_chunk)
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# API Keys - hardcoded for convenience
|
| 112 |
# Replace these with your actual API keys
|
|
@@ -388,6 +497,55 @@ h1, h2, h3, h4, h5, h6, p, span, div, button, input, textarea, label {
|
|
| 388 |
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 389 |
transform: translateY(-2px);
|
| 390 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
"""
|
| 392 |
|
| 393 |
# Contextual API class - UPDATED WITH NEW MODEL ID
|
|
@@ -437,6 +595,7 @@ class ContextualAPIUtils:
|
|
| 437 |
return response_content, response_time, retrieval_text, error_message
|
| 438 |
|
| 439 |
# Contextual AI rating - UPDATED PROMPT FORMAT
|
|
|
|
| 440 |
def get_contextual_rating(contextual_api, user_input):
|
| 441 |
rating_prompt = f"Content to rate: {user_input}"
|
| 442 |
response_text, response_time, retrieval_text, error = contextual_api.chat(rating_prompt)
|
|
@@ -450,7 +609,38 @@ def get_contextual_rating(contextual_api, user_input):
|
|
| 450 |
elif "caution" in response_text.lower() or "warning" in response_text.lower():
|
| 451 |
safety_level = "warning"
|
| 452 |
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
# LlamaGuard rating
|
| 456 |
LLAMA_HARM_CATEGORIES = {
|
|
@@ -516,7 +706,6 @@ def get_openai_moderation(openai_client, user_input):
|
|
| 516 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 517 |
|
| 518 |
# Perspective API
|
| 519 |
-
|
| 520 |
def get_perspective_rating(api_key, user_input):
|
| 521 |
url = 'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'
|
| 522 |
params = {'key': api_key}
|
|
@@ -552,7 +741,6 @@ def get_perspective_rating(api_key, user_input):
|
|
| 552 |
except Exception as e:
|
| 553 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 554 |
|
| 555 |
-
|
| 556 |
# Updated to only require one input
|
| 557 |
def rate_user_input(user_input):
|
| 558 |
# Initialize APIs with hardcoded keys
|
|
@@ -566,7 +754,6 @@ def rate_user_input(user_input):
|
|
| 566 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
| 567 |
perspective_rating, perspective_safety = get_perspective_rating(PERSPECTIVE_API_KEY, user_input)
|
| 568 |
|
| 569 |
-
|
| 570 |
# Format responses carefully to avoid random line breaks
|
| 571 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
| 572 |
# Don't add line breaks to contextual rating
|
|
@@ -651,257 +838,15 @@ def create_gradio_app():
|
|
| 651 |
border_color_primary="#E0E0E0"
|
| 652 |
)
|
| 653 |
|
| 654 |
-
#
|
| 655 |
-
custom_css = CUSTOM_CSS
|
| 656 |
-
/* Policy preview popup styles */
|
| 657 |
-
.policy-popup {
|
| 658 |
-
display: none;
|
| 659 |
-
position: fixed;
|
| 660 |
-
top: 0;
|
| 661 |
-
left: 0;
|
| 662 |
-
width: 100%;
|
| 663 |
-
height: 100%;
|
| 664 |
-
background-color: rgba(0,0,0,0.7);
|
| 665 |
-
z-index: 1000;
|
| 666 |
-
justify-content: center;
|
| 667 |
-
align-items: center;
|
| 668 |
-
}
|
| 669 |
-
|
| 670 |
-
.policy-popup-content {
|
| 671 |
-
background-color: white;
|
| 672 |
-
width: 80%;
|
| 673 |
-
height: 80%;
|
| 674 |
-
border-radius: 8px;
|
| 675 |
-
padding: 20px;
|
| 676 |
-
position: relative;
|
| 677 |
-
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
| 678 |
-
display: flex;
|
| 679 |
-
flex-direction: column;
|
| 680 |
-
}
|
| 681 |
-
|
| 682 |
-
.policy-popup-header {
|
| 683 |
-
display: flex;
|
| 684 |
-
justify-content: space-between;
|
| 685 |
-
align-items: center;
|
| 686 |
-
margin-bottom: 15px;
|
| 687 |
-
border-bottom: 1px solid #eee;
|
| 688 |
-
padding-bottom: 10px;
|
| 689 |
-
}
|
| 690 |
-
|
| 691 |
-
.policy-popup-title {
|
| 692 |
-
font-weight: bold;
|
| 693 |
-
font-size: 18px;
|
| 694 |
-
}
|
| 695 |
-
|
| 696 |
-
.policy-popup-close {
|
| 697 |
-
background-color: #222222;
|
| 698 |
-
color: white;
|
| 699 |
-
border: none;
|
| 700 |
-
border-radius: 4px;
|
| 701 |
-
padding: 5px 10px;
|
| 702 |
-
cursor: pointer;
|
| 703 |
-
}
|
| 704 |
-
|
| 705 |
-
.policy-popup-close:hover {
|
| 706 |
-
background-color: #000000;
|
| 707 |
-
}
|
| 708 |
-
|
| 709 |
-
.policy-iframe-container {
|
| 710 |
-
flex: 1;
|
| 711 |
-
overflow: hidden;
|
| 712 |
-
}
|
| 713 |
-
|
| 714 |
-
.policy-iframe {
|
| 715 |
-
width: 100%;
|
| 716 |
-
height: 100%;
|
| 717 |
-
border: 1px solid #eee;
|
| 718 |
-
}
|
| 719 |
-
|
| 720 |
-
/* Fallback for when PDF can't be displayed in iframe */
|
| 721 |
-
.policy-fallback {
|
| 722 |
-
padding: 20px;
|
| 723 |
-
text-align: center;
|
| 724 |
-
}
|
| 725 |
-
|
| 726 |
-
.policy-fallback a {
|
| 727 |
-
display: inline-block;
|
| 728 |
-
margin-top: 15px;
|
| 729 |
-
padding: 10px 15px;
|
| 730 |
-
background-color: #FCA539;
|
| 731 |
-
color: #000000;
|
| 732 |
-
text-decoration: none;
|
| 733 |
-
border-radius: 4px;
|
| 734 |
-
font-weight: bold;
|
| 735 |
-
}
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
"""
|
| 739 |
|
| 740 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 741 |
# Add loading spinner
|
| 742 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 743 |
|
| 744 |
-
#
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
# Add policy popup HTML with reliable PDF loading that works on first click
|
| 748 |
-
policy_popup_html = """
|
| 749 |
-
<div id="policy-popup" class="policy-popup">
|
| 750 |
-
<div class="policy-popup-content">
|
| 751 |
-
<div class="policy-popup-header">
|
| 752 |
-
<div class="policy-popup-title">Hate Speech Policy</div>
|
| 753 |
-
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 754 |
-
</div>
|
| 755 |
-
<div class="policy-iframe-container">
|
| 756 |
-
<!-- PDF viewer iframe -->
|
| 757 |
-
<iframe class="policy-iframe" id="policy-iframe"></iframe>
|
| 758 |
-
|
| 759 |
-
<!-- Loading indicator while PDF loads -->
|
| 760 |
-
<div id="pdf-loading" style="display: flex; justify-content: center; align-items: center; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(255,255,255,0.8);">
|
| 761 |
-
<div style="text-align: center;">
|
| 762 |
-
<div style="width: 50px; height: 50px; border: 5px solid #f3f3f3; border-top: 5px solid #FCA539; border-radius: 50%; margin: 0 auto 15px auto; animation: spin 1s linear infinite;"></div>
|
| 763 |
-
<p>Loading policy document...</p>
|
| 764 |
-
</div>
|
| 765 |
-
</div>
|
| 766 |
-
<style>
|
| 767 |
-
@keyframes spin {
|
| 768 |
-
0% { transform: rotate(0deg); }
|
| 769 |
-
100% { transform: rotate(360deg); }
|
| 770 |
-
}
|
| 771 |
-
</style>
|
| 772 |
-
|
| 773 |
-
<!-- Fallback content if iframe fails -->
|
| 774 |
-
<div class="policy-fallback" id="policy-fallback" style="display:none;">
|
| 775 |
-
<p>The policy document couldn't be displayed in the preview.</p>
|
| 776 |
-
<a href="#" id="policy-download-link" target="_blank">Download Policy PDF</a>
|
| 777 |
-
<button onclick="retryPdfLoad()" style="display: block; margin-top: 15px; padding: 8px 15px; background: #FCA539; border: none; border-radius: 4px; cursor: pointer;">Try Again</button>
|
| 778 |
-
</div>
|
| 779 |
-
</div>
|
| 780 |
-
</div>
|
| 781 |
-
</div>
|
| 782 |
-
|
| 783 |
-
<script>
|
| 784 |
-
// Store the PDF URL globally once found
|
| 785 |
-
let pdfPolicyUrl = null;
|
| 786 |
-
let pdfLoadAttempted = false;
|
| 787 |
-
|
| 788 |
-
// Find the PDF URL as soon as possible
|
| 789 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 790 |
-
findPdfUrl();
|
| 791 |
-
});
|
| 792 |
-
|
| 793 |
-
// Function to find the PDF URL in various locations
|
| 794 |
-
function findPdfUrl() {
|
| 795 |
-
// Try to find the file component
|
| 796 |
-
const fileComponents = document.querySelectorAll("div.file-component");
|
| 797 |
-
for (const comp of fileComponents) {
|
| 798 |
-
const downloadBtn = comp.querySelector("a[download]");
|
| 799 |
-
if (downloadBtn && downloadBtn.href && downloadBtn.href.includes("file=")) {
|
| 800 |
-
pdfPolicyUrl = downloadBtn.href;
|
| 801 |
-
console.log("Found PDF URL:", pdfPolicyUrl);
|
| 802 |
-
return pdfPolicyUrl;
|
| 803 |
-
}
|
| 804 |
-
}
|
| 805 |
-
|
| 806 |
-
// Try to find any link with the PDF name
|
| 807 |
-
const allLinks = document.querySelectorAll("a");
|
| 808 |
-
for (const link of allLinks) {
|
| 809 |
-
if (link.href && (link.href.includes("Hate%20Speech%20Policy.pdf") ||
|
| 810 |
-
link.href.includes("file=") && link.textContent.includes("Download"))) {
|
| 811 |
-
pdfPolicyUrl = link.href;
|
| 812 |
-
console.log("Found PDF URL from links:", pdfPolicyUrl);
|
| 813 |
-
return pdfPolicyUrl;
|
| 814 |
-
}
|
| 815 |
-
}
|
| 816 |
-
|
| 817 |
-
// If we didn't find it, try again in a second
|
| 818 |
-
setTimeout(findPdfUrl, 1000);
|
| 819 |
-
return null;
|
| 820 |
-
}
|
| 821 |
-
|
| 822 |
-
// Function to actually load the PDF
|
| 823 |
-
function loadPolicyPdf(url) {
|
| 824 |
-
const iframe = document.getElementById("policy-iframe");
|
| 825 |
-
const loadingIndicator = document.getElementById("pdf-loading");
|
| 826 |
-
const fallback = document.getElementById("policy-fallback");
|
| 827 |
-
const downloadLink = document.getElementById("policy-download-link");
|
| 828 |
-
|
| 829 |
-
// Show loading, hide fallback
|
| 830 |
-
loadingIndicator.style.display = "flex";
|
| 831 |
-
fallback.style.display = "none";
|
| 832 |
-
iframe.style.display = "block";
|
| 833 |
-
|
| 834 |
-
// Set the download link
|
| 835 |
-
downloadLink.href = url;
|
| 836 |
-
|
| 837 |
-
// Set iframe source to PDF
|
| 838 |
-
iframe.src = url;
|
| 839 |
-
|
| 840 |
-
// Set a timeout to hide loading regardless of outcome
|
| 841 |
-
setTimeout(function() {
|
| 842 |
-
loadingIndicator.style.display = "none";
|
| 843 |
-
}, 3000);
|
| 844 |
-
|
| 845 |
-
// When iframe loads, hide loading indicator
|
| 846 |
-
iframe.onload = function() {
|
| 847 |
-
loadingIndicator.style.display = "none";
|
| 848 |
-
pdfLoadAttempted = true;
|
| 849 |
-
};
|
| 850 |
-
|
| 851 |
-
// Handle errors
|
| 852 |
-
iframe.onerror = function() {
|
| 853 |
-
loadingIndicator.style.display = "none";
|
| 854 |
-
fallback.style.display = "block";
|
| 855 |
-
iframe.style.display = "none";
|
| 856 |
-
pdfLoadAttempted = true;
|
| 857 |
-
};
|
| 858 |
-
}
|
| 859 |
-
|
| 860 |
-
// Function to retry loading the PDF
|
| 861 |
-
function retryPdfLoad() {
|
| 862 |
-
if (pdfPolicyUrl) {
|
| 863 |
-
loadPolicyPdf(pdfPolicyUrl);
|
| 864 |
-
} else {
|
| 865 |
-
// Try to find the URL first
|
| 866 |
-
const url = findPdfUrl();
|
| 867 |
-
if (url) {
|
| 868 |
-
loadPolicyPdf(url);
|
| 869 |
-
}
|
| 870 |
-
}
|
| 871 |
-
}
|
| 872 |
-
|
| 873 |
-
// Function to handle opening the policy popup
|
| 874 |
-
function openPolicyPopup() {
|
| 875 |
-
// Display the popup immediately
|
| 876 |
-
document.getElementById('policy-popup').style.display = 'flex';
|
| 877 |
-
|
| 878 |
-
// If we've never tried to load the PDF, or it failed previously, try loading it
|
| 879 |
-
if (!pdfLoadAttempted) {
|
| 880 |
-
if (pdfPolicyUrl) {
|
| 881 |
-
// We already have the URL, load the PDF
|
| 882 |
-
loadPolicyPdf(pdfPolicyUrl);
|
| 883 |
-
} else {
|
| 884 |
-
// Try to find the URL first
|
| 885 |
-
const url = findPdfUrl();
|
| 886 |
-
if (url) {
|
| 887 |
-
loadPolicyPdf(url);
|
| 888 |
-
} else {
|
| 889 |
-
// Show fallback if we can't find the URL
|
| 890 |
-
document.getElementById("pdf-loading").style.display = "none";
|
| 891 |
-
document.getElementById("policy-iframe").style.display = "none";
|
| 892 |
-
document.getElementById("policy-fallback").style.display = "block";
|
| 893 |
-
}
|
| 894 |
-
}
|
| 895 |
-
}
|
| 896 |
-
}
|
| 897 |
-
|
| 898 |
-
// Make functions globally available
|
| 899 |
-
window.openPolicyPopup = openPolicyPopup;
|
| 900 |
-
window.retryPdfLoad = retryPdfLoad;
|
| 901 |
-
</script>
|
| 902 |
-
"""
|
| 903 |
-
|
| 904 |
-
gr.HTML(policy_popup_html)
|
| 905 |
|
| 906 |
gr.Markdown("# Safety Oracle for Rating Hate Speech [BETA]")
|
| 907 |
gr.HTML("""
|
|
@@ -950,10 +895,10 @@ def create_gradio_app():
|
|
| 950 |
rate_btn = gr.Button("Rate Content", elem_classes=["gray-button"], scale=1)
|
| 951 |
|
| 952 |
user_input = gr.Textbox(
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
)
|
| 957 |
|
| 958 |
# 🌟 Contextual Safety Oracle
|
| 959 |
gr.HTML("""
|
|
@@ -1025,18 +970,18 @@ def create_gradio_app():
|
|
| 1025 |
|
| 1026 |
# Bind rating button with loading indicator
|
| 1027 |
rate_btn.click(
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
).then(
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
).then(
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
)
|
| 1040 |
|
| 1041 |
return app
|
| 1042 |
|
|
|
|
| 10 |
from together import Together
|
| 11 |
import openai
|
| 12 |
import json
|
| 13 |
+
from pathlib import Path
|
| 14 |
|
| 15 |
# Load environment variables (for local development)
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
+
# Google Drive Embed URL for PDF
|
| 19 |
+
PDF_EMBED_URL = "https://drive.google.com/file/d/1WZCQpIWfelbxgBr8dNiW2rqVGaDyf-Gi/preview"
|
| 20 |
+
|
| 21 |
+
# Custom PDF Viewer Modal (injected HTML)
|
| 22 |
+
PDF_MODAL_HTML = f"""
|
| 23 |
+
<div id='policy-popup' class='pdf-modal' style='display:none;'>
|
| 24 |
+
<div class='pdf-modal-content'>
|
| 25 |
+
<button class='close-btn' onclick="document.getElementById('policy-popup').style.display='none'; document.body.style.overflow='auto';">×</button>
|
| 26 |
+
<div class='pdf-container'>
|
| 27 |
+
<iframe class='pdf-embed' src='{PDF_EMBED_URL}' allow="autoplay"></iframe>
|
| 28 |
+
</div>
|
| 29 |
+
</div>
|
| 30 |
+
</div>
|
| 31 |
+
<script>
|
| 32 |
+
function openPolicyPopup() {{
|
| 33 |
+
document.getElementById('policy-popup').style.display = 'flex';
|
| 34 |
+
document.body.style.overflow = 'hidden';
|
| 35 |
+
}}
|
| 36 |
+
</script>
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
# Replace your process_retrieval_text function with this updated version
|
| 40 |
def process_retrieval_text(retrieval_text, user_input):
|
| 41 |
"""
|
| 42 |
Process the retrieval text by identifying proper document boundaries
|
|
|
|
| 67 |
content_match = re.search(r'Content:\s*(.*)', doc_info, re.DOTALL)
|
| 68 |
content = content_match.group(1).strip() if content_match else "No content available"
|
| 69 |
|
| 70 |
+
# Format with clear section headers and better spacing
|
| 71 |
formatted_html = f"""
|
| 72 |
<div class='doc-section'>
|
| 73 |
+
<h3 class="doc-number">Evidence Section {i+1}</h3>
|
| 74 |
+
|
| 75 |
+
<div class="doc-section-info">
|
| 76 |
+
<p><strong>Document Title:</strong> {doc_name}</p>
|
| 77 |
+
<div class="subsection-info">
|
| 78 |
+
<p><strong>Page Number:</strong> Page {page}</p>
|
| 79 |
+
</div>
|
| 80 |
+
</div>
|
| 81 |
+
|
| 82 |
+
<div class="doc-content-container">
|
| 83 |
+
<h4>Content:</h4>
|
| 84 |
+
<div class='doc-content'>{content}</div>
|
| 85 |
+
</div>
|
| 86 |
</div>
|
| 87 |
"""
|
| 88 |
chunks.append(formatted_html)
|
|
|
|
| 113 |
chunks.append(current_chunk)
|
| 114 |
|
| 115 |
# Format each chunk with better section styling
|
| 116 |
+
chunks = [f"""
|
| 117 |
+
<div class='doc-section'>
|
| 118 |
+
<h3 class="doc-title">Evidence Section {i+1}</h3>
|
| 119 |
+
<div class='doc-content'>{chunk.strip()}</div>
|
| 120 |
+
</div>
|
| 121 |
+
""" for i, chunk in enumerate(chunks)]
|
| 122 |
|
| 123 |
# Extract keywords from user input (longer than 3 chars)
|
| 124 |
keywords = re.findall(r'\b\w{4,}\b', user_input.lower())
|
|
|
|
| 137 |
|
| 138 |
highlighted_chunks.append(highlighted_chunk)
|
| 139 |
|
| 140 |
+
# Add some additional CSS for the knowledge sections
|
| 141 |
+
additional_css = """
|
| 142 |
+
<style>
|
| 143 |
+
.knowledge-sections {
|
| 144 |
+
border-radius: 8px;
|
| 145 |
+
background: #f9f9f9;
|
| 146 |
+
padding: 15px;
|
| 147 |
+
font-family: 'All Round Gothic Demi', 'Poppins', sans-serif !important;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
.doc-section {
|
| 151 |
+
margin-bottom: 25px;
|
| 152 |
+
padding: 15px;
|
| 153 |
+
background: white;
|
| 154 |
+
border-radius: 8px;
|
| 155 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
.doc-number, .doc-title {
|
| 159 |
+
margin-top: 0;
|
| 160 |
+
padding-bottom: 10px;
|
| 161 |
+
border-bottom: 1px solid #eee;
|
| 162 |
+
color: #222;
|
| 163 |
+
font-size: 18px;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
.doc-section-info {
|
| 167 |
+
margin: 10px 0;
|
| 168 |
+
padding: 8px;
|
| 169 |
+
background: #f5f5f5;
|
| 170 |
+
border-radius: 4px;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
.doc-section-info p {
|
| 174 |
+
margin: 5px 0;
|
| 175 |
+
font-size: 16px;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
.subsection-info {
|
| 179 |
+
margin-left: 15px;
|
| 180 |
+
padding-left: 10px;
|
| 181 |
+
border-left: 2px solid #ddd;
|
| 182 |
+
margin-top: 5px;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
.subsection-info p {
|
| 186 |
+
font-size: 14px;
|
| 187 |
+
color: #555;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
.doc-content-container {
|
| 191 |
+
margin-top: 15px;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
.doc-content-container h4 {
|
| 195 |
+
margin-bottom: 8px;
|
| 196 |
+
font-size: 16px;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
.doc-content {
|
| 200 |
+
padding: 12px;
|
| 201 |
+
background: #f9f9f9;
|
| 202 |
+
border-left: 3px solid #FCA539;
|
| 203 |
+
line-height: 1.6;
|
| 204 |
+
border-radius: 4px;
|
| 205 |
+
white-space: pre-line;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
.highlight-match {
|
| 209 |
+
background-color: #FCA539;
|
| 210 |
+
color: black;
|
| 211 |
+
font-weight: bold;
|
| 212 |
+
padding: 0 2px;
|
| 213 |
+
border-radius: 2px;
|
| 214 |
+
}
|
| 215 |
+
</style>
|
| 216 |
+
"""
|
| 217 |
+
|
| 218 |
+
return additional_css + "<div class='knowledge-sections'>" + "".join(highlighted_chunks) + "</div>"
|
| 219 |
|
| 220 |
# API Keys - hardcoded for convenience
|
| 221 |
# Replace these with your actual API keys
|
|
|
|
| 497 |
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 498 |
transform: translateY(-2px);
|
| 499 |
}
|
| 500 |
+
|
| 501 |
+
/* PDF Modal Styling */
|
| 502 |
+
.pdf-modal {
|
| 503 |
+
display: none;
|
| 504 |
+
position: fixed;
|
| 505 |
+
top: 0;
|
| 506 |
+
left: 0;
|
| 507 |
+
width: 100%;
|
| 508 |
+
height: 100%;
|
| 509 |
+
background-color: rgba(0,0,0,0.7);
|
| 510 |
+
z-index: 1000;
|
| 511 |
+
justify-content: center;
|
| 512 |
+
align-items: center;
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
.pdf-modal-content {
|
| 516 |
+
background-color: white;
|
| 517 |
+
width: 80%;
|
| 518 |
+
height: 80%;
|
| 519 |
+
border-radius: 8px;
|
| 520 |
+
padding: 20px;
|
| 521 |
+
position: relative;
|
| 522 |
+
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
.close-btn {
|
| 526 |
+
position: absolute;
|
| 527 |
+
right: 15px;
|
| 528 |
+
top: 15px;
|
| 529 |
+
font-size: 24px;
|
| 530 |
+
cursor: pointer;
|
| 531 |
+
background: #222;
|
| 532 |
+
color: white;
|
| 533 |
+
border: none;
|
| 534 |
+
border-radius: 4px;
|
| 535 |
+
padding: 5px 15px;
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
.pdf-container {
|
| 539 |
+
width: 100%;
|
| 540 |
+
height: calc(100% - 40px);
|
| 541 |
+
margin-top: 40px;
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
.pdf-embed {
|
| 545 |
+
width: 100%;
|
| 546 |
+
height: 100%;
|
| 547 |
+
border: 1px solid #eee;
|
| 548 |
+
}
|
| 549 |
"""
|
| 550 |
|
| 551 |
# Contextual API class - UPDATED WITH NEW MODEL ID
|
|
|
|
| 595 |
return response_content, response_time, retrieval_text, error_message
|
| 596 |
|
| 597 |
# Contextual AI rating - UPDATED PROMPT FORMAT
|
| 598 |
+
# Replace your get_contextual_rating function with this enhanced version
|
| 599 |
def get_contextual_rating(contextual_api, user_input):
|
| 600 |
rating_prompt = f"Content to rate: {user_input}"
|
| 601 |
response_text, response_time, retrieval_text, error = contextual_api.chat(rating_prompt)
|
|
|
|
| 609 |
elif "caution" in response_text.lower() or "warning" in response_text.lower():
|
| 610 |
safety_level = "warning"
|
| 611 |
|
| 612 |
+
# Parse specific parts of the response
|
| 613 |
+
try:
|
| 614 |
+
# Look for policy rating
|
| 615 |
+
rating_match = re.search(r'Policy Rating: (.*?)(?:Policy|$)', response_text, re.IGNORECASE)
|
| 616 |
+
policy_rating = rating_match.group(1).strip() if rating_match else ""
|
| 617 |
+
|
| 618 |
+
# Look for policy category
|
| 619 |
+
category_match = re.search(r'Policy Category: (.*?)(?:Explanation|$)', response_text, re.IGNORECASE)
|
| 620 |
+
policy_category = category_match.group(1).strip() if category_match else ""
|
| 621 |
+
|
| 622 |
+
# Look for explanation
|
| 623 |
+
explanation_match = re.search(r'Explanation: (.*)', response_text, re.IGNORECASE)
|
| 624 |
+
explanation = explanation_match.group(1).strip() if explanation_match else response_text
|
| 625 |
+
|
| 626 |
+
# Format with HTML
|
| 627 |
+
formatted_text = ""
|
| 628 |
+
if policy_rating:
|
| 629 |
+
formatted_text += f"<strong>Policy Rating:</strong> {policy_rating}<br><br>"
|
| 630 |
+
if policy_category:
|
| 631 |
+
formatted_text += f"<strong>Policy Category:</strong> {policy_category}<br><br>"
|
| 632 |
+
if explanation:
|
| 633 |
+
formatted_text += f"<strong>Explanation:</strong> {explanation}"
|
| 634 |
+
|
| 635 |
+
# If we couldn't extract anything, use the original
|
| 636 |
+
if not formatted_text:
|
| 637 |
+
formatted_text = response_text
|
| 638 |
+
|
| 639 |
+
except Exception as e:
|
| 640 |
+
print(f"Error formatting rating: {e}")
|
| 641 |
+
formatted_text = response_text
|
| 642 |
+
|
| 643 |
+
return formatted_text, retrieval_text, safety_level
|
| 644 |
|
| 645 |
# LlamaGuard rating
|
| 646 |
LLAMA_HARM_CATEGORIES = {
|
|
|
|
| 706 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 707 |
|
| 708 |
# Perspective API
|
|
|
|
| 709 |
def get_perspective_rating(api_key, user_input):
|
| 710 |
url = 'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'
|
| 711 |
params = {'key': api_key}
|
|
|
|
| 741 |
except Exception as e:
|
| 742 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 743 |
|
|
|
|
| 744 |
# Updated to only require one input
|
| 745 |
def rate_user_input(user_input):
|
| 746 |
# Initialize APIs with hardcoded keys
|
|
|
|
| 754 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
| 755 |
perspective_rating, perspective_safety = get_perspective_rating(PERSPECTIVE_API_KEY, user_input)
|
| 756 |
|
|
|
|
| 757 |
# Format responses carefully to avoid random line breaks
|
| 758 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
| 759 |
# Don't add line breaks to contextual rating
|
|
|
|
| 838 |
border_color_primary="#E0E0E0"
|
| 839 |
)
|
| 840 |
|
| 841 |
+
# Use the custom CSS with PDF modal styling
|
| 842 |
+
custom_css = CUSTOM_CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
|
| 844 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 845 |
# Add loading spinner
|
| 846 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 847 |
|
| 848 |
+
# Add the PDF modal HTML directly (defined at the top of the file)
|
| 849 |
+
gr.HTML(PDF_MODAL_HTML)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 850 |
|
| 851 |
gr.Markdown("# Safety Oracle for Rating Hate Speech [BETA]")
|
| 852 |
gr.HTML("""
|
|
|
|
| 895 |
rate_btn = gr.Button("Rate Content", elem_classes=["gray-button"], scale=1)
|
| 896 |
|
| 897 |
user_input = gr.Textbox(
|
| 898 |
+
placeholder="Type content to evaluate here...",
|
| 899 |
+
lines=6,
|
| 900 |
+
label=""
|
| 901 |
+
)
|
| 902 |
|
| 903 |
# 🌟 Contextual Safety Oracle
|
| 904 |
gr.HTML("""
|
|
|
|
| 970 |
|
| 971 |
# Bind rating button with loading indicator
|
| 972 |
rate_btn.click(
|
| 973 |
+
show_loading,
|
| 974 |
+
inputs=None,
|
| 975 |
+
outputs=loading_spinner
|
| 976 |
+
).then(
|
| 977 |
+
rate_user_input,
|
| 978 |
+
inputs=[user_input],
|
| 979 |
+
outputs=[contextual_results, llama_results, openai_results, perspective_results]
|
| 980 |
+
).then(
|
| 981 |
+
hide_loading,
|
| 982 |
+
inputs=None,
|
| 983 |
+
outputs=loading_spinner
|
| 984 |
+
)
|
| 985 |
|
| 986 |
return app
|
| 987 |
|
test/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
test/Hate Speech Policy.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0722f7379e6ebfb13fdf4595fbda155ead833f2258246f996b454e76f5b8ce39
|
| 3 |
+
size 487967
|
test/README.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: test
|
| 3 |
+
app_file: test.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 5.23.3
|
| 6 |
+
---
|
test/test.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def create_policy_popup_app():
|
| 4 |
+
# Your Google Drive file ID
|
| 5 |
+
pdf_id = "1WZCQpIWfelbxgBr8dNiW2rqVGaDyf-Gi"
|
| 6 |
+
embed_url = f"https://drive.google.com/file/d/{pdf_id}/preview"
|
| 7 |
+
|
| 8 |
+
custom_css = """
|
| 9 |
+
.pdf-modal {
|
| 10 |
+
display: none;
|
| 11 |
+
position: fixed;
|
| 12 |
+
z-index: 1000;
|
| 13 |
+
left: 0;
|
| 14 |
+
top: 0;
|
| 15 |
+
width: 100%;
|
| 16 |
+
height: 100%;
|
| 17 |
+
background-color: rgba(0,0,0,0.7);
|
| 18 |
+
overflow: auto;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
.pdf-modal-content {
|
| 22 |
+
background-color: white;
|
| 23 |
+
margin: 2% auto;
|
| 24 |
+
padding: 20px;
|
| 25 |
+
width: 90%;
|
| 26 |
+
max-width: 900px;
|
| 27 |
+
height: 90%;
|
| 28 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
|
| 29 |
+
border-radius: 8px;
|
| 30 |
+
position: relative;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
.close-btn {
|
| 34 |
+
position: absolute;
|
| 35 |
+
right: 15px;
|
| 36 |
+
top: 15px;
|
| 37 |
+
font-size: 24px;
|
| 38 |
+
cursor: pointer;
|
| 39 |
+
background: #222;
|
| 40 |
+
color: white;
|
| 41 |
+
border: none;
|
| 42 |
+
border-radius: 4px;
|
| 43 |
+
padding: 5px 15px;
|
| 44 |
+
z-index: 1001;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
.pdf-container {
|
| 48 |
+
width: 100%;
|
| 49 |
+
height: calc(100% - 40px);
|
| 50 |
+
margin-top: 40px;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
.pdf-embed {
|
| 54 |
+
width: 100%;
|
| 55 |
+
height: 100%;
|
| 56 |
+
border: 1px solid #ddd;
|
| 57 |
+
}
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
modal_html = f"""
|
| 61 |
+
<div id="pdfModal" class="pdf-modal">
|
| 62 |
+
<div class="pdf-modal-content">
|
| 63 |
+
<button class="close-btn" onclick="document.getElementById('pdfModal').style.display='none';document.body.style.overflow='auto';">×</button>
|
| 64 |
+
<div class="pdf-container">
|
| 65 |
+
<iframe class="pdf-embed" src="{embed_url}" type="application/pdf"></iframe>
|
| 66 |
+
</div>
|
| 67 |
+
</div>
|
| 68 |
+
</div>
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
with gr.Blocks(css=custom_css) as app:
|
| 72 |
+
gr.Markdown("## View Hate Speech Policy")
|
| 73 |
+
gr.HTML(modal_html)
|
| 74 |
+
|
| 75 |
+
open_button = gr.Button("📄 Open Policy Document")
|
| 76 |
+
|
| 77 |
+
open_button.click(
|
| 78 |
+
fn=lambda: None,
|
| 79 |
+
inputs=None,
|
| 80 |
+
outputs=None,
|
| 81 |
+
js="""
|
| 82 |
+
() => {
|
| 83 |
+
const modal = document.getElementById('pdfModal');
|
| 84 |
+
if (modal) {
|
| 85 |
+
modal.style.display = 'block';
|
| 86 |
+
document.body.style.overflow = 'hidden';
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
"""
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
return app
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
app = create_policy_popup_app()
|
| 96 |
+
app.launch()
|