Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +79 -255
hate_speech_demo.py
CHANGED
|
@@ -606,267 +606,92 @@ def random_test_case():
|
|
| 606 |
|
| 607 |
# Simplified Gradio app for Hate Speech Rating
|
| 608 |
def create_gradio_app():
|
| 609 |
-
# Create theme
|
| 610 |
-
theme = gr.themes.Default().set(
|
| 611 |
-
body_text_size="16px",
|
| 612 |
-
body_text_color="#333333",
|
| 613 |
-
button_primary_background_fill="#FCA539",
|
| 614 |
-
button_primary_text_color="#000000",
|
| 615 |
-
button_secondary_background_fill="#FCA539",
|
| 616 |
-
button_secondary_text_color="#000000",
|
| 617 |
-
background_fill_primary="#FFFFFF",
|
| 618 |
-
background_fill_secondary="#F8F9FA",
|
| 619 |
-
block_title_text_weight="600",
|
| 620 |
-
block_border_width="1px",
|
| 621 |
-
block_shadow="0 1px 3px rgba(0,0,0,0.1)",
|
| 622 |
-
border_color_primary="#E0E0E0"
|
| 623 |
-
)
|
| 624 |
-
|
| 625 |
-
# Add CSS for the policy popup and custom button color
|
| 626 |
-
custom_css = CUSTOM_CSS + """
|
| 627 |
-
/* Policy preview popup styles */
|
| 628 |
-
.policy-popup {
|
| 629 |
-
display: none;
|
| 630 |
-
position: fixed;
|
| 631 |
-
top: 0;
|
| 632 |
-
left: 0;
|
| 633 |
-
width: 100%;
|
| 634 |
-
height: 100%;
|
| 635 |
-
background-color: rgba(0,0,0,0.7);
|
| 636 |
-
z-index: 1000;
|
| 637 |
-
justify-content: center;
|
| 638 |
-
align-items: center;
|
| 639 |
-
}
|
| 640 |
-
|
| 641 |
-
.policy-popup-content {
|
| 642 |
-
background-color: white;
|
| 643 |
-
width: 80%;
|
| 644 |
-
height: 80%;
|
| 645 |
-
border-radius: 8px;
|
| 646 |
-
padding: 20px;
|
| 647 |
-
position: relative;
|
| 648 |
-
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
| 649 |
-
display: flex;
|
| 650 |
-
flex-direction: column;
|
| 651 |
-
}
|
| 652 |
-
|
| 653 |
-
.policy-popup-header {
|
| 654 |
-
display: flex;
|
| 655 |
-
justify-content: space-between;
|
| 656 |
-
align-items: center;
|
| 657 |
-
margin-bottom: 15px;
|
| 658 |
-
border-bottom: 1px solid #eee;
|
| 659 |
-
padding-bottom: 10px;
|
| 660 |
-
}
|
| 661 |
-
|
| 662 |
-
.policy-popup-title {
|
| 663 |
-
font-weight: bold;
|
| 664 |
-
font-size: 18px;
|
| 665 |
-
}
|
| 666 |
-
|
| 667 |
-
.policy-popup-close {
|
| 668 |
-
background-color: #222222;
|
| 669 |
-
color: white;
|
| 670 |
-
border: none;
|
| 671 |
-
border-radius: 4px;
|
| 672 |
-
padding: 5px 10px;
|
| 673 |
-
cursor: pointer;
|
| 674 |
-
}
|
| 675 |
-
|
| 676 |
-
.policy-popup-close:hover {
|
| 677 |
-
background-color: #000000;
|
| 678 |
-
}
|
| 679 |
-
|
| 680 |
-
.policy-iframe-container {
|
| 681 |
-
flex: 1;
|
| 682 |
-
overflow: hidden;
|
| 683 |
-
}
|
| 684 |
-
|
| 685 |
-
.policy-iframe {
|
| 686 |
-
width: 100%;
|
| 687 |
-
height: 100%;
|
| 688 |
-
border: 1px solid #eee;
|
| 689 |
-
}
|
| 690 |
-
|
| 691 |
-
/* Fallback for when PDF can't be displayed in iframe */
|
| 692 |
-
.policy-fallback {
|
| 693 |
-
padding: 20px;
|
| 694 |
-
text-align: center;
|
| 695 |
-
}
|
| 696 |
-
|
| 697 |
-
.policy-fallback a {
|
| 698 |
-
display: inline-block;
|
| 699 |
-
margin-top: 15px;
|
| 700 |
-
padding: 10px 15px;
|
| 701 |
-
background-color: #FCA539;
|
| 702 |
-
color: #000000;
|
| 703 |
-
text-decoration: none;
|
| 704 |
-
border-radius: 4px;
|
| 705 |
-
font-weight: bold;
|
| 706 |
-
}
|
| 707 |
-
|
| 708 |
-
/* Custom gray button style */
|
| 709 |
-
.gray-button {
|
| 710 |
-
background-color: #c4c4c3 !important;
|
| 711 |
-
color: #000000 !important;
|
| 712 |
-
}
|
| 713 |
-
"""
|
| 714 |
|
| 715 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 716 |
-
#
|
| 717 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 718 |
|
| 719 |
# Create a file component to serve the PDF (hidden from UI)
|
| 720 |
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 721 |
|
| 722 |
-
# Add policy popup HTML
|
| 723 |
-
|
| 724 |
-
<div id="policy-popup" class="policy-popup">
|
| 725 |
-
<div class="policy-popup-content">
|
| 726 |
-
<div class="policy-popup-header">
|
| 727 |
-
<div class="policy-popup-title">Hate Speech Policy</div>
|
| 728 |
-
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 729 |
-
</div>
|
| 730 |
-
<div class="policy-iframe-container">
|
| 731 |
-
<!-- Primary method: Try Google PDF Viewer -->
|
| 732 |
-
<iframe class="policy-iframe" id="policy-iframe"></iframe>
|
| 733 |
-
|
| 734 |
-
<!-- Fallback content if iframe fails -->
|
| 735 |
-
<div class="policy-fallback" id="policy-fallback" style="display:none;">
|
| 736 |
-
<p>The policy document couldn't be displayed in the preview.</p>
|
| 737 |
-
<a href="#" id="policy-download-link" target="_blank">Download Policy PDF</a>
|
| 738 |
-
</div>
|
| 739 |
-
</div>
|
| 740 |
-
</div>
|
| 741 |
-
</div>
|
| 742 |
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
const googleViewerUrl = "https://docs.google.com/viewer?embedded=true&url=";
|
| 752 |
-
|
| 753 |
-
// 2. Direct link as fallback
|
| 754 |
-
let directPdfUrl = "";
|
| 755 |
-
|
| 756 |
-
// Find the PDF link by looking for file links in the DOM
|
| 757 |
-
const links = document.querySelectorAll("a");
|
| 758 |
-
for (const link of links) {
|
| 759 |
-
if (link.href && link.href.includes(encodeURIComponent(pdfFileName))) {
|
| 760 |
-
directPdfUrl = link.href;
|
| 761 |
-
break;
|
| 762 |
-
}
|
| 763 |
-
}
|
| 764 |
-
|
| 765 |
-
// Set the iframe source if we found a link
|
| 766 |
-
const iframe = document.getElementById("policy-iframe");
|
| 767 |
-
const fallback = document.getElementById("policy-fallback");
|
| 768 |
-
const downloadLink = document.getElementById("policy-download-link");
|
| 769 |
-
|
| 770 |
-
if (directPdfUrl) {
|
| 771 |
-
// Try Google Viewer first
|
| 772 |
-
iframe.src = googleViewerUrl + encodeURIComponent(directPdfUrl);
|
| 773 |
-
iframe.style.display = "block";
|
| 774 |
-
fallback.style.display = "none";
|
| 775 |
|
| 776 |
-
|
| 777 |
-
|
| 778 |
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
iframe.style.display = "none";
|
| 782 |
-
fallback.style.display = "block";
|
| 783 |
-
};
|
| 784 |
-
} else {
|
| 785 |
-
// No direct URL found, show fallback
|
| 786 |
-
iframe.style.display = "none";
|
| 787 |
-
fallback.style.display = "block";
|
| 788 |
-
downloadLink.href = "#";
|
| 789 |
-
downloadLink.textContent = "PDF not available";
|
| 790 |
-
}
|
| 791 |
-
|
| 792 |
-
// Display the popup
|
| 793 |
-
document.getElementById('policy-popup').style.display = 'flex';
|
| 794 |
-
}
|
| 795 |
-
</script>
|
| 796 |
-
"""
|
| 797 |
-
|
| 798 |
-
gr.HTML(policy_popup_html)
|
| 799 |
|
| 800 |
-
|
| 801 |
-
gr.
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
"Reach out to Bertie Vidgen, Head of Human Data at Contextual AI, to find out more or sign up as a design partner at bertie@contextual.ai \n"
|
| 814 |
-
"## <span class='safety-warning-red'>SAFETY WARNING</span> \n"
|
| 815 |
-
"Some of the randomly generated test cases contain hateful language that you might find offensive or upsetting."
|
| 816 |
-
)
|
| 817 |
|
| 818 |
with gr.Row():
|
| 819 |
with gr.Column(scale=1, elem_classes=["model-column"]):
|
| 820 |
-
|
| 821 |
gr.HTML("""
|
| 822 |
<div>
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
|
| 856 |
-
#
|
| 857 |
-
|
| 858 |
-
return """<script>
|
| 859 |
-
const spinner = document.getElementById('loading-spinner');
|
| 860 |
-
if (spinner) spinner.style.display = 'block';
|
| 861 |
-
</script>"""
|
| 862 |
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
const spinner = document.getElementById('loading-spinner');
|
| 866 |
-
if (spinner) spinner.style.display = 'none';
|
| 867 |
-
</script>"""
|
| 868 |
|
| 869 |
-
# Bind
|
| 870 |
random_test_btn.click(
|
| 871 |
show_loading,
|
| 872 |
inputs=None,
|
|
@@ -881,20 +706,19 @@ def create_gradio_app():
|
|
| 881 |
outputs=loading_spinner
|
| 882 |
)
|
| 883 |
|
| 884 |
-
# Bind rating button with loading indicator
|
| 885 |
rate_btn.click(
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
).then(
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
).then(
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
)
|
| 898 |
|
| 899 |
return app
|
| 900 |
|
|
|
|
| 606 |
|
| 607 |
# Simplified Gradio app for Hate Speech Rating
|
| 608 |
def create_gradio_app():
|
| 609 |
+
# Create theme and basic setup as before...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
|
| 611 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 612 |
+
# First, define all UI elements
|
| 613 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 614 |
|
| 615 |
# Create a file component to serve the PDF (hidden from UI)
|
| 616 |
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 617 |
|
| 618 |
+
# Add policy popup HTML
|
| 619 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
|
| 621 |
+
gr.Markdown("# Hate Speech Rating Oracle [BETA]")
|
| 622 |
+
# Markdown content...
|
| 623 |
+
|
| 624 |
+
# Define input elements first
|
| 625 |
+
with gr.Row():
|
| 626 |
+
with gr.Column(scale=1):
|
| 627 |
+
# Random test case button at the top
|
| 628 |
+
random_test_btn = gr.Button("🎲 Random Test Case", elem_classes=["orange-button"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
|
| 630 |
+
# Rate Content button
|
| 631 |
+
rate_btn = gr.Button("Rate Content", variant="primary", size="lg", elem_classes=["gray-button"])
|
| 632 |
|
| 633 |
+
# Input field below both buttons
|
| 634 |
+
user_input = gr.Textbox(label="Input content to rate:", placeholder="Type content to evaluate here...", lines=6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
|
| 636 |
+
# Define output elements
|
| 637 |
+
with gr.Row():
|
| 638 |
+
with gr.Column(scale=1, elem_classes=["model-column"]):
|
| 639 |
+
# Contextual AI section
|
| 640 |
+
gr.HTML("""
|
| 641 |
+
<div>
|
| 642 |
+
<h3 class="result-header">🌟 Contextual Safety Oracle</h3>
|
| 643 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 644 |
+
<a href="#" class="knowledge-button" onclick="openPolicyPopup(); return false;">View policy</a>
|
| 645 |
+
</div>
|
| 646 |
+
</div>
|
| 647 |
+
""")
|
| 648 |
+
contextual_results = gr.HTML('<div class="rating-box contextual-box empty-rating">Rating will appear here</div>')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
with gr.Row():
|
| 651 |
with gr.Column(scale=1, elem_classes=["model-column"]):
|
| 652 |
+
# LlamaGuard section
|
| 653 |
gr.HTML("""
|
| 654 |
<div>
|
| 655 |
+
<h3 class="result-header">🦙 LlamaGuard 3.0</h3>
|
| 656 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 657 |
+
<a href="https://github.com/meta-llama/PurpleLlama/blob/main/Llama-Guard3/8B/MODEL_CARD.md"
|
| 658 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 659 |
+
</div>
|
| 660 |
+
</div>
|
| 661 |
+
""")
|
| 662 |
+
llama_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 663 |
+
with gr.Column(scale=1, elem_classes=["model-column"]):
|
| 664 |
+
# OpenAI section
|
| 665 |
+
gr.HTML("""
|
| 666 |
+
<div>
|
| 667 |
+
<h3 class="result-header">🧷 OpenAI Moderation</h3>
|
| 668 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 669 |
+
<a href="https://platform.openai.com/docs/guides/moderation"
|
| 670 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 671 |
+
</div>
|
| 672 |
+
</div>
|
| 673 |
+
""")
|
| 674 |
+
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 675 |
+
with gr.Column(scale=1, elem_classes=["model-column"]):
|
| 676 |
+
# Perspective API section
|
| 677 |
+
gr.HTML("""
|
| 678 |
+
<div>
|
| 679 |
+
<h3 class="result-header">👁️ Perspective API</h3>
|
| 680 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 681 |
+
<a href="https://developers.perspectiveapi.com/s/about-the-api"
|
| 682 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 683 |
+
</div>
|
| 684 |
+
</div>
|
| 685 |
+
""")
|
| 686 |
+
perspective_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 687 |
|
| 688 |
+
# Hidden element for retrieved knowledge
|
| 689 |
+
retrieved_knowledge = gr.HTML('', visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
|
| 691 |
+
# After defining all UI elements, set up the event handlers
|
| 692 |
+
# Define show/hide loading functions first...
|
|
|
|
|
|
|
|
|
|
| 693 |
|
| 694 |
+
# Bind the buttons
|
| 695 |
random_test_btn.click(
|
| 696 |
show_loading,
|
| 697 |
inputs=None,
|
|
|
|
| 706 |
outputs=loading_spinner
|
| 707 |
)
|
| 708 |
|
|
|
|
| 709 |
rate_btn.click(
|
| 710 |
+
show_loading,
|
| 711 |
+
inputs=None,
|
| 712 |
+
outputs=loading_spinner
|
| 713 |
+
).then(
|
| 714 |
+
rate_user_input,
|
| 715 |
+
inputs=[user_input],
|
| 716 |
+
outputs=[contextual_results, llama_results, openai_results, perspective_results, retrieved_knowledge]
|
| 717 |
+
).then(
|
| 718 |
+
hide_loading,
|
| 719 |
+
inputs=None,
|
| 720 |
+
outputs=loading_spinner
|
| 721 |
+
)
|
| 722 |
|
| 723 |
return app
|
| 724 |
|