Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +75 -62
hate_speech_demo.py
CHANGED
|
@@ -741,9 +741,20 @@ def create_gradio_app():
|
|
| 741 |
# Add loading spinner
|
| 742 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 743 |
|
| 744 |
-
#
|
| 745 |
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 746 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
# Add policy popup HTML with robust PDF loading - ALWAYS LOADS FIRST TIME
|
| 748 |
policy_popup_html = """
|
| 749 |
<div id="policy-popup" class="policy-popup">
|
|
@@ -780,48 +791,47 @@ def create_gradio_app():
|
|
| 780 |
</div>
|
| 781 |
|
| 782 |
<script>
|
| 783 |
-
//
|
| 784 |
document.addEventListener('DOMContentLoaded', function() {
|
| 785 |
-
//
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
// First try to get from the file component
|
| 790 |
-
for (const comp of fileComponents) {
|
| 791 |
-
const downloadBtn = comp.querySelector("a");
|
| 792 |
-
if (downloadBtn && downloadBtn.href && downloadBtn.href.includes("file=")) {
|
| 793 |
-
pdfUrl = downloadBtn.href;
|
| 794 |
-
break;
|
| 795 |
-
}
|
| 796 |
-
}
|
| 797 |
-
|
| 798 |
-
// If no file component found, try to find any PDF link
|
| 799 |
-
if (!pdfUrl) {
|
| 800 |
-
const allLinks = document.querySelectorAll("a");
|
| 801 |
-
for (const link of allLinks) {
|
| 802 |
-
if (link.href && link.href.includes("Hate%20Speech%20Policy.pdf")) {
|
| 803 |
-
pdfUrl = link.href;
|
| 804 |
-
break;
|
| 805 |
-
}
|
| 806 |
-
}
|
| 807 |
-
}
|
| 808 |
-
|
| 809 |
-
// If we found a PDF link, preload it in a hidden iframe
|
| 810 |
-
if (pdfUrl) {
|
| 811 |
-
// Create hidden preloader
|
| 812 |
-
const preloader = document.createElement('iframe');
|
| 813 |
-
preloader.style.display = 'none';
|
| 814 |
-
preloader.src = pdfUrl;
|
| 815 |
-
document.body.appendChild(preloader);
|
| 816 |
|
| 817 |
-
//
|
| 818 |
-
|
| 819 |
|
| 820 |
-
|
| 821 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
});
|
| 823 |
|
| 824 |
-
// Function to handle opening the policy popup -
|
| 825 |
function openPolicyPopup() {
|
| 826 |
// Display the popup immediately
|
| 827 |
const popup = document.getElementById('policy-popup');
|
|
@@ -838,31 +848,39 @@ def create_gradio_app():
|
|
| 838 |
iframe.style.display = "block";
|
| 839 |
fallback.style.display = "none";
|
| 840 |
|
| 841 |
-
//
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
return;
|
| 845 |
-
}
|
| 846 |
|
| 847 |
-
//
|
| 848 |
-
|
|
|
|
|
|
|
| 849 |
|
| 850 |
-
//
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
const
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 857 |
}
|
| 858 |
}
|
| 859 |
|
| 860 |
-
// If
|
| 861 |
if (!pdfUrl) {
|
| 862 |
const allLinks = document.querySelectorAll("a");
|
| 863 |
for (const link of allLinks) {
|
| 864 |
if (link.href && link.href.includes("Hate%20Speech%20Policy.pdf")) {
|
| 865 |
pdfUrl = link.href;
|
|
|
|
| 866 |
break;
|
| 867 |
}
|
| 868 |
}
|
|
@@ -870,10 +888,9 @@ def create_gradio_app():
|
|
| 870 |
|
| 871 |
// If we found a URL, load it
|
| 872 |
if (pdfUrl) {
|
| 873 |
-
window.pdfPolicyUrl = pdfUrl; // Cache for future use
|
| 874 |
loadPdf(pdfUrl);
|
| 875 |
} else {
|
| 876 |
-
//
|
| 877 |
loadingIndicator.style.display = "none";
|
| 878 |
iframe.style.display = "none";
|
| 879 |
fallback.style.display = "block";
|
|
@@ -892,18 +909,14 @@ def create_gradio_app():
|
|
| 892 |
// Set download link in case user wants to download
|
| 893 |
downloadLink.href = pdfUrl;
|
| 894 |
|
| 895 |
-
// Load PDF into iframe
|
| 896 |
iframe.src = pdfUrl;
|
| 897 |
|
| 898 |
// Check if iframe loaded successfully
|
| 899 |
let loadTimeout = setTimeout(function() {
|
| 900 |
-
// If still loading after
|
| 901 |
loadingIndicator.style.display = "none";
|
| 902 |
-
|
| 903 |
-
// Try Google Viewer as a last resort
|
| 904 |
-
const googleViewerUrl = "https://docs.google.com/viewer?embedded=true&url=";
|
| 905 |
-
iframe.src = googleViewerUrl + encodeURIComponent(pdfUrl);
|
| 906 |
-
}, 5000);
|
| 907 |
|
| 908 |
// When iframe loads, hide loading indicator
|
| 909 |
iframe.onload = function() {
|
|
|
|
| 741 |
# Add loading spinner
|
| 742 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 743 |
|
| 744 |
+
# Add a hidden file component to serve the PDF
|
| 745 |
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 746 |
|
| 747 |
+
# Add a "file preview" component that can be accessed
|
| 748 |
+
gr.HTML("""
|
| 749 |
+
<div id="pdf-container" style="display: none;">
|
| 750 |
+
<h3>Hate Speech Policy Document</h3>
|
| 751 |
+
<div class="file-preview">
|
| 752 |
+
<div class="file-name">Hate Speech Policy.pdf</div>
|
| 753 |
+
<a href="file=Hate Speech Policy.pdf" download="Hate Speech Policy.pdf" id="pdf-direct-link">Download</a>
|
| 754 |
+
</div>
|
| 755 |
+
</div>
|
| 756 |
+
""")
|
| 757 |
+
|
| 758 |
# Add policy popup HTML with robust PDF loading - ALWAYS LOADS FIRST TIME
|
| 759 |
policy_popup_html = """
|
| 760 |
<div id="policy-popup" class="policy-popup">
|
|
|
|
| 791 |
</div>
|
| 792 |
|
| 793 |
<script>
|
| 794 |
+
// DIRECTLY ACCESS THE PDF FILE COMPONENT
|
| 795 |
document.addEventListener('DOMContentLoaded', function() {
|
| 796 |
+
// Get references to all file components
|
| 797 |
+
setTimeout(function() {
|
| 798 |
+
const policyButtons = document.querySelectorAll('a[onclick*="openPolicyPopup"]');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 799 |
|
| 800 |
+
// Look for file component
|
| 801 |
+
const fileComponents = document.querySelectorAll('.file-preview');
|
| 802 |
|
| 803 |
+
if (fileComponents.length > 0) {
|
| 804 |
+
// Found at least one file component, look for our PDF
|
| 805 |
+
for (const comp of fileComponents) {
|
| 806 |
+
const fileNameElement = comp.querySelector('.file-name');
|
| 807 |
+
if (fileNameElement && fileNameElement.textContent.includes('Hate Speech Policy.pdf')) {
|
| 808 |
+
// This is our PDF! Get the download link
|
| 809 |
+
const downloadBtn = comp.querySelector('a[download]');
|
| 810 |
+
if (downloadBtn && downloadBtn.href) {
|
| 811 |
+
// Store the URL globally for immediate access
|
| 812 |
+
window.pdfPolicyUrl = downloadBtn.href;
|
| 813 |
+
console.log('Found PDF at', window.pdfPolicyUrl);
|
| 814 |
+
|
| 815 |
+
// Update all the policy buttons to use this direct URL
|
| 816 |
+
policyButtons.forEach(btn => {
|
| 817 |
+
btn.setAttribute('data-pdf-url', window.pdfPolicyUrl);
|
| 818 |
+
});
|
| 819 |
+
|
| 820 |
+
// Preload the PDF in a hidden iframe for faster loading
|
| 821 |
+
const preloader = document.createElement('iframe');
|
| 822 |
+
preloader.style.display = 'none';
|
| 823 |
+
preloader.src = window.pdfPolicyUrl;
|
| 824 |
+
document.body.appendChild(preloader);
|
| 825 |
+
|
| 826 |
+
break;
|
| 827 |
+
}
|
| 828 |
+
}
|
| 829 |
+
}
|
| 830 |
+
}
|
| 831 |
+
}, 1000); // Give the page a second to fully load
|
| 832 |
});
|
| 833 |
|
| 834 |
+
// Function to handle opening the policy popup - OPTIMIZED FOR SPEED
|
| 835 |
function openPolicyPopup() {
|
| 836 |
// Display the popup immediately
|
| 837 |
const popup = document.getElementById('policy-popup');
|
|
|
|
| 848 |
iframe.style.display = "block";
|
| 849 |
fallback.style.display = "none";
|
| 850 |
|
| 851 |
+
// DIRECT METHOD: Check if the link that was clicked has the PDF URL
|
| 852 |
+
const clickedButton = event ? event.currentTarget : null;
|
| 853 |
+
let pdfUrl = clickedButton ? clickedButton.getAttribute('data-pdf-url') : null;
|
|
|
|
|
|
|
| 854 |
|
| 855 |
+
// If we don't have it from the button, try the cached version
|
| 856 |
+
if (!pdfUrl && window.pdfPolicyUrl) {
|
| 857 |
+
pdfUrl = window.pdfPolicyUrl;
|
| 858 |
+
}
|
| 859 |
|
| 860 |
+
// If we still don't have it, look for it directly
|
| 861 |
+
if (!pdfUrl) {
|
| 862 |
+
// Find all file components and look for our PDF
|
| 863 |
+
const fileComponents = document.querySelectorAll('.file-preview');
|
| 864 |
+
for (const comp of fileComponents) {
|
| 865 |
+
const fileNameElement = comp.querySelector('.file-name');
|
| 866 |
+
if (fileNameElement && fileNameElement.textContent.includes('Hate Speech Policy.pdf')) {
|
| 867 |
+
const downloadBtn = comp.querySelector('a[download]');
|
| 868 |
+
if (downloadBtn && downloadBtn.href) {
|
| 869 |
+
pdfUrl = downloadBtn.href;
|
| 870 |
+
window.pdfPolicyUrl = pdfUrl; // Cache for future use
|
| 871 |
+
break;
|
| 872 |
+
}
|
| 873 |
+
}
|
| 874 |
}
|
| 875 |
}
|
| 876 |
|
| 877 |
+
// If we still don't have it, look for any link with the PDF name
|
| 878 |
if (!pdfUrl) {
|
| 879 |
const allLinks = document.querySelectorAll("a");
|
| 880 |
for (const link of allLinks) {
|
| 881 |
if (link.href && link.href.includes("Hate%20Speech%20Policy.pdf")) {
|
| 882 |
pdfUrl = link.href;
|
| 883 |
+
window.pdfPolicyUrl = pdfUrl; // Cache for future use
|
| 884 |
break;
|
| 885 |
}
|
| 886 |
}
|
|
|
|
| 888 |
|
| 889 |
// If we found a URL, load it
|
| 890 |
if (pdfUrl) {
|
|
|
|
| 891 |
loadPdf(pdfUrl);
|
| 892 |
} else {
|
| 893 |
+
// No PDF found, show fallback
|
| 894 |
loadingIndicator.style.display = "none";
|
| 895 |
iframe.style.display = "none";
|
| 896 |
fallback.style.display = "block";
|
|
|
|
| 909 |
// Set download link in case user wants to download
|
| 910 |
downloadLink.href = pdfUrl;
|
| 911 |
|
| 912 |
+
// Load PDF directly into iframe - no Google Viewer which adds extra delay
|
| 913 |
iframe.src = pdfUrl;
|
| 914 |
|
| 915 |
// Check if iframe loaded successfully
|
| 916 |
let loadTimeout = setTimeout(function() {
|
| 917 |
+
// If still loading after 3 seconds, hide the loading indicator anyway
|
| 918 |
loadingIndicator.style.display = "none";
|
| 919 |
+
}, 3000);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 920 |
|
| 921 |
// When iframe loads, hide loading indicator
|
| 922 |
iframe.onload = function() {
|