Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +143 -46
hate_speech_demo.py
CHANGED
|
@@ -743,7 +743,7 @@ def create_gradio_app():
|
|
| 743 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 744 |
|
| 745 |
# Create a file component to serve the PDF (hidden from UI)
|
| 746 |
-
pdf_file = gr.File("Hate Speech Policy.pdf", visible=True, label="Policy
|
| 747 |
|
| 748 |
# Add policy popup HTML with reliable PDF loading that works on first click
|
| 749 |
# Replace the problematic policy_popup_html with this fixed version
|
|
@@ -756,34 +756,41 @@ def create_gradio_app():
|
|
| 756 |
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 757 |
</div>
|
| 758 |
<div class="policy-iframe-container">
|
| 759 |
-
<!--
|
| 760 |
-
<div id="pdf-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 761 |
<img src="https://img.icons8.com/color/96/000000/pdf.png" style="width: 96px; height: 96px; margin-bottom: 20px;">
|
| 762 |
-
<h3>
|
| 763 |
<p style="text-align: center; max-width: 80%; margin: 15px auto;">
|
| 764 |
-
|
| 765 |
</p>
|
| 766 |
<a id="policy-download-link" href="#" class="evidence-button" target="_blank" style="margin-top: 20px;">
|
| 767 |
Download Policy PDF
|
| 768 |
</a>
|
| 769 |
-
<
|
| 770 |
-
|
| 771 |
-
</
|
| 772 |
</div>
|
| 773 |
|
| 774 |
-
<!-- Loading indicator (only shown briefly) -->
|
| 775 |
-
<div id="pdf-loading" style="display: none; justify-content: center; align-items: center; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(255,255,255,0.8);">
|
| 776 |
-
<div style="text-align: center;">
|
| 777 |
-
<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>
|
| 778 |
-
<p>Finding document...</p>
|
| 779 |
-
</div>
|
| 780 |
-
</div>
|
| 781 |
<style>
|
| 782 |
@keyframes spin {
|
| 783 |
0% { transform: rotate(0deg); }
|
| 784 |
100% { transform: rotate(360deg); }
|
| 785 |
}
|
| 786 |
-
.evidence-button {
|
| 787 |
background: #FCA539 !important;
|
| 788 |
color: #000000 !important;
|
| 789 |
font-weight: bold;
|
|
@@ -797,22 +804,28 @@ def create_gradio_app():
|
|
| 797 |
text-decoration: none !important;
|
| 798 |
margin-top: 10px;
|
| 799 |
margin-bottom: 5px;
|
|
|
|
| 800 |
}
|
| 801 |
-
.evidence-button:hover {
|
| 802 |
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 803 |
transform: translateY(-2px);
|
| 804 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 805 |
</style>
|
| 806 |
</div>
|
| 807 |
</div>
|
| 808 |
</div>
|
| 809 |
|
| 810 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
// Function to find PDF URL from various Gradio components
|
| 812 |
function findPdfUrl() {
|
| 813 |
-
// Display loading indicator
|
| 814 |
-
document.getElementById('pdf-loading').style.display = 'flex';
|
| 815 |
-
|
| 816 |
// Method 1: Try to find the PDF URL from file components
|
| 817 |
const fileComponents = document.querySelectorAll(".file-component");
|
| 818 |
for (const comp of fileComponents) {
|
|
@@ -820,7 +833,6 @@ function findPdfUrl() {
|
|
| 820 |
if (downloadBtn && downloadBtn.href &&
|
| 821 |
(downloadBtn.href.includes("Hate Speech Policy.pdf") ||
|
| 822 |
downloadBtn.href.includes("Hate%20Speech%20Policy.pdf"))) {
|
| 823 |
-
document.getElementById('pdf-loading').style.display = 'none';
|
| 824 |
return downloadBtn.href;
|
| 825 |
}
|
| 826 |
}
|
|
@@ -832,19 +844,21 @@ function findPdfUrl() {
|
|
| 832 |
(link.href.includes("Hate Speech Policy.pdf") ||
|
| 833 |
link.href.includes("Hate%20Speech%20Policy.pdf") ||
|
| 834 |
(link.href.includes("file=") && link.download))) {
|
| 835 |
-
document.getElementById('pdf-loading').style.display = 'none';
|
| 836 |
return link.href;
|
| 837 |
}
|
| 838 |
}
|
| 839 |
|
| 840 |
-
// Method 3:
|
| 841 |
-
const
|
| 842 |
-
for (const
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
|
|
|
|
|
|
|
|
|
| 848 |
}
|
| 849 |
}
|
| 850 |
}
|
|
@@ -857,42 +871,125 @@ function findPdfUrl() {
|
|
| 857 |
"file/Hate%20Speech%20Policy.pdf"
|
| 858 |
];
|
| 859 |
|
| 860 |
-
// Method 5: If not found, hide loading and use a fallback message
|
| 861 |
-
document.getElementById('pdf-loading').style.display = 'none';
|
| 862 |
-
console.log("PDF URL not found - using fallback path");
|
| 863 |
-
|
| 864 |
// Return a default path that might work
|
| 865 |
return paths[0];
|
| 866 |
}
|
| 867 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
// Function to open the policy popup
|
| 869 |
function openPolicyPopup() {
|
| 870 |
// Display the popup
|
| 871 |
document.getElementById('policy-popup').style.display = 'flex';
|
| 872 |
|
| 873 |
-
//
|
| 874 |
-
|
|
|
|
|
|
|
| 875 |
|
| 876 |
-
//
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 880 |
}
|
| 881 |
}
|
| 882 |
|
| 883 |
// Make functions globally available
|
| 884 |
window.openPolicyPopup = openPolicyPopup;
|
|
|
|
| 885 |
|
| 886 |
// Initialize when the document is loaded
|
| 887 |
document.addEventListener('DOMContentLoaded', function() {
|
| 888 |
// Find the PDF URL in advance
|
| 889 |
-
|
| 890 |
|
| 891 |
-
//
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
}
|
| 896 |
});
|
| 897 |
</script>
|
| 898 |
"""
|
|
|
|
| 743 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 744 |
|
| 745 |
# Create a file component to serve the PDF (hidden from UI)
|
| 746 |
+
pdf_file = gr.File("Hate Speech Policy.pdf", visible=True, label="Hate Speech Policy Document")
|
| 747 |
|
| 748 |
# Add policy popup HTML with reliable PDF loading that works on first click
|
| 749 |
# Replace the problematic policy_popup_html with this fixed version
|
|
|
|
| 756 |
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 757 |
</div>
|
| 758 |
<div class="policy-iframe-container">
|
| 759 |
+
<!-- PDF.js viewer setup -->
|
| 760 |
+
<div id="pdf-viewer" style="width: 100%; height: 100%; display: flex; flex-direction: column;">
|
| 761 |
+
<!-- We'll load PDF.js and then render the PDF here -->
|
| 762 |
+
<iframe id="pdf-js-viewer" style="flex: 1; border: none; width: 100%; height: 100%;"></iframe>
|
| 763 |
+
</div>
|
| 764 |
+
|
| 765 |
+
<!-- Loading indicator -->
|
| 766 |
+
<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);">
|
| 767 |
+
<div style="text-align: center;">
|
| 768 |
+
<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>
|
| 769 |
+
<p>Loading document...</p>
|
| 770 |
+
</div>
|
| 771 |
+
</div>
|
| 772 |
+
|
| 773 |
+
<!-- Fallback for when viewer fails -->
|
| 774 |
+
<div id="pdf-fallback" style="display: none; flex-direction: column; align-items: center; justify-content: center; height: 100%;">
|
| 775 |
<img src="https://img.icons8.com/color/96/000000/pdf.png" style="width: 96px; height: 96px; margin-bottom: 20px;">
|
| 776 |
+
<h3>PDF Viewer Unavailable</h3>
|
| 777 |
<p style="text-align: center; max-width: 80%; margin: 15px auto;">
|
| 778 |
+
We couldn't display the PDF directly. Please download it using the button below.
|
| 779 |
</p>
|
| 780 |
<a id="policy-download-link" href="#" class="evidence-button" target="_blank" style="margin-top: 20px;">
|
| 781 |
Download Policy PDF
|
| 782 |
</a>
|
| 783 |
+
<button onclick="retryPdfLoad()" class="retry-button" style="margin-top: 15px;">
|
| 784 |
+
Try Again
|
| 785 |
+
</button>
|
| 786 |
</div>
|
| 787 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 788 |
<style>
|
| 789 |
@keyframes spin {
|
| 790 |
0% { transform: rotate(0deg); }
|
| 791 |
100% { transform: rotate(360deg); }
|
| 792 |
}
|
| 793 |
+
.evidence-button, .retry-button {
|
| 794 |
background: #FCA539 !important;
|
| 795 |
color: #000000 !important;
|
| 796 |
font-weight: bold;
|
|
|
|
| 804 |
text-decoration: none !important;
|
| 805 |
margin-top: 10px;
|
| 806 |
margin-bottom: 5px;
|
| 807 |
+
border: none;
|
| 808 |
}
|
| 809 |
+
.evidence-button:hover, .retry-button:hover {
|
| 810 |
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 811 |
transform: translateY(-2px);
|
| 812 |
}
|
| 813 |
+
.retry-button {
|
| 814 |
+
background: #666 !important;
|
| 815 |
+
color: white !important;
|
| 816 |
+
}
|
| 817 |
</style>
|
| 818 |
</div>
|
| 819 |
</div>
|
| 820 |
</div>
|
| 821 |
|
| 822 |
<script>
|
| 823 |
+
// We need to load PDF.js dynamically
|
| 824 |
+
let pdfViewerInitialized = false;
|
| 825 |
+
let pdfUrl = null;
|
| 826 |
+
|
| 827 |
// Function to find PDF URL from various Gradio components
|
| 828 |
function findPdfUrl() {
|
|
|
|
|
|
|
|
|
|
| 829 |
// Method 1: Try to find the PDF URL from file components
|
| 830 |
const fileComponents = document.querySelectorAll(".file-component");
|
| 831 |
for (const comp of fileComponents) {
|
|
|
|
| 833 |
if (downloadBtn && downloadBtn.href &&
|
| 834 |
(downloadBtn.href.includes("Hate Speech Policy.pdf") ||
|
| 835 |
downloadBtn.href.includes("Hate%20Speech%20Policy.pdf"))) {
|
|
|
|
| 836 |
return downloadBtn.href;
|
| 837 |
}
|
| 838 |
}
|
|
|
|
| 844 |
(link.href.includes("Hate Speech Policy.pdf") ||
|
| 845 |
link.href.includes("Hate%20Speech%20Policy.pdf") ||
|
| 846 |
(link.href.includes("file=") && link.download))) {
|
|
|
|
| 847 |
return link.href;
|
| 848 |
}
|
| 849 |
}
|
| 850 |
|
| 851 |
+
// Method 3: Look for file inputs or elements
|
| 852 |
+
const fileInputs = document.querySelectorAll("input[type='file']");
|
| 853 |
+
for (const input of fileInputs) {
|
| 854 |
+
if (input.value && input.value.includes("Hate Speech Policy.pdf")) {
|
| 855 |
+
// This means we found the file input, but need to find its download URL
|
| 856 |
+
const parentContainer = input.closest("div[class*='component']");
|
| 857 |
+
if (parentContainer) {
|
| 858 |
+
const downloadLinks = parentContainer.querySelectorAll("a[download]");
|
| 859 |
+
if (downloadLinks.length > 0) {
|
| 860 |
+
return downloadLinks[0].href;
|
| 861 |
+
}
|
| 862 |
}
|
| 863 |
}
|
| 864 |
}
|
|
|
|
| 871 |
"file/Hate%20Speech%20Policy.pdf"
|
| 872 |
];
|
| 873 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 874 |
// Return a default path that might work
|
| 875 |
return paths[0];
|
| 876 |
}
|
| 877 |
|
| 878 |
+
// Load PDF.js library
|
| 879 |
+
function loadPdfJsLibrary() {
|
| 880 |
+
if (typeof pdfjsLib !== 'undefined') {
|
| 881 |
+
return Promise.resolve(); // Already loaded
|
| 882 |
+
}
|
| 883 |
+
|
| 884 |
+
return new Promise((resolve, reject) => {
|
| 885 |
+
// Load PDF.js from CDN
|
| 886 |
+
const pdfJsScript = document.createElement('script');
|
| 887 |
+
pdfJsScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js';
|
| 888 |
+
pdfJsScript.onload = resolve;
|
| 889 |
+
pdfJsScript.onerror = reject;
|
| 890 |
+
document.head.appendChild(pdfJsScript);
|
| 891 |
+
});
|
| 892 |
+
}
|
| 893 |
+
|
| 894 |
+
// Display the PDF using PDF.js viewer
|
| 895 |
+
function displayPdfUsingViewer(pdfUrl) {
|
| 896 |
+
// Get the iframe element
|
| 897 |
+
const viewerIframe = document.getElementById('pdf-js-viewer');
|
| 898 |
+
|
| 899 |
+
// Mozilla's PDF.js viewer URL
|
| 900 |
+
const viewerUrl = 'https://mozilla.github.io/pdf.js/web/viewer.html';
|
| 901 |
+
|
| 902 |
+
// Set the iframe source to the PDF.js viewer with our PDF URL as a parameter
|
| 903 |
+
viewerIframe.src = `${viewerUrl}?file=${encodeURIComponent(pdfUrl)}`;
|
| 904 |
+
|
| 905 |
+
// Hide loading indicator once iframe has loaded
|
| 906 |
+
viewerIframe.onload = function() {
|
| 907 |
+
document.getElementById('pdf-loading').style.display = 'none';
|
| 908 |
+
document.getElementById('pdf-viewer').style.display = 'flex';
|
| 909 |
+
};
|
| 910 |
+
|
| 911 |
+
// Show fallback if viewer fails to load after a timeout
|
| 912 |
+
setTimeout(function() {
|
| 913 |
+
if (document.getElementById('pdf-loading').style.display === 'flex') {
|
| 914 |
+
showFallback();
|
| 915 |
+
}
|
| 916 |
+
}, 8000); // 8 second timeout
|
| 917 |
+
}
|
| 918 |
+
|
| 919 |
+
// Show fallback download option
|
| 920 |
+
function showFallback() {
|
| 921 |
+
document.getElementById('pdf-loading').style.display = 'none';
|
| 922 |
+
document.getElementById('pdf-viewer').style.display = 'none';
|
| 923 |
+
document.getElementById('pdf-fallback').style.display = 'flex';
|
| 924 |
+
|
| 925 |
+
// Set download link
|
| 926 |
+
const downloadLink = document.getElementById('policy-download-link');
|
| 927 |
+
if (downloadLink && pdfUrl) {
|
| 928 |
+
downloadLink.href = pdfUrl;
|
| 929 |
+
}
|
| 930 |
+
}
|
| 931 |
+
|
| 932 |
+
// Function to retry loading the PDF
|
| 933 |
+
function retryPdfLoad() {
|
| 934 |
+
// Show loading, hide fallback
|
| 935 |
+
document.getElementById('pdf-loading').style.display = 'flex';
|
| 936 |
+
document.getElementById('pdf-fallback').style.display = 'none';
|
| 937 |
+
|
| 938 |
+
// Find the URL again (in case it changed)
|
| 939 |
+
pdfUrl = findPdfUrl();
|
| 940 |
+
|
| 941 |
+
// Try displaying the PDF
|
| 942 |
+
displayPdfUsingViewer(pdfUrl);
|
| 943 |
+
}
|
| 944 |
+
|
| 945 |
// Function to open the policy popup
|
| 946 |
function openPolicyPopup() {
|
| 947 |
// Display the popup
|
| 948 |
document.getElementById('policy-popup').style.display = 'flex';
|
| 949 |
|
| 950 |
+
// Show loading indicator
|
| 951 |
+
document.getElementById('pdf-loading').style.display = 'flex';
|
| 952 |
+
document.getElementById('pdf-viewer').style.display = 'none';
|
| 953 |
+
document.getElementById('pdf-fallback').style.display = 'none';
|
| 954 |
|
| 955 |
+
// If we've already found the PDF URL
|
| 956 |
+
if (pdfUrl) {
|
| 957 |
+
displayPdfUsingViewer(pdfUrl);
|
| 958 |
+
} else {
|
| 959 |
+
// Find the PDF URL
|
| 960 |
+
pdfUrl = findPdfUrl();
|
| 961 |
+
|
| 962 |
+
// Set the download link for fallback
|
| 963 |
+
const downloadLink = document.getElementById('policy-download-link');
|
| 964 |
+
if (downloadLink) {
|
| 965 |
+
downloadLink.href = pdfUrl;
|
| 966 |
+
}
|
| 967 |
+
|
| 968 |
+
// Initialize PDF.js and display the PDF
|
| 969 |
+
loadPdfJsLibrary()
|
| 970 |
+
.then(() => {
|
| 971 |
+
displayPdfUsingViewer(pdfUrl);
|
| 972 |
+
})
|
| 973 |
+
.catch(error => {
|
| 974 |
+
console.error('Error loading PDF.js:', error);
|
| 975 |
+
showFallback();
|
| 976 |
+
});
|
| 977 |
}
|
| 978 |
}
|
| 979 |
|
| 980 |
// Make functions globally available
|
| 981 |
window.openPolicyPopup = openPolicyPopup;
|
| 982 |
+
window.retryPdfLoad = retryPdfLoad;
|
| 983 |
|
| 984 |
// Initialize when the document is loaded
|
| 985 |
document.addEventListener('DOMContentLoaded', function() {
|
| 986 |
// Find the PDF URL in advance
|
| 987 |
+
pdfUrl = findPdfUrl();
|
| 988 |
|
| 989 |
+
// Load PDF.js in advance
|
| 990 |
+
loadPdfJsLibrary().catch(err => {
|
| 991 |
+
console.warn('PDF.js preload failed:', err);
|
| 992 |
+
});
|
|
|
|
| 993 |
});
|
| 994 |
</script>
|
| 995 |
"""
|